From 34825ac0e338e592cfaee6ad4b763c396fa6b31f Mon Sep 17 00:00:00 2001 From: Benjamin Pannell Date: Wed, 15 Jan 2014 13:38:44 +0200 Subject: [PATCH] Added tests for Instance.save() --- test/instance_db.js | 49 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/test/instance_db.js b/test/instance_db.js index 7925dbe..be101bb 100644 --- a/test/instance_db.js +++ b/test/instance_db.js @@ -12,6 +12,7 @@ describe('orm', function () { var model = new Model(db, 'instances', { username: String, + age: Number, sessions: [{ id: String, expires: Date @@ -38,6 +39,7 @@ describe('orm', function () { it('should correctly store changes made to the instance', function(done) { model.create({ username: 'billy', + age: 10, sessions: [{ id: 'aaaa', expires: new Date() @@ -45,6 +47,8 @@ describe('orm', function () { }, function(err, original) { if(err) return done(err); + original.age = 12; + original.sessions.push({ id: 'bbbb', expires: new Date() @@ -53,12 +57,57 @@ describe('orm', function () { original.save(function(err, updated) { if(err) return done(err); updated.should.equal(original); + updated.age.should.eql(12); updated.sessions.length.toString().should.eql('2'); done(); }); }); }); + + it('should allow passing of specific change-sets', function(done) { + model.create({ + username: 'bob', + age: 12, + sessions: [{ + id: 'aaaa', + expires: new Date() + }] + }, function(err, instance) { + if(err) return done(err); + + instance.save({ $set: { sessions: [] }, $inc: { age: 1 }}, function(err) { + if(err) return done(err); + instance.age.should.eql(13); + instance.sessions.should.eql([]); + done(); + }); + }); + }); + + it('should allow conditions and changesets to be used', function(done) { + model.create({ + username: 'sally', + age: 15, + sessions: [{ + id: 'aaaa', + expires: new Date() + }] + }, function(err, instance) { + if(err) return done(err); + instance.save({ age: 14 }, { $inc: { age: 1 }}, function(err) { + if(err) return done(err); + instance.age.should.eql(15); + + instance.save({ age: 15 }, { $inc: { age: 1 }}, function(err) { + if(err) return done(err); + instance.age.should.eql(16); + + done(); + }); + }); + }); + }); }); }); });