不要怂,就是干,撸起袖子干!

Commit 6c68eb11 by Sascha Depold

Merge branch 'master' into milestones/2.0.0

2 parents e4ebdbd1 25a4f4b3
Showing with 97 additions and 18 deletions
......@@ -570,10 +570,23 @@ module.exports = (function() {
* @return {Object} A promise which fires `success`, `error`, `complete` and `sql`.
*/
DAOFactory.prototype.update = function(attrValueHash, where, options) {
options = options || {}
options.validate = options.validate || true
if(this.options.timestamps) {
var attr = this.options.underscored ? 'updated_at' : 'updatedAt'
attrValueHash[attr] = Utils.now()
}
if (options.validate === true) {
var validate = this.build(attrValueHash).validate()
if (validate !== null && Object.keys(validate).length > 0) {
return new Utils.CustomEventEmitter(function(emitter) {
emitter.emit('error', validate)
}).run()
}
}
return this.QueryInterface.bulkUpdate(this.tableName, attrValueHash, where, options)
}
......
......@@ -260,6 +260,50 @@ describe(Support.getTestDialectTeaser("DaoValidator"), function() {
}
}
describe('#update', function() {
it('should be able to emit an error upon updating when a validation has failed from an instance', function(done) {
var Model = this.sequelize.define('model', {
name: {
type: Sequelize.STRING,
validate: {
notNull: true, // won't allow null
notEmpty: true // don't allow empty strings
}
}
})
Model.sync({ force: true }).success(function() {
Model.create({name: 'World'}).success(function(model) {
model.updateAttributes({name: ''}).error(function(err) {
expect(err).to.deep.equal({ name: [ 'String is empty: name', 'String is empty: name' ] })
done()
})
})
})
})
it('should be able to emit an error upon updating when a validation has failed from the factory', function(done) {
var Model = this.sequelize.define('model', {
name: {
type: Sequelize.STRING,
validate: {
notNull: true, // won't allow null
notEmpty: true // don't allow empty strings
}
}
})
Model.sync({ force: true }).success(function() {
Model.create({name: 'World'}).success(function(model) {
Model.update({name: ''}, {id: 1}).error(function(err) {
expect(err).to.deep.equal({ name: [ 'String is empty: name', 'String is empty: name' ] })
done()
})
})
})
})
})
describe('#create', function() {
describe('generic', function() {
beforeEach(function(done) {
......@@ -315,7 +359,7 @@ describe(Support.getTestDialectTeaser("DaoValidator"), function() {
})
})
describe('explicitly validating id/primary/auto incremented columns', function() {
describe('explicitly validating primary/auto incremented columns', function() {
it('should emit an error when we try to enter in a string for the id key without validation arguments', function(done) {
var User = this.sequelize.define('UserId', {
id: {
......@@ -336,41 +380,63 @@ describe(Support.getTestDialectTeaser("DaoValidator"), function() {
})
})
it('should emit an error when we try to enter in a string for the id key with validation arguments', function(done) {
it('should emit an error when we try to enter in a string for an auto increment key (not named id)', function(done) {
var User = this.sequelize.define('UserId', {
id: {
username: {
type: Sequelize.INTEGER,
autoIncrement: true,
primaryKey: true,
validate: {
isInt: { args: true, msg: 'ID must be an integer!' }
isInt: { args: true, msg: 'Username must be an integer!' }
}
}
})
User.sync({ force: true }).success(function() {
User.create({id: 'helloworld'}).error(function(err) {
expect(err).to.deep.equal({id: ['ID must be an integer!']})
User.create({username: 'helloworldhelloworld'}).error(function(err) {
expect(err).to.deep.equal({username: ['Username must be an integer!']})
done()
})
})
})
it('should emit an error when we try to enter in a string for an auto increment key (not named id)', function(done) {
var User = this.sequelize.define('UserId', {
username: {
type: Sequelize.INTEGER,
autoIncrement: true,
primaryKey: true,
validate: {
isInt: { args: true, msg: 'Username must be an integer!' }
describe("primaryKey with the name as id with arguments for it's validation", function() {
beforeEach(function(done) {
this.User = this.sequelize.define('UserId', {
id: {
type: Sequelize.INTEGER,
autoIncrement: true,
primaryKey: true,
validate: {
isInt: { args: true, msg: 'ID must be an integer!' }
}
}
}
})
this.User.sync({ force: true }).success(function() {
done()
})
})
User.sync({ force: true }).success(function() {
User.create({username: 'helloworldhelloworld'}).error(function(err) {
expect(err).to.deep.equal({username: ['Username must be an integer!']})
it('should emit an error when we try to enter in a string for the id key with validation arguments', function(done) {
this.User.create({id: 'helloworld'}).error(function(err) {
expect(err).to.deep.equal({id: ['ID must be an integer!']})
done()
})
})
it('should emit an error when we try to enter in a string for an auto increment key through .build().validate()', function(done) {
var user = this.User.build({id: 'helloworld'})
, errors = user.validate()
expect(errors).to.deep.equal({ id: [ 'ID must be an integer!' ] })
done()
})
it('should emit an error when we try to .save()', function(done) {
var user = this.User.build({id: 'helloworld'})
user.save().error(function(err) {
expect(err).to.deep.equal({ id: [ 'ID must be an integer!' ] })
done()
})
})
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!