Permalink
Browse files

failing test for multi-query populate

  • Loading branch information...
1 parent c2708f5 commit cb7ac9d35478fde454988f5e7619e1f0cf2d1a86 @aheckmann aheckmann committed Oct 12, 2011
Showing with 56 additions and 36 deletions.
  1. +56 −36 test/model.ref.test.js
View
92 test/model.ref.test.js
@@ -1035,52 +1035,72 @@ module.exports = {
'populating more than one array at a time': function () {
var db = start()
- , BlogPost = db.model('RefBlogPost', posts)
, User = db.model('RefUser', users)
+ , M = db.model('PopMultiSubDocs', new Schema({
+ users: [{ type: ObjectId, ref: 'RefUser' }]
+ , fans: [{ type: ObjectId, ref: 'RefUser' }]
+ , comments: [Comment]
+ }))
User.create({
email : 'fan1@learnboost.com'
- }, function (err, fan1) {
+ }, {
+ name : 'Fan 2'
+ , email : 'fan2@learnboost.com'
+ , gender : 'female'
+ }, {
+ name: 'Fan 3'
+ }, function (err, fan1, fan2, fan3) {
should.strictEqual(err, null);
- User.create({
- name : 'Fan 2'
- , email : 'fan2@learnboost.com'
- , gender : 'female'
- }, function (err, fan2) {
+ M.create({
+ users: [fan3]
+ , fans: [fan1]
+ , comments: [
+ { _creator: fan1, content: 'bejeah!' }
+ , { _creator: fan2, content: 'chickfila' }
+ ]
+ }, {
+ users: [fan1]
+ , fans: [fan2]
+ , comments: [
+ { _creator: fan3, content: 'hello' }
+ , { _creator: fan1, content: 'world' }
+ ]
+ }, function (err, post1, post2) {
should.strictEqual(err, null);
- BlogPost.create({
- title : 'Woot'
- , fans : [fan1, fan2]
- , comments: [
- { _creator: fan1, content: 'bejeah!' }
- , { _creator: fan2, content: 'chickfila' }
- ]
- }, function (err, post1) {
+ M.where('_id').in([post1, post2])
+ .populate('fans', 'name', { gender: 'female' })
+ .populate('users', 'name', { gender: 'male' })
+ .populate('comments._creator', ['email'], { name: null })
+ .run(function (err, posts) {
+ db.close();
should.strictEqual(err, null);
- BlogPost
- .findById(post1._id)
- .populate('comments._creator', ['email'], { name: null })
- .populate('fans', 'name', { gender: 'female' })
- .run(function (err, p) {
- db.close();
- should.strictEqual(err, null);
-
- should.exist(p);
- should.strictEqual(p.fans.length, 1);
- p.fans[0].name.should.equal('Fan 2');
- p.fans[0].isInit('email').should.be.false;
- p.fans[0].isInit('gender').should.be.false;
- p.comments.length.should.equal(2);
- should.exist(p.comments[0]._creator.email);
- p.comments[0]._creator.email.should.equal('fan1@learnboost.com');
- p.comments[0]._creator.isInit('name').should.be.false;
- p.comments[0].content.should.equal('bejeah!');
- should.not.exist(p.comments[1]._creator);
- p.comments[1].content.should.equal('chickfila');
- });
+ should.exist(posts);
+ posts.length.should.equal(2);
+ var p1 = posts[0];
+ var p2 = posts[1];
+ should.strictEqual(p1.fans.length, 0);
+ should.strictEqual(p2.fans.length, 1);
+ p2.fans[0].name.should.equal('Fan 2');
+ p2.fans[0].isInit('email').should.be.false;
+ p2.fans[0].isInit('gender').should.be.false;
+ p1.comments.length.should.equal(2);
+ p2.comments.length.should.equal(2);
+ should.exist(p1.comments[0]._creator.email);
+ should.exist(p2.comments[0]._creator.email);
+ p1.comments[0]._creator.email.should.equal('fan1@learnboost.com');
+ p2.comments[1]._creator.email.should.equal('fan1@learnboost.com');
+ p1.comments[0]._creator.isInit('name').should.be.false;
+ p2.comments[1]._creator.isInit('name').should.be.false;
+ p1.comments[0].content.should.equal('bejeah!');
+ p2.comments[1].content.should.equal('world');
+ should.not.exist(p1.comments[1]._creator);
+ should.not.exist(p2.comments[0]._creator);
+ p1.comments[1].content.should.equal('chickfila');
+ p1.comments[0].content.should.equal('hello');
});
});
});

0 comments on commit cb7ac9d

Please sign in to comment.