Skip to content

Commit

Permalink
tests; adding docs to populated arrays
Browse files Browse the repository at this point in the history
add tests for docs with String, Buffer, and Number _ids

relates to #570
  • Loading branch information
aheckmann committed Feb 3, 2013
1 parent 9f0f204 commit 63db499
Showing 1 changed file with 117 additions and 79 deletions.
196 changes: 117 additions & 79 deletions test/model.populate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
})
})
})
Expand Down

0 comments on commit 63db499

Please sign in to comment.