Skip to content

Commit

Permalink
Apply feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
ssh24 committed Jul 31, 2017
1 parent 5fe2c6b commit 8e3865a
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 19 deletions.
3 changes: 0 additions & 3 deletions lib/dao.js
Original file line number Diff line number Diff line change
Expand Up @@ -2698,9 +2698,6 @@ DataAccessObject.updateAll = function(where, data, options, cb) {
}
update = inst.toObject(false);

Model.applyProperties(update, inst);
Model = Model.lookupModel(update);

// validation required
inst.isValid(function(valid) {
if (valid) {
Expand Down
114 changes: 98 additions & 16 deletions test/validations.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -417,18 +417,28 @@ describe('validations', function() {
});

it('succeeds when validate condition is met', function(done) {
Employee.updateAll({where: {id: 1}}, {name: 'Foo-new', age: 5},
var data = {name: 'Foo-new', age: 5};
Employee.updateAll({id: 1}, data,
function(err, emp) {
should.not.exist(err);
should.exist(emp);
done();
should.equal(emp.count, 1);
Employee.find({where: {id: 1}}, function(err, emp) {
should.not.exist(err);
should.exist(emp);
data.id = 1;
should.deepEqual(data, emp[0].toObject());
done();
});
});
});

it('throws err when validate condition is not met', function(done) {
Employee.updateAll({where: {id: 1}}, {name: 'Foo-new'},
function(err, emp) {
should.exist(err);
should.equal(err.statusCode, 422);
should.equal(err.details.messages.age[0], 'can\'t be blank');
done();
});
});
Expand Down Expand Up @@ -464,18 +474,29 @@ describe('validations', function() {
});

it('succeeds when validate condition is met', function(done) {
Employee.updateAll({where: {id: 1}}, {age: 5},
var data = {age: 5};
Employee.updateAll({id: 1}, data,
function(err, emp) {
should.not.exist(err);
should.exist(emp);
done();
should.equal(emp.count, 1);
Employee.find({where: {id: 1}}, function(err, emp) {
should.not.exist(err);
should.exist(emp);
data.id = 1;
data.name = 'Foo';
should.deepEqual(data, emp[0].toObject());
done();
});
});
});

it('throws err when validate condition is not met', function(done) {
Employee.updateAll({where: {id: 1}}, {name: 'Foo-new', age: 5},
function(err, emp) {
should.exist(err);
should.equal(err.statusCode, 422);
should.equal(err.details.messages.name[0], 'can\'t be set');
done();
});
});
Expand Down Expand Up @@ -710,18 +731,28 @@ describe('validations', function() {
});

it('succeeds when validate condition is met', function(done) {
Employee.updateAll({where: {id: 1}}, {name: 'Foo-new', age: 5},
var data = {name: 'Foo-new', age: 5};
Employee.updateAll({id: 1}, data,
function(err, emp) {
should.not.exist(err);
should.exist(emp);
done();
should.equal(emp.count, 1);
Employee.find({where: {id: 1}}, function(err, emp) {
should.not.exist(err);
should.exist(emp);
data.id = 1;
should.deepEqual(data, emp[0].toObject());
done();
});
});
});

it('throws err when validate condition is not met', function(done) {
Employee.updateAll({where: {id: 1}}, {name: 'Bar', age: 5},
function(err, emp) {
should.exist(err);
should.equal(err.statusCode, 422);
should.equal(err.details.messages.name[0], 'is not unique');
done();
});
});
Expand Down Expand Up @@ -774,18 +805,28 @@ describe('validations', function() {
});

it('succeeds when validate condition is met', function(done) {
Employee.updateAll({where: {id: 1}}, {name: 'Foo Mo', age: 5},
var data = {name: 'Foo Mo', age: 5};
Employee.updateAll({id: 1}, data,
function(err, emp) {
should.not.exist(err);
should.exist(emp);
done();
should.equal(emp.count, 1);
Employee.find({where: {id: 1}}, function(err, emp) {
should.not.exist(err);
should.exist(emp);
data.id = 1;
should.deepEqual(data, emp[0].toObject());
done();
});
});
});

it('throws err when validate condition is not met', function(done) {
Employee.updateAll({where: {id: 1}}, {name: '45foo', age: 5},
function(err, emp) {
should.exist(err);
should.equal(err.statusCode, 422);
should.equal(err.details.messages.name[0], 'is invalid');
done();
});
});
Expand Down Expand Up @@ -870,18 +911,28 @@ describe('validations', function() {
});

it('succeeds when validate condition is met', function(done) {
Employee.updateAll({where: {id: 1}}, {name: 'Foo-new', age: 5},
var data = {name: 'Foo-new', age: 5};
Employee.updateAll({id: 1}, data,
function(err, emp) {
should.not.exist(err);
should.exist(emp);
done();
should.equal(emp.count, 1);
Employee.find({where: {id: 1}}, function(err, emp) {
should.not.exist(err);
should.exist(emp);
data.id = 1;
should.deepEqual(data, emp[0].toObject());
done();
});
});
});

it('throws err when validate condition is not met', function(done) {
Employee.updateAll({where: {id: 1}}, {age: {someAge: 5}},
function(err, emp) {
should.exist(err);
should.equal(err.statusCode, 422);
should.equal(err.details.messages.age[0], 'is not a number');
done();
});
});
Expand Down Expand Up @@ -976,18 +1027,29 @@ describe('validations', function() {
});

it('succeeds when validate condition is met', function(done) {
Employee.updateAll({where: {id: 1}}, {name: 'Foo-new', age: 5},
var data = {name: 'Foo-new', age: 5};
Employee.updateAll({id: 1}, data,
function(err, emp) {
should.not.exist(err);
should.exist(emp);
done();
should.equal(emp.count, 1);
Employee.find({where: {id: 1}}, function(err, emp) {
should.not.exist(err);
should.exist(emp);
data.id = 1;
should.deepEqual(data, emp[0].toObject());
done();
});
});
});

it('throws err when validate condition is not met', function(done) {
Employee.updateAll({where: {id: 1}}, {name: 'Foo-new2', age: 5},
function(err, emp) {
should.exist(err);
should.equal(err.statusCode, 422);
should.equal(err.details.messages.name[0], 'is not included in ' +
'the list');
done();
});
});
Expand Down Expand Up @@ -1063,18 +1125,28 @@ describe('validations', function() {
});

it('succeeds when validate condition is met', function(done) {
Employee.updateAll({where: {id: 1}}, {name: 'Foo-new', age: 5},
var data = {name: 'Foo-new', age: 5};
Employee.updateAll({id: 1}, data,
function(err, emp) {
should.not.exist(err);
should.exist(emp);
done();
should.equal(emp.count, 1);
Employee.find({where: {id: 1}}, function(err, emp) {
should.not.exist(err);
should.exist(emp);
data.id = 1;
should.deepEqual(data, emp[0].toObject());
done();
});
});
});

it('throws err when validate condition is not met', function(done) {
Employee.updateAll({where: {id: 1}}, {name: 'Bob', age: 5},
function(err, emp) {
should.exist(err);
should.equal(err.statusCode, 422);
should.equal(err.details.messages.name[0], 'is reserved');
done();
});
});
Expand Down Expand Up @@ -1102,18 +1174,28 @@ describe('validations', function() {
});

it('succeeds when validate condition is met', function(done) {
Employee.updateAll({where: {id: 1}}, {name: 'Foo-new', age: 5},
var data = {name: 'Foo-new', age: 5};
Employee.updateAll({id: 1}, data,
function(err, emp) {
should.not.exist(err);
should.exist(emp);
done();
should.equal(emp.count, 1);
Employee.find({where: {id: 1}}, function(err, emp) {
should.not.exist(err);
should.exist(emp);
data.id = 1;
should.deepEqual(data, emp[0].toObject());
done();
});
});
});

it('throws err when validate condition is not met', function(done) {
Employee.updateAll({where: {id: 1}}, {name: 'Bob', age: 5},
function(err, emp) {
should.exist(err);
should.equal(err.statusCode, 422);
should.equal(err.details.messages.name[0], 'too short');
done();
});
});
Expand Down

0 comments on commit 8e3865a

Please sign in to comment.