From 311ab293d2083d3bf22befe9da99295704ec94b7 Mon Sep 17 00:00:00 2001 From: Simon Ho Date: Tue, 24 Mar 2015 21:30:40 -0700 Subject: [PATCH] Clean up delete and update tests --- test/manipulation.test.js | 265 +++++++++++++++++++------------------- 1 file changed, 130 insertions(+), 135 deletions(-) diff --git a/test/manipulation.test.js b/test/manipulation.test.js index 91eeebe8..dedf4aeb 100644 --- a/test/manipulation.test.js +++ b/test/manipulation.test.js @@ -757,64 +757,62 @@ describe('manipulation', function () { }); describe('deleteAll/destroyAll', function () { + beforeEach(function clearOldData(done) { + Person.deleteAll(done); + }); + + beforeEach(function createTestData(done) { + Person.create([{ + name: 'John' + }, { + name: 'Jane' + }], done); + }); + it('should be defined as function', function() { Person.deleteAll.should.be.a.Function; Person.destroyAll.should.be.a.Function; }); - context('with multiple instances in the database', function() { - beforeEach(function deleteFixtures(done) { - Person.deleteAll(done); - }); - - beforeEach(function createFixtures(done) { - Person.create([{ - name: 'John' - }, { - name: 'Jane' - }], done); - }); - - it('should only delete instances that satisfy the where condition', - function(done) { - Person.deleteAll({name: 'John'}, function(err, info) { + it('should only delete instances that satisfy the where condition', + function(done) { + Person.deleteAll({name: 'John'}, function(err, info) { + if (err) return done(err); + info.should.have.property('count', 1); + Person.find({where: {name: 'John'}}, function(err, data) { if (err) return done(err); - info.count.should.equal(1); - Person.find({where: {name: 'John'}}, function(err, data) { + data.should.have.length(0); + Person.find({where: {name: 'Jane'}}, function(err, data) { if (err) return done(err); - data.should.have.length(0); - Person.find({where: {name: 'Jane'}}, function(err, data) { - if (err) return done(err); - data.should.have.length(1); - done(); - }); - }); - }); - }); - - it('should report zero deleted instances when no matches are found', - function(done) { - Person.deleteAll({name: 'does-not-match'}, function(err, info) { - if (err) return done(err); - info.count.should.equal(0); - Person.count(function(err, count) { - if (err) return done(err); - count.should.equal(2); + data.should.have.length(1); done(); }); }); }); + }); - it('should delete all instances when the where condition is not provided', - function(done) { - Person.deleteAll(function (err, info) { + it('should report zero deleted instances when no matches are found', + function(done) { + Person.deleteAll({name: 'does-not-match'}, function(err, info) { + if (err) return done(err); + info.should.have.property('count', 0); + Person.count(function(err, count) { if (err) return done(err); - info.count.should.equal(2); - Person.count(function(err, count) { - if (err) return done(err); - count.should.equal(0); - done(); - }); + count.should.equal(2); + done(); + }); + }); + }); + + it('should delete all instances when the where condition is not provided', + function(done) { + Person.deleteAll(function (err, info) { + if (err) return done(err); + info.should.have.property('count', 2); + Person.count(function(err, count) { + if (err) return done(err); + count.should.equal(0); + done(); }); }); }); @@ -1017,105 +1015,102 @@ describe('manipulation', function () { }); describe('update/updateAll', function() { + beforeEach(function clearOldData(done) { + Person.destroyAll(done); + }); + + beforeEach(function createTestData(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 defined as a function', function() { Person.update.should.be.a.Function; Person.updateAll.should.be.a.Function; }); - context('with multiple instances in the database', function() { - beforeEach(function deleteFixtures(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 not update instances that do not satisfy the where condition', - function(done) { - Person.update({name: 'Harry Hoe'}, {name: 'Marta Moe'}, function(err, - info) { + it('should not update instances that do not satisfy the where condition', + function(done) { + Person.update({name: 'Harry Hoe'}, {name: 'Marta Moe'}, function(err, + info) { + if (err) return done(err); + info.should.have.property('count', 0); + Person.find({where: {name: 'Harry Hoe'}}, function(err, people) { if (err) return done(err); - should.not.exist(err); - info.count.should.equal(0); - Person.find({where: {name: 'Harry Hoe'}}, function(err, people) { - if (err) return done(err); - people.should.be.empty; - done(); - }); - }); - }); - - it('should only update instances that satisfy the where condition', - function(done) { - Person.update({name: 'Brett Boe'}, {name: 'Harry Hoe'}, function(err, - info) { - if (err) return done(err); - info.count.should.equal(1); - Person.find({where: {age: 19}}, function(err, people) { - if (err) return done(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, info) { - if (err) return done(err); - info.count.should.equal(5); - Person.find({where: {name: 'Brett Boe'}}, function(err, people) { - if (err) return done(err); - people.should.be.empty; - Person.find({where: {name: 'Harry Hoe'}}, function(err, people) { - if (err) return done(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, info) { - if (err) return done(err); - info.count.should.equal(1); - Person.find({where: {name: 'Brett Boe'}}, function(err, people) { - if (err) return done(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'); + people.should.be.empty; done(); }); }); }); + + it('should only update instances that satisfy the where condition', + function(done) { + Person.update({name: 'Brett Boe'}, {name: 'Harry Hoe'}, function(err, + info) { + if (err) return done(err); + info.should.have.property('count', 1); + Person.find({where: {age: 19}}, function(err, people) { + if (err) return done(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, info) { + if (err) return done(err); + info.should.have.property('count', 5); + Person.find({where: {name: 'Brett Boe'}}, function(err, people) { + if (err) return done(err); + people.should.be.empty; + Person.find({where: {name: 'Harry Hoe'}}, function(err, people) { + if (err) return done(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, info) { + if (err) return done(err); + info.should.have.property('count', 1); + Person.find({where: {name: 'Brett Boe'}}, function(err, people) { + if (err) return done(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(); + }); + }); }); });