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

Commit 65fe70fd by Daniel Durante

Merge pull request #813 from durango/id-validations

Added specs to explicitly test for auto incremented/primary key validati...
2 parents da325d96 b907c1ad
Showing with 95 additions and 31 deletions
...@@ -254,46 +254,110 @@ describe(Support.getTestDialectTeaser("DaoValidator"), function() { ...@@ -254,46 +254,110 @@ describe(Support.getTestDialectTeaser("DaoValidator"), function() {
} }
describe('#create', function() { describe('#create', function() {
beforeEach(function(done) { describe('generic', function() {
var self = this beforeEach(function(done) {
var self = this
var Project = this.sequelize.define('Project', {
name: { var Project = this.sequelize.define('Project', {
type: Sequelize.STRING, name: {
allowNull: false, type: Sequelize.STRING,
defaultValue: 'unknown', allowNull: false,
validate: { defaultValue: 'unknown',
isIn: [['unknown', 'hello', 'test']] validate: {
isIn: [['unknown', 'hello', 'test']]
}
} }
} })
var Task = this.sequelize.define('Task', {
something: Sequelize.INTEGER
})
Project.hasOne(Task)
Task.hasOne(Project)
Project.sync({ force: true }).success(function() {
Task.sync({ force: true }).success(function() {
self.Project = Project
self.Task = Task
done()
})
})
})
it('correctly validates using create method ', function(done) {
var self = this
this.Project.create({}).success(function(project) {
self.Task.create({something: 1}).success(function(task) {
project.setTask(task).success(function(task) {
expect(task.ProjectId).to.not.be.null
task.setProject(project).success(function(project) {
expect(project.ProjectId).to.not.be.null
done()
})
})
})
})
}) })
})
describe('explicitly validating id/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: {
type: Sequelize.INTEGER,
autoIncrement: true,
primaryKey: true,
validate: {
isInt: true
}
}
})
var Task = this.sequelize.define('Task', { User.sync({ force: true }).success(function() {
something: Sequelize.INTEGER User.create({id: 'helloworld'}).error(function(err) {
expect(err).to.deep.equal({id: ['Invalid integer: id']})
done()
})
})
}) })
Project.hasOne(Task) it('should emit an error when we try to enter in a string for the id key with validation arguments', function(done) {
Task.hasOne(Project) var User = this.sequelize.define('UserId', {
id: {
type: Sequelize.INTEGER,
autoIncrement: true,
primaryKey: true,
validate: {
isInt: { args: true, msg: 'ID must be an integer!' }
}
}
})
Project.sync({ force: true }).success(function() { User.sync({ force: true }).success(function() {
Task.sync({ force: true }).success(function() { User.create({id: 'helloworld'}).error(function(err) {
self.Project = Project expect(err).to.deep.equal({id: ['ID must be an integer!']})
self.Task = Task done()
done() })
}) })
}) })
})
it('correctly validates using create method ', 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 self = this var User = this.sequelize.define('UserId', {
this.Project.create({}).success(function(project) { username: {
self.Task.create({something: 1}).success(function(task) { type: Sequelize.INTEGER,
project.setTask(task).success(function(task) { autoIncrement: true,
expect(task.ProjectId).to.not.be.null primaryKey: true,
task.setProject(project).success(function(project) { validate: {
expect(project.ProjectId).to.not.be.null isInt: { args: true, msg: 'Username must be an integer!' }
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!']})
done()
}) })
}) })
}) })
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!