Merge pull request #537 from strongloop/cleanup/update-and-delete-tests

Clean up delete and update tests
This commit is contained in:
Simon Ho 2015-03-24 21:40:46 -07:00
commit b5188a5af4
1 changed files with 130 additions and 135 deletions

View File

@ -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();
});
});
});
});