From 589887715c6d93fbf6b5f2c64aafbf4241c0627b Mon Sep 17 00:00:00 2001 From: Anatoliy Chakkaev Date: Thu, 28 Mar 2013 15:27:27 +0400 Subject: [PATCH] Tests for data manipulation --- test/manipulation.test.js | 66 ++++++++++++++++++++++++++++++++++++--- 1 file changed, 62 insertions(+), 4 deletions(-) diff --git a/test/manipulation.test.js b/test/manipulation.test.js index 490ab3d9..723e2770 100644 --- a/test/manipulation.test.js +++ b/test/manipulation.test.js @@ -76,10 +76,68 @@ describe('manipulation', function() { }); describe('save', function() { - it('should save new object'); - it('should save existing object'); - it('should save invalid object (skipping validation)'); - it('should save throw error on validation'); + + it('should save new object', function(done) { + var p = new Person; + p.save(function(err) { + should.not.exist(err); + should.exist(p.id); + done(); + }); + }); + + it('should save existing object', function(done) { + Person.findOne(function(err, p) { + should.not.exist(err); + p.name = 'Hans'; + p.propertyChanged('name').should.be.true; + p.save(function(err) { + should.not.exist(err); + p.propertyChanged('name').should.be.false; + Person.findOne(function(err, p) { + should.not.exist(err); + p.name.should.equal('Hans'); + p.propertyChanged('name').should.be.false; + done(); + }); + }); + }); + }); + + it('should save invalid object (skipping validation)', function(done) { + Person.findOne(function(err, p) { + should.not.exist(err); + p.isValid = function(done) { + process.nextTick(done); + return false; + }; + p.name = 'Nana'; + p.save(function(err) { + should.exist(err); + p.propertyChanged('name').should.be.true; + p.save({validate: false}, function(err) { + should.not.exist(err); + p.propertyChanged('name').should.be.false; + done(); + }); + }); + }); + }); + + it('should save throw error on validation', function() { + Person.findOne(function(err, p) { + should.not.exist(err); + p.isValid = function(cb) { + cb(false); + return false; + }; + (function() { + p.save({ + 'throws': true + }); + }).should.throw('Validation error'); + }); + }); }); describe('destroy', function() {