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,22 +756,30 @@ describe('manipulation', function () {
it('should destroy filtered set of records');
});
describe('deleteAll/destroyAll', function () {
beforeEach(function clearOldData(done) {
describe('when deleteAll/destroyAll executes', function () {
it('should be defined as function', function() {
Person.deleteAll.should.be.a.Function;
Person.destroyAll.should.be.a.Function;
});
describe('with multiple instances in the database', function() {
beforeEach(function deleteFixtures(done) {
Person.deleteAll(done);
});
beforeEach(function createTestData(done) {
Person.create([
{ name: 'John' },
{ name: 'Jane' }
], 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, result) {
it('should only delete instances that satisfy the where condition',
function(done) {
Person.deleteAll({name: 'John'}, function(err, info) {
if (err) return done(err);
result.should.have.property('count', 1);
info.count.should.equal(1);
Person.find({where: {name: 'John'}}, function(err, data) {
if (err) return done(err);
data.should.have.length(0);
@ -784,10 +792,11 @@ describe('manipulation', function () {
});
});
it('should report zero deleted instances', function (done) {
Person.deleteAll({name: 'does-not-match'}, function (err, result) {
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);
result.should.have.property('count', 0);
info.count.should.equal(0);
Person.count(function(err, count) {
if (err) return done(err);
count.should.equal(2);
@ -796,10 +805,11 @@ describe('manipulation', function () {
});
});
it('should delete all instances when "where" is not provided', function(done) {
Person.deleteAll(function (err, result) {
it('should delete all instances when the where condition is not provided',
function(done) {
Person.deleteAll(function (err, info) {
if (err) return done(err);
result.should.have.property('count', 2);
info.count.should.equal(2);
Person.count(function(err, count) {
if (err) return done(err);
count.should.equal(0);
@ -808,6 +818,7 @@ describe('manipulation', function () {
});
});
});
});
describe('initialize', function () {
it('should initialize object properly', function () {
@ -1005,8 +1016,14 @@ describe('manipulation', function () {
});
});
describe('update/updateAll', function() {
beforeEach(function destroyFixtures(done) {
describe('when update/updateAll executes', function() {
it('should be defined as a function', function() {
Person.update.should.be.a.Function;
Person.updateAll.should.be.a.Function;
});
describe('with existing instances in the database', function() {
beforeEach(function deleteFixtures(done) {
Person.destroyAll(done);
});
@ -1029,19 +1046,15 @@ describe('manipulation', function () {
}], 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) {
info) {
if (err) return done(err);
should.not.exist(err);
results.should.have.property('count', 0);
info.count.should.equal(0);
Person.find({where: {name: 'Harry Hoe'}}, function(err, people) {
should.not.exist(err);
if (err) return done(err);
people.should.be.empty;
done();
});
@ -1051,11 +1064,11 @@ describe('manipulation', function () {
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.should.have.property('count', 1);
info) {
if (err) return done(err);
info.count.should.equal(1);
Person.find({where: {age: 19}}, function(err, people) {
should.not.exist(err);
if (err) return done(err);
people.should.have.length(1);
people[0].name.should.equal('Harry Hoe');
done();
@ -1065,14 +1078,14 @@ describe('manipulation', function () {
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.should.have.property('count', 5);
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) {
should.not.exist(err);
if (err) return done(err);
people.should.be.empty;
Person.find({where: {name: 'Harry Hoe'}}, function(err, people) {
should.not.exist(err);
if (err) return done(err);
people.should.have.length(5);
done();
});
@ -1083,11 +1096,11 @@ describe('manipulation', function () {
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.should.have.property('count', 1)
function(err, info) {
if (err) return done(err);
info.count.should.equal(1);
Person.find({where: {name: 'Brett Boe'}}, function(err, people) {
should.not.exist(err);
if (err) return done(err);
people.should.have.length(1);
people[0].name.should.equal('Brett Boe');
done();
@ -1105,3 +1118,4 @@ describe('manipulation', function () {
});
});
});
});