Permalink
Browse files

updated dbref test

  • Loading branch information...
aheckmann committed Aug 27, 2011
1 parent f4522d1 commit 35a2bcda9c3b8bf2a7a9f30f2c2b8cfa7a1e297d
Showing with 51 additions and 53 deletions.
  1. +51 −53 test/model.ref.test.js
View
@@ -429,79 +429,77 @@ module.exports = {
});
},
- 'test populating an array of references and changing one': function () {
+ 'test populating an array of refs, changing one, and removing one': function () {
var db = start()
, BlogPost = db.model('RefBlogPost', posts)
, User = db.model('RefUser', users);
User.create({
name : 'Fan 1'
, email : 'fan1@learnboost.com'
- }, function (err, fan1) {
+ }, {
+ name : 'Fan 2'
+ , email : 'fan2@learnboost.com'
+ }, {
+ name : 'Fan 3'
+ , email : 'fan3@learnboost.com'
+ }, {
+ name : 'Fan 4'
+ , email : 'fan4@learnboost.com'
+ }, function (err, fan1, fan2, fan3, fan4) {
should.strictEqual(err, null);
- User.create({
- name : 'Fan 2'
- , email : 'fan2@learnboost.com'
- }, function (err, fan2) {
+ BlogPost.create({
+ title : 'Woot'
+ , fans : [fan1, fan2]
+ }, {
+ title : 'Woot'
+ , fans : [fan2, fan1]
+ }, function (err, post1, post2) {
should.strictEqual(err, null);
- User.create({
- name : 'Fan 3'
- , email : 'fan3@learnboost.com'
- }, function (err, fan3) {
+ BlogPost
+ .find({ _id: { $in: [post1._id, post2._id ] } })
+ .populate('fans', ['name'])
+ .run(function (err, blogposts) {
should.strictEqual(err, null);
- User.create({
- name : 'Fan 4'
- , email : 'fan4@learnboost.com'
- }, function (err, fan4) {
- should.strictEqual(err, null);
-
- BlogPost.create({
- title : 'Woot'
- , fans : [fan1, fan2]
- }, function (err, post1) {
- should.strictEqual(err, null);
-
- BlogPost.create({
- title : 'Woot'
- , fans : [fan2, fan1]
- }, function (err, post2) {
- should.strictEqual(err, null);
+ blogposts[0].fans[0].name.should.equal('Fan 1');
+ blogposts[0].fans[0].isInit('email').should.be.false;
+ blogposts[0].fans[1].name.should.equal('Fan 2');
+ blogposts[0].fans[1].isInit('email').should.be.false;
- BlogPost
- .find({ _id: { $in: [post1._id, post2._id ] } })
- .populate('fans', ['name'])
- .run(function (err, blogposts) {
- should.strictEqual(err, null);
+ blogposts[1].fans[0].name.should.equal('Fan 2');
+ blogposts[1].fans[0].isInit('email').should.be.false;
+ blogposts[1].fans[1].name.should.equal('Fan 1');
+ blogposts[1].fans[1].isInit('email').should.be.false;
- blogposts[0].fans[0].name.should.equal('Fan 1');
- blogposts[0].fans[0].isInit('email').should.be.false;
- blogposts[0].fans[1].name.should.equal('Fan 2');
- blogposts[0].fans[1].isInit('email').should.be.false;
+ blogposts[1].fans = [fan3, fan4];
- blogposts[1].fans[0].name.should.equal('Fan 2');
- blogposts[1].fans[0].isInit('email').should.be.false;
- blogposts[1].fans[1].name.should.equal('Fan 1');
- blogposts[1].fans[1].isInit('email').should.be.false;
+ blogposts[1].save(function (err) {
+ should.strictEqual(err, null);
- blogposts[1].fans = [fan3, fan4];
+ BlogPost
+ .findById(blogposts[1]._id, [], { populate: ['fans'] })
+ .run(function (err, post) {
+ should.strictEqual(err, null);
- blogposts[1].save(function (err) {
- should.strictEqual(err, null);
+ post.fans[0].name.should.equal('Fan 3');
+ post.fans[1].name.should.equal('Fan 4');
- BlogPost
- .findById(blogposts[1]._id, [], { populate: ['fans'] })
- .run(function (err, post) {
- should.strictEqual(err, null);
+ post.fans.splice(0, 1);
+ post.save(function (err) {
+ should.strictEqual(err, null);
- post.fans[0].name.should.equal('Fan 3');
- post.fans[1].name.should.equal('Fan 4');
- db.close();
- });
- });
- });
+ BlogPost
+ .findById(post._id)
+ .populate('fans')
+ .run(function (err, post) {
+ db.close();
+ should.strictEqual(err, null);
+ post.fans.length.should.equal(1);
+ post.fans[0].name.should.equal('Fan 4');
+ });
});
});
});

0 comments on commit 35a2bcd

Please sign in to comment.