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 95a23b6d
authored
Aug 03, 2013
by
Sascha Depold
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'master' of github.com:sequelize/sequelize
2 parents
d336d620
85c231b9
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
71 additions
and
6 deletions
lib/dao.js
test/dao-factory.test.js
test/mysql/dao-factory.test.js
lib/dao.js
View file @
95a23b6
...
...
@@ -125,10 +125,24 @@ module.exports = (function() {
for
(
var
attrName
in
this
.
daoFactory
.
rawAttributes
)
{
if
(
this
.
daoFactory
.
rawAttributes
.
hasOwnProperty
(
attrName
))
{
var
definition
=
this
.
daoFactory
.
rawAttributes
[
attrName
]
,
isEnum
=
(
definition
.
type
&&
(
definition
.
type
.
toString
()
===
DataTypes
.
ENUM
.
toString
()))
,
isHstore
=
(
!!
definition
.
type
&&
!!
definition
.
type
.
type
&&
definition
.
type
.
type
===
DataTypes
.
HSTORE
.
type
)
,
hasValue
=
(
typeof
values
[
attrName
]
!==
'undefined'
)
,
valueOutOfScope
=
((
definition
.
values
||
[]).
indexOf
(
values
[
attrName
])
===
-
1
)
,
isEnum
=
definition
.
type
&&
(
definition
.
type
.
toString
()
===
DataTypes
.
ENUM
.
toString
())
,
isHstore
=
!!
definition
.
type
&&
!!
definition
.
type
.
type
&&
definition
.
type
.
type
===
DataTypes
.
HSTORE
.
type
,
hasValue
=
values
[
attrName
]
!==
undefined
,
isMySQL
=
this
.
daoFactory
.
daoFactoryManager
.
sequelize
.
options
.
dialect
===
"mysql"
,
ciCollation
=
!!
this
.
daoFactory
.
options
.
collate
&&
this
.
daoFactory
.
options
.
collate
.
match
(
/_ci$/i
)
,
valueOutOfScope
if
(
isEnum
&&
isMySQL
&&
ciCollation
&&
hasValue
)
{
var
scopeIndex
=
(
definition
.
values
||
[]).
map
(
function
(
d
)
{
return
d
.
toLowerCase
()
}).
indexOf
(
values
[
attrName
].
toLowerCase
())
valueOutOfScope
=
scopeIndex
===
-
1
// We'll return what the actual case will be, since a simple SELECT query would do the same...
if
(
!
valueOutOfScope
)
{
values
[
attrName
]
=
definition
.
values
[
scopeIndex
]
}
}
else
{
valueOutOfScope
=
((
definition
.
values
||
[]).
indexOf
(
values
[
attrName
])
===
-
1
)
}
if
(
isEnum
&&
hasValue
&&
valueOutOfScope
)
{
throw
new
Error
(
'Value "'
+
values
[
attrName
]
+
'" for ENUM '
+
attrName
+
' is out of allowed scope. Allowed values: '
+
definition
.
values
.
join
(
', '
))
...
...
test/dao-factory.test.js
View file @
95a23b6
...
...
@@ -861,8 +861,8 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
expect
(
users
[
1
].
username
).
to
.
equal
(
"Bill"
)
expect
(
users
[
2
].
username
).
to
.
equal
(
"Bob"
)
expect
(
parseInt
(
+
users
[
0
].
updatedAt
/
5000
,
10
)).
to
.
equal
(
parseInt
(
+
new
Date
()
/
5000
,
10
)
)
expect
(
parseInt
(
+
users
[
1
].
updatedAt
/
5000
,
10
)).
to
.
equal
(
parseInt
(
+
new
Date
()
/
5000
,
10
)
)
expect
(
parseInt
(
+
users
[
0
].
updatedAt
/
5000
,
10
)).
to
.
be
.
closeTo
(
parseInt
(
+
new
Date
()
/
5000
,
10
),
1
)
expect
(
parseInt
(
+
users
[
1
].
updatedAt
/
5000
,
10
)).
to
.
be
.
closeTo
(
parseInt
(
+
new
Date
()
/
5000
,
10
),
1
)
done
()
})
...
...
test/mysql/dao-factory.test.js
View file @
95a23b6
...
...
@@ -85,6 +85,57 @@ if (dialect.match(/^mysql/)) {
})
})
describe
(
'validations'
,
function
()
{
describe
(
'enums'
,
function
()
{
it
(
'enum data type should be case insensitive if my collation allows it'
,
function
(
done
)
{
var
User
=
this
.
sequelize
.
define
(
'User'
+
config
.
rand
(),
{
mood
:
{
type
:
DataTypes
.
ENUM
,
values
:
[
'HAPPY'
,
'sad'
,
'WhatEver'
]
}
},
{
collate
:
'utf8_general_ci'
})
User
.
sync
({
force
:
true
}).
success
(
function
()
{
User
.
create
({
mood
:
'happy'
}).
success
(
function
(
user
)
{
expect
(
user
).
to
.
exist
expect
(
user
.
mood
).
to
.
equal
(
'HAPPY'
)
var
u
=
User
.
build
({
mood
:
'SAD'
})
u
.
save
().
success
(
function
(
_user
)
{
expect
(
_user
).
to
.
exist
expect
(
_user
.
mood
).
to
.
equal
(
'sad'
)
done
()
})
})
})
})
it
(
'enum data type should be case sensitive if my collation enforces it'
,
function
(
done
)
{
var
User
=
this
.
sequelize
.
define
(
'User'
+
config
.
rand
(),
{
mood
:
{
type
:
DataTypes
.
ENUM
,
values
:
[
'HAPPY'
,
'sad'
,
'WhatEver'
]
}
},
{
collate
:
'latin1_bin'
})
User
.
sync
({
force
:
true
}).
success
(
function
()
{
expect
(
function
()
{
User
.
create
({
mood
:
'happy'
})
}).
to
.
throw
(
Error
,
'Value "happy" for ENUM mood is out of allowed scope. Allowed values: HAPPY, sad, WhatEver'
)
expect
(
function
()
{
var
u
=
User
.
build
({
mood
:
'SAD'
})
u
.
save
()
}).
to
.
throw
(
Error
,
'Value "SAD" for ENUM mood is out of allowed scope. Allowed values: HAPPY, sad, WhatEver'
)
done
()
})
})
})
})
describe
(
'primaryKeys'
,
function
()
{
it
(
"determines the correct primaryKeys"
,
function
(
done
)
{
var
User
=
this
.
sequelize
.
define
(
'User'
+
config
.
rand
(),
{
...
...
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