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 f5873a90
authored
Mar 11, 2014
by
Jan Aagaard Meier
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix for default values and setters
1 parent
2dfd1cec
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
37 additions
and
12 deletions
lib/dao.js
test/associations/has-many.test.js
test/dao-factory.test.js
test/dao.test.js
lib/dao.js
View file @
f5873a9
...
...
@@ -116,7 +116,6 @@ module.exports = (function() {
if
(
typeof
key
===
"object"
)
{
values
=
key
options
=
value
options
||
(
options
=
{})
if
(
options
.
reset
)
{
...
...
@@ -161,7 +160,7 @@ module.exports = (function() {
}
else
{
options
||
(
options
=
{})
originalValue
=
this
.
dataValues
[
key
]
// If not raw, and there's a customer setter
if
(
!
options
.
raw
&&
this
.
_customSetters
[
key
])
{
this
.
_customSetters
[
key
].
call
(
this
,
value
,
key
)
...
...
@@ -640,8 +639,9 @@ module.exports = (function() {
// private
var
initValues
=
function
(
values
,
options
)
{
var
defaults
,
key
;
var
defaults
,
defaultsOptions
=
_
.
defaults
({
raw
:
true
},
options
)
,
key
;
values
=
values
&&
_
.
clone
(
values
)
||
{}
...
...
@@ -670,7 +670,7 @@ module.exports = (function() {
delete
defaults
[
this
.
Model
.
_timestampAttributes
.
updatedAt
];
}
if
(
this
.
Model
.
_timestampAttributes
.
crea
tedAt
&&
defaults
[
this
.
Model
.
_timestampAttributes
.
deletedAt
])
{
if
(
this
.
Model
.
_timestampAttributes
.
dele
tedAt
&&
defaults
[
this
.
Model
.
_timestampAttributes
.
deletedAt
])
{
this
.
dataValues
[
this
.
Model
.
_timestampAttributes
.
deletedAt
]
=
Utils
.
toDefaultValue
(
defaults
[
this
.
Model
.
_timestampAttributes
.
deletedAt
]);
delete
defaults
[
this
.
Model
.
_timestampAttributes
.
deletedAt
];
}
...
...
@@ -678,7 +678,7 @@ module.exports = (function() {
if
(
Object
.
keys
(
defaults
).
length
)
{
for
(
key
in
defaults
)
{
if
(
!
values
.
hasOwnProperty
(
key
))
{
values
[
key
]
=
Utils
.
toDefaultValue
(
defaults
[
key
]
)
this
.
set
(
key
,
Utils
.
toDefaultValue
(
defaults
[
key
]),
defaultsOptions
)
}
}
}
...
...
test/associations/has-many.test.js
View file @
f5873a9
...
...
@@ -844,13 +844,13 @@ describe(Support.getTestDialectTeaser("HasMany"), function() {
,
spy
=
sinon
.
spy
()
this
.
User
.
create
({
username
:
'foo'
}).
success
(
function
(
user
)
{
self
.
Task
.
create
({
title
:
'task1'
}).
success
(
function
(
task1
)
{
self
.
Task
.
create
({
title
:
'task2'
}).
success
(
function
(
task2
)
{
self
.
Task
.
create
({
id
:
12
,
title
:
'task1'
}).
success
(
function
(
task1
)
{
self
.
Task
.
create
({
id
:
15
,
title
:
'task2'
}).
success
(
function
(
task2
)
{
user
.
setTasks
([
task1
,
task2
]).
on
(
'sql'
,
spy
).
on
(
'sql'
,
_
.
after
(
2
,
function
(
sql
)
{
expect
(
sql
).
to
.
have
.
string
(
"INSERT INTO"
)
expect
(
sql
).
to
.
have
.
string
(
"
VALUES (1,1),(1,2)"
)
var
tickChar
=
(
Support
.
getTestDialect
()
===
'postgres'
)
?
'"'
:
'`'
expect
(
sql
).
to
.
have
.
string
(
"
INSERT INTO %TasksUsers% (%UserId%,%TaskId%) VALUES (1,12),(1,15)"
.
replace
(
/%/g
,
tickChar
)
)
})).
success
(
function
()
{
expect
(
spy
.
calledTwice
).
to
.
be
.
ok
expect
(
spy
.
calledTwice
).
to
.
be
.
ok
// Once for SELECT, once for INSERT
done
()
})
})
...
...
test/dao-factory.test.js
View file @
f5873a9
...
...
@@ -253,6 +253,31 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
})
})
it
(
"returns proper defaultValues after save when setter is set"
,
function
(
done
)
{
var
titleSetter
=
sinon
.
spy
()
,
Task
=
this
.
sequelize
.
define
(
'TaskBuild'
,
{
title
:
{
type
:
Sequelize
.
STRING
(
50
),
allowNull
:
false
,
defaultValue
:
''
}
},
{
setterMethods
:
{
title
:
titleSetter
}
})
Task
.
sync
({
force
:
true
}).
success
(
function
()
{
Task
.
build
().
save
().
success
(
function
(
record
)
{
expect
(
record
.
title
).
to
.
be
.
a
(
'string'
)
expect
(
record
.
title
).
to
.
equal
(
''
)
expect
(
titleSetter
.
notCalled
).
to
.
be
.
ok
// The setter method should not be invoked for default values
done
()
}).
error
(
done
)
}).
error
(
done
)
})
it
(
'should work with both paranoid and underscored being true'
,
function
(
done
)
{
var
UserTable
=
this
.
sequelize
.
define
(
'UserCol'
,
{
aNumber
:
Sequelize
.
INTEGER
...
...
test/dao.test.js
View file @
f5873a9
...
...
@@ -1080,7 +1080,7 @@ describe(Support.getTestDialectTeaser("DAO"), function () {
it
(
'returns a response that can be stringified'
,
function
(
done
)
{
var
user
=
this
.
User
.
build
({
username
:
'test.user'
,
age
:
99
,
isAdmin
:
true
})
expect
(
JSON
.
stringify
(
user
)).
to
.
deep
.
equal
(
'{"
username":"test.user","age":99,"isAdmin":true,"id":null
}'
)
expect
(
JSON
.
stringify
(
user
)).
to
.
deep
.
equal
(
'{"
id":null,"username":"test.user","age":99,"isAdmin":true
}'
)
done
()
})
...
...
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