Merge pull request #530 from strongloop/fix/clean-up-delete-and-update-mainipulation-tests

Clean up update/delete manipulation tests
This commit is contained in:
Simon Ho 2015-03-23 14:54:53 -07:00
commit 77ff8541cc
1 changed files with 130 additions and 116 deletions

View File

@ -756,54 +756,65 @@ describe('manipulation', function () {
it('should destroy filtered set of records'); it('should destroy filtered set of records');
}); });
describe('deleteAll/destroyAll', function () { describe('when deleteAll/destroyAll executes', function () {
beforeEach(function clearOldData(done) { it('should be defined as function', function() {
Person.deleteAll(done); Person.deleteAll.should.be.a.Function;
Person.destroyAll.should.be.a.Function;
}); });
beforeEach(function createTestData(done) { describe('with multiple instances in the database', function() {
Person.create([ beforeEach(function deleteFixtures(done) {
{ name: 'John' }, Person.deleteAll(done);
{ name: 'Jane' } });
], done);
});
it('should only delete instances that satisfy the where condition', function (done) { beforeEach(function createFixtures(done) {
Person.deleteAll({name: 'John'}, function (err, result) { Person.create([{
if (err) return done(err); name: 'John'
result.should.have.property('count', 1); }, {
Person.find({where: {name: 'John'}}, function (err, data) { name: 'Jane'
}], done);
});
it('should only delete instances that satisfy the where condition',
function(done) {
Person.deleteAll({name: 'John'}, function(err, info) {
if (err) return done(err); if (err) return done(err);
data.should.have.length(0); info.count.should.equal(1);
Person.find({where: {name: 'Jane'}}, function (err, data) { Person.find({where: {name: 'John'}}, function(err, data) {
if (err) return done(err); if (err) return done(err);
data.should.have.length(1); 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);
done(); done();
}); });
}); });
}); });
});
it('should report zero deleted instances', function (done) { it('should delete all instances when the where condition is not provided',
Person.deleteAll({name: 'does-not-match'}, function (err, result) { function(done) {
if (err) return done(err); Person.deleteAll(function (err, info) {
result.should.have.property('count', 0);
Person.count(function(err, count) {
if (err) return done(err); if (err) return done(err);
count.should.equal(2); info.count.should.equal(2);
done(); Person.count(function(err, count) {
}); if (err) return done(err);
}); count.should.equal(0);
}); done();
});
it('should delete all instances when "where" is not provided', function(done) {
Person.deleteAll(function (err, result) {
if (err) return done(err);
result.should.have.property('count', 2);
Person.count(function(err, count) {
if (err) return done(err);
count.should.equal(0);
done();
}); });
}); });
}); });
@ -1005,103 +1016,106 @@ describe('manipulation', function () {
}); });
}); });
describe('update/updateAll', function() { describe('when update/updateAll executes', function() {
beforeEach(function destroyFixtures(done) { it('should be defined as a function', function() {
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.update.should.be.a.Function;
Person.updateAll.should.be.a.Function; Person.updateAll.should.be.a.Function;
}); });
it('should not update instances that do not satisfy the where condition', describe('with existing instances in the database', function() {
function(done) { beforeEach(function deleteFixtures(done) {
Person.update({name: 'Harry Hoe'}, {name: 'Marta Moe'}, function(err, Person.destroyAll(done);
results) {
should.not.exist(err);
results.should.have.property('count', 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', beforeEach(function createFixtures(done) {
function(done) { Person.create([{
Person.update({name: 'Brett Boe'}, {name: 'Harry Hoe'}, function(err, name: 'Brett Boe',
results) { age: 19
should.not.exist(err); }, {
results.should.have.property('count', 1); name: 'Carla Coe',
Person.find({where: {age: 19}}, function(err, people) { age: 20
should.not.exist(err); }, {
people.should.have.length(1); name: 'Donna Doe',
people[0].name.should.equal('Harry Hoe'); age: 21
done(); }, {
}); name: 'Frank Foe',
age: 22
}, {
name: 'Grace Goe',
age: 23
}], done);
}); });
});
it('should update all instances when the where condition is not provided', it('should not update instances that do not satisfy the where condition',
function(done) { function(done) {
Person.update({name: 'Harry Hoe'}, function(err, results) { Person.update({name: 'Harry Hoe'}, {name: 'Marta Moe'}, function(err,
should.not.exist(err); info) {
results.should.have.property('count', 5); if (err) return done(err);
Person.find({where: {name: 'Brett Boe'}}, function(err, people) {
should.not.exist(err); should.not.exist(err);
people.should.be.empty; info.count.should.equal(0);
Person.find({where: {name: 'Harry Hoe'}}, function(err, people) { Person.find({where: {name: 'Harry Hoe'}}, function(err, people) {
should.not.exist(err); if (err) return done(err);
people.should.have.length(5); people.should.be.empty;
done(); done();
}); });
}); });
}); });
});
it('should ignore where conditions with undefined values', it('should update instances that satisfy the where condition',
function(done) { function(done) {
Person.update({name: 'Brett Boe'}, {name: undefined, gender: 'male'}, Person.update({name: 'Brett Boe'}, {name: 'Harry Hoe'}, function(err,
function(err, results) { info) {
should.not.exist(err); if (err) return done(err);
results.should.have.property('count', 1) info.count.should.equal(1);
Person.find({where: {name: 'Brett Boe'}}, function(err, people) { Person.find({where: {age: 19}}, function(err, people) {
should.not.exist(err); if (err) return done(err);
people.should.have.length(1); people.should.have.length(1);
people[0].name.should.equal('Brett Boe'); 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');
done(); 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();
});
});
}); });
}); });