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

Commit 4a0a869b by Sushant

(tests) #3534, values modified in validate hooks are saved

1 parent 6486f3c3
# Next # Future
- [FIXED] Confirmed that values modified in validation hooks are preserved [#3534](https://github.com/sequelize/sequelize/issues/3534)
- [FIXED] Support lower case type names in SQLite [#5482](https://github.com/sequelize/sequelize/issues/5482) - [FIXED] Support lower case type names in SQLite [#5482](https://github.com/sequelize/sequelize/issues/5482)
# 3.20.0 # 3.20.0
......
...@@ -41,6 +41,74 @@ describe(Support.getTestDialectTeaser('Hooks'), function() { ...@@ -41,6 +41,74 @@ describe(Support.getTestDialectTeaser('Hooks'), function() {
}); });
}); });
describe('#3534, hooks modifications', function() {
it('fields modified in hooks are saved', function() {
var self = this;
this.User.afterValidate(function(user, options) {
//if username is defined and has more than 5 char
user.username = user.username
? (user.username.length < 5 ? null : user.username)
: null;
user.username = user.username || 'Samorost 3';
});
this.User.beforeValidate(function(user, options) {
user.mood = user.mood || 'neutral';
});
return this.User.create({username: 'T', mood: 'neutral'}).then(function(user) {
expect(user.mood).to.equal('neutral');
expect(user.username).to.equal('Samorost 3');
//change attributes
user.mood = 'sad';
user.username = 'Samorost Good One';
return user.save();
}).then(function(uSaved) {
expect(uSaved.mood).to.equal('sad');
expect(uSaved.username).to.equal('Samorost Good One');
//change attributes, expect to be replaced by hooks
uSaved.username = 'One';
return uSaved.save();
}).then(function(uSaved) {
//attributes were replaced by hooks ?
expect(uSaved.mood).to.equal('sad');
expect(uSaved.username).to.equal('Samorost 3');
return self.User.findById(uSaved.id);
}).then(function(uFetched) {
expect(uFetched.mood).to.equal('sad');
expect(uFetched.username).to.equal('Samorost 3');
uFetched.mood = null;
uFetched.username = 'New Game is Needed';
return uFetched.save();
}).then(function(uFetchedSaved) {
expect(uFetchedSaved.mood).to.equal('neutral');
expect(uFetchedSaved.username).to.equal('New Game is Needed');
return self.User.findById(uFetchedSaved.id);
}).then(function(uFetched) {
expect(uFetched.mood).to.equal('neutral');
expect(uFetched.username).to.equal('New Game is Needed');
//expect to be replaced by hooks
uFetched.username = 'New';
uFetched.mood = 'happy';
return uFetched.save();
}).then(function(uFetchedSaved) {
expect(uFetchedSaved.mood).to.equal('happy');
expect(uFetchedSaved.username).to.equal('Samorost 3');
});
});
});
describe('on error', function() { describe('on error', function() {
it('should emit an error from after hook', function() { it('should emit an error from after hook', function() {
this.User.afterValidate(function(user, options) { this.User.afterValidate(function(user, options) {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!