From 04f35b31b448bb2b98e44cb0a4ec587773c748e8 Mon Sep 17 00:00:00 2001 From: Simon Ho Date: Wed, 11 Mar 2015 21:10:30 -0700 Subject: [PATCH] Memory connector returns updated records count The memory connector now includes the number of updated items in its callback when updating records. --- lib/connectors/memory.js | 7 +-- lib/connectors/transient.js | 3 +- test/basic-querying.test.js | 57 -------------------- test/manipulation.test.js | 100 ++++++++++++++++++++++++++++++++++++ 4 files changed, 106 insertions(+), 61 deletions(-) diff --git a/lib/connectors/memory.js b/lib/connectors/memory.js index e94c3702..36c0efd8 100644 --- a/lib/connectors/memory.js +++ b/lib/connectors/memory.js @@ -585,9 +585,11 @@ Memory.prototype.update = filter = applyFilter({where: where}); var ids = Object.keys(cache); + var count = 0; async.each(ids, function (id, done) { var inst = self.fromDb(model, cache[id]); if (!filter || filter(inst)) { + count++; // The id value from the cache is string // Get the real id from the inst id = self.getIdValue(model, inst); @@ -596,9 +598,8 @@ Memory.prototype.update = process.nextTick(done); } }, function (err) { - if (!err) { - self.saveToFile(null, cb); - } + if (err) return cb(err); + self.saveToFile({count: count}, cb); }); }; diff --git a/lib/connectors/transient.js b/lib/connectors/transient.js index ce8354ce..250efa12 100644 --- a/lib/connectors/transient.js +++ b/lib/connectors/transient.js @@ -97,7 +97,8 @@ Transient.prototype.save = function save(model, data, callback) { Transient.prototype.update = Transient.prototype.updateAll = function updateAll(model, where, data, cb) { - this.flush('update', null, cb); + var count = 0; + this.flush('update', count, cb); }; Transient.prototype.updateAttributes = function updateAttributes(model, id, data, cb) { diff --git a/test/basic-querying.test.js b/test/basic-querying.test.js index f333fb67..288452de 100644 --- a/test/basic-querying.test.js +++ b/test/basic-querying.test.js @@ -599,64 +599,7 @@ describe('basic-querying', function () { }); }); }); - }); - - - describe('updateAll ', function () { - - beforeEach(seed); - - it('should only update instances that satisfy the where condition', function (done) { - User.update({name: 'John Lennon'}, {name: 'John Smith'}, function () { - User.find({where: {name: 'John Lennon'}}, function (err, data) { - should.not.exist(err); - data.length.should.equal(0); - User.find({where: {name: 'John Smith'}}, function (err, data) { - should.not.exist(err); - data.length.should.equal(1); - done(); - }); - }); - }); - }); - - it('should update all instances without where', function (done) { - User.update({name: 'John Smith'}, function () { - User.find({where: {name: 'John Lennon'}}, function (err, data) { - should.not.exist(err); - data.length.should.equal(0); - User.find({where: {name: 'John Smith'}}, function (err, data) { - should.not.exist(err); - data.length.should.equal(6); - done(); - }); - }); - }); - }); - - it('should ignore undefined values of data', function(done) { - User.update({name: 'John Lennon'}, {name: undefined, - email: 'johnl@b3atl3s.co.uk'}, function(err) { - should.not.exist(err); - User.find({where: {name: 'John Lennon'}}, function(err, data) { - should.not.exist(err); - data.length.should.equal(1); - data[0].email.should.equal('johnl@b3atl3s.co.uk'); - done(); - }); - }); - }); - - it('should coerce data', function (done) { - User.update({name: 'John Lennon'}, {birthday: 'invalidate'}, function (err) { - should.exist(err); - done(); - }); - }); - - }); - }); function seed(done) { diff --git a/test/manipulation.test.js b/test/manipulation.test.js index 912b29a8..713d8588 100644 --- a/test/manipulation.test.js +++ b/test/manipulation.test.js @@ -1,4 +1,5 @@ // This test written in mocha+should.js +var async = require('async'); var should = require('./init.js'); var db, Person; @@ -982,6 +983,105 @@ describe('manipulation', function () { e.should.be.eql(new Error('Invalid date: X')); } }); + }); + describe('update/updateAll', function() { + beforeEach(function destroyFixtures(done) { + Person.destroyAll(done); + }); + + beforeEach(function createFixtures(done) { + Person.create([{ + name: 'Brett Boe', + age: 19 + }, { + name: 'Carla Coe', + age: 20 + }, { + name: 'Donna Doe', + age: 21 + }, { + name: 'Frank Foe', + age: 22 + }, { + name: 'Grace Goe', + age: 23 + }], done); + }); + + it('should be a function', function() { + Person.update.should.be.a.Function; + Person.updateAll.should.be.a.Function; + }); + + it('should not update instances that do not satisfy the where condition', + function(done) { + Person.update({name: 'Harry Hoe'}, {name: 'Marta Moe'}, function(err, + results) { + should.not.exist(err); + results.count.should.equal(0); + Person.find({where: {name: 'Harry Hoe'}}, function(err, people) { + should.not.exist(err); + people.should.be.empty; + done(); + }); + }); + }); + + it('should update instances that satisfy the where condition', + function(done) { + Person.update({name: 'Brett Boe'}, {name: 'Harry Hoe'}, function(err, + results) { + should.not.exist(err); + results.count.should.equal(1); + Person.find({where: {age: 19}}, function(err, people) { + should.not.exist(err); + people.should.have.length(1); + people[0].name.should.equal('Harry Hoe'); + done(); + }); + }); + }); + + it('should update all instances when the where condition is not provided', + function(done) { + Person.update({name: 'Harry Hoe'}, function(err, results) { + should.not.exist(err); + results.count.should.equal(5); + Person.find({where: {name: 'Brett Boe'}}, function(err, people) { + should.not.exist(err); + people.should.be.empty; + Person.find({where: {name: 'Harry Hoe'}}, function(err, people) { + should.not.exist(err); + people.should.have.length(5); + done(); + }); + }); + }); + }); + + it('should ignore where conditions with undefined values', + function(done) { + Person.update({name: 'Brett Boe'}, {name: undefined, gender: 'male'}, + function(err, results) { + should.not.exist(err); + results.count.should.equal(1); + Person.find({where: {name: 'Brett Boe'}}, function(err, people) { + should.not.exist(err); + people.should.have.length(1); + people[0].name.should.equal('Brett Boe'); + done(); + }); + }); + }); + + it('should not coerce invalid values provided in where conditions', + function(done) { + Person.update({name: 'Brett Boe'}, {dob: 'Carla Coe'}, function(err) { + should.exist(err); + err.message.should.equal('Invalid date: Carla Coe'); + done(); + }); + }); }); });