PageRenderTime 33ms CodeModel.GetById 13ms app.highlight 15ms RepoModel.GetById 2ms app.codeStats 0ms

/node_modules/mquery/test/utils.test.js

https://bitbucket.org/coleman333/smartsite
JavaScript | 143 lines | 114 code | 29 blank | 0 comment | 7 complexity | 2f2909e131f635d8367831ff78633c24 MD5 | raw file
  1
  2var utils = require('../lib/utils');
  3var assert = require('assert');
  4
  5var mongo;
  6try {
  7  mongo = new require('mongodb');
  8} catch (e) {}
  9
 10describe('lib/utils', function() {
 11  describe('clone', function() {
 12    it('clones constructors named ObjectId', function(done) {
 13      function ObjectId (id) {
 14        this.id = id;
 15      }
 16
 17      var o1 = new ObjectId('1234');
 18      var o2 = utils.clone(o1);
 19      assert.ok(o2 instanceof ObjectId);
 20
 21      done();
 22    });
 23
 24    it('clones constructors named ObjectID', function(done) {
 25      function ObjectID (id) {
 26        this.id = id;
 27      }
 28
 29      var o1 = new ObjectID('1234');
 30      var o2 = utils.clone(o1);
 31
 32      assert.ok(o2 instanceof ObjectID);
 33      done();
 34    });
 35
 36    it('does not clone constructors named ObjectIdd', function(done) {
 37      function ObjectIdd (id) {
 38        this.id = id;
 39      }
 40
 41      var o1 = new ObjectIdd('1234');
 42      var o2 = utils.clone(o1);
 43      assert.ok(!(o2 instanceof ObjectIdd));
 44
 45      done();
 46    });
 47
 48    it('optionally clones ObjectId constructors using its clone method', function(done) {
 49      function ObjectID (id) {
 50        this.id = id;
 51        this.cloned = false;
 52      }
 53
 54      ObjectID.prototype.clone = function () {
 55        var ret = new ObjectID(this.id);
 56        ret.cloned = true;
 57        return ret;
 58      }
 59
 60      var id = 1234;
 61      var o1 = new ObjectID(id);
 62      assert.equal(id, o1.id);
 63      assert.equal(false, o1.cloned);
 64
 65      var o2 = utils.clone(o1);
 66      assert.ok(o2 instanceof ObjectID);
 67      assert.equal(id, o2.id);
 68      assert.ok(o2.cloned);
 69      done();
 70    });
 71
 72    it('clones mongodb.ReadPreferences', function (done) {
 73      if (!mongo) return done();
 74
 75      var tags = [
 76        {dc: 'tag1'}
 77      ];
 78      var prefs = [
 79        new mongo.ReadPreference("primary"),
 80        new mongo.ReadPreference(mongo.ReadPreference.PRIMARY_PREFERRED),
 81        new mongo.ReadPreference("primary", tags),
 82        mongo.ReadPreference("primary", tags)
 83      ];
 84
 85      var prefsCloned = utils.clone(prefs);
 86
 87      for (var i = 0; i < prefsCloned.length; i++) {
 88        assert.notEqual(prefs[i], prefsCloned[i]);
 89        assert.ok(prefsCloned[i] instanceof mongo.ReadPreference);
 90        assert.ok(prefsCloned[i].isValid());
 91        if (prefs[i].tags) {
 92          assert.ok(prefsCloned[i].tags);
 93          assert.notEqual(prefs[i].tags, prefsCloned[i].tags);
 94          assert.notEqual(prefs[i].tags[0], prefsCloned[i].tags[0]);
 95        } else {
 96          assert.equal(prefsCloned[i].tags, null);
 97        }
 98      }
 99
100      done();
101    });
102
103    it('clones mongodb.Binary', function(done){
104      if (!mongo) return done();
105
106      var buf = new Buffer('hi');
107      var binary= new mongo.Binary(buf, 2);
108      var clone = utils.clone(binary);
109      assert.equal(binary.sub_type, clone.sub_type);
110      assert.equal(String(binary.buffer), String(buf));
111      assert.ok(binary !== clone);
112      done();
113    })
114
115    it('handles objects with no constructor', function(done) {
116      var name ='335';
117
118      var o = Object.create(null);
119      o.name = name;
120
121      var clone;
122      assert.doesNotThrow(function() {
123        clone = utils.clone(o);
124      });
125
126      assert.equal(name, clone.name);
127      assert.ok(o != clone);
128      done();
129    });
130
131    it('handles buffers', function(done){
132      var buff = new Buffer(10);
133      buff.fill(1);
134      var clone = utils.clone(buff);
135
136      for (var i = 0; i < buff.length; i++) {
137        assert.equal(buff[i], clone[i]);
138      }
139
140      done();
141    });
142  });
143});