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

Commit 713b42f2 by Mick Hansen

tests: add tests covering .save() with fields changed/added in hooks and validation

1 parent 5cb09bbe
Showing with 133 additions and 0 deletions
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
var chai = require('chai') var chai = require('chai')
, expect = chai.expect , expect = chai.expect
, Sequelize = require('../index')
, Support = require(__dirname + '/support') , Support = require(__dirname + '/support')
, DataTypes = require(__dirname + '/../lib/data-types') , DataTypes = require(__dirname + '/../lib/data-types')
, dialect = Support.getTestDialect() , dialect = Support.getTestDialect()
...@@ -813,6 +814,138 @@ describe(Support.getTestDialectTeaser('Instance'), function() { ...@@ -813,6 +814,138 @@ describe(Support.getTestDialectTeaser('Instance'), function() {
}); });
}); });
describe('hooks', function () {
it('should update attributes added in hooks when default fields are used', function () {
var User = this.sequelize.define('User' + config.rand(), {
name: DataTypes.STRING,
bio: DataTypes.TEXT,
email: DataTypes.STRING
});
User.beforeUpdate(function(instance, options) {
instance.set('email', 'B');
});
return User.sync({force: true}).then(function() {
return User.create({
name: 'A',
bio: 'A',
email: 'A'
}).then(function (user) {
return user.set({
name: 'B',
bio: 'B'
}).save();
}).then(function () {
return User.findOne({});
}).then(function (user) {
expect(user.get('name')).to.equal('B');
expect(user.get('bio')).to.equal('B');
expect(user.get('email')).to.equal('B');
});
});
});
it('should update attributes changed in hooks when default fields are used', function () {
var User = this.sequelize.define('User' + config.rand(), {
name: DataTypes.STRING,
bio: DataTypes.TEXT,
email: DataTypes.STRING
});
User.beforeUpdate(function(instance, options) {
instance.set('email', 'C');
});
return User.sync({force: true}).then(function() {
return User.create({
name: 'A',
bio: 'A',
email: 'A'
}).then(function (user) {
return user.set({
name: 'B',
bio: 'B',
email: 'B'
}).save();
}).then(function () {
return User.findOne({});
}).then(function (user) {
expect(user.get('name')).to.equal('B');
expect(user.get('bio')).to.equal('B');
expect(user.get('email')).to.equal('C');
});
});
});
it('should validate attributes added in hooks when default fields are used', function () {
var User = this.sequelize.define('User' + config.rand(), {
name: DataTypes.STRING,
bio: DataTypes.TEXT,
email: {
type: DataTypes.STRING,
validate: {
isEmail: true
}
}
});
User.beforeUpdate(function(instance, options) {
instance.set('email', 'B');
});
return User.sync({force: true}).then(function () {
return User.create({
name: 'A',
bio: 'A',
email: 'valid.email@gmail.com'
}).then(function (user) {
return expect(user.set({
name: 'B'
}).save()).to.be.rejectedWith(Sequelize.ValidationError);
}).then(function () {
return User.findOne({}).then(function (user) {
expect(user.get('email')).to.equal('valid.email@gmail.com');
});
});
});
});
it('should validate attributes changed in hooks when default fields are used', function () {
var User = this.sequelize.define('User' + config.rand(), {
name: DataTypes.STRING,
bio: DataTypes.TEXT,
email: {
type: DataTypes.STRING,
validate: {
isEmail: true
}
}
});
User.beforeUpdate(function(instance, options) {
instance.set('email', 'B');
});
return User.sync({force: true}).then(function () {
return User.create({
name: 'A',
bio: 'A',
email: 'valid.email@gmail.com'
}).then(function (user) {
return expect(user.set({
name: 'B',
email: 'still.valid.email@gmail.com'
}).save()).to.be.rejectedWith(Sequelize.ValidationError);
}).then(function () {
return User.findOne({}).then(function (user) {
expect(user.get('email')).to.equal('valid.email@gmail.com');
});
});
});
});
});
it('stores an entry in the database', function(done) { it('stores an entry in the database', function(done) {
var username = 'user' var username = 'user'
, User = this.User , User = this.User
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!