From 63db49981a706947f61f89af17610a10113dab5d Mon Sep 17 00:00:00 2001 From: Aaron Heckmann Date: Sun, 3 Feb 2013 11:04:22 -0800 Subject: [PATCH] tests; adding docs to populated arrays add tests for docs with String, Buffer, and Number _ids relates to #570 --- test/model.populate.test.js | 196 +++++++++++++++++++++--------------- 1 file changed, 117 insertions(+), 79 deletions(-) diff --git a/test/model.populate.test.js b/test/model.populate.test.js index 0c10d8a9481..e46cb5fdb22 100644 --- a/test/model.populate.test.js +++ b/test/model.populate.test.js @@ -2006,104 +2006,142 @@ describe('model: populate:', function(){ }) describe('setting populated paths (gh-570)', function(){ - var db; - var B; - var U; - var u1, u2; - var b1, b2 + var types = { + 'ObjectId': DocObjectId + , 'String': String + , 'Number': Number + , 'Buffer': Buffer + } - before(function(done){ - db = start() - B = db.model('RefBlogPost', posts + random()) - U = db.model('RefUser', users + random()); + var construct = {}; + construct.String = random; + construct.ObjectId = DocObjectId; + construct.Number = random; + construct.Buffer = function () { + return new Buffer(random()); + } - U.create({ - name : 'Fan 1' - , email : 'fan1@learnboost.com' - }, { - name : 'Fan 2' - , email : 'fan2@learnboost.com' - }, function (err, fan1, fan2) { - assert.ifError(err); - u1 = fan1; - u2 = fan2; + Object.keys(types).forEach(function (id) { + describe('should not cast to _id of type ' + id, function(){ + var ID = types[id]; + var db; + var B, U; + var u1, u2; + var b1, b2 + + before(function(done){ + var refuser = 'RefUser-'+id; + var bSchema = Schema({ + title: String + , fans: [{type: id, ref: refuser }] + , _creator: { type: id, ref: refuser } + }); - B.create({ - title : 'Woot' - , fans : [fan1, fan2] - , _creator: fan1 - }, { - title : 'Woot2' - , fans : [fan2, fan1] - , _creator: fan2 - }, function (err, post1, post2) { - assert.ifError(err); - b1 = post1; - b2 = post2; - done(); - }); - }); - }) + var uSchema = Schema({ + _id: id + , name: String + , email: String + }); - after(function(){ - db.close() - }) + db = start() + B = db.model('RefBlogPost-'+id, bSchema, posts + random()) + U = db.model('RefUser-'+id, uSchema, users + random()); + + U.create({ + _id: construct[id]() + , name : 'Fan 1' + , email : 'fan1@learnboost.com' + }, { + _id: construct[id]() + , name : 'Fan 2' + , email : 'fan2@learnboost.com' + }, function (err, fan1, fan2) { + assert.ifError(err); + u1 = fan1; + u2 = fan2; - describe('should not cast to _id', function(){ - it('if a document', function(done){ - B.findById(b1).populate('fans _creator').exec(function (err, doc) { - assert.ifError(err); + B.create({ + title : 'Woot' + , fans : [fan1, fan2] + , _creator: fan1 + }, { + title : 'Woot2' + , fans : [fan2, fan1] + , _creator: fan2 + }, function (err, post1, post2) { + assert.ifError(err); + b1 = post1; + b2 = post2; + done(); + }); + }); + }) - var user3 = new U({ name: 'user3' }); - doc.fans.push(user3); - assert.equal(doc.fans[2], user3); + after(function(done){ + db.close(done) + }) - var user4 = new U({ name: 'user4' }); - doc.fans.nonAtomicPush(user4); - assert.equal(doc.fans[3], user4); + function user (name) { + return new U({ _id: construct[id](), name: name }); + } - var user5 = new U({ name: 'user5' }); - doc.fans.splice(2, 1, user5); - assert.equal(doc.fans[2], user5); + it('if a document', function(done){ + B.findById(b1).populate('fans _creator').exec(function (err, doc) { + assert.ifError(err); - var user6 = new U({ name: 'user6' }); - doc.fans.unshift(user6); - assert.equal(doc.fans[0], user6); + var user3 = user('user3'); + doc.fans.push(user3); + assert.equal(doc.fans[2], user3); - var user7 = new U({ name: 'user7' }); - doc.fans.addToSet(user7); - assert.equal(doc.fans[5], user7); + var user4 = user('user4'); + doc.fans.nonAtomicPush(user4); + assert.equal(doc.fans[3], user4); - doc.fans.forEach(function (doc) { - assert.ok(doc instanceof U); - }) + var user5 = user('user5'); + doc.fans.splice(2, 1, user5); + assert.equal(doc.fans[2], user5); - var user8 = new U({ name: 'user8' }); - doc.fans.set(0, user8); - assert.equal(doc.fans[0], user8); + var user6 = user('user6'); + doc.fans.unshift(user6); + assert.equal(doc.fans[0], user6); - doc.fans.push(null); - assert.equal(doc.fans[6], null); + var user7 = user('user7'); + doc.fans.addToSet(user7); + assert.equal(doc.fans[5], user7); - var _id = new DocObjectId; - doc.fans.addToSet(_id); - assert.equal(doc.fans[7], _id); + doc.fans.forEach(function (doc) { + assert.ok(doc instanceof U); + }) - doc.save(function (err) { - assert.ifError(err); - B.findById(b1).exec(function (err, doc) { + var user8 = user('user8'); + doc.fans.set(0, user8); + assert.equal(doc.fans[0], user8); + + doc.fans.push(null); + assert.equal(doc.fans[6], null); + + var _id = construct[id](); + doc.fans.addToSet(_id); + if (Buffer.isBuffer(_id)) { + assert.equal(doc.fans[7].toString('utf8'), _id.toString('utf8')); + } else { + assert.equal(doc.fans[7], _id); + } + + doc.save(function (err) { assert.ifError(err); - assert.equal(8, doc.fans.length); - assert.equal(doc.fans[0], user8.id); - assert.equal(doc.fans[5], user7.id); - assert.equal(doc.fans[6], null); - assert.equal(doc.fans[7], String(_id)); - done(); + B.findById(b1).exec(function (err, doc) { + assert.ifError(err); + assert.equal(8, doc.fans.length); + assert.equal(doc.fans[0], user8.id); + assert.equal(doc.fans[5], user7.id); + assert.equal(doc.fans[6], null); + assert.equal(doc.fans[7], String(_id)); + done(); + }) }) }) }) - // TODO test setting _id directly - // TODO test with Buffer, Number, String _ids too }) }) })