-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: instance beforeUpdate hooks should not modify any Raw if there i…
…s no Raw assignment in them (#283) Co-authored-by: JimmyDaddy <[email protected]>
- Loading branch information
Showing
3 changed files
with
69 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,7 +27,8 @@ const attributes = { | |
}, | ||
desc: { | ||
type: DataTypes.STRING, | ||
} | ||
}, | ||
fingerprint: DataTypes.TEXT, | ||
}; | ||
|
||
describe('hooks', function() { | ||
|
@@ -136,6 +137,10 @@ describe('hooks', function() { | |
constructor(opts) { | ||
super(opts); | ||
} | ||
|
||
getFingerprint() { | ||
return this.attribute('fingerprint'); | ||
} | ||
} | ||
User.init(attributes, { | ||
hooks: { | ||
|
@@ -149,6 +154,16 @@ describe('hooks', function() { | |
obj.status = 11; | ||
} | ||
}, | ||
}, | ||
}, { | ||
set fingerprint(value) { | ||
if (this.attribute('fingerprint') != null) { | ||
throw new Error('user fingerprint cannot be modified'); | ||
} | ||
this.attribute('fingerprint', value); | ||
}, | ||
get fingerprint() { | ||
return undefined; | ||
} | ||
}); | ||
|
||
|
@@ -175,6 +190,34 @@ describe('hooks', function() { | |
}); | ||
assert.equal(user.email, '[email protected]'); | ||
assert.equal(user.status, 11); | ||
|
||
// instance.update before hooks special logic: setup_hooks.js#L131-L151 | ||
assert.deepEqual(user.fingerprint, undefined); | ||
assert.deepEqual(user.getFingerprint(), null); | ||
|
||
await assert.doesNotReject(async () => { | ||
await user.update({ | ||
fingerprint: 'halo' | ||
}); | ||
}); | ||
assert.deepEqual(user.fingerprint, undefined); | ||
assert.deepEqual(user.getFingerprint(), 'halo'); | ||
await assert.rejects(async () => { | ||
await user.update({ | ||
fingerprint: 'halo' | ||
}); | ||
}, /Error: user fingerprint cannot be modified/); | ||
|
||
await assert.doesNotReject(async () => { | ||
await user.update({ | ||
fingerprint: 'halo', | ||
nickname: 'Elden Lord', | ||
}, { | ||
fields: [ 'nickname' ] | ||
}); | ||
}); | ||
assert.equal(user.nickname, 'Elden Lord'); | ||
|
||
}); | ||
|
||
it('update skip hooks', async () => { | ||
|