Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
public
/
sequelize
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
不要怂,就是干,撸起袖子干!
Commit 5cb09bbe
authored
Jan 07, 2015
by
Mick Hansen
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
test(instance): more cases for update with hooks
1 parent
b206ebff
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
69 additions
and
2 deletions
lib/instance.js
test/instance/update.test.js
lib/instance.js
View file @
5cb09bb
...
...
@@ -643,8 +643,9 @@ module.exports = (function() {
// Transfer database generated values (defaults, autoincrement, etc)
Object
.
keys
(
self
.
Model
.
rawAttributes
).
forEach
(
function
(
attr
)
{
if
(
self
.
Model
.
rawAttributes
[
attr
].
field
&&
values
[
self
.
Model
.
rawAttributes
[
attr
].
field
]
!==
undefined
&&
self
.
Model
.
rawAttributes
[
attr
].
field
!==
attr
)
{
values
[
self
.
Model
.
rawAttributes
[
attr
].
field
]
!==
undefined
&&
self
.
Model
.
rawAttributes
[
attr
].
field
!==
attr
)
{
values
[
attr
]
=
values
[
self
.
Model
.
rawAttributes
[
attr
].
field
];
delete
values
[
self
.
Model
.
rawAttributes
[
attr
].
field
];
}
...
...
test/instance/update.test.js
View file @
5cb09bb
...
...
@@ -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
()
{
var
User
=
this
.
sequelize
.
define
(
'User'
+
config
.
rand
(),
{
name
:
DataTypes
.
STRING
,
...
...
@@ -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
()
{
...
...
Write
Preview
Markdown
is supported
Attach a file
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to post a comment