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

Commit ce00eeac by Daniel Durante

Added specs for the new Model.update behavior, also added even more spec tests f…

…or column id/auto increment/primary key field validating properly. Closes #220 and #799
1 parent 21fcf6a3
Showing with 84 additions and 18 deletions
......@@ -253,6 +253,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) {
......@@ -301,7 +345,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: {
......@@ -322,41 +366,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!