Skip to content

Commit

Permalink
Added tests for Instance.save()
Browse files Browse the repository at this point in the history
  • Loading branch information
notheotherben committed Jan 15, 2014
1 parent dc745c1 commit 34825ac
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions test/instance_db.js
Expand Up @@ -12,6 +12,7 @@ describe('orm', function () {

var model = new Model(db, 'instances', {
username: String,
age: Number,
sessions: [{
id: String,
expires: Date
Expand All @@ -38,13 +39,16 @@ 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()
}]
}, function(err, original) {
if(err) return done(err);

original.age = 12;

original.sessions.push({
id: 'bbbb',
expires: new Date()
Expand All @@ -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();
});
});
});
});
});
});
});
Expand Down

0 comments on commit 34825ac

Please sign in to comment.