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

Commit 5cb09bbe by Mick Hansen

test(instance): more cases for update with hooks

1 parent b206ebff
Showing with 68 additions and 1 deletions
...@@ -644,7 +644,8 @@ module.exports = (function() { ...@@ -644,7 +644,8 @@ module.exports = (function() {
Object.keys(self.Model.rawAttributes).forEach(function (attr) { Object.keys(self.Model.rawAttributes).forEach(function (attr) {
if (self.Model.rawAttributes[attr].field && if (self.Model.rawAttributes[attr].field &&
values[self.Model.rawAttributes[attr].field] !== undefined && values[self.Model.rawAttributes[attr].field] !== undefined &&
self.Model.rawAttributes[attr].field !== attr) { self.Model.rawAttributes[attr].field !== attr
) {
values[attr] = values[self.Model.rawAttributes[attr].field]; values[attr] = values[self.Model.rawAttributes[attr].field];
delete values[self.Model.rawAttributes[attr].field]; delete values[self.Model.rawAttributes[attr].field];
} }
......
...@@ -143,6 +143,38 @@ describe(Support.getTestDialectTeaser('Instance'), function() { ...@@ -143,6 +143,38 @@ describe(Support.getTestDialectTeaser('Instance'), function() {
}); });
}); });
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.update({
name: 'B',
bio: 'B',
email: 'B'
});
}).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 () { it('should validate attributes added in hooks when default fields are used', function () {
var User = this.sequelize.define('User' + config.rand(), { var User = this.sequelize.define('User' + config.rand(), {
name: DataTypes.STRING, name: DataTypes.STRING,
...@@ -175,6 +207,40 @@ describe(Support.getTestDialectTeaser('Instance'), function() { ...@@ -175,6 +207,40 @@ describe(Support.getTestDialectTeaser('Instance'), function() {
}); });
}); });
}); });
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.update({
name: 'B',
email: 'still.valid.email@gmail.com'
})).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 not set attributes that are not specified by fields', function () { it('should not set attributes that are not specified by fields', function () {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!