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 5cb80fd6
authored
Dec 21, 2011
by
sdepold
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
check specs for every single dialect
1 parent
c7d8063d
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
422 additions
and
386 deletions
spec/model-factory.spec.js
spec/model-factory.spec.js
View file @
5cb80fd
var
config
=
require
(
"./config/config"
)
var
config
=
require
(
"./config/config"
)
,
Sequelize
=
require
(
"../index"
)
,
Sequelize
=
require
(
"../index"
)
,
sequelize
=
new
Sequelize
(
config
.
database
,
config
.
username
,
config
.
password
,
{
logging
:
false
})
,
dialects
=
[
'sqlite'
,
'mysql'
]
,
Helpers
=
new
(
require
(
"./config/helpers"
))(
sequelize
)
describe
(
'ModelFactory'
,
function
()
{
describe
(
'ModelFactory'
,
function
()
{
var
User
=
null
dialects
.
forEach
(
function
(
dialect
)
{
describe
(
'with dialect "'
+
dialect
+
'"'
,
function
()
{
var
setup
=
function
(
options
)
{
var
User
=
null
User
=
sequelize
.
define
(
'User'
,
options
||
{
,
sequelize
=
new
Sequelize
(
config
.
database
,
config
.
username
,
config
.
password
,
{
age
:
Sequelize
.
INTEGER
,
logging
:
false
,
name
:
Sequelize
.
STRING
,
dialect
:
dialect
bio
:
Sequelize
.
TEXT
})
}
)
,
Helpers
=
new
(
require
(
"./config/helpers"
))(
sequelize
)
Helpers
.
dropAllTables
()
var
setup
=
function
(
options
)
{
User
=
sequelize
.
define
(
'User'
,
options
||
{
age
:
Sequelize
.
INTEGER
,
name
:
Sequelize
.
STRING
,
bio
:
Sequelize
.
TEXT
})
Helpers
.
async
(
function
(
done
)
{
Helpers
.
dropAllTables
()
User
.
sync
({
force
:
true
}).
success
(
done
)
})
}
beforeEach
(
function
()
{
setup
()
})
Helpers
.
async
(
function
(
done
)
{
afterEach
(
function
()
{
Helpers
.
dropAllTables
()
})
User
.
sync
({
force
:
true
}).
success
(
done
).
error
(
function
(
err
)
{
console
.
log
(
err
)
})
})
}
var
checkMatchForDialects
=
function
(
value
,
expectations
)
{
if
(
!!
expectations
[
dialect
])
expect
(
value
).
toMatch
(
expectations
[
dialect
])
else
throw
new
Error
(
'Undefined expectation for "'
+
dialect
+
'"!'
)
}
beforeEach
(
function
()
{
setup
()
})
afterEach
(
function
()
{
Helpers
.
dropAllTables
()
})
describe
(
'constructor'
,
function
()
{
it
(
"uses the passed model name as tablename if freezeTableName"
,
function
()
{
var
User
=
sequelize
.
define
(
'User'
,
{},
{
freezeTableName
:
true
})
expect
(
User
.
tableName
).
toEqual
(
'User'
)
})
describe
(
'constructor'
,
function
()
{
it
(
"uses the pluralized modelname as tablename unless freezeTableName"
,
function
()
{
it
(
"uses the passed model name as tablename if freezeTableName"
,
function
()
{
var
User
=
sequelize
.
define
(
'User'
,
{},
{
freezeTableName
:
false
})
var
User
=
sequelize
.
define
(
'User'
,
{},
{
freezeTableName
:
true
})
expect
(
User
.
tableName
).
toEqual
(
'Users'
)
expect
(
User
.
tableName
).
toEqual
(
'User'
)
})
})
it
(
"uses the pluralized modelname as tablename unless freezeTableName"
,
function
()
{
it
(
"attaches class and instance methods"
,
function
()
{
var
User
=
sequelize
.
define
(
'User'
,
{},
{
freezeTableName
:
false
})
var
User
=
sequelize
.
define
(
'User'
,
{},
{
expect
(
User
.
tableName
).
toEqual
(
'Users'
)
classMethods
:
{
doSmth
:
function
(){
return
1
}
},
})
instanceMethods
:
{
makeItSo
:
function
(){
return
2
}}
})
expect
(
User
.
doSmth
).
toBeDefined
()
expect
(
User
.
doSmth
()).
toEqual
(
1
)
expect
(
User
.
makeItSo
).
toBeUndefined
()
it
(
"attaches class and instance methods"
,
function
()
{
expect
(
User
.
build
().
makeItSo
).
toBeDefined
()
var
User
=
sequelize
.
define
(
'User'
,
{},
{
expect
(
User
.
build
().
makeItSo
()).
toEqual
(
2
)
classMethods
:
{
doSmth
:
function
(){
return
1
}
},
})
instanceMethods
:
{
makeItSo
:
function
(){
return
2
}}
})
expect
(
User
.
doSmth
).
toBeDefined
()
expect
(
User
.
doSmth
()).
toEqual
(
1
)
expect
(
User
.
makeItSo
).
toBeUndefined
()
expect
(
User
.
build
().
makeItSo
).
toBeDefined
()
it
(
"throws an error if 2 autoIncrements are passed"
,
function
()
{
expect
(
User
.
build
().
makeItSo
()).
toEqual
(
2
)
expect
(
function
()
{
})
var
User
=
sequelize
.
define
(
'User'
,
{
userid
:
{
type
:
Sequelize
.
INTEGER
,
primaryKey
:
true
,
autoIncrement
:
true
},
userscore
:
{
type
:
Sequelize
.
INTEGER
,
primaryKey
:
true
,
autoIncrement
:
true
},
})
}).
toThrow
(
'Invalid model definition. Only one autoincrement field allowed.'
)
it
(
"throws an error if 2 autoIncrements are passed"
,
function
()
{
expect
(
function
()
{
var
User
=
sequelize
.
define
(
'User'
,
{
userid
:
{
type
:
Sequelize
.
INTEGER
,
primaryKey
:
true
,
autoIncrement
:
true
},
userscore
:
{
type
:
Sequelize
.
INTEGER
,
primaryKey
:
true
,
autoIncrement
:
true
},
})
})
})
.
toThrow
(
'Invalid model definition. Only one autoincrement field allowed.'
)
})
})
describe
(
'build'
,
function
()
{
})
it
(
"doesn't create database entries"
,
function
()
{
Helpers
.
async
(
function
(
done
)
{
User
.
build
({
name
:
'John Wayne'
,
bio
:
'noot'
})
User
.
all
().
success
(
function
(
users
)
{
expect
(
users
.
length
).
toEqual
(
0
)
done
()
})
})
})
describe
(
'build'
,
function
()
{
it
(
"fills the objects with default values"
,
function
()
{
it
(
"doesn't create database entries"
,
function
()
{
var
Task
=
sequelize
.
define
(
'Task'
+
config
.
rand
(),
{
Helpers
.
async
(
function
(
done
)
{
title
:
{
type
:
Sequelize
.
STRING
,
defaultValue
:
'a task!'
},
User
.
build
({
name
:
'John Wayne'
,
bio
:
'noot'
})
foo
:
{
type
:
Sequelize
.
INTEGER
,
defaultValue
:
2
},
User
.
all
().
success
(
function
(
users
)
{
bar
:
{
type
:
Sequelize
.
DATE
},
expect
(
users
.
length
).
toEqual
(
0
)
foobar
:
{
type
:
Sequelize
.
TEXT
,
defaultValue
:
'asd'
},
done
()
flag
:
{
type
:
Sequelize
.
BOOLEAN
,
defaultValue
:
false
}
})
expect
(
Task
.
build
().
title
).
toEqual
(
'a task!'
)
expect
(
Task
.
build
().
foo
).
toEqual
(
2
)
expect
(
Task
.
build
().
bar
).
toEqual
(
null
)
expect
(
Task
.
build
().
foobar
).
toEqual
(
'asd'
)
expect
(
Task
.
build
().
flag
).
toEqual
(
false
)
})
})
})
})
})
it
(
"fills the objects with default values"
,
function
()
{
describe
(
'create'
,
function
()
{
var
Task
=
sequelize
.
define
(
'Task'
+
config
.
rand
(),
{
it
(
"doesn't allow duplicated records with unique:true"
,
function
()
{
title
:
{
type
:
Sequelize
.
STRING
,
defaultValue
:
'a task!'
},
setup
({
username
:
{
type
:
Sequelize
.
STRING
,
unique
:
true
}
})
foo
:
{
type
:
Sequelize
.
INTEGER
,
defaultValue
:
2
},
bar
:
{
type
:
Sequelize
.
DATE
},
Helpers
.
async
(
function
(
done
)
{
foobar
:
{
type
:
Sequelize
.
TEXT
,
defaultValue
:
'asd'
},
User
.
create
({
username
:
'foo'
}).
success
(
function
()
{
flag
:
{
type
:
Sequelize
.
BOOLEAN
,
defaultValue
:
false
}
User
.
create
({
username
:
'foo'
}).
error
(
function
(
err
)
{
})
expect
(
err
).
toBeDefined
()
expect
(
Task
.
build
().
title
).
toEqual
(
'a task!'
)
expect
(
Task
.
build
().
foo
).
toEqual
(
2
)
expect
(
Task
.
build
().
bar
).
toEqual
(
null
)
expect
(
Task
.
build
().
foobar
).
toEqual
(
'asd'
)
expect
(
Task
.
build
().
flag
).
toEqual
(
false
)
})
})
describe
(
'create'
,
function
()
{
checkMatchForDialects
(
err
.
message
,
{
it
(
"doesn't allow duplicated records with unique:true"
,
function
()
{
sqlite
:
/.*SQLITE_CONSTRAINT.*/
,
setup
({
username
:
{
type
:
Sequelize
.
STRING
,
unique
:
true
}
})
mysql
:
/.*Duplicate
\
entry.*/
})
Helpers
.
async
(
function
(
done
)
{
done
()
User
.
create
({
username
:
'foo'
}).
success
(
function
()
{
})
User
.
create
({
username
:
'foo'
}).
error
(
function
(
err
)
{
})
expect
(
err
.
message
).
toEqual
(
"Duplicate entry 'foo' for key 'username'"
)
done
()
})
})
})
})
})
})
it
(
"raises an error if created object breaks definition contraints"
,
function
()
{
it
(
"raises an error if created object breaks definition contraints"
,
function
()
{
setup
({
setup
({
username
:
{
type
:
Sequelize
.
STRING
,
unique
:
true
},
username
:
{
type
:
Sequelize
.
STRING
,
unique
:
true
},
smth
:
{
type
:
Sequelize
.
STRING
,
allowNull
:
false
}
smth
:
{
type
:
Sequelize
.
STRING
,
allowNull
:
false
}
})
})
Helpers
.
async
(
function
(
done
)
{
Helpers
.
async
(
function
(
done
)
{
User
.
create
({
username
:
'foo'
,
smth
:
null
}).
error
(
function
(
err
)
{
User
.
create
({
username
:
'foo'
,
smth
:
null
}).
error
(
function
(
err
)
{
expect
(
err
.
message
).
toEqual
(
"Column 'smth' cannot be null"
)
expect
(
err
).
toBeDefined
(
)
User
.
create
({
username
:
'foo'
,
smth
:
'foo'
}).
success
(
function
()
{
checkMatchForDialects
(
err
.
message
,
{
User
.
create
({
username
:
'foo'
,
smth
:
'bar'
}).
error
(
function
(
err
)
{
sqlite
:
/.*SQLITE_CONSTRAINT.*/
,
expect
(
err
.
message
).
toEqual
(
"Duplicate entry 'foo' for key 'username'"
)
mysql
:
"Column 'smth' cannot be null"
done
()
})
User
.
create
({
username
:
'foo'
,
smth
:
'foo'
}).
success
(
function
()
{
User
.
create
({
username
:
'foo'
,
smth
:
'bar'
}).
error
(
function
(
err
)
{
expect
(
err
).
toBeDefined
()
checkMatchForDialects
(
err
.
message
,
{
sqlite
:
/.*SQLITE_CONSTRAINT.*/
,
mysql
:
"Duplicate entry 'foo' for key 'username'"
})
done
()
})
})
})
})
})
})
})
})
})
})
it
(
'sets auto increment fields'
,
function
()
{
it
(
'sets auto increment fields'
,
function
()
{
setup
({
setup
({
userid
:
{
type
:
Sequelize
.
INTEGER
,
autoIncrement
:
true
,
primaryKey
:
true
,
allowNull
:
false
}
userid
:
{
type
:
Sequelize
.
INTEGER
,
autoIncrement
:
true
,
primaryKey
:
true
,
allowNull
:
false
}
})
})
Helpers
.
async
(
function
(
done
)
{
Helpers
.
async
(
function
(
done
)
{
User
.
create
({}).
on
(
'success'
,
function
(
user
)
{
User
.
create
({}).
on
(
'success'
,
function
(
user
)
{
expect
(
user
.
userid
).
toEqual
(
1
)
expect
(
user
.
userid
).
toEqual
(
1
)
done
()
done
()
})
})
})
})
Helpers
.
async
(
function
(
done
)
{
Helpers
.
async
(
function
(
done
)
{
User
.
create
({}).
on
(
'success'
,
function
(
user
)
{
User
.
create
({}).
on
(
'success'
,
function
(
user
)
{
expect
(
user
.
userid
).
toEqual
(
2
)
expect
(
user
.
userid
).
toEqual
(
2
)
done
()
done
()
})
})
})
})
})
})
it
(
'allows the usage of options as attribute'
,
function
()
{
it
(
'allows the usage of options as attribute'
,
function
()
{
setup
({
name
:
Sequelize
.
STRING
,
options
:
Sequelize
.
TEXT
})
setup
({
name
:
Sequelize
.
STRING
,
options
:
Sequelize
.
TEXT
})
Helpers
.
async
(
function
(
done
)
{
Helpers
.
async
(
function
(
done
)
{
var
options
=
JSON
.
stringify
({
foo
:
'bar'
,
bar
:
'foo'
})
var
options
=
JSON
.
stringify
({
foo
:
'bar'
,
bar
:
'foo'
})
User
User
.
create
({
name
:
'John Doe'
,
options
:
options
})
.
create
({
name
:
'John Doe'
,
options
:
options
})
.
success
(
function
(
user
)
{
.
success
(
function
(
user
)
{
expect
(
user
.
options
).
toEqual
(
options
)
expect
(
user
.
options
).
toEqual
(
options
)
done
()
done
()
})
})
})
})
})
})
describe
(
'destroy'
,
function
()
{
it
(
'deletes a record from the database if model is not paranoid'
,
function
()
{
Helpers
.
async
(
function
(
done
)
{
User
=
sequelize
.
define
(
'User'
,
{
name
:
Sequelize
.
STRING
,
bio
:
Sequelize
.
TEXT
})
})
User
.
sync
({
force
:
true
}).
success
(
done
)
})
})
Helpers
.
async
(
function
(
done
)
{
describe
(
'destroy'
,
function
()
{
User
.
create
({
name
:
'hallo'
,
bio
:
'welt'
}).
success
(
function
(
u
)
{
it
(
'deletes a record from the database if model is not paranoid'
,
function
()
{
User
.
all
().
success
(
function
(
users
)
{
Helpers
.
async
(
function
(
done
)
{
expect
(
users
.
length
).
toEqual
(
1
)
User
=
sequelize
.
define
(
'User'
,
{
u
.
destroy
().
success
(
function
()
{
name
:
Sequelize
.
STRING
,
bio
:
Sequelize
.
TEXT
})
User
.
sync
({
force
:
true
}).
success
(
done
)
})
Helpers
.
async
(
function
(
done
)
{
User
.
create
({
name
:
'hallo'
,
bio
:
'welt'
}).
success
(
function
(
u
)
{
User
.
all
().
success
(
function
(
users
)
{
User
.
all
().
success
(
function
(
users
)
{
expect
(
users
.
length
).
toEqual
(
0
)
expect
(
users
.
length
).
toEqual
(
1
)
done
()
u
.
destroy
().
success
(
function
()
{
})
User
.
all
().
success
(
function
(
users
)
{
expect
(
users
.
length
).
toEqual
(
0
)
done
()
}).
error
(
function
(
err
)
{
console
.
log
(
err
)
})
}).
error
(
function
(
err
)
{
console
.
log
(
err
)
})
}).
error
(
function
(
err
)
{
console
.
log
(
err
)
})
})
})
})
})
})
})
})
})
it
(
'marks the database entry as deleted if model is paranoid'
,
function
()
{
it
(
'marks the database entry as deleted if model is paranoid'
,
function
()
{
Helpers
.
async
(
function
(
done
)
{
Helpers
.
async
(
function
(
done
)
{
User
=
sequelize
.
define
(
'User'
,
{
User
=
sequelize
.
define
(
'User'
,
{
name
:
Sequelize
.
STRING
,
bio
:
Sequelize
.
TEXT
name
:
Sequelize
.
STRING
,
bio
:
Sequelize
.
TEXT
},
{
paranoid
:
true
})
},
{
paranoid
:
true
})
User
.
sync
({
force
:
true
}).
success
(
done
)
User
.
sync
({
force
:
true
}).
success
(
done
)
})
})
Helpers
.
async
(
function
(
done
)
{
Helpers
.
async
(
function
(
done
)
{
User
.
create
({
name
:
'asd'
,
bio
:
'asd'
}).
success
(
function
(
u
)
{
User
.
create
({
name
:
'asd'
,
bio
:
'asd'
}).
success
(
function
(
u
)
{
expect
(
u
.
deletedAt
).
toBeNull
()
expect
(
u
.
deletedAt
).
toBeNull
()
u
.
destroy
().
success
(
function
(
u
)
{
u
.
destroy
().
success
(
function
(
u
)
{
expect
(
u
.
deletedAt
).
toBeTruthy
()
expect
(
u
.
deletedAt
).
toBeTruthy
()
done
()
done
()
})
})
})
})
})
})
})
})
})
})
describe
(
'find'
,
function
()
{
describe
(
'find'
,
function
()
{
var
users
=
[]
var
users
=
[]
beforeEach
(
function
()
{
beforeEach
(
function
()
{
Helpers
.
Factories
.
User
({
name
:
'user'
,
bio
:
'foobar'
},
function
(
_users
)
{
Helpers
.
Factories
.
User
({
name
:
'user'
,
bio
:
'foobar'
},
function
(
_users
)
{
users
=
_users
users
=
_users
},
2
)
},
2
)
})
})
it
(
"should make aliased attributes available"
,
function
()
{
it
(
"should make aliased attributes available"
,
function
()
{
Helpers
.
async
(
function
(
done
)
{
Helpers
.
async
(
function
(
done
)
{
User
.
find
({
where
:
'id = 1'
,
attributes
:
[
'id'
,
[
'name'
,
'username'
]]
}).
success
(
function
(
user
)
{
User
.
find
({
where
:
'id = 1'
,
attributes
:
[
'id'
,
[
'name'
,
'username'
]]
}).
success
(
function
(
user
)
{
expect
(
user
.
username
).
toEqual
(
'user'
)
expect
(
user
.
username
).
toEqual
(
'user'
)
done
()
done
()
})
})
})
})
})
})
it
(
'returns a single model'
,
function
()
{
it
(
'returns a single model'
,
function
()
{
Helpers
.
async
(
function
(
done
)
{
Helpers
.
async
(
function
(
done
)
{
User
.
find
(
users
[
0
].
id
).
success
(
function
(
user
)
{
User
.
find
(
users
[
0
].
id
).
success
(
function
(
user
)
{
expect
(
Array
.
isArray
(
user
)).
toBeFalsy
()
expect
(
Array
.
isArray
(
user
)).
toBeFalsy
()
expect
(
user
.
id
).
toEqual
(
users
[
0
].
id
)
expect
(
user
.
id
).
toEqual
(
users
[
0
].
id
)
done
()
done
()
})
})
})
})
})
})
it
(
'finds a specific user via where option'
,
function
()
{
it
(
'finds a specific user via where option'
,
function
()
{
Helpers
.
async
(
function
(
done
)
{
Helpers
.
async
(
function
(
done
)
{
User
.
find
({
where
:
{
name
:
'user'
}}).
success
(
function
(
user
)
{
User
.
find
({
where
:
{
name
:
'user'
}}).
success
(
function
(
user
)
{
expect
(
user
.
name
).
toEqual
(
'user'
)
expect
(
user
.
name
).
toEqual
(
'user'
)
done
()
done
()
})
})
})
})
})
})
it
(
"doesn't find a user if conditions are not matching"
,
function
()
{
it
(
"doesn't find a user if conditions are not matching"
,
function
()
{
Helpers
.
async
(
function
(
done
)
{
Helpers
.
async
(
function
(
done
)
{
User
.
find
({
where
:
{
name
:
'foo'
}
}).
success
(
function
(
user
)
{
User
.
find
({
where
:
{
name
:
'foo'
}
}).
success
(
function
(
user
)
{
expect
(
user
).
toBeNull
()
expect
(
user
).
toBeNull
()
done
()
done
()
})
})
})
})
})
})
it
(
'ignores passed limit option'
,
function
()
{
it
(
'ignores passed limit option'
,
function
()
{
Helpers
.
async
(
function
(
done
)
{
Helpers
.
async
(
function
(
done
)
{
User
.
find
({
limit
:
10
}).
success
(
function
(
user
)
{
User
.
find
({
limit
:
10
}).
success
(
function
(
user
)
{
// it returns an object instead of an array
// it returns an object instead of an array
expect
(
Array
.
isArray
(
user
)).
toBeFalsy
()
expect
(
Array
.
isArray
(
user
)).
toBeFalsy
()
expect
(
user
.
hasOwnProperty
(
'name'
)).
toBeTruthy
()
expect
(
user
.
hasOwnProperty
(
'name'
)).
toBeTruthy
()
done
()
done
()
})
})
})
})
})
})
it
(
'finds entries via primary keys'
,
function
()
{
it
(
'finds entries via primary keys'
,
function
()
{
setup
({
setup
({
identifier
:
{
type
:
Sequelize
.
STRING
,
primaryKey
:
true
},
identifier
:
{
type
:
Sequelize
.
STRING
,
primaryKey
:
true
},
name
:
Sequelize
.
STRING
name
:
Sequelize
.
STRING
})
})
Helpers
.
async
(
function
(
done
)
{
Helpers
.
async
(
function
(
done
)
{
User
.
create
({
identifier
:
'an identifier'
,
name
:
'John'
}).
success
(
function
(
u
)
{
User
.
create
({
identifier
:
'an identifier'
,
name
:
'John'
}).
success
(
function
(
u
)
{
expect
(
u
.
id
).
toBeUndefined
()
expect
(
u
.
id
).
toBeUndefined
()
User
.
find
(
'an identifier'
).
success
(
function
(
u2
)
{
User
.
find
(
'an identifier'
).
success
(
function
(
u2
)
{
expect
(
u2
.
identifier
).
toEqual
(
'an identifier'
)
expect
(
u2
.
identifier
).
toEqual
(
'an identifier'
)
expect
(
u2
.
name
).
toEqual
(
'John'
)
expect
(
u2
.
name
).
toEqual
(
'John'
)
done
()
done
()
})
})
})
})
})
})
})
})
})
})
describe
(
'findAll'
,
function
()
{
describe
(
'findAll'
,
function
()
{
var
users
=
[]
var
users
=
[]
beforeEach
(
function
()
{
beforeEach
(
function
()
{
Helpers
.
Factories
.
User
({
name
:
'user'
,
bio
:
'foobar'
},
function
(
_users
)
{
Helpers
.
Factories
.
User
({
name
:
'user'
,
bio
:
'foobar'
},
function
(
_users
)
{
users
=
_users
users
=
_users
},
2
)
},
2
)
})
it
(
"finds all entries"
,
function
()
{
Helpers
.
async
(
function
(
done
)
{
User
.
findAll
().
on
(
'success'
,
function
(
_users
)
{
expect
(
_users
.
length
).
toEqual
(
2
)
done
()
})
})
})
})
it
(
"finds all users matching the passed conditions"
,
function
()
{
it
(
"finds all entries"
,
function
()
{
Helpers
.
async
(
function
(
done
)
{
Helpers
.
async
(
function
(
done
)
{
User
.
findAll
({
where
:
"id != "
+
users
[
1
].
id
}).
success
(
function
(
_users
)
{
User
.
findAll
().
on
(
'success'
,
function
(
_users
)
{
expect
(
_users
.
length
).
toEqual
(
1
)
expect
(
_users
.
length
).
toEqual
(
2
)
done
()
done
()
})
})
})
})
})
})
it
(
"can also handle array notation"
,
function
()
{
it
(
"finds all users matching the passed conditions"
,
function
()
{
Helpers
.
async
(
function
(
done
){
Helpers
.
async
(
function
(
done
)
{
User
.
findAll
({
where
:
[
'id = ?'
,
users
[
1
].
id
]}).
success
(
function
(
_users
)
{
User
.
findAll
({
where
:
"id != "
+
users
[
1
].
id
}).
success
(
function
(
_users
)
{
expect
(
_users
.
length
).
toEqual
(
1
)
expect
(
_users
.
length
).
toEqual
(
1
)
expect
(
_users
[
0
].
id
).
toEqual
(
users
[
1
].
id
)
done
()
done
()
})
})
})
})
})
})
it
(
"sorts the results"
,
function
()
{
it
(
"can also handle array notation"
,
function
()
{
Helpers
.
async
(
function
(
done
)
{
Helpers
.
async
(
function
(
done
){
User
.
findAll
({
order
:
"id DESC"
}).
success
(
function
(
users
)
{
User
.
findAll
({
where
:
[
'id = ?'
,
users
[
1
].
id
]}).
success
(
function
(
_users
)
{
expect
(
users
[
0
].
id
).
toBeGreaterThan
(
users
[
1
].
id
)
expect
(
_users
.
length
).
toEqual
(
1
)
done
()
expect
(
_users
[
0
].
id
).
toEqual
(
users
[
1
].
id
)
done
()
})
})
})
})
})
})
it
(
"handles offset and limit"
,
function
()
{
setup
()
Helpers
.
Factories
.
User
({
name
:
'user'
,
bio
:
'foobar'
},
null
,
10
)
it
(
"sorts the results"
,
function
()
{
Helpers
.
async
(
function
(
done
)
{
Helpers
.
async
(
function
(
done
)
{
User
.
findAll
({
order
:
"id DESC"
}).
success
(
function
(
users
)
{
User
.
findAll
({
limit
:
2
,
offset
:
2
}).
success
(
function
(
users
)
{
expect
(
users
[
0
].
id
).
toBeGreaterThan
(
users
[
1
].
id
)
expect
(
users
.
length
).
toEqual
(
2
)
done
(
)
expect
(
users
[
0
].
id
).
toEqual
(
3
)
}
)
done
(
)
}
)
})
})
})
})
})
describe
(
'all'
,
function
()
{
it
(
"handles offset and limit"
,
function
()
{
beforeEach
(
function
()
{
setup
()
Helpers
.
Factories
.
User
({
name
:
'user'
,
bio
:
'foobar'
},
null
,
2
)
})
it
(
"should return all users"
,
function
()
{
Helpers
.
Factories
.
User
({
name
:
'user'
,
bio
:
'foobar'
},
null
,
10
)
Helpers
.
async
(
function
(
done
)
{
User
.
all
().
on
(
'success'
,
function
(
users
)
{
done
()
expect
(
users
.
length
).
toEqual
(
2
)
}).
on
(
'failure'
,
function
(
err
)
{
console
.
log
(
err
)
})
})
})
})
describe
(
'count'
,
function
()
{
Helpers
.
async
(
function
(
done
)
{
it
(
'counts all created objects'
,
function
()
{
User
.
findAll
({
limit
:
2
,
offset
:
2
}).
success
(
function
(
users
)
{
Helpers
.
async
(
function
(
done
)
{
expect
(
users
.
length
).
toEqual
(
2
)
User
.
create
({
name
:
'user1'
}).
success
(
function
()
{
expect
(
users
[
0
].
id
).
toEqual
(
3
)
User
.
create
({
name
:
'user2'
}).
success
(
done
)
done
()
})
})
})
})
})
})
Helpers
.
async
(
function
(
done
)
{
describe
(
'all'
,
function
()
{
User
.
count
().
success
(
function
(
count
)
{
beforeEach
(
function
()
{
expect
(
count
).
toEqual
(
2
)
Helpers
.
Factories
.
User
({
name
:
'user'
,
bio
:
'foobar'
},
null
,
2
)
done
()
})
})
})
})
it
(
'filters object'
,
function
()
{
it
(
"should return all users"
,
function
()
{
Helpers
.
async
(
function
(
done
)
{
Helpers
.
async
(
function
(
done
)
{
User
.
create
({
name
:
'user1'
}).
success
(
function
()
{
User
.
all
().
on
(
'success'
,
function
(
users
)
{
User
.
create
({
name
:
'foo'
}).
success
(
done
)
done
()
expect
(
users
.
length
).
toEqual
(
2
)
}).
on
(
'failure'
,
function
(
err
)
{
console
.
log
(
err
)
})
})
})
})
})
})
Helpers
.
async
(
function
(
done
)
{
describe
(
'count'
,
function
()
{
User
.
count
({
where
:
"name LIKE '%us%'"
}).
success
(
function
(
count
)
{
it
(
'counts all created objects'
,
function
()
{
expect
(
count
).
toEqual
(
1
)
Helpers
.
async
(
function
(
done
)
{
done
()
User
.
create
({
name
:
'user1'
}).
success
(
function
()
{
User
.
create
({
name
:
'user2'
}).
success
(
done
)
})
})
Helpers
.
async
(
function
(
done
)
{
User
.
count
().
success
(
function
(
count
)
{
expect
(
count
).
toEqual
(
2
)
done
()
})
})
})
})
})
})
})
describe
(
'min'
,
function
()
{
it
(
'filters object'
,
function
()
{
it
(
"should return the min value"
,
function
()
{
Helpers
.
async
(
function
(
done
)
{
for
(
var
i
=
2
;
i
<
5
;
i
++
)
Helpers
.
Factories
.
User
({
age
:
i
})
User
.
create
({
name
:
'user1'
}).
success
(
function
()
{
User
.
create
({
name
:
'foo'
}).
success
(
done
)
})
})
Helpers
.
async
(
function
(
done
)
{
Helpers
.
async
(
function
(
done
)
{
User
.
min
(
'age'
).
on
(
'success'
,
function
(
min
)
{
User
.
count
({
where
:
"name LIKE '%us%'"
}).
success
(
function
(
count
)
{
expect
(
min
).
toEqual
(
2
);
done
()
expect
(
count
).
toEqual
(
1
)
done
()
})
})
})
})
})
})
})
})
describe
(
'max
'
,
function
()
{
describe
(
'min
'
,
function
()
{
it
(
"should return the max
value"
,
function
()
{
it
(
"should return the min
value"
,
function
()
{
for
(
var
i
=
2
;
i
<=
5
;
i
++
)
Helpers
.
Factories
.
User
({
age
:
i
})
for
(
var
i
=
2
;
i
<
5
;
i
++
)
Helpers
.
Factories
.
User
({
age
:
i
})
Helpers
.
async
(
function
(
done
)
{
Helpers
.
async
(
function
(
done
)
{
User
.
max
(
'age'
).
on
(
'success'
,
function
(
max
)
{
User
.
min
(
'age'
).
on
(
'success'
,
function
(
min
)
{
expect
(
max
).
toEqual
(
5
);
done
()
expect
(
min
).
toEqual
(
2
);
done
()
})
})
})
})
})
})
})
})
describe
(
'equals
'
,
function
()
{
describe
(
'max
'
,
function
()
{
it
(
"correctly determines equality of objects
"
,
function
()
{
it
(
"should return the max value
"
,
function
()
{
setup
({
name
:
Sequelize
.
STRING
,
bio
:
Sequelize
.
TEXT
})
for
(
var
i
=
2
;
i
<=
5
;
i
++
)
Helpers
.
Factories
.
User
({
age
:
i
})
Helpers
.
async
(
function
(
done
)
{
Helpers
.
async
(
function
(
done
)
{
User
.
create
({
name
:
'hallo'
,
bio
:
'welt'
}).
success
(
function
(
u
)
{
User
.
max
(
'age'
).
on
(
'success'
,
function
(
max
)
{
expect
(
u
.
equals
(
u
)).
toBeTruthy
()
expect
(
max
).
toEqual
(
5
);
done
()
done
()
})
})
})
})
})
})
})
it
(
"correctly determines equality with multiple primary keys"
,
function
()
{
describe
(
'equals'
,
function
()
{
setup
({
it
(
"correctly determines equality of objects"
,
function
()
{
foo
:
{
type
:
Sequelize
.
STRING
,
primaryKey
:
true
},
setup
({
name
:
Sequelize
.
STRING
,
bio
:
Sequelize
.
TEXT
})
bar
:
{
type
:
Sequelize
.
STRING
,
primaryKey
:
true
},
name
:
Sequelize
.
STRING
,
bio
:
Sequelize
.
TEXT
})
Helpers
.
async
(
function
(
done
)
{
Helpers
.
async
(
function
(
done
)
{
User
.
create
({
foo
:
'1'
,
bar
:
'2'
,
name
:
'hallo'
,
bio
:
'welt'
}).
success
(
function
(
u
)
{
User
.
create
({
name
:
'hallo'
,
bio
:
'welt'
}).
success
(
function
(
u
)
{
expect
(
u
.
equals
(
u
)).
toBeTruthy
()
expect
(
u
.
equals
(
u
)).
toBeTruthy
()
done
()
done
()
}).
error
(
function
(
err
)
{
})
console
.
log
(
err
)
}
)
})
})
})
})
})
describe
(
'equalsOneOf'
,
function
()
{
// sqlite can't handle multiple primary keys
beforeEach
(
function
(
)
{
if
(
dialect
!=
'sqlite'
)
{
setup
(
{
it
(
"correctly determines equality with multiple primary keys"
,
function
()
{
foo
:
{
type
:
Sequelize
.
STRING
,
primaryKey
:
true
},
setup
({
bar
:
{
type
:
Sequelize
.
STRING
,
primaryKey
:
true
},
foo
:
{
type
:
Sequelize
.
STRING
,
primaryKey
:
true
},
name
:
Sequelize
.
STRING
,
bio
:
Sequelize
.
TEXT
bar
:
{
type
:
Sequelize
.
STRING
,
primaryKey
:
true
},
})
name
:
Sequelize
.
STRING
,
bio
:
Sequelize
.
TEXT
})
})
it
(
'determines equality if one is matching'
,
function
()
{
Helpers
.
async
(
function
(
done
)
{
Helpers
.
async
(
function
(
done
)
{
User
.
create
({
foo
:
'1'
,
bar
:
'2'
,
name
:
'hallo'
,
bio
:
'welt'
}).
success
(
function
(
u
)
{
User
.
create
({
foo
:
'1'
,
bar
:
'2'
,
name
:
'hallo'
,
bio
:
'welt'
}).
success
(
function
(
u
)
{
expect
(
u
.
equals
(
u
)).
toBeTruthy
()
expect
(
u
.
equalsOneOf
([
u
,
{
a
:
1
}])).
toBeTruthy
()
done
()
done
()
}).
error
(
function
(
err
)
{
console
.
log
(
err
)
})
})
})
})
}
})
})
})
it
(
"doesn't determine equality if none is matching"
,
function
()
{
describe
(
'equalsOneOf'
,
function
()
{
Helpers
.
async
(
function
(
done
)
{
// sqlite can't handle multiple primary keys
User
.
create
({
foo
:
'1'
,
bar
:
'2'
,
name
:
'hallo'
,
bio
:
'welt'
}).
success
(
function
(
u
)
{
if
(
dialect
!=
'sqlite'
)
{
expect
(
u
.
equalsOneOf
([{
b
:
2
},
{
a
:
1
}])).
toBeFalsy
()
beforeEach
(
function
()
{
done
()
setup
({
})
foo
:
{
type
:
Sequelize
.
STRING
,
primaryKey
:
true
},
})
bar
:
{
type
:
Sequelize
.
STRING
,
primaryKey
:
true
},
})
name
:
Sequelize
.
STRING
,
bio
:
Sequelize
.
TEXT
})
})
})
describe
(
'Mixin'
,
function
()
{
it
(
'determines equality if one is matching'
,
function
()
{
var
ModelFactory
=
require
(
"../lib/model-factory"
)
Helpers
.
async
(
function
(
done
)
{
User
.
create
({
foo
:
'1'
,
bar
:
'2'
,
name
:
'hallo'
,
bio
:
'welt'
}).
success
(
function
(
u
)
{
expect
(
u
.
equalsOneOf
([
u
,
{
a
:
1
}])).
toBeTruthy
()
done
()
})
})
})
it
(
"adds the mixed-in functions to the model"
,
function
()
{
it
(
"doesn't determine equality if none is matching"
,
function
()
{
expect
(
ModelFactory
.
prototype
.
hasOne
).
toBeDefined
()
Helpers
.
async
(
function
(
done
)
{
expect
(
ModelFactory
.
prototype
.
hasMany
).
toBeDefined
()
User
.
create
({
foo
:
'1'
,
bar
:
'2'
,
name
:
'hallo'
,
bio
:
'welt'
}).
success
(
function
(
u
)
{
expect
(
ModelFactory
.
prototype
.
belongsTo
).
toBeDefined
()
expect
(
u
.
equalsOneOf
([{
b
:
2
},
{
a
:
1
}])).
toBeFalsy
()
})
done
()
})
})
})
})
}
})
describe
(
'sync'
,
function
()
{
describe
(
'Mixin'
,
function
()
{
it
(
'works with correct database credentials'
,
function
()
{
var
ModelFactory
=
require
(
"../lib/model-factory"
)
Helpers
.
async
(
function
(
done
)
{
User
.
sync
().
success
(
done
)
it
(
"adds the mixed-in functions to the model"
,
function
()
{
expect
(
ModelFactory
.
prototype
.
hasOne
).
toBeDefined
()
expect
(
ModelFactory
.
prototype
.
hasMany
).
toBeDefined
()
expect
(
ModelFactory
.
prototype
.
belongsTo
).
toBeDefined
()
})
})
})
})
it
(
"fails with incorrect database credentials"
,
function
()
{
describe
(
'sync'
,
function
()
{
Helpers
.
async
(
function
(
done
)
{
it
(
'works with correct database credentials'
,
function
()
{
var
sequelize2
=
new
Sequelize
(
'foo'
,
'bar'
,
null
,
{
logging
:
false
})
Helpers
.
async
(
function
(
done
)
{
,
User2
=
sequelize2
.
define
(
'User'
,
{
name
:
Sequelize
.
STRING
,
bio
:
Sequelize
.
TEXT
})
User
.
sync
().
success
(
done
)
})
})
it
(
"fails with incorrect database credentials"
,
function
()
{
Helpers
.
async
(
function
(
done
)
{
var
sequelize2
=
new
Sequelize
(
'foo'
,
'bar'
,
null
,
{
logging
:
false
})
,
User2
=
sequelize2
.
define
(
'User'
,
{
name
:
Sequelize
.
STRING
,
bio
:
Sequelize
.
TEXT
})
User2
.
sync
().
error
(
function
(
err
)
{
User2
.
sync
().
error
(
function
(
err
)
{
expect
(
err
.
message
).
toEqual
(
"Access denied for user ''@'localhost' to database 'foo'"
)
expect
(
err
.
message
).
toEqual
(
"Access denied for user ''@'localhost' to database 'foo'"
)
done
()
done
()
})
})
})
})
})
})
})
})
describe
(
'drop should work'
,
function
()
{
describe
(
'drop should work'
,
function
()
{
it
(
'correctly succeeds'
,
function
()
{
it
(
'correctly succeeds'
,
function
()
{
Helpers
.
async
(
function
(
done
)
{
Helpers
.
async
(
function
(
done
)
{
User
.
drop
().
success
(
done
)
User
.
drop
().
success
(
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