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 dad4c5c6
authored
Jul 24, 2013
by
Daniel Durante
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added assets and sequelize test.
1 parent
9d56d77f
Hide whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
469 additions
and
0 deletions
test/assets/migrations/20111117063700-createPerson.js
test/assets/migrations/20111130161100-emptyMigration.js
test/assets/migrations/20111205064000-renamePersonToUser.js
test/assets/migrations/20111205162700-addSignatureColumnToUser.js
test/assets/migrations/20111206061400-removeShopIdColumnFromUser.js
test/assets/migrations/20111206063000-changeSignatureColumnOfUserToMendatory.js
test/assets/migrations/20111206163300-renameSignatureColumnOfUserToSig.js
test/assets/project.js
test/sequelize.test.js
test/assets/migrations/20111117063700-createPerson.js
0 → 100644
View file @
dad4c5c
module
.
exports
=
{
up
:
function
(
migration
,
DataTypes
,
done
)
{
migration
.
createTable
(
'Person'
,
{
name
:
DataTypes
.
STRING
,
isBetaMember
:
{
type
:
DataTypes
.
BOOLEAN
,
defaultValue
:
false
,
allowNull
:
false
}
})
.
complete
(
done
)
},
down
:
function
(
migration
,
DataTypes
,
done
)
{
migration
.
dropTable
(
'Person'
).
complete
(
done
)
}
}
test/assets/migrations/20111130161100-emptyMigration.js
0 → 100644
View file @
dad4c5c
module
.
exports
=
{
up
:
function
(
migration
,
DataTypes
,
done
)
{
done
()
},
down
:
function
(
migration
,
DataTypes
,
done
)
{
done
()
}
}
test/assets/migrations/20111205064000-renamePersonToUser.js
0 → 100644
View file @
dad4c5c
module
.
exports
=
{
up
:
function
(
migration
,
DataTypes
,
done
)
{
migration
.
renameTable
(
'Person'
,
'User'
).
complete
(
done
)
},
down
:
function
(
migration
,
DataTypes
,
done
)
{
migration
.
renameTable
(
'User'
,
'Person'
).
complete
(
done
)
}
}
test/assets/migrations/20111205162700-addSignatureColumnToUser.js
0 → 100644
View file @
dad4c5c
module
.
exports
=
{
up
:
function
(
migration
,
DataTypes
,
done
)
{
migration
.
addColumn
(
'User'
,
'isAdmin'
,
{
type
:
DataTypes
.
BOOLEAN
,
defaultValue
:
false
,
allowNull
:
false
})
.
complete
(
function
(
err
)
{
if
(
err
)
{
done
(
err
)
}
else
{
migration
.
addColumn
(
'User'
,
'signature'
,
DataTypes
.
TEXT
)
.
complete
(
function
(
err
)
{
if
(
err
)
{
done
(
err
)
}
else
{
migration
.
addColumn
(
'User'
,
'shopId'
,
{
type
:
DataTypes
.
INTEGER
,
allowNull
:
true
}).
complete
(
done
)
}
})
}
})
},
down
:
function
(
migration
,
DataTypes
,
done
)
{
migration
.
removeColumn
(
'User'
,
'signature'
).
complete
(
function
(
err
)
{
if
(
err
)
{
done
(
err
)
}
else
{
migration
.
removeColumn
(
'User'
,
'shopId'
).
complete
(
function
(
err
)
{
if
(
err
)
{
done
(
err
)
}
else
{
migration
.
removeColumn
(
'User'
,
'isAdmin'
).
complete
(
done
)
}
})
}
})
}
}
test/assets/migrations/20111206061400-removeShopIdColumnFromUser.js
0 → 100644
View file @
dad4c5c
module
.
exports
=
{
up
:
function
(
migration
,
DataTypes
,
done
)
{
migration
.
removeColumn
(
'User'
,
'shopId'
).
complete
(
done
)
},
down
:
function
(
migration
,
DataTypes
,
done
)
{
migration
.
addColumn
(
'User'
,
'shopId'
,
{
type
:
DataTypes
.
INTEGER
,
allowNull
:
true
}).
complete
(
done
)
}
}
test/assets/migrations/20111206063000-changeSignatureColumnOfUserToMendatory.js
0 → 100644
View file @
dad4c5c
module
.
exports
=
{
up
:
function
(
migration
,
DataTypes
,
done
)
{
migration
.
changeColumn
(
'User'
,
'signature'
,
{
type
:
DataTypes
.
STRING
,
allowNull
:
false
,
defaultValue
:
'Signature'
}).
complete
(
done
)
},
down
:
function
(
migration
,
DataTypes
,
done
)
{
done
()
}
}
test/assets/migrations/20111206163300-renameSignatureColumnOfUserToSig.js
0 → 100644
View file @
dad4c5c
module
.
exports
=
{
up
:
function
(
migration
,
DataTypes
,
done
)
{
migration
.
renameColumn
(
'User'
,
'signature'
,
'sig'
).
complete
(
done
)
},
down
:
function
(
migration
,
DataTypes
,
done
)
{
migration
.
renameColumn
(
'User'
,
'sig'
,
'signature'
).
complete
(
done
)
}
}
test/assets/project.js
0 → 100644
View file @
dad4c5c
module
.
exports
=
function
(
sequelize
,
DataTypes
)
{
return
sequelize
.
define
(
'Project'
+
parseInt
(
Math
.
random
()
*
9999999999999999
),
{
name
:
DataTypes
.
STRING
})
}
\ No newline at end of file
test/sequelize.test.js
0 → 100644
View file @
dad4c5c
var
chai
=
require
(
'chai'
)
,
expect
=
chai
.
expect
,
Support
=
require
(
__dirname
+
'/support'
)
,
DataTypes
=
require
(
__dirname
+
"/../lib/data-types"
)
,
dialect
=
Support
.
getTestDialect
()
,
_
=
require
(
'lodash'
)
,
Sequelize
=
require
(
__dirname
+
'/../index'
)
,
config
=
require
(
__dirname
+
"/config/config"
)
,
moment
=
require
(
'moment'
)
chai
.
Assertion
.
includeStack
=
true
var
qq
=
function
(
str
)
{
if
(
dialect
==
'postgres'
||
dialect
==
'sqlite'
)
{
return
'"'
+
str
+
'"'
}
else
if
(
dialect
==
'mysql'
)
{
return
'`'
+
str
+
'`'
}
else
{
return
str
}
}
describe
(
Support
.
getTestDialectTeaser
(
"Sequelize"
),
function
()
{
describe
(
'constructor'
,
function
()
{
it
(
'should pass the global options correctly'
,
function
(
done
)
{
var
sequelize
=
Support
.
createSequelizeInstance
({
logging
:
false
,
define
:
{
underscored
:
true
}
})
,
DAO
=
sequelize
.
define
(
'dao'
,
{
name
:
DataTypes
.
STRING
})
expect
(
DAO
.
options
.
underscored
).
to
.
be
.
ok
done
()
})
it
(
'should correctly set the host and the port'
,
function
(
done
)
{
var
sequelize
=
Support
.
createSequelizeInstance
({
host
:
'127.0.0.1'
,
port
:
1234
})
expect
(
sequelize
.
config
.
port
).
to
.
equal
(
1234
)
expect
(
sequelize
.
config
.
host
).
to
.
equal
(
'127.0.0.1'
)
done
()
})
})
describe
(
'isDefined'
,
function
()
{
it
(
"returns false if the dao wasn't defined before"
,
function
()
{
expect
(
this
.
sequelize
.
isDefined
(
'Project'
)).
to
.
be
.
false
})
it
(
"returns true if the dao was defined before"
,
function
()
{
this
.
sequelize
.
define
(
'Project'
,
{
name
:
DataTypes
.
STRING
})
expect
(
this
.
sequelize
.
isDefined
(
'Project'
)).
to
.
be
.
true
})
})
describe
(
'query'
,
function
()
{
beforeEach
(
function
(
done
)
{
this
.
User
=
this
.
sequelize
.
define
(
'User'
,
{
username
:
DataTypes
.
STRING
})
this
.
insertQuery
=
"INSERT INTO "
+
qq
(
this
.
User
.
tableName
)
+
" (username, "
+
qq
(
"createdAt"
)
+
", "
+
qq
(
"updatedAt"
)
+
") VALUES ('john', '2012-01-01 10:10:10', '2012-01-01 10:10:10')"
this
.
User
.
sync
({
force
:
true
}).
success
(
function
()
{
done
()
})
})
it
(
'executes a query the internal way'
,
function
(
done
)
{
this
.
sequelize
.
query
(
this
.
insertQuery
,
null
,
{
raw
:
true
})
.
complete
(
function
(
err
,
result
)
{
expect
(
err
).
to
.
be
.
null
expect
(
result
).
to
.
be
.
null
done
()
})
})
it
(
'executes a query if only the sql is passed'
,
function
(
done
)
{
this
.
sequelize
.
query
(
this
.
insertQuery
)
.
complete
(
function
(
err
,
result
)
{
expect
(
err
).
to
.
be
.
null
expect
(
result
).
to
.
not
.
exist
done
()
})
})
it
(
'executes select queries correctly'
,
function
(
done
)
{
var
self
=
this
self
.
sequelize
.
query
(
this
.
insertQuery
).
success
(
function
()
{
self
.
sequelize
.
query
(
"select * from "
+
qq
(
self
.
User
.
tableName
)
+
""
)
.
complete
(
function
(
err
,
users
)
{
expect
(
err
).
to
.
be
.
null
expect
(
users
.
map
(
function
(
u
){
return
u
.
username
})).
to
.
include
(
'john'
)
done
()
})
})
})
it
(
'executes select queries correctly when quoteIdentifiers is false'
,
function
(
done
)
{
var
self
=
this
,
seq
=
Object
.
create
(
self
.
sequelize
)
seq
.
options
.
quoteIdentifiers
=
false
seq
.
query
(
this
.
insertQuery
).
success
(
function
()
{
seq
.
query
(
"select * from "
+
qq
(
self
.
User
.
tableName
)
+
""
)
.
complete
(
function
(
err
,
users
)
{
expect
(
err
).
to
.
be
.
null
expect
(
users
.
map
(
function
(
u
){
return
u
.
username
})).
to
.
include
(
'john'
)
done
()
})
})
})
it
(
'executes select query and parses dot notation results'
,
function
(
done
)
{
var
self
=
this
self
.
sequelize
.
query
(
'DELETE FROM '
+
qq
(
self
.
User
.
tableName
)).
complete
(
function
()
{
self
.
sequelize
.
query
(
self
.
insertQuery
).
success
(
function
()
{
self
.
sequelize
.
query
(
"select username as "
+
qq
(
"user.username"
)
+
" from "
+
qq
(
self
.
User
.
tableName
)
+
""
)
.
complete
(
function
(
err
,
users
)
{
expect
(
err
).
to
.
be
.
null
expect
(
users
.
map
(
function
(
u
){
return
u
.
user
})).
to
.
deep
.
equal
([{
'username'
:
'john'
}])
done
()
})
})
})
})
if
(
dialect
==
'mysql'
)
{
it
(
'executes stored procedures'
,
function
(
done
)
{
var
self
=
this
self
.
sequelize
.
query
(
this
.
insertQuery
).
success
(
function
()
{
self
.
sequelize
.
query
(
'DROP PROCEDURE IF EXISTS foo'
).
success
(
function
()
{
self
.
sequelize
.
query
(
"CREATE PROCEDURE foo()\nSELECT * FROM "
+
self
.
User
.
tableName
+
";"
).
success
(
function
()
{
self
.
sequelize
.
query
(
'CALL foo()'
).
success
(
function
(
users
)
{
expect
(
users
.
map
(
function
(
u
){
return
u
.
username
})).
to
.
include
(
'john'
)
done
()
})
})
})
})
})
}
else
{
console
.
log
(
'FIXME: I want to be supported in this dialect as well :-('
)
}
it
(
'uses the passed DAOFactory'
,
function
(
done
)
{
var
self
=
this
self
.
sequelize
.
query
(
this
.
insertQuery
).
success
(
function
()
{
self
.
sequelize
.
query
(
"SELECT * FROM "
+
qq
(
self
.
User
.
tableName
)
+
";"
,
self
.
User
).
success
(
function
(
users
)
{
expect
(
users
[
0
].
__factory
).
to
.
equal
(
self
.
User
)
done
()
})
})
})
it
(
'destructs dot separated attributes when doing a raw query'
,
function
(
done
)
{
var
tickChar
=
(
dialect
===
'postgres'
)
?
'"'
:
'`'
,
sql
=
"select 1 as "
+
Sequelize
.
Utils
.
addTicks
(
'foo.bar.baz'
,
tickChar
)
this
.
sequelize
.
query
(
sql
,
null
,
{
raw
:
true
}).
success
(
function
(
result
)
{
expect
(
result
).
to
.
deep
.
equal
([
{
foo
:
{
bar
:
{
baz
:
1
}
}
}
])
done
()
})
})
it
(
'replaces token with the passed array'
,
function
(
done
)
{
this
.
sequelize
.
query
(
'select ? as foo, ? as bar'
,
null
,
{
raw
:
true
},
[
1
,
2
]).
success
(
function
(
result
)
{
expect
(
result
).
to
.
deep
.
equal
([{
foo
:
1
,
bar
:
2
}])
done
()
})
})
it
(
'handles AS in conjunction with functions just fine'
,
function
(
done
)
{
this
.
sequelize
.
query
(
'SELECT '
+
(
dialect
===
"sqlite"
?
'date(\'now\')'
:
'NOW()'
)
+
' AS t'
).
success
(
function
(
result
)
{
expect
(
moment
(
result
[
0
].
t
).
isValid
()).
to
.
be
.
true
done
()
})
})
})
describe
(
'define'
,
function
()
{
it
(
"adds a new dao to the dao manager"
,
function
(
done
)
{
expect
(
this
.
sequelize
.
daoFactoryManager
.
all
.
length
).
to
.
equal
(
0
)
this
.
sequelize
.
define
(
'foo'
,
{
title
:
DataTypes
.
STRING
})
expect
(
this
.
sequelize
.
daoFactoryManager
.
all
.
length
).
to
.
equal
(
1
)
done
()
})
it
(
"overwrites global options"
,
function
(
done
)
{
var
sequelize
=
Support
.
createSequelizeInstance
({
define
:
{
collate
:
'utf8_general_ci'
}
})
var
DAO
=
sequelize
.
define
(
'foo'
,
{
bar
:
DataTypes
.
STRING
},
{
collate
:
'utf8_bin'
})
expect
(
DAO
.
options
.
collate
).
to
.
equal
(
'utf8_bin'
)
done
()
})
it
(
"inherits global collate option"
,
function
(
done
)
{
var
sequelize
=
Support
.
createSequelizeInstance
({
define
:
{
collate
:
'utf8_general_ci'
}
})
var
DAO
=
sequelize
.
define
(
'foo'
,
{
bar
:
DataTypes
.
STRING
})
expect
(
DAO
.
options
.
collate
).
to
.
equal
(
'utf8_general_ci'
)
done
()
})
it
(
"inherits global classMethods and instanceMethods"
,
function
(
done
)
{
var
sequelize
=
Support
.
createSequelizeInstance
({
define
:
{
classMethods
:
{
globalClassMethod
:
function
()
{}
},
instanceMethods
:
{
globalInstanceMethod
:
function
()
{}
}
}
})
var
DAO
=
sequelize
.
define
(
'foo'
,
{
bar
:
DataTypes
.
STRING
},
{
classMethods
:
{
localClassMethod
:
function
()
{}
}
})
expect
(
typeof
DAO
.
options
.
classMethods
.
globalClassMethod
).
to
.
equal
(
'function'
)
expect
(
typeof
DAO
.
options
.
classMethods
.
localClassMethod
).
to
.
equal
(
'function'
)
expect
(
typeof
DAO
.
options
.
instanceMethods
.
globalInstanceMethod
).
to
.
equal
(
'function'
)
done
()
})
it
(
"uses the passed tableName"
,
function
(
done
)
{
var
self
=
this
,
Photo
=
this
.
sequelize
.
define
(
'Foto'
,
{
name
:
DataTypes
.
STRING
},
{
tableName
:
'photos'
})
Photo
.
sync
({
force
:
true
}).
success
(
function
()
{
self
.
sequelize
.
getQueryInterface
().
showAllTables
().
success
(
function
(
tableNames
)
{
expect
(
tableNames
).
to
.
include
(
'photos'
)
done
()
})
})
})
})
describe
(
'sync'
,
function
()
{
it
(
"synchronizes all daos"
,
function
(
done
)
{
var
Project
=
this
.
sequelize
.
define
(
'project'
+
config
.
rand
(),
{
title
:
DataTypes
.
STRING
})
var
Task
=
this
.
sequelize
.
define
(
'task'
+
config
.
rand
(),
{
title
:
DataTypes
.
STRING
})
Project
.
sync
({
force
:
true
}).
success
(
function
()
{
Task
.
sync
({
force
:
true
}).
success
(
function
()
{
Project
.
create
({
title
:
'bla'
}).
success
(
function
()
{
Task
.
create
({
title
:
'bla'
}).
success
(
function
(
task
){
expect
(
task
).
to
.
exist
expect
(
task
.
title
).
to
.
equal
(
'bla'
)
done
()
})
})
})
})
})
it
(
'works with correct database credentials'
,
function
(
done
)
{
var
User
=
this
.
sequelize
.
define
(
'User'
,
{
username
:
DataTypes
.
STRING
})
User
.
sync
().
success
(
function
()
{
expect
(
true
).
to
.
be
.
true
done
()
})
})
it
(
"fails with incorrect database credentials"
,
function
(
done
)
{
var
sequelize2
=
Support
.
getSequelizeInstance
(
'foo'
,
'bar'
,
null
,
{
logging
:
false
})
,
User2
=
sequelize2
.
define
(
'User'
,
{
name
:
DataTypes
.
STRING
,
bio
:
DataTypes
.
TEXT
})
User2
.
sync
().
error
(
function
(
err
)
{
expect
(
err
.
message
).
to
.
match
(
/.*Access
\
denied.*/
)
done
()
})
})
})
describe
(
'drop should work'
,
function
()
{
it
(
'correctly succeeds'
,
function
(
done
)
{
var
User
=
this
.
sequelize
.
define
(
'Users'
,
{
username
:
DataTypes
.
STRING
})
User
.
sync
({
force
:
true
}).
success
(
function
()
{
User
.
drop
().
success
(
function
()
{
expect
(
true
).
to
.
be
.
true
done
()
})
})
})
})
describe
(
'import'
,
function
()
{
it
(
"imports a dao definition from a file"
,
function
(
done
)
{
var
Project
=
this
.
sequelize
.
import
(
__dirname
+
"/assets/project"
)
expect
(
Project
).
to
.
exist
done
()
})
})
describe
(
'define'
,
function
()
{
[
{
type
:
DataTypes
.
ENUM
,
values
:
[
'scheduled'
,
'active'
,
'finished'
]},
DataTypes
.
ENUM
(
'scheduled'
,
'active'
,
'finished'
)
].
forEach
(
function
(
status
)
{
describe
(
'enum'
,
function
()
{
beforeEach
(
function
(
done
)
{
this
.
Review
=
this
.
sequelize
.
define
(
'review'
,
{
status
:
status
})
this
.
Review
.
sync
({
force
:
true
}).
success
(
function
()
{
done
()
})
})
it
(
'raises an error if no values are defined'
,
function
(
done
)
{
var
self
=
this
expect
(
function
()
{
self
.
sequelize
.
define
(
'omnomnom'
,
{
bla
:
{
type
:
DataTypes
.
ENUM
}
})
}).
to
.
throw
(
Error
,
'Values for ENUM haven\'t been defined.'
)
done
()
})
it
(
'correctly stores values'
,
function
(
done
)
{
this
.
Review
.
create
({
status
:
'active'
}).
success
(
function
(
review
)
{
expect
(
review
.
status
).
to
.
equal
(
'active'
)
done
()
})
})
it
(
'correctly loads values'
,
function
(
done
)
{
var
self
=
this
this
.
Review
.
create
({
status
:
'active'
}).
success
(
function
()
{
self
.
Review
.
findAll
().
success
(
function
(
reviews
)
{
expect
(
reviews
[
0
].
status
).
to
.
equal
(
'active'
)
done
()
})
})
})
it
(
"doesn't save an instance if value is not in the range of enums"
,
function
(
done
)
{
var
self
=
this
expect
(
function
()
{
self
.
Review
.
create
({
status
:
'fnord'
})
}).
to
.
throw
(
Error
,
'Value "fnord" for ENUM status is out of allowed scope. Allowed values: scheduled, active, finished'
)
done
()
})
})
})
describe
(
'table'
,
function
()
{
[
{
id
:
{
type
:
DataTypes
.
BIGINT
}
},
{
id
:
{
type
:
DataTypes
.
STRING
,
allowNull
:
true
}
},
{
id
:
{
type
:
DataTypes
.
BIGINT
,
allowNull
:
false
,
primaryKey
:
true
,
autoIncrement
:
true
}
}
].
forEach
(
function
(
customAttributes
)
{
it
(
'should be able to override options on the default attributes'
,
function
(
done
)
{
var
Picture
=
this
.
sequelize
.
define
(
'picture'
,
_
.
cloneDeep
(
customAttributes
))
Picture
.
sync
({
force
:
true
}).
success
(
function
()
{
Object
.
keys
(
customAttributes
).
forEach
(
function
(
attribute
)
{
Object
.
keys
(
customAttributes
[
attribute
]).
forEach
(
function
(
option
)
{
var
optionValue
=
customAttributes
[
attribute
][
option
];
expect
(
Picture
.
rawAttributes
[
attribute
][
option
]).
to
.
be
.
equal
(
optionValue
)
})
})
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