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 713b42f2
authored
Jan 08, 2015
by
Mick Hansen
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
tests: add tests covering .save() with fields changed/added in hooks and validation
1 parent
5cb09bbe
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
133 additions
and
0 deletions
test/instance.test.js
test/instance.test.js
View file @
713b42f
...
@@ -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
...
...
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