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 ab62a2eb
authored
Dec 30, 2013
by
Mick Hansen
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
reset dao-factory.test.js to state of master
1 parent
4b7c0e94
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
4 additions
and
2423 deletions
test/dao-factory.test.js
test/dao-factory.test.js
View file @
ab62a2e
...
@@ -677,2433 +677,13 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
...
@@ -677,2433 +677,13 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
expect
(
users
[
1
].
username
).
to
.
equal
(
"Bill"
)
expect
(
users
[
1
].
username
).
to
.
equal
(
"Bill"
)
expect
(
users
[
2
].
username
).
to
.
equal
(
"Bob"
)
expect
(
users
[
2
].
username
).
to
.
equal
(
"Bob"
)
<<<<<<<
HEAD
expect
(
parseInt
(
+
users
[
0
].
updatedAt
/
5000
,
10
)).
to
.
be
.
closeTo
(
parseInt
(
+
new
Date
()
/
5000
,
10
),
1
)
it
(
'allows the usage of options as attribute'
,
function
(
done
)
{
expect
(
parseInt
(
+
users
[
1
].
updatedAt
/
5000
,
10
)).
to
.
be
.
closeTo
(
parseInt
(
+
new
Date
()
/
5000
,
10
),
1
)
var
User
=
this
.
sequelize
.
define
(
'UserWithNameAndOptions'
,
{
name
:
Sequelize
.
STRING
,
options
:
Sequelize
.
TEXT
})
var
options
=
JSON
.
stringify
({
foo
:
'bar'
,
bar
:
'foo'
})
User
.
sync
({
force
:
true
}).
success
(
function
()
{
User
.
create
({
name
:
'John Doe'
,
options
:
options
})
.
success
(
function
(
user
)
{
expect
(
user
.
options
).
to
.
equal
(
options
)
done
()
})
})
})
it
(
'allows sql logging'
,
function
(
done
)
{
var
User
=
this
.
sequelize
.
define
(
'UserWithUniqueNameAndNonNullSmth'
,
{
name
:
{
type
:
Sequelize
.
STRING
,
unique
:
true
},
smth
:
{
type
:
Sequelize
.
STRING
,
allowNull
:
false
}
})
User
.
sync
({
force
:
true
}).
success
(
function
()
{
User
.
create
({
name
:
'Fluffy Bunny'
,
smth
:
'else'
})
.
on
(
'sql'
,
function
(
sql
)
{
expect
(
sql
).
to
.
exist
expect
(
sql
.
toUpperCase
().
indexOf
(
"INSERT"
)).
to
.
be
.
above
(
-
1
)
done
()
})
})
})
it
(
'should only store the values passed in the whitelist'
,
function
(
done
)
{
var
self
=
this
,
data
=
{
username
:
'Peter'
,
secretValue
:
'42'
}
this
.
User
.
create
(
data
,
{
fields
:
[
'username'
]
}).
success
(
function
(
user
)
{
self
.
User
.
find
(
user
.
id
).
success
(
function
(
_user
)
{
expect
(
_user
.
username
).
to
.
equal
(
data
.
username
)
expect
(
_user
.
secretValue
).
not
.
to
.
equal
(
data
.
secretValue
)
expect
(
_user
.
secretValue
).
to
.
equal
(
null
)
done
()
})
})
})
it
(
'should store all values if no whitelist is specified'
,
function
(
done
)
{
var
self
=
this
,
data
=
{
username
:
'Peter'
,
secretValue
:
'42'
}
this
.
User
.
create
(
data
).
success
(
function
(
user
)
{
self
.
User
.
find
(
user
.
id
).
success
(
function
(
_user
)
{
expect
(
_user
.
username
).
to
.
equal
(
data
.
username
)
expect
(
_user
.
secretValue
).
to
.
equal
(
data
.
secretValue
)
done
()
})
})
})
it
(
'can omit autoincremental columns'
,
function
(
done
)
{
var
self
=
this
,
data
=
{
title
:
'Iliad'
}
,
dataTypes
=
[
Sequelize
.
INTEGER
,
Sequelize
.
BIGINT
]
,
chain
=
new
Sequelize
.
Utils
.
QueryChainer
()
,
chain2
=
new
Sequelize
.
Utils
.
QueryChainer
()
,
books
=
[]
dataTypes
.
forEach
(
function
(
dataType
,
index
)
{
books
[
index
]
=
self
.
sequelize
.
define
(
'Book'
+
index
,
{
id
:
{
type
:
dataType
,
primaryKey
:
true
,
autoIncrement
:
true
},
title
:
Sequelize
.
TEXT
})
})
books
.
forEach
(
function
(
b
)
{
chain
.
add
(
b
.
sync
({
force
:
true
}))
})
chain
.
run
().
success
(
function
()
{
books
.
forEach
(
function
(
b
)
{
chain2
.
add
(
b
.
create
(
data
))
})
chain2
.
run
().
success
(
function
(
results
)
{
results
.
forEach
(
function
(
book
,
index
)
{
expect
(
book
.
title
).
to
.
equal
(
data
.
title
)
expect
(
book
.
author
).
to
.
equal
(
data
.
author
)
expect
(
books
[
index
].
rawAttributes
.
id
.
type
.
toString
())
.
to
.
equal
(
dataTypes
[
index
].
toString
())
})
done
()
})
})
})
it
(
'saves data with single quote'
,
function
(
done
)
{
var
quote
=
"single'quote"
,
self
=
this
this
.
User
.
create
({
data
:
quote
}).
success
(
function
(
user
)
{
expect
(
user
.
data
).
to
.
equal
(
quote
)
self
.
User
.
find
({
where
:
{
id
:
user
.
id
}}).
success
(
function
(
user
)
{
expect
(
user
.
data
).
to
.
equal
(
quote
)
done
()
})
})
})
it
(
'saves data with double quote'
,
function
(
done
)
{
var
quote
=
'double"quote'
,
self
=
this
this
.
User
.
create
({
data
:
quote
}).
success
(
function
(
user
)
{
expect
(
user
.
data
).
to
.
equal
(
quote
)
self
.
User
.
find
({
where
:
{
id
:
user
.
id
}}).
success
(
function
(
user
)
{
expect
(
user
.
data
).
to
.
equal
(
quote
)
done
()
})
})
})
it
(
'saves stringified JSON data'
,
function
(
done
)
{
var
json
=
JSON
.
stringify
({
key
:
'value'
})
,
self
=
this
this
.
User
.
create
({
data
:
json
}).
success
(
function
(
user
)
{
expect
(
user
.
data
).
to
.
equal
(
json
)
self
.
User
.
find
({
where
:
{
id
:
user
.
id
}}).
success
(
function
(
user
)
{
expect
(
user
.
data
).
to
.
equal
(
json
)
done
()
})
})
})
it
(
'stores the current date in createdAt'
,
function
(
done
)
{
this
.
User
.
create
({
username
:
'foo'
}).
success
(
function
(
user
)
{
expect
(
parseInt
(
+
user
.
createdAt
/
5000
,
10
)).
to
.
be
.
closeTo
(
parseInt
(
+
new
Date
()
/
5000
,
10
),
1.5
)
done
()
})
})
it
(
'allows setting custom IDs'
,
function
(
done
)
{
var
self
=
this
this
.
User
.
create
({
id
:
42
}).
success
(
function
(
user
)
{
expect
(
user
.
id
).
to
.
equal
(
42
)
self
.
User
.
find
(
42
).
success
(
function
(
user
)
{
expect
(
user
).
to
.
exist
done
()
})
})
})
describe
(
'enums'
,
function
()
{
it
(
'correctly restores enum values'
,
function
(
done
)
{
var
self
=
this
,
Item
=
self
.
sequelize
.
define
(
'Item'
,
{
state
:
{
type
:
Sequelize
.
ENUM
,
values
:
[
'available'
,
'in_cart'
,
'shipped'
]
}
})
Item
.
sync
({
force
:
true
}).
success
(
function
()
{
Item
.
create
({
state
:
'available'
}).
success
(
function
(
_item
)
{
Item
.
find
({
where
:
{
state
:
'available'
}}).
success
(
function
(
item
)
{
expect
(
item
.
id
).
to
.
equal
(
_item
.
id
)
done
()
})
})
})
})
it
(
'allows null values'
,
function
(
done
)
{
var
Enum
=
this
.
sequelize
.
define
(
'Enum'
,
{
state
:
{
type
:
Sequelize
.
ENUM
,
values
:
[
'happy'
,
'sad'
],
allowNull
:
true
}
})
Enum
.
sync
({
force
:
true
}).
success
(
function
()
{
Enum
.
create
({
state
:
null
}).
success
(
function
(
_enum
)
{
expect
(
_enum
.
state
).
to
.
be
.
null
done
()
})
})
})
describe
(
'can safely sync multiple times'
,
function
(
done
)
{
it
(
'through the factory'
,
function
(
done
)
{
var
Enum
=
this
.
sequelize
.
define
(
'Enum'
,
{
state
:
{
type
:
Sequelize
.
ENUM
,
values
:
[
'happy'
,
'sad'
],
allowNull
:
true
}
})
Enum
.
sync
({
force
:
true
}).
success
(
function
()
{
Enum
.
sync
().
success
(
function
()
{
Enum
.
sync
({
force
:
true
}).
complete
(
done
)
})
})
})
it
(
'through sequelize'
,
function
(
done
)
{
var
self
=
this
,
Enum
=
this
.
sequelize
.
define
(
'Enum'
,
{
state
:
{
type
:
Sequelize
.
ENUM
,
values
:
[
'happy'
,
'sad'
],
allowNull
:
true
}
})
this
.
sequelize
.
sync
({
force
:
true
}).
success
(
function
()
{
self
.
sequelize
.
sync
().
success
(
function
()
{
self
.
sequelize
.
sync
({
force
:
true
}).
complete
(
done
)
})
})
})
})
})
})
describe
(
'bulkCreate'
,
function
()
{
it
(
"supports transactions"
,
function
(
done
)
{
Support
.
prepareTransactionTest
(
this
.
sequelize
,
function
(
sequelize
)
{
var
User
=
sequelize
.
define
(
'User'
,
{
username
:
Sequelize
.
STRING
})
User
.
sync
({
force
:
true
}).
success
(
function
()
{
sequelize
.
transaction
(
function
(
t
)
{
User
.
bulkCreate
([{
username
:
'foo'
},
{
username
:
'bar'
}],
{
transaction
:
t
})
.
success
(
function
()
{
User
.
count
().
success
(
function
(
count1
)
{
User
.
count
({
transaction
:
t
}).
success
(
function
(
count2
)
{
expect
(
count1
).
to
.
equal
(
0
)
expect
(
count2
).
to
.
equal
(
2
)
t
.
rollback
().
success
(
function
(){
done
()
})
})
})
})
})
})
})
})
it
(
'properly handles disparate field lists'
,
function
(
done
)
{
var
self
=
this
,
data
=
[{
username
:
'Peter'
,
secretValue
:
'42'
},
{
username
:
'Paul'
},
{
username
:
'Steve'
}]
this
.
User
.
bulkCreate
(
data
).
success
(
function
()
{
self
.
User
.
findAll
({
where
:
{
username
:
'Paul'
}}).
success
(
function
(
users
)
{
expect
(
users
.
length
).
to
.
equal
(
1
)
expect
(
users
[
0
].
username
).
to
.
equal
(
"Paul"
)
expect
(
users
[
0
].
secretValue
).
to
.
be
.
null
done
()
})
})
})
it
(
'inserts multiple values respecting the white list'
,
function
(
done
)
{
var
self
=
this
,
data
=
[{
username
:
'Peter'
,
secretValue
:
'42'
},
{
username
:
'Paul'
,
secretValue
:
'23'
}]
this
.
User
.
bulkCreate
(
data
,
{
fields
:
[
'username'
]
}).
success
(
function
()
{
self
.
User
.
findAll
({
order
:
'id'
}).
success
(
function
(
users
)
{
expect
(
users
.
length
).
to
.
equal
(
2
)
expect
(
users
[
0
].
username
).
to
.
equal
(
"Peter"
)
expect
(
users
[
0
].
secretValue
).
to
.
be
.
null
;
expect
(
users
[
1
].
username
).
to
.
equal
(
"Paul"
)
expect
(
users
[
1
].
secretValue
).
to
.
be
.
null
;
done
()
})
})
})
it
(
'should store all values if no whitelist is specified'
,
function
(
done
)
{
var
self
=
this
,
data
=
[{
username
:
'Peter'
,
secretValue
:
'42'
},
{
username
:
'Paul'
,
secretValue
:
'23'
}]
this
.
User
.
bulkCreate
(
data
).
success
(
function
()
{
self
.
User
.
findAll
({
order
:
'id'
}).
success
(
function
(
users
)
{
expect
(
users
.
length
).
to
.
equal
(
2
)
expect
(
users
[
0
].
username
).
to
.
equal
(
"Peter"
)
expect
(
users
[
0
].
secretValue
).
to
.
equal
(
'42'
)
expect
(
users
[
1
].
username
).
to
.
equal
(
"Paul"
)
expect
(
users
[
1
].
secretValue
).
to
.
equal
(
'23'
)
done
()
})
})
})
it
(
'saves data with single quote'
,
function
(
done
)
{
var
self
=
this
,
quote
=
"Single'Quote"
,
data
=
[{
username
:
'Peter'
,
data
:
quote
},
{
username
:
'Paul'
,
data
:
quote
}]
this
.
User
.
bulkCreate
(
data
).
success
(
function
()
{
self
.
User
.
findAll
({
order
:
'id'
}).
success
(
function
(
users
)
{
expect
(
users
.
length
).
to
.
equal
(
2
)
expect
(
users
[
0
].
username
).
to
.
equal
(
"Peter"
)
expect
(
users
[
0
].
data
).
to
.
equal
(
quote
)
expect
(
users
[
1
].
username
).
to
.
equal
(
"Paul"
)
expect
(
users
[
1
].
data
).
to
.
equal
(
quote
)
done
()
})
})
})
it
(
'saves data with double quote'
,
function
(
done
)
{
var
self
=
this
,
quote
=
'Double"Quote'
,
data
=
[{
username
:
'Peter'
,
data
:
quote
},
{
username
:
'Paul'
,
data
:
quote
}]
this
.
User
.
bulkCreate
(
data
).
success
(
function
()
{
self
.
User
.
findAll
({
order
:
'id'
}).
success
(
function
(
users
)
{
expect
(
users
.
length
).
to
.
equal
(
2
)
expect
(
users
[
0
].
username
).
to
.
equal
(
"Peter"
)
expect
(
users
[
0
].
data
).
to
.
equal
(
quote
)
expect
(
users
[
1
].
username
).
to
.
equal
(
"Paul"
)
expect
(
users
[
1
].
data
).
to
.
equal
(
quote
)
done
()
})
})
})
it
(
'saves stringified JSON data'
,
function
(
done
)
{
var
self
=
this
,
json
=
JSON
.
stringify
({
key
:
'value'
})
,
data
=
[{
username
:
'Peter'
,
data
:
json
},
{
username
:
'Paul'
,
data
:
json
}]
this
.
User
.
bulkCreate
(
data
).
success
(
function
()
{
self
.
User
.
findAll
({
order
:
'id'
}).
success
(
function
(
users
)
{
expect
(
users
.
length
).
to
.
equal
(
2
)
expect
(
users
[
0
].
username
).
to
.
equal
(
"Peter"
)
expect
(
users
[
0
].
data
).
to
.
equal
(
json
)
expect
(
users
[
1
].
username
).
to
.
equal
(
"Paul"
)
expect
(
users
[
1
].
data
).
to
.
equal
(
json
)
done
()
})
})
})
it
(
'stores the current date in createdAt'
,
function
(
done
)
{
var
self
=
this
,
data
=
[{
username
:
'Peter'
},
{
username
:
'Paul'
}]
this
.
User
.
bulkCreate
(
data
).
success
(
function
()
{
self
.
User
.
findAll
({
order
:
'id'
}).
success
(
function
(
users
)
{
expect
(
users
.
length
).
to
.
equal
(
2
)
expect
(
users
[
0
].
username
).
to
.
equal
(
'Peter'
)
expect
(
parseInt
(
+
users
[
0
].
createdAt
/
5000
,
10
)).
to
.
be
.
closeTo
(
parseInt
(
+
new
Date
()
/
5000
,
10
),
1.5
)
expect
(
users
[
1
].
username
).
to
.
equal
(
'Paul'
)
expect
(
parseInt
(
+
users
[
1
].
createdAt
/
5000
,
10
)).
to
.
be
.
closeTo
(
parseInt
(
+
new
Date
()
/
5000
,
10
),
1.5
)
done
()
})
})
})
it
(
'emits an error when validate is set to true'
,
function
(
done
)
{
var
Tasks
=
this
.
sequelize
.
define
(
'Task'
,
{
name
:
{
type
:
Sequelize
.
STRING
,
validate
:
{
notNull
:
{
args
:
true
,
msg
:
'name cannot be null'
}
}
},
code
:
{
type
:
Sequelize
.
STRING
,
validate
:
{
len
:
[
3
,
10
]
}
}
})
Tasks
.
sync
({
force
:
true
}).
success
(
function
()
{
Tasks
.
bulkCreate
([
{
name
:
'foo'
,
code
:
'123'
},
{
code
:
'1234'
},
{
name
:
'bar'
,
code
:
'1'
}
],
{
validate
:
true
}).
error
(
function
(
errors
)
{
expect
(
errors
).
to
.
not
.
be
.
null
expect
(
errors
).
to
.
be
.
instanceof
(
Array
)
expect
(
errors
).
to
.
have
.
length
(
2
)
expect
(
errors
[
0
].
record
.
code
).
to
.
equal
(
'1234'
)
expect
(
errors
[
0
].
errors
.
name
[
0
]).
to
.
equal
(
'name cannot be null'
)
expect
(
errors
[
1
].
record
.
name
).
to
.
equal
(
'bar'
)
expect
(
errors
[
1
].
record
.
code
).
to
.
equal
(
'1'
)
expect
(
errors
[
1
].
errors
.
code
[
0
]).
to
.
match
(
/String is not in range/
)
done
()
})
})
})
it
(
"doesn't emit an error when validate is set to true but our selectedValues are fine"
,
function
(
done
)
{
var
Tasks
=
this
.
sequelize
.
define
(
'Task'
,
{
name
:
{
type
:
Sequelize
.
STRING
,
validate
:
{
notNull
:
{
args
:
true
,
msg
:
'name cannot be null'
}
}
},
code
:
{
type
:
Sequelize
.
STRING
,
validate
:
{
len
:
[
3
,
10
]
}
}
})
Tasks
.
sync
({
force
:
true
}).
success
(
function
()
{
Tasks
.
bulkCreate
([
{
name
:
'foo'
,
code
:
'123'
},
{
code
:
'1234'
}
],
{
fields
:
[
'code'
],
validate
:
true
}).
success
(
function
()
{
// we passed!
done
()
})
})
})
describe
(
'enums'
,
function
()
{
it
(
'correctly restores enum values'
,
function
(
done
)
{
var
self
=
this
,
Item
=
self
.
sequelize
.
define
(
'Item'
,
{
state
:
{
type
:
Sequelize
.
ENUM
,
values
:
[
'available'
,
'in_cart'
,
'shipped'
]
},
name
:
Sequelize
.
STRING
})
Item
.
sync
({
force
:
true
}).
success
(
function
()
{
Item
.
bulkCreate
([{
state
:
'in_cart'
,
name
:
'A'
},
{
state
:
'available'
,
name
:
'B'
}]).
success
(
function
()
{
Item
.
find
({
where
:
{
state
:
'available'
}}).
success
(
function
(
item
)
{
expect
(
item
.
name
).
to
.
equal
(
'B'
)
done
()
})
})
})
})
})
})
describe
(
'update'
,
function
()
{
it
(
'supports transactions'
,
function
(
done
)
{
Support
.
prepareTransactionTest
(
this
.
sequelize
,
function
(
sequelize
)
{
var
User
=
sequelize
.
define
(
'User'
,
{
username
:
Sequelize
.
STRING
})
User
.
sync
({
force
:
true
}).
success
(
function
()
{
User
.
create
({
username
:
'foo'
}).
success
(
function
()
{
sequelize
.
transaction
(
function
(
t
)
{
User
.
update
({
username
:
'bar'
},
{},
{
transaction
:
t
}).
success
(
function
()
{
User
.
all
().
success
(
function
(
users1
)
{
User
.
all
({
transaction
:
t
}).
success
(
function
(
users2
)
{
expect
(
users1
[
0
].
username
).
to
.
equal
(
'foo'
)
expect
(
users2
[
0
].
username
).
to
.
equal
(
'bar'
)
t
.
rollback
().
success
(
function
(){
done
()
})
})
})
})
})
})
})
})
})
it
(
'updates the attributes that we select only without updating createdAt'
,
function
(
done
)
{
var
User
=
this
.
sequelize
.
define
(
'User1'
,
{
username
:
Sequelize
.
STRING
,
secretValue
:
Sequelize
.
STRING
},
{
paranoid
:
true
})
User
.
sync
({
force
:
true
}).
success
(
function
()
{
User
.
create
({
username
:
'Peter'
,
secretValue
:
'42'
}).
success
(
function
(
user
)
{
user
.
updateAttributes
({
secretValue
:
'43'
},
[
'secretValue'
]).
on
(
'sql'
,
function
(
sql
)
{
expect
(
sql
).
to
.
match
(
/UPDATE
\s
+
[
`"
]
+User1s
[
`"
]
+
\s
+SET
\s
+
[
`"
]
+secretValue
[
`"
]
='43',
[
`"
]
+updatedAt
[
`"
]
+='
[^
`",
]
+'
\s
+WHERE
[
`"
]
+id
[
`"
]
+=1/
)
done
()
})
})
})
})
it
(
'allows sql logging of updated statements'
,
function
(
done
)
{
var
User
=
this
.
sequelize
.
define
(
'User'
,
{
name
:
Sequelize
.
STRING
,
bio
:
Sequelize
.
TEXT
},
{
paranoid
:
true
})
User
.
sync
({
force
:
true
}).
success
(
function
()
{
User
.
create
({
name
:
'meg'
,
bio
:
'none'
}).
success
(
function
(
u
)
{
expect
(
u
).
to
.
exist
expect
(
u
).
not
.
to
.
be
.
null
u
.
updateAttributes
({
name
:
'brian'
}).
on
(
'sql'
,
function
(
sql
)
{
expect
(
sql
).
to
.
exist
expect
(
sql
.
toUpperCase
().
indexOf
(
"UPDATE"
)).
to
.
be
.
above
(
-
1
)
done
()
})
})
})
})
it
(
'updates only values that match filter'
,
function
(
done
)
{
var
self
=
this
,
data
=
[{
username
:
'Peter'
,
secretValue
:
'42'
},
{
username
:
'Paul'
,
secretValue
:
'42'
},
{
username
:
'Bob'
,
secretValue
:
'43'
}]
this
.
User
.
bulkCreate
(
data
).
success
(
function
()
{
self
.
User
.
update
({
username
:
'Bill'
},
{
secretValue
:
'42'
})
.
success
(
function
()
{
self
.
User
.
findAll
({
order
:
'id'
}).
success
(
function
(
users
)
{
expect
(
users
.
length
).
to
.
equal
(
3
)
users
.
forEach
(
function
(
user
)
{
if
(
user
.
secretValue
==
'42'
)
{
expect
(
user
.
username
).
to
.
equal
(
"Bill"
)
}
else
{
expect
(
user
.
username
).
to
.
equal
(
"Bob"
)
}
})
done
()
})
})
})
})
it
(
'updates with casting'
,
function
(
done
)
{
var
self
=
this
this
.
User
.
create
({
username
:
'John'
}).
success
(
function
(
user
)
{
self
.
User
.
update
({
username
:
self
.
sequelize
.
cast
(
'1'
,
'char'
)},
{
username
:
'John'
}).
success
(
function
()
{
self
.
User
.
all
().
success
(
function
(
users
)
{
expect
(
users
[
0
].
username
).
to
.
equal
(
'1'
)
done
()
})
})
})
})
it
(
'updates with function and column value'
,
function
(
done
)
{
var
self
=
this
this
.
User
.
create
({
username
:
'John'
}).
success
(
function
(
user
)
{
self
.
User
.
update
({
username
:
self
.
sequelize
.
fn
(
'upper'
,
self
.
sequelize
.
col
(
'username'
))},
{
username
:
'John'
}).
success
(
function
()
{
self
.
User
.
all
().
success
(
function
(
users
)
{
expect
(
users
[
0
].
username
).
to
.
equal
(
'JOHN'
)
done
()
})
})
})
})
it
(
'sets updatedAt to the current timestamp'
,
function
(
done
)
{
var
self
=
this
,
data
=
[{
username
:
'Peter'
,
secretValue
:
'42'
},
{
username
:
'Paul'
,
secretValue
:
'42'
},
{
username
:
'Bob'
,
secretValue
:
'43'
}]
this
.
User
.
bulkCreate
(
data
).
success
(
function
()
{
self
.
User
.
update
({
username
:
'Bill'
},
{
secretValue
:
'42'
}).
success
(
function
()
{
self
.
User
.
findAll
({
order
:
'id'
}).
success
(
function
(
users
)
{
expect
(
users
.
length
).
to
.
equal
(
3
)
expect
(
users
[
0
].
username
).
to
.
equal
(
"Bill"
)
expect
(
users
[
1
].
username
).
to
.
equal
(
"Bill"
)
expect
(
users
[
2
].
username
).
to
.
equal
(
"Bob"
)
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
()
})
})
})
})
})
describe
(
'destroy'
,
function
()
{
it
(
'supports transactions'
,
function
(
done
)
{
Support
.
prepareTransactionTest
(
this
.
sequelize
,
function
(
sequelize
)
{
var
User
=
sequelize
.
define
(
'User'
,
{
username
:
Sequelize
.
STRING
})
User
.
sync
({
force
:
true
}).
success
(
function
()
{
User
.
create
({
username
:
'foo'
}).
success
(
function
()
{
sequelize
.
transaction
(
function
(
t
)
{
User
.
destroy
({},
{
transaction
:
t
}).
success
(
function
()
{
User
.
count
().
success
(
function
(
count1
)
{
User
.
count
({
transaction
:
t
}).
success
(
function
(
count2
)
{
expect
(
count1
).
to
.
equal
(
1
)
expect
(
count2
).
to
.
equal
(
0
)
t
.
rollback
().
success
(
function
(){
done
()
})
})
})
})
})
})
})
})
})
it
(
'deletes values that match filter'
,
function
(
done
)
{
var
self
=
this
,
data
=
[{
username
:
'Peter'
,
secretValue
:
'42'
},
{
username
:
'Paul'
,
secretValue
:
'42'
},
{
username
:
'Bob'
,
secretValue
:
'43'
}]
this
.
User
.
bulkCreate
(
data
).
success
(
function
()
{
self
.
User
.
destroy
({
secretValue
:
'42'
})
.
success
(
function
()
{
self
.
User
.
findAll
({
order
:
'id'
}).
success
(
function
(
users
)
{
expect
(
users
.
length
).
to
.
equal
(
1
)
expect
(
users
[
0
].
username
).
to
.
equal
(
"Bob"
)
done
()
})
})
})
})
it
(
'sets deletedAt to the current timestamp if paranoid is true'
,
function
(
done
)
{
var
self
=
this
,
ident
=
self
.
sequelize
.
queryInterface
.
QueryGenerator
.
quoteIdentifier
,
escape
=
self
.
sequelize
.
queryInterface
.
QueryGenerator
.
quote
,
ParanoidUser
=
self
.
sequelize
.
define
(
'ParanoidUser'
,
{
username
:
Sequelize
.
STRING
,
secretValue
:
Sequelize
.
STRING
,
data
:
Sequelize
.
STRING
,
intVal
:
{
type
:
Sequelize
.
INTEGER
,
defaultValue
:
1
}
},
{
paranoid
:
true
})
,
data
=
[{
username
:
'Peter'
,
secretValue
:
'42'
},
{
username
:
'Paul'
,
secretValue
:
'42'
},
{
username
:
'Bob'
,
secretValue
:
'43'
}]
ParanoidUser
.
sync
({
force
:
true
}).
success
(
function
()
{
ParanoidUser
.
bulkCreate
(
data
).
success
(
function
()
{
// since we save in UTC, let's format to UTC time
var
date
=
moment
().
utc
().
format
(
'YYYY-MM-DD h:mm'
)
ParanoidUser
.
destroy
({
secretValue
:
'42'
}).
success
(
function
()
{
ParanoidUser
.
findAll
({
order
:
'id'
}).
success
(
function
(
users
)
{
expect
(
users
.
length
).
to
.
equal
(
1
)
expect
(
users
[
0
].
username
).
to
.
equal
(
"Bob"
)
self
.
sequelize
.
query
(
'SELECT * FROM '
+
ident
(
'ParanoidUsers'
)
+
' WHERE '
+
ident
(
'deletedAt'
)
+
' IS NOT NULL ORDER BY '
+
ident
(
'id'
),
null
,
{
raw
:
true
}).
success
(
function
(
users
)
{
expect
(
users
[
0
].
username
).
to
.
equal
(
"Peter"
)
expect
(
users
[
1
].
username
).
to
.
equal
(
"Paul"
)
if
(
dialect
===
"sqlite"
)
{
expect
(
moment
(
users
[
0
].
deletedAt
).
format
(
'YYYY-MM-DD h:mm'
)).
to
.
equal
(
date
)
expect
(
moment
(
users
[
1
].
deletedAt
).
format
(
'YYYY-MM-DD h:mm'
)).
to
.
equal
(
date
)
}
else
{
expect
(
moment
(
users
[
0
].
deletedAt
).
utc
().
format
(
'YYYY-MM-DD h:mm'
)).
to
.
equal
(
date
)
expect
(
moment
(
users
[
1
].
deletedAt
).
utc
().
format
(
'YYYY-MM-DD h:mm'
)).
to
.
equal
(
date
)
}
done
()
})
})
})
})
})
})
describe
(
"can't find records marked as deleted with paranoid being true"
,
function
()
{
it
(
'with the DAOFactory'
,
function
(
done
)
{
var
User
=
this
.
sequelize
.
define
(
'UserCol'
,
{
username
:
Sequelize
.
STRING
},
{
paranoid
:
true
})
User
.
sync
({
force
:
true
}).
success
(
function
()
{
User
.
bulkCreate
([
{
username
:
'Toni'
},
{
username
:
'Tobi'
},
{
username
:
'Max'
}
]).
success
(
function
()
{
User
.
find
(
1
).
success
(
function
(
user
)
{
user
.
destroy
().
success
(
function
()
{
User
.
find
(
1
).
success
(
function
(
user
)
{
expect
(
user
).
to
.
be
.
null
User
.
count
().
success
(
function
(
cnt
)
{
expect
(
cnt
).
to
.
equal
(
2
)
User
.
all
().
success
(
function
(
users
)
{
expect
(
users
).
to
.
have
.
length
(
2
)
done
()
})
})
})
})
})
})
})
})
})
})
describe
(
'find'
,
function
()
{
it
(
'supports transactions'
,
function
(
done
)
{
Support
.
prepareTransactionTest
(
this
.
sequelize
,
function
(
sequelize
)
{
var
User
=
sequelize
.
define
(
'User'
,
{
username
:
Sequelize
.
STRING
})
User
.
sync
({
force
:
true
}).
success
(
function
()
{
sequelize
.
transaction
(
function
(
t
)
{
User
.
create
({
username
:
'foo'
},
{
transaction
:
t
}).
success
(
function
()
{
User
.
find
({
username
:
'foo'
}).
success
(
function
(
user1
)
{
User
.
find
({
username
:
'foo'
},
{
transaction
:
t
}).
success
(
function
(
user2
)
{
expect
(
user1
).
to
.
be
.
null
expect
(
user2
).
to
.
not
.
be
.
null
t
.
rollback
().
success
(
function
()
{
done
()
})
})
})
})
})
})
})
})
describe
(
'general / basic function'
,
function
()
{
beforeEach
(
function
(
done
)
{
var
self
=
this
this
.
User
.
create
({
username
:
'barfooz'
}).
success
(
function
(
user
)
{
self
.
UserPrimary
=
self
.
sequelize
.
define
(
'UserPrimary'
,
{
specialkey
:
{
type
:
DataTypes
.
STRING
,
primaryKey
:
true
}
})
self
.
UserPrimary
.
sync
({
force
:
true
}).
success
(
function
()
{
self
.
UserPrimary
.
create
({
specialkey
:
'a string'
}).
success
(
function
()
{
self
.
user
=
user
done
()
})
})
})
})
it
(
'does not modify the passed arguments'
,
function
(
done
)
{
var
options
=
{
where
:
[
'specialkey = ?'
,
'awesome'
]}
this
.
UserPrimary
.
find
(
options
).
success
(
function
(
user
)
{
expect
(
options
).
to
.
deep
.
equal
({
where
:
[
'specialkey = ?'
,
'awesome'
]})
done
()
})
})
it
(
'doesn\'t throw an error when entering in a non integer value for a specified primary field'
,
function
(
done
)
{
this
.
UserPrimary
.
find
(
'a string'
).
success
(
function
(
user
)
{
expect
(
user
.
specialkey
).
to
.
equal
(
'a string'
)
done
()
})
})
it
(
'doesn\'t throw an error when entering in a non integer value'
,
function
(
done
)
{
this
.
User
.
find
(
'a string value'
).
success
(
function
(
user
)
{
expect
(
user
).
to
.
be
.
null
done
()
})
})
it
(
'returns a single dao'
,
function
(
done
)
{
var
self
=
this
this
.
User
.
find
(
this
.
user
.
id
).
success
(
function
(
user
)
{
expect
(
Array
.
isArray
(
user
)).
to
.
not
.
be
.
ok
expect
(
user
.
id
).
to
.
equal
(
self
.
user
.
id
)
expect
(
user
.
id
).
to
.
equal
(
1
)
done
()
})
})
it
(
'returns a single dao given a string id'
,
function
(
done
)
{
var
self
=
this
this
.
User
.
find
(
this
.
user
.
id
+
''
).
success
(
function
(
user
)
{
expect
(
Array
.
isArray
(
user
)).
to
.
not
.
be
.
ok
expect
(
user
.
id
).
to
.
equal
(
self
.
user
.
id
)
expect
(
user
.
id
).
to
.
equal
(
1
)
done
()
})
})
it
(
"should make aliased attributes available"
,
function
(
done
)
{
this
.
User
.
find
({
where
:
{
id
:
1
},
attributes
:
[
'id'
,
[
'username'
,
'name'
]]
}).
success
(
function
(
user
)
{
expect
(
user
.
name
).
to
.
equal
(
'barfooz'
)
done
()
})
})
it
(
"should not try to convert boolean values if they are not selected"
,
function
(
done
)
{
var
UserWithBoolean
=
this
.
sequelize
.
define
(
'UserBoolean'
,
{
active
:
Sequelize
.
BOOLEAN
})
UserWithBoolean
.
sync
({
force
:
true
}).
success
(
function
()
{
UserWithBoolean
.
create
({
active
:
true
}).
success
(
function
(
user
)
{
UserWithBoolean
.
find
({
where
:
{
id
:
user
.
id
},
attributes
:
[
'id'
]
}).
success
(
function
(
user
)
{
expect
(
user
.
active
).
not
.
to
.
exist
done
()
})
})
})
})
it
(
'finds a specific user via where option'
,
function
(
done
)
{
this
.
User
.
find
({
where
:
{
username
:
'barfooz'
}
}).
success
(
function
(
user
)
{
expect
(
user
.
username
).
to
.
equal
(
'barfooz'
)
done
()
})
})
it
(
"doesn't find a user if conditions are not matching"
,
function
(
done
)
{
this
.
User
.
find
({
where
:
{
username
:
'foo'
}
}).
success
(
function
(
user
)
{
expect
(
user
).
to
.
be
.
null
done
()
})
})
it
(
'allows sql logging'
,
function
(
done
)
{
this
.
User
.
find
({
where
:
{
username
:
'foo'
}
}).
on
(
'sql'
,
function
(
sql
)
{
expect
(
sql
).
to
.
exist
expect
(
sql
.
toUpperCase
().
indexOf
(
"SELECT"
)).
to
.
be
.
above
(
-
1
)
done
()
})
})
it
(
'ignores passed limit option'
,
function
(
done
)
{
this
.
User
.
find
({
limit
:
10
}).
success
(
function
(
user
)
{
// it returns an object instead of an array
expect
(
Array
.
isArray
(
user
)).
to
.
not
.
be
.
ok
expect
(
user
.
dataValues
.
hasOwnProperty
(
'username'
)).
to
.
be
.
ok
done
()
})
})
it
(
'finds entries via primary keys'
,
function
(
done
)
{
var
self
=
this
,
UserPrimary
=
self
.
sequelize
.
define
(
'UserWithPrimaryKey'
,
{
identifier
:
{
type
:
Sequelize
.
STRING
,
primaryKey
:
true
},
name
:
Sequelize
.
STRING
})
UserPrimary
.
sync
({
force
:
true
}).
success
(
function
()
{
UserPrimary
.
create
({
identifier
:
'an identifier'
,
name
:
'John'
}).
success
(
function
(
u
)
{
expect
(
u
.
id
).
not
.
to
.
exist
UserPrimary
.
find
(
'an identifier'
).
success
(
function
(
u2
)
{
expect
(
u2
.
identifier
).
to
.
equal
(
'an identifier'
)
expect
(
u2
.
name
).
to
.
equal
(
'John'
)
done
()
})
})
})
})
it
(
'finds entries via a string primary key called id'
,
function
(
done
)
{
var
self
=
this
,
UserPrimary
=
self
.
sequelize
.
define
(
'UserWithPrimaryKey'
,
{
id
:
{
type
:
Sequelize
.
STRING
,
primaryKey
:
true
},
name
:
Sequelize
.
STRING
})
UserPrimary
.
sync
({
force
:
true
}).
success
(
function
()
{
UserPrimary
.
create
({
id
:
'a string based id'
,
name
:
'Johnno'
}).
success
(
function
(
u
)
{
UserPrimary
.
find
(
'a string based id'
).
success
(
function
(
u2
)
{
expect
(
u2
.
id
).
to
.
equal
(
'a string based id'
)
expect
(
u2
.
name
).
to
.
equal
(
'Johnno'
)
done
()
})
})
})
})
it
(
'returns the selected fields as instance.selectedValues'
,
function
(
done
)
{
var
self
=
this
this
.
User
.
create
({
username
:
'JohnXOXOXO'
}).
success
(
function
()
{
self
.
User
.
find
({
where
:
{
username
:
'JohnXOXOXO'
},
attributes
:
[
'username'
]
}).
success
(
function
(
user
)
{
expect
(
user
.
selectedValues
).
to
.
have
.
property
(
'username'
,
'JohnXOXOXO'
)
done
()
})
})
})
it
(
'returns the selected fields and all fields of the included table as instance.selectedValues'
,
function
(
done
)
{
var
self
=
this
self
.
Mission
=
self
.
sequelize
.
define
(
'Mission'
,
{
title
:
{
type
:
Sequelize
.
STRING
,
defaultValue
:
'a mission!!'
},
foo
:
{
type
:
Sequelize
.
INTEGER
,
defaultValue
:
2
},
})
self
.
Mission
.
belongsTo
(
self
.
User
)
self
.
User
.
hasMany
(
self
.
Mission
)
self
.
Mission
.
sync
({
force
:
true
}).
success
(
function
()
{
self
.
Mission
.
create
().
success
(
function
(
mission
)
{
self
.
User
.
create
({
username
:
'John DOE'
}).
success
(
function
(
user
)
{
mission
.
setUser
(
user
).
success
(
function
()
{
self
.
User
.
find
({
where
:
{
username
:
'John DOE'
},
attributes
:
[
'username'
],
include
:
[
self
.
Mission
]
}).
success
(
function
(
user
)
{
expect
(
user
.
selectedValues
).
to
.
deep
.
equal
({
username
:
'John DOE'
})
done
()
})
})
})
})
})
})
it
(
'returns the selected fields for both the base table and the included table as instance.selectedValues'
,
function
(
done
)
{
var
self
=
this
self
.
Mission
=
self
.
sequelize
.
define
(
'Mission'
,
{
title
:
{
type
:
Sequelize
.
STRING
,
defaultValue
:
'another mission!!'
},
foo
:
{
type
:
Sequelize
.
INTEGER
,
defaultValue
:
4
},
})
self
.
Mission
.
belongsTo
(
self
.
User
)
self
.
User
.
hasMany
(
self
.
Mission
)
self
.
Mission
.
sync
({
force
:
true
}).
success
(
function
()
{
self
.
Mission
.
create
().
success
(
function
(
mission
)
{
self
.
User
.
create
({
username
:
'Brain Picker'
}).
success
(
function
(
user
)
{
mission
.
setUser
(
user
).
success
(
function
()
{
self
.
User
.
find
({
where
:
{
username
:
'Brain Picker'
},
attributes
:
[
'username'
],
include
:
[{
model
:
self
.
Mission
,
as
:
self
.
Mission
.
tableName
,
attributes
:
[
'title'
]}]
}).
success
(
function
(
user
)
{
expect
(
user
.
selectedValues
).
to
.
deep
.
equal
({
username
:
'Brain Picker'
})
expect
(
user
.
missions
[
0
].
selectedValues
).
to
.
deep
.
equal
({
id
:
1
,
title
:
'another mission!!'
})
expect
(
user
.
missions
[
0
].
foo
).
not
.
to
.
exist
done
()
})
})
})
})
})
})
it
(
'always honors ZERO as primary key'
,
function
(
_done
)
{
var
self
=
this
,
permutations
=
[
0
,
'0'
,
{
where
:
{
id
:
0
}},
{
where
:
{
id
:
'0'
}}
]
,
done
=
_
.
after
(
2
*
permutations
.
length
,
_done
)
this
.
User
.
bulkCreate
([{
username
:
'jack'
},
{
username
:
'jack'
}]).
success
(
function
()
{
permutations
.
forEach
(
function
(
perm
)
{
self
.
User
.
find
(
perm
).
done
(
function
(
err
,
user
)
{
expect
(
err
).
to
.
be
.
null
expect
(
user
).
to
.
be
.
null
done
()
}).
on
(
'sql'
,
function
(
s
)
{
expect
(
s
.
indexOf
(
0
)).
not
.
to
.
equal
(
-
1
)
done
()
})
})
})
})
it
(
'should allow us to find IDs using capital letters'
,
function
(
done
)
{
var
User
=
this
.
sequelize
.
define
(
'User'
+
config
.
rand
(),
{
ID
:
{
type
:
Sequelize
.
INTEGER
,
primaryKey
:
true
,
autoIncrement
:
true
},
Login
:
{
type
:
Sequelize
.
STRING
}
})
User
.
sync
({
force
:
true
}).
success
(
function
()
{
User
.
create
({
Login
:
'foo'
}).
success
(
function
()
{
User
.
find
(
1
).
success
(
function
(
user
)
{
expect
(
user
).
to
.
exist
expect
(
user
.
ID
).
to
.
equal
(
1
)
done
()
})
})
})
})
})
describe
(
'eager loading'
,
function
()
{
beforeEach
(
function
(
done
)
{
var
self
=
this
self
.
Task
=
self
.
sequelize
.
define
(
'Task'
,
{
title
:
Sequelize
.
STRING
})
self
.
Worker
=
self
.
sequelize
.
define
(
'Worker'
,
{
name
:
Sequelize
.
STRING
})
this
.
init
=
function
(
callback
)
{
self
.
Task
.
sync
({
force
:
true
}).
success
(
function
()
{
self
.
Worker
.
sync
({
force
:
true
}).
success
(
function
()
{
self
.
Worker
.
create
({
name
:
'worker'
}).
success
(
function
(
worker
)
{
self
.
Task
.
create
({
title
:
'homework'
}).
success
(
function
(
task
)
{
self
.
worker
=
worker
self
.
task
=
task
callback
()
})
})
})
})
}
done
()
})
describe
(
'belongsTo'
,
function
()
{
describe
(
'generic'
,
function
()
{
it
(
'throws an error about unexpected input if include contains a non-object'
,
function
(
done
)
{
var
self
=
this
expect
(
function
()
{
self
.
Worker
.
find
({
include
:
[
1
]
})
}).
to
.
throw
(
Error
,
'Include unexpected. Element has to be either an instance of DAOFactory or an object.'
)
done
()
})
it
(
'throws an error about missing attributes if include contains an object with daoFactory'
,
function
(
done
)
{
var
self
=
this
expect
(
function
()
{
self
.
Worker
.
find
({
include
:
[
{
daoFactory
:
self
.
Worker
}
]
})
}).
to
.
throw
(
Error
,
'Include malformed. Expected attributes: daoFactory, as!'
)
done
()
})
it
(
'throws an error if included DaoFactory is not associated'
,
function
(
done
)
{
var
self
=
this
expect
(
function
()
{
self
.
Worker
.
find
({
include
:
[
self
.
Task
]
})
}).
to
.
throw
(
Error
,
'Task is not associated to Worker!'
)
done
()
})
it
(
'returns the associated worker via task.worker'
,
function
(
done
)
{
var
self
=
this
this
.
Task
.
belongsTo
(
this
.
Worker
)
this
.
init
(
function
()
{
self
.
task
.
setWorker
(
self
.
worker
).
success
(
function
()
{
self
.
Task
.
find
({
where
:
{
title
:
'homework'
},
include
:
[
self
.
Worker
]
}).
complete
(
function
(
err
,
task
)
{
expect
(
err
).
to
.
be
.
null
expect
(
task
).
to
.
exist
expect
(
task
.
worker
).
to
.
exist
expect
(
task
.
worker
.
name
).
to
.
equal
(
'worker'
)
done
()
})
})
})
})
})
it
(
'returns the private and public ip'
,
function
(
done
)
{
var
self
=
Object
.
create
(
this
)
self
.
Domain
=
self
.
sequelize
.
define
(
'Domain'
,
{
ip
:
Sequelize
.
STRING
})
self
.
Environment
=
self
.
sequelize
.
define
(
'Environment'
,
{
name
:
Sequelize
.
STRING
})
self
.
Environment
.
belongsTo
(
self
.
Domain
,
{
as
:
'PrivateDomain'
,
foreignKey
:
'privateDomainId'
})
.
belongsTo
(
self
.
Domain
,
{
as
:
'PublicDomain'
,
foreignKey
:
'publicDomainId'
})
self
.
Domain
.
sync
({
force
:
true
}).
success
(
function
()
{
self
.
Environment
.
sync
({
force
:
true
}).
success
(
function
()
{
self
.
Domain
.
create
({
ip
:
'192.168.0.1'
}).
success
(
function
(
privateIp
)
{
self
.
Domain
.
create
({
ip
:
'91.65.189.19'
}).
success
(
function
(
publicIp
)
{
self
.
Environment
.
create
({
name
:
'environment'
}).
success
(
function
(
env
)
{
env
.
setPrivateDomain
(
privateIp
).
success
(
function
()
{
env
.
setPublicDomain
(
publicIp
).
success
(
function
()
{
self
.
Environment
.
find
({
where
:
{
name
:
'environment'
},
include
:
[
{
daoFactory
:
self
.
Domain
,
as
:
'PrivateDomain'
},
{
daoFactory
:
self
.
Domain
,
as
:
'PublicDomain'
}
]
}).
complete
(
function
(
err
,
environment
)
{
expect
(
err
).
to
.
be
.
null
expect
(
environment
).
to
.
exist
expect
(
environment
.
privateDomain
).
to
.
exist
expect
(
environment
.
privateDomain
.
ip
).
to
.
equal
(
'192.168.0.1'
)
expect
(
environment
.
publicDomain
).
to
.
exist
expect
(
environment
.
publicDomain
.
ip
).
to
.
equal
(
'91.65.189.19'
)
done
()
})
})
})
})
})
})
})
})
})
it
(
'eager loads with non-id primary keys'
,
function
(
done
)
{
var
self
=
this
self
.
User
=
self
.
sequelize
.
define
(
'UserPKeagerbelong'
,
{
username
:
{
type
:
Sequelize
.
STRING
,
primaryKey
:
true
}
})
self
.
Group
=
self
.
sequelize
.
define
(
'GroupPKeagerbelong'
,
{
name
:
{
type
:
Sequelize
.
STRING
,
primaryKey
:
true
}
})
self
.
User
.
belongsTo
(
self
.
Group
)
self
.
sequelize
.
sync
({
force
:
true
}).
success
(
function
()
{
self
.
User
.
create
({
username
:
'someone'
,
GroupPKeagerbelongId
:
'people'
}).
success
(
function
()
{
self
.
Group
.
create
({
name
:
'people'
}).
success
(
function
()
{
self
.
User
.
find
({
where
:
{
username
:
'someone'
},
include
:
[
self
.
Group
]
}).
complete
(
function
(
err
,
someUser
)
{
expect
(
err
).
to
.
be
.
null
expect
(
someUser
).
to
.
exist
expect
(
someUser
.
username
).
to
.
equal
(
'someone'
)
expect
(
someUser
.
groupPKeagerbelong
.
name
).
to
.
equal
(
'people'
)
done
()
})
})
})
})
})
it
(
'getting parent data in many to one relationship'
,
function
(
done
)
{
var
self
=
this
;
var
User
=
self
.
sequelize
.
define
(
'User'
,
{
id
:
{
type
:
Sequelize
.
INTEGER
,
autoIncrement
:
true
,
primaryKey
:
true
},
username
:
{
type
:
Sequelize
.
STRING
}
})
var
Message
=
self
.
sequelize
.
define
(
'Message'
,
{
id
:
{
type
:
Sequelize
.
INTEGER
,
autoIncrement
:
true
,
primaryKey
:
true
},
user_id
:
{
type
:
Sequelize
.
INTEGER
},
message
:
{
type
:
Sequelize
.
STRING
}
})
User
.
hasMany
(
Message
)
Message
.
belongsTo
(
User
,
{
foreignKey
:
'user_id'
})
Message
.
sync
({
force
:
true
}).
success
(
function
()
{
User
.
sync
({
force
:
true
}).
success
(
function
()
{
User
.
create
({
username
:
'test_testerson'
}).
success
(
function
(
user
)
{
Message
.
create
({
user_id
:
user
.
id
,
message
:
'hi there!'
}).
success
(
function
(
message
)
{
Message
.
create
({
user_id
:
user
.
id
,
message
:
'a second message'
}).
success
(
function
(
message
)
{
Message
.
findAll
({
where
:
{
user_id
:
user
.
id
},
attributes
:
[
'user_id'
,
'message'
],
include
:
[{
model
:
User
,
as
:
User
.
tableName
,
attributes
:
[
'username'
]
}]
}).
success
(
function
(
messages
)
{
expect
(
messages
.
length
).
to
.
equal
(
2
);
expect
(
messages
[
0
].
message
).
to
.
equal
(
'hi there!'
);
expect
(
messages
[
0
].
user
.
username
).
to
.
equal
(
'test_testerson'
);
expect
(
messages
[
1
].
message
).
to
.
equal
(
'a second message'
);
expect
(
messages
[
1
].
user
.
username
).
to
.
equal
(
'test_testerson'
);
done
()
})
})
})
})
})
})
})
it
(
'allows mulitple assocations of the same model with different alias'
,
function
(
done
)
{
var
self
=
this
this
.
Worker
.
belongsTo
(
this
.
Task
,
{
as
:
'ToDo'
})
this
.
Worker
.
belongsTo
(
this
.
Task
,
{
as
:
'DoTo'
})
this
.
init
(
function
()
{
self
.
Worker
.
find
({
include
:
[
{
model
:
self
.
Task
,
as
:
'ToDo'
},
{
model
:
self
.
Task
,
as
:
'DoTo'
}
]
}).
success
(
function
()
{
// Just being able to include both shows that this test works, so no assertions needed
done
()
})
})
})
})
describe
(
'hasOne'
,
function
()
{
beforeEach
(
function
(
done
)
{
var
self
=
this
this
.
Worker
.
hasOne
(
this
.
Task
)
this
.
init
(
function
()
{
self
.
worker
.
setTask
(
self
.
task
).
success
(
function
()
{
done
()
})
})
})
it
(
'throws an error if included DaoFactory is not associated'
,
function
(
done
)
{
var
self
=
this
expect
(
function
()
{
self
.
Task
.
find
({
include
:
[
self
.
Worker
]
})
}).
to
.
throw
(
Error
,
'Worker is not associated to Task!'
)
done
()
})
it
(
'returns the associated task via worker.task'
,
function
(
done
)
{
this
.
Worker
.
find
({
where
:
{
name
:
'worker'
},
include
:
[
this
.
Task
]
}).
complete
(
function
(
err
,
worker
)
{
expect
(
err
).
to
.
be
.
null
expect
(
worker
).
to
.
exist
expect
(
worker
.
task
).
to
.
exist
expect
(
worker
.
task
.
title
).
to
.
equal
(
'homework'
)
done
()
})
})
it
(
'eager loads with non-id primary keys'
,
function
(
done
)
{
var
self
=
this
self
.
User
=
self
.
sequelize
.
define
(
'UserPKeagerone'
,
{
username
:
{
type
:
Sequelize
.
STRING
,
primaryKey
:
true
}
})
self
.
Group
=
self
.
sequelize
.
define
(
'GroupPKeagerone'
,
{
name
:
{
type
:
Sequelize
.
STRING
,
primaryKey
:
true
}
})
self
.
Group
.
hasOne
(
self
.
User
)
self
.
sequelize
.
sync
({
force
:
true
}).
success
(
function
()
{
self
.
User
.
create
({
username
:
'someone'
,
GroupPKeageroneId
:
'people'
}).
success
(
function
()
{
self
.
Group
.
create
({
name
:
'people'
}).
success
(
function
()
{
self
.
Group
.
find
({
where
:
{
name
:
'people'
},
include
:
[
self
.
User
]
}).
complete
(
function
(
err
,
someGroup
)
{
expect
(
err
).
to
.
be
.
null
expect
(
someGroup
).
to
.
exist
expect
(
someGroup
.
name
).
to
.
equal
(
'people'
)
expect
(
someGroup
.
userPKeagerone
.
username
).
to
.
equal
(
'someone'
)
done
()
})
})
})
})
})
})
describe
(
'hasOne with alias'
,
function
()
{
it
(
'throws an error if included DaoFactory is not referenced by alias'
,
function
(
done
)
{
var
self
=
this
expect
(
function
()
{
self
.
Worker
.
find
({
include
:
[
self
.
Task
]
})
}).
to
.
throw
(
Error
,
'Task is not associated to Worker!'
)
done
()
})
describe
(
'alias'
,
function
(
done
)
{
beforeEach
(
function
(
done
)
{
var
self
=
this
this
.
Worker
.
hasOne
(
this
.
Task
,
{
as
:
'ToDo'
})
this
.
init
(
function
()
{
self
.
worker
.
setToDo
(
self
.
task
).
success
(
function
()
{
done
()
})
})
})
it
(
'throws an error if alias is not associated'
,
function
(
done
)
{
var
self
=
this
expect
(
function
()
{
self
.
Worker
.
find
({
include
:
[
{
daoFactory
:
self
.
Task
,
as
:
'Work'
}
]
})
}).
to
.
throw
(
Error
,
'Task (Work) is not associated to Worker!'
)
done
()
})
it
(
'returns the associated task via worker.task'
,
function
(
done
)
{
this
.
Worker
.
find
({
where
:
{
name
:
'worker'
},
include
:
[
{
daoFactory
:
this
.
Task
,
as
:
'ToDo'
}
]
}).
complete
(
function
(
err
,
worker
)
{
expect
(
err
).
to
.
be
.
null
expect
(
worker
).
to
.
exist
expect
(
worker
.
toDo
).
to
.
exist
expect
(
worker
.
toDo
.
title
).
to
.
equal
(
'homework'
)
done
()
})
})
it
(
'returns the associated task via worker.task when daoFactory is aliased with model'
,
function
(
done
)
{
this
.
Worker
.
find
({
where
:
{
name
:
'worker'
},
include
:
[
{
model
:
this
.
Task
,
as
:
'ToDo'
}
]
}).
complete
(
function
(
err
,
worker
)
{
expect
(
worker
.
toDo
.
title
).
to
.
equal
(
'homework'
)
done
()
})
})
it
(
'allows mulitple assocations of the same model with different alias'
,
function
(
done
)
{
var
self
=
this
this
.
Worker
.
hasOne
(
this
.
Task
,
{
as
:
'DoTo'
})
this
.
init
(
function
()
{
self
.
Worker
.
find
({
include
:
[
{
model
:
self
.
Task
,
as
:
'ToDo'
},
{
model
:
self
.
Task
,
as
:
'DoTo'
}
]
}).
success
(
function
()
{
// Just being able to include both shows that this test works, so no assertions needed
done
()
})
})
})
})
})
describe
(
'hasMany'
,
function
()
{
beforeEach
(
function
(
done
)
{
var
self
=
this
this
.
Worker
.
hasMany
(
this
.
Task
)
this
.
init
(
function
()
{
self
.
worker
.
setTasks
([
self
.
task
]).
success
(
function
()
{
done
()
})
})
})
it
(
'throws an error if included DaoFactory is not associated'
,
function
(
done
)
{
var
self
=
this
expect
(
function
()
{
self
.
Task
.
find
({
include
:
[
self
.
Worker
]
})
}).
to
.
throw
(
Error
,
'Worker is not associated to Task!'
)
done
()
})
it
(
'returns the associated tasks via worker.tasks'
,
function
(
done
)
{
this
.
Worker
.
find
({
where
:
{
name
:
'worker'
},
include
:
[
this
.
Task
]
}).
complete
(
function
(
err
,
worker
)
{
expect
(
err
).
to
.
be
.
null
expect
(
worker
).
to
.
exist
expect
(
worker
.
tasks
).
to
.
exist
expect
(
worker
.
tasks
[
0
].
title
).
to
.
equal
(
'homework'
)
done
()
})
})
it
(
'including two has many relations should not result in duplicate values'
,
function
(
done
)
{
var
self
=
this
self
.
Contact
=
self
.
sequelize
.
define
(
'Contact'
,
{
name
:
DataTypes
.
TEXT
})
self
.
Photo
=
self
.
sequelize
.
define
(
'Photo'
,
{
img
:
DataTypes
.
TEXT
})
self
.
PhoneNumber
=
self
.
sequelize
.
define
(
'PhoneNumber'
,
{
phone
:
DataTypes
.
TEXT
})
self
.
Contact
.
hasMany
(
self
.
Photo
,
{
as
:
'Photos'
})
self
.
Contact
.
hasMany
(
self
.
PhoneNumber
)
self
.
sequelize
.
sync
({
force
:
true
}).
success
(
function
()
{
self
.
Contact
.
create
({
name
:
'Boris'
}).
success
(
function
(
someContact
)
{
self
.
Photo
.
create
({
img
:
'img.jpg'
}).
success
(
function
(
somePhoto
)
{
self
.
PhoneNumber
.
create
({
phone
:
'000000'
}).
success
(
function
(
somePhone1
)
{
self
.
PhoneNumber
.
create
({
phone
:
'111111'
}).
success
(
function
(
somePhone2
)
{
someContact
.
setPhotos
([
somePhoto
]).
complete
(
function
(
err
,
data
)
{
expect
(
err
).
to
.
be
.
null
someContact
.
setPhoneNumbers
([
somePhone1
,
somePhone2
]).
complete
(
function
(
err
,
data
)
{
self
.
Contact
.
find
({
where
:
{
name
:
'Boris'
},
include
:
[
self
.
PhoneNumber
,
{
daoFactory
:
self
.
Photo
,
as
:
'Photos'
}]
}).
complete
(
function
(
err
,
fetchedContact
)
{
expect
(
err
).
to
.
be
.
null
expect
(
fetchedContact
).
to
.
exist
expect
(
fetchedContact
.
photos
.
length
).
to
.
equal
(
1
)
expect
(
fetchedContact
.
phoneNumbers
.
length
).
to
.
equal
(
2
)
done
()
})
})
})
})
})
})
})
})
})
it
(
'eager loads with non-id primary keys'
,
function
(
done
)
{
var
self
=
this
self
.
User
=
self
.
sequelize
.
define
(
'UserPKeagerone'
,
{
username
:
{
type
:
Sequelize
.
STRING
,
primaryKey
:
true
}
})
self
.
Group
=
self
.
sequelize
.
define
(
'GroupPKeagerone'
,
{
name
:
{
type
:
Sequelize
.
STRING
,
primaryKey
:
true
}
})
self
.
Group
.
hasMany
(
self
.
User
)
self
.
User
.
hasMany
(
self
.
Group
)
self
.
sequelize
.
sync
({
force
:
true
}).
success
(
function
()
{
self
.
User
.
create
({
username
:
'someone'
}).
success
(
function
(
someUser
)
{
self
.
Group
.
create
({
name
:
'people'
}).
success
(
function
(
someGroup
)
{
someUser
.
setGroupPKeagerones
([
someGroup
]).
complete
(
function
(
err
,
data
)
{
expect
(
err
).
to
.
be
.
null
self
.
User
.
find
({
where
:
{
username
:
'someone'
},
include
:
[
self
.
Group
]
}).
complete
(
function
(
err
,
someUser
)
{
expect
(
err
).
to
.
be
.
null
expect
(
someUser
).
to
.
exist
expect
(
someUser
.
username
).
to
.
equal
(
'someone'
)
expect
(
someUser
.
groupPKeagerones
[
0
].
name
).
to
.
equal
(
'people'
)
done
()
})
})
})
})
})
})
})
describe
(
'hasMany with alias'
,
function
()
{
it
(
'throws an error if included DaoFactory is not referenced by alias'
,
function
(
done
)
{
var
self
=
this
expect
(
function
()
{
self
.
Worker
.
find
({
include
:
[
self
.
Task
]
})
}).
to
.
throw
(
Error
,
'Task is not associated to Worker!'
)
done
()
})
describe
(
'alias'
,
function
()
{
beforeEach
(
function
(
done
)
{
var
self
=
this
this
.
Worker
.
hasMany
(
this
.
Task
,
{
as
:
'ToDos'
})
this
.
init
(
function
()
{
self
.
worker
.
setToDos
([
self
.
task
]).
success
(
function
()
{
done
()
})
})
})
it
(
'throws an error if alias is not associated'
,
function
(
done
)
{
var
self
=
this
expect
(
function
()
{
self
.
Worker
.
find
({
include
:
[
{
daoFactory
:
self
.
Task
,
as
:
'Work'
}
]
})
}).
to
.
throw
(
Error
,
'Task (Work) is not associated to Worker!'
)
done
()
})
it
(
'returns the associated task via worker.task'
,
function
(
done
)
{
this
.
Worker
.
find
({
where
:
{
name
:
'worker'
},
include
:
[
{
daoFactory
:
this
.
Task
,
as
:
'ToDos'
}
]
}).
complete
(
function
(
err
,
worker
)
{
expect
(
err
).
to
.
be
.
null
expect
(
worker
).
to
.
exist
expect
(
worker
.
toDos
).
to
.
exist
expect
(
worker
.
toDos
[
0
].
title
).
to
.
equal
(
'homework'
)
done
()
})
})
it
(
'returns the associated task via worker.task when daoFactory is aliased with model'
,
function
(
done
)
{
this
.
Worker
.
find
({
where
:
{
name
:
'worker'
},
include
:
[
{
model
:
this
.
Task
,
as
:
'ToDos'
}
]
}).
complete
(
function
(
err
,
worker
)
{
expect
(
worker
.
toDos
[
0
].
title
).
to
.
equal
(
'homework'
)
done
()
})
})
it
(
'allows mulitple assocations of the same model with different alias'
,
function
(
done
)
{
var
self
=
this
this
.
Worker
.
hasMany
(
this
.
Task
,
{
as
:
'DoTos'
})
this
.
init
(
function
()
{
self
.
Worker
.
find
({
include
:
[
{
model
:
self
.
Task
,
as
:
'ToDos'
},
{
model
:
self
.
Task
,
as
:
'DoTos'
}
]
}).
success
(
function
()
{
// Just being able to include both shows that this test works, so no assertions needed
done
()
})
})
})
})
})
})
describe
(
'queryOptions'
,
function
()
{
beforeEach
(
function
(
done
)
{
var
self
=
this
this
.
User
.
create
({
username
:
'barfooz'
}).
success
(
function
(
user
)
{
self
.
user
=
user
done
()
})
})
it
(
"should return a DAO when queryOptions are not set"
,
function
(
done
)
{
var
self
=
this
this
.
User
.
find
({
where
:
{
username
:
'barfooz'
}}).
done
(
function
(
err
,
user
)
{
expect
(
user
).
to
.
be
.
instanceOf
(
self
.
User
.
DAO
)
done
()
})
})
it
(
"should return a DAO when raw is false"
,
function
(
done
)
{
var
self
=
this
this
.
User
.
find
({
where
:
{
username
:
'barfooz'
}},
{
raw
:
false
}).
done
(
function
(
err
,
user
)
{
expect
(
user
).
to
.
be
.
instanceOf
(
self
.
User
.
DAO
)
done
()
})
})
it
(
"should return raw data when raw is true"
,
function
(
done
)
{
var
self
=
this
this
.
User
.
find
({
where
:
{
username
:
'barfooz'
}},
{
raw
:
true
}).
done
(
function
(
err
,
user
)
{
expect
(
user
).
to
.
not
.
be
.
instanceOf
(
self
.
User
.
DAO
)
expect
(
user
).
to
.
be
.
instanceOf
(
Object
)
done
()
})
})
})
})
describe
(
'findAll'
,
function
()
{
it
(
'supports transactions'
,
function
(
done
)
{
Support
.
prepareTransactionTest
(
this
.
sequelize
,
function
(
sequelize
)
{
var
User
=
sequelize
.
define
(
'User'
,
{
username
:
Sequelize
.
STRING
})
User
.
sync
({
force
:
true
}).
success
(
function
()
{
sequelize
.
transaction
(
function
(
t
)
{
User
.
create
({
username
:
'foo'
},
{
transaction
:
t
}).
success
(
function
()
{
User
.
findAll
({
username
:
'foo'
}).
success
(
function
(
users1
)
{
User
.
findAll
({
transaction
:
t
}).
success
(
function
(
users2
)
{
User
.
findAll
({
username
:
'foo'
},
{
transaction
:
t
}).
success
(
function
(
users3
)
{
expect
(
users1
.
length
).
to
.
equal
(
0
)
expect
(
users2
.
length
).
to
.
equal
(
1
)
expect
(
users3
.
length
).
to
.
equal
(
1
)
t
.
rollback
().
success
(
function
()
{
done
()
})
})
})
})
})
})
})
})
})
describe
(
'special where conditions/smartWhere object'
,
function
()
{
beforeEach
(
function
(
done
)
{
var
self
=
this
this
.
User
.
bulkCreate
([
{
username
:
'boo'
,
intVal
:
5
,
theDate
:
'2013-01-01 12:00'
},
{
username
:
'boo2'
,
intVal
:
10
,
theDate
:
'2013-01-10 12:00'
}
]).
success
(
function
(
user2
)
{
done
()
})
})
it
(
'should be able to find rows where attribute is in a list of values'
,
function
(
done
)
{
this
.
User
.
findAll
({
where
:
{
username
:
[
'boo'
,
'boo2'
]
}
}).
success
(
function
(
users
){
expect
(
users
).
to
.
have
.
length
(
2
);
done
()
});
})
it
(
'should not break when trying to find rows using an array of primary keys'
,
function
(
done
)
{
this
.
User
.
findAll
({
where
:
{
id
:
[
1
,
2
,
3
]
}
}).
success
(
function
(
users
){
done
();
});
})
it
(
'should be able to find a row using like'
,
function
(
done
)
{
this
.
User
.
findAll
({
where
:
{
username
:
{
like
:
'%2'
}
}
}).
success
(
function
(
users
)
{
expect
(
users
).
to
.
be
.
an
.
instanceof
(
Array
)
expect
(
users
).
to
.
have
.
length
(
1
)
expect
(
users
[
0
].
username
).
to
.
equal
(
'boo2'
)
expect
(
users
[
0
].
intVal
).
to
.
equal
(
10
)
done
()
})
})
it
(
'should be able to find a row using not like'
,
function
(
done
)
{
this
.
User
.
findAll
({
where
:
{
username
:
{
nlike
:
'%2'
}
}
}).
success
(
function
(
users
)
{
expect
(
users
).
to
.
be
.
an
.
instanceof
(
Array
)
expect
(
users
).
to
.
have
.
length
(
1
)
expect
(
users
[
0
].
username
).
to
.
equal
(
'boo'
)
expect
(
users
[
0
].
intVal
).
to
.
equal
(
5
)
done
()
})
})
it
(
'should be able to find a row between a certain date using the between shortcut'
,
function
(
done
)
{
this
.
User
.
findAll
({
where
:
{
theDate
:
{
'..'
:
[
'2013-01-02'
,
'2013-01-11'
]
}
}
}).
success
(
function
(
users
)
{
expect
(
users
[
0
].
username
).
to
.
equal
(
'boo2'
)
expect
(
users
[
0
].
intVal
).
to
.
equal
(
10
)
done
()
})
})
it
(
'should be able to find a row not between a certain integer using the not between shortcut'
,
function
(
done
)
{
this
.
User
.
findAll
({
where
:
{
intVal
:
{
'!..'
:
[
8
,
10
]
}
}
}).
success
(
function
(
users
)
{
expect
(
users
[
0
].
username
).
to
.
equal
(
'boo'
)
expect
(
users
[
0
].
intVal
).
to
.
equal
(
5
)
done
()
})
})
it
(
'should be able to handle false/true values just fine...'
,
function
(
done
)
{
var
User
=
this
.
User
,
escapeChar
=
(
dialect
===
"postgres"
)
?
'"'
:
'`'
User
.
bulkCreate
([
{
username
:
'boo5'
,
aBool
:
false
},
{
username
:
'boo6'
,
aBool
:
true
}
]).
success
(
function
()
{
User
.
all
({
where
:
[
escapeChar
+
'aBool'
+
escapeChar
+
' = ?'
,
false
]}).
success
(
function
(
users
)
{
expect
(
users
).
to
.
have
.
length
(
1
)
expect
(
users
[
0
].
username
).
to
.
equal
(
'boo5'
)
User
.
all
({
where
:
[
escapeChar
+
'aBool'
+
escapeChar
+
' = ?'
,
true
]}).
success
(
function
(
_users
)
{
expect
(
_users
).
to
.
have
.
length
(
1
)
expect
(
_users
[
0
].
username
).
to
.
equal
(
'boo6'
)
done
()
})
})
})
})
it
(
'should be able to handle false/true values through associations as well...'
,
function
(
done
)
{
var
User
=
this
.
User
,
escapeChar
=
(
dialect
===
"postgres"
)
?
'"'
:
'`'
var
Passports
=
this
.
sequelize
.
define
(
'Passports'
,
{
isActive
:
Sequelize
.
BOOLEAN
})
User
.
hasMany
(
Passports
)
Passports
.
belongsTo
(
User
)
User
.
sync
({
force
:
true
}).
success
(
function
()
{
Passports
.
sync
({
force
:
true
}).
success
(
function
()
{
User
.
bulkCreate
([
{
username
:
'boo5'
,
aBool
:
false
},
{
username
:
'boo6'
,
aBool
:
true
}
]).
success
(
function
()
{
Passports
.
bulkCreate
([
{
isActive
:
true
},
{
isActive
:
false
}
]).
success
(
function
()
{
User
.
find
(
1
).
success
(
function
(
user
)
{
Passports
.
find
(
1
).
success
(
function
(
passport
)
{
user
.
setPassports
([
passport
]).
success
(
function
()
{
User
.
find
(
2
).
success
(
function
(
_user
)
{
Passports
.
find
(
2
).
success
(
function
(
_passport
)
{
_user
.
setPassports
([
_passport
]).
success
(
function
()
{
_user
.
getPassports
({
where
:
[
escapeChar
+
'isActive'
+
escapeChar
+
' = ?'
,
false
]}).
success
(
function
(
theFalsePassport
)
{
user
.
getPassports
({
where
:
[
escapeChar
+
'isActive'
+
escapeChar
+
' = ?'
,
true
]}).
success
(
function
(
theTruePassport
)
{
expect
(
theFalsePassport
).
to
.
have
.
length
(
1
)
expect
(
theFalsePassport
[
0
].
isActive
).
to
.
be
.
false
expect
(
theTruePassport
).
to
.
have
.
length
(
1
)
expect
(
theTruePassport
[
0
].
isActive
).
to
.
be
.
true
done
()
})
})
})
})
})
})
})
})
})
})
})
})
})
it
(
'should be able to return a record with primaryKey being null for new inserts'
,
function
(
done
)
{
var
Session
=
this
.
sequelize
.
define
(
'Session'
,
{
token
:
{
type
:
DataTypes
.
TEXT
,
allowNull
:
false
},
lastUpdate
:
{
type
:
DataTypes
.
DATE
,
allowNull
:
false
}
},
{
charset
:
'utf8'
,
collate
:
'utf8_general_ci'
,
omitNull
:
true
})
,
User
=
this
.
sequelize
.
define
(
'User'
,
{
name
:
{
type
:
DataTypes
.
STRING
,
allowNull
:
false
,
unique
:
true
},
password
:
{
type
:
DataTypes
.
STRING
,
allowNull
:
false
},
isAdmin
:
{
type
:
DataTypes
.
BOOLEAN
,
allowNull
:
false
,
defaultValue
:
false
}
},
{
charset
:
'utf8'
,
collate
:
'utf8_general_ci'
})
User
.
hasMany
(
Session
,
{
as
:
'Sessions'
})
Session
.
belongsTo
(
User
)
Session
.
sync
({
force
:
true
}).
success
(
function
()
{
User
.
sync
({
force
:
true
}).
success
(
function
()
{
User
.
create
({
name
:
'Name1'
,
password
:
'123'
,
isAdmin
:
false
}).
success
(
function
(
user
)
{
var
sess
=
Session
.
build
({
lastUpdate
:
new
Date
(),
token
:
'123'
})
user
.
addSession
(
sess
).
success
(
function
(
u
)
{
expect
(
u
.
token
).
to
.
equal
(
'123'
)
done
()
})
})
})
})
})
it
(
'should be able to find a row between a certain date'
,
function
(
done
)
{
this
.
User
.
findAll
({
where
:
{
theDate
:
{
between
:
[
'2013-01-02'
,
'2013-01-11'
]
}
}
}).
success
(
function
(
users
)
{
expect
(
users
[
0
].
username
).
to
.
equal
(
'boo2'
)
expect
(
users
[
0
].
intVal
).
to
.
equal
(
10
)
done
()
})
})
it
(
'should be able to find a row between a certain date and an additional where clause'
,
function
(
done
)
{
this
.
User
.
findAll
({
where
:
{
theDate
:
{
between
:
[
'2013-01-02'
,
'2013-01-11'
]
},
intVal
:
10
}
}).
success
(
function
(
users
)
{
expect
(
users
[
0
].
username
).
to
.
equal
(
'boo2'
)
expect
(
users
[
0
].
intVal
).
to
.
equal
(
10
)
done
()
})
})
it
(
'should be able to find a row not between a certain integer'
,
function
(
done
)
{
this
.
User
.
findAll
({
where
:
{
intVal
:
{
nbetween
:
[
8
,
10
]
}
}
}).
success
(
function
(
users
)
{
expect
(
users
[
0
].
username
).
to
.
equal
(
'boo'
)
expect
(
users
[
0
].
intVal
).
to
.
equal
(
5
)
done
()
})
})
it
(
'should be able to find a row using not between and between logic'
,
function
(
done
)
{
this
.
User
.
findAll
({
where
:
{
theDate
:
{
between
:
[
'2012-12-10'
,
'2013-01-02'
],
nbetween
:
[
'2013-01-04'
,
'2013-01-20'
]
}
}
}).
success
(
function
(
users
)
{
expect
(
users
[
0
].
username
).
to
.
equal
(
'boo'
)
expect
(
users
[
0
].
intVal
).
to
.
equal
(
5
)
done
()
})
})
it
(
'should be able to find a row using not between and between logic with dates'
,
function
(
done
)
{
this
.
User
.
findAll
({
where
:
{
theDate
:
{
between
:
[
new
Date
(
'2012-12-10'
),
new
Date
(
'2013-01-02'
)],
nbetween
:
[
new
Date
(
'2013-01-04'
),
new
Date
(
'2013-01-20'
)]
}
}
}).
success
(
function
(
users
)
{
expect
(
users
[
0
].
username
).
to
.
equal
(
'boo'
)
expect
(
users
[
0
].
intVal
).
to
.
equal
(
5
)
done
()
})
})
it
(
'should be able to find a row using greater than or equal to logic with dates'
,
function
(
done
)
{
this
.
User
.
findAll
({
where
:
{
theDate
:
{
gte
:
new
Date
(
'2013-01-09'
)
}
}
}).
success
(
function
(
users
)
{
expect
(
users
[
0
].
username
).
to
.
equal
(
'boo2'
)
expect
(
users
[
0
].
intVal
).
to
.
equal
(
10
)
done
()
})
})
it
(
'should be able to find a row using greater than or equal to'
,
function
(
done
)
{
this
.
User
.
find
({
where
:
{
intVal
:
{
gte
:
6
}
}
}).
success
(
function
(
user
)
{
expect
(
user
.
username
).
to
.
equal
(
'boo2'
)
expect
(
user
.
intVal
).
to
.
equal
(
10
)
done
()
})
})
it
(
'should be able to find a row using greater than'
,
function
(
done
)
{
this
.
User
.
find
({
where
:
{
intVal
:
{
gt
:
5
}
}
}).
success
(
function
(
user
)
{
expect
(
user
.
username
).
to
.
equal
(
'boo2'
)
expect
(
user
.
intVal
).
to
.
equal
(
10
)
done
()
})
})
it
(
'should be able to find a row using lesser than or equal to'
,
function
(
done
)
{
this
.
User
.
find
({
where
:
{
intVal
:
{
lte
:
5
}
}
}).
success
(
function
(
user
)
{
expect
(
user
.
username
).
to
.
equal
(
'boo'
)
expect
(
user
.
intVal
).
to
.
equal
(
5
)
done
()
})
})
it
(
'should be able to find a row using lesser than'
,
function
(
done
)
{
this
.
User
.
find
({
where
:
{
intVal
:
{
lt
:
6
}
}
}).
success
(
function
(
user
)
{
expect
(
user
.
username
).
to
.
equal
(
'boo'
)
expect
(
user
.
intVal
).
to
.
equal
(
5
)
done
()
})
})
it
(
'should have no problem finding a row using lesser and greater than'
,
function
(
done
)
{
this
.
User
.
findAll
({
where
:
{
intVal
:
{
lt
:
6
,
gt
:
4
}
}
}).
success
(
function
(
users
)
{
expect
(
users
[
0
].
username
).
to
.
equal
(
'boo'
)
expect
(
users
[
0
].
intVal
).
to
.
equal
(
5
)
done
()
})
})
it
(
'should be able to find a row using not equal to logic'
,
function
(
done
)
{
this
.
User
.
find
({
where
:
{
intVal
:
{
ne
:
10
}
}
}).
success
(
function
(
user
)
{
expect
(
user
.
username
).
to
.
equal
(
'boo'
)
expect
(
user
.
intVal
).
to
.
equal
(
5
)
done
()
})
})
it
(
'should be able to find multiple users with any of the special where logic properties'
,
function
(
done
)
{
this
.
User
.
findAll
({
where
:
{
intVal
:
{
lte
:
10
}
}
}).
success
(
function
(
users
)
{
expect
(
users
[
0
].
username
).
to
.
equal
(
'boo'
)
expect
(
users
[
0
].
intVal
).
to
.
equal
(
5
)
expect
(
users
[
1
].
username
).
to
.
equal
(
'boo2'
)
expect
(
users
[
1
].
intVal
).
to
.
equal
(
10
)
done
()
})
})
})
describe
(
'eager loading'
,
function
()
{
describe
(
'belongsTo'
,
function
()
{
beforeEach
(
function
(
done
)
{
var
self
=
this
self
.
Task
=
self
.
sequelize
.
define
(
'TaskBelongsTo'
,
{
title
:
Sequelize
.
STRING
})
self
.
Worker
=
self
.
sequelize
.
define
(
'Worker'
,
{
name
:
Sequelize
.
STRING
})
self
.
Task
.
belongsTo
(
self
.
Worker
)
self
.
Worker
.
sync
({
force
:
true
}).
success
(
function
()
{
self
.
Task
.
sync
({
force
:
true
}).
success
(
function
()
{
self
.
Worker
.
create
({
name
:
'worker'
}).
success
(
function
(
worker
)
{
self
.
Task
.
create
({
title
:
'homework'
}).
success
(
function
(
task
)
{
self
.
worker
=
worker
self
.
task
=
task
self
.
task
.
setWorker
(
self
.
worker
).
success
(
function
()
{
done
()
})
})
})
})
})
})
it
(
'throws an error about unexpected input if include contains a non-object'
,
function
(
done
)
{
var
self
=
this
expect
(
function
()
{
self
.
Worker
.
all
({
include
:
[
1
]
})
}).
to
.
throw
(
Error
,
'Include unexpected. Element has to be either an instance of DAOFactory or an object.'
)
done
()
})
it
(
'throws an error about missing attributes if include contains an object with daoFactory'
,
function
(
done
)
{
var
self
=
this
expect
(
function
()
{
self
.
Worker
.
all
({
include
:
[
{
daoFactory
:
self
.
Worker
}
]
})
}).
to
.
throw
(
Error
,
'Include malformed. Expected attributes: daoFactory, as!'
)
done
()
})
it
(
'throws an error if included DaoFactory is not associated'
,
function
(
done
)
{
var
self
=
this
expect
(
function
()
{
self
.
Worker
.
all
({
include
:
[
self
.
Task
]
})
}).
to
.
throw
(
Error
,
'TaskBelongsTo is not associated to Worker!'
)
done
()
})
it
(
'returns the associated worker via task.worker'
,
function
(
done
)
{
this
.
Task
.
all
({
where
:
{
title
:
'homework'
},
include
:
[
this
.
Worker
]
}).
complete
(
function
(
err
,
tasks
)
{
expect
(
err
).
to
.
be
.
null
expect
(
tasks
).
to
.
exist
expect
(
tasks
[
0
].
worker
).
to
.
exist
expect
(
tasks
[
0
].
worker
.
name
).
to
.
equal
(
'worker'
)
done
()
})
})
})
describe
(
'hasOne'
,
function
()
{
beforeEach
(
function
(
done
)
{
var
self
=
this
self
.
Task
=
self
.
sequelize
.
define
(
'TaskHasOne'
,
{
title
:
Sequelize
.
STRING
})
self
.
Worker
=
self
.
sequelize
.
define
(
'Worker'
,
{
name
:
Sequelize
.
STRING
})
self
.
Worker
.
hasOne
(
self
.
Task
)
self
.
Worker
.
sync
({
force
:
true
}).
success
(
function
()
{
self
.
Task
.
sync
({
force
:
true
}).
success
(
function
()
{
self
.
Worker
.
create
({
name
:
'worker'
}).
success
(
function
(
worker
)
{
self
.
Task
.
create
({
title
:
'homework'
}).
success
(
function
(
task
)
{
self
.
worker
=
worker
self
.
task
=
task
self
.
worker
.
setTaskHasOne
(
self
.
task
).
success
(
function
()
{
done
()
})
})
})
})
})
})
it
(
'throws an error if included DaoFactory is not associated'
,
function
(
done
)
{
var
self
=
this
expect
(
function
()
{
self
.
Task
.
all
({
include
:
[
self
.
Worker
]
})
}).
to
.
throw
(
Error
,
'Worker is not associated to TaskHasOne!'
)
done
()
})
it
(
'returns the associated task via worker.task'
,
function
(
done
)
{
this
.
Worker
.
all
({
where
:
{
name
:
'worker'
},
include
:
[
this
.
Task
]
}).
complete
(
function
(
err
,
workers
)
{
expect
(
err
).
to
.
be
.
null
expect
(
workers
).
to
.
exist
expect
(
workers
[
0
].
taskHasOne
).
to
.
exist
expect
(
workers
[
0
].
taskHasOne
.
title
).
to
.
equal
(
'homework'
)
done
()
})
})
})
describe
(
'hasOne with alias'
,
function
()
{
beforeEach
(
function
(
done
)
{
var
self
=
this
self
.
Task
=
self
.
sequelize
.
define
(
'Task'
,
{
title
:
Sequelize
.
STRING
})
self
.
Worker
=
self
.
sequelize
.
define
(
'Worker'
,
{
name
:
Sequelize
.
STRING
})
self
.
Worker
.
hasOne
(
self
.
Task
,
{
as
:
'ToDo'
})
self
.
Worker
.
sync
({
force
:
true
}).
success
(
function
()
{
self
.
Task
.
sync
({
force
:
true
}).
success
(
function
()
{
self
.
Worker
.
create
({
name
:
'worker'
}).
success
(
function
(
worker
)
{
self
.
Task
.
create
({
title
:
'homework'
}).
success
(
function
(
task
)
{
self
.
worker
=
worker
self
.
task
=
task
self
.
worker
.
setToDo
(
self
.
task
).
success
(
function
()
{
done
()
})
})
})
})
})
})
it
(
'throws an error if included DaoFactory is not referenced by alias'
,
function
(
done
)
{
var
self
=
this
expect
(
function
()
{
self
.
Worker
.
all
({
include
:
[
self
.
Task
]
})
}).
to
.
throw
(
Error
,
'Task is not associated to Worker!'
)
done
()
})
it
(
'throws an error if alias is not associated'
,
function
(
done
)
{
var
self
=
this
expect
(
function
()
{
self
.
Worker
.
all
({
include
:
[
{
daoFactory
:
self
.
Task
,
as
:
'Work'
}
]
})
}).
to
.
throw
(
Error
,
'Task (Work) is not associated to Worker!'
)
done
()
})
it
(
'returns the associated task via worker.task'
,
function
(
done
)
{
this
.
Worker
.
all
({
where
:
{
name
:
'worker'
},
include
:
[
{
daoFactory
:
this
.
Task
,
as
:
'ToDo'
}
]
}).
complete
(
function
(
err
,
workers
)
{
expect
(
err
).
to
.
be
.
null
expect
(
workers
).
to
.
exist
expect
(
workers
[
0
].
toDo
).
to
.
exist
expect
(
workers
[
0
].
toDo
.
title
).
to
.
equal
(
'homework'
)
done
()
})
})
it
(
'returns the associated task via worker.task when daoFactory is aliased with model'
,
function
(
done
)
{
this
.
Worker
.
all
({
where
:
{
name
:
'worker'
},
include
:
[
{
model
:
this
.
Task
,
as
:
'ToDo'
}
]
}).
complete
(
function
(
err
,
workers
)
{
expect
(
workers
[
0
].
toDo
.
title
).
to
.
equal
(
'homework'
)
done
()
})
})
})
describe
(
'hasMany'
,
function
()
{
beforeEach
(
function
(
done
)
{
var
self
=
this
self
.
Task
=
self
.
sequelize
.
define
(
'Task'
,
{
title
:
Sequelize
.
STRING
})
self
.
Worker
=
self
.
sequelize
.
define
(
'Worker'
,
{
name
:
Sequelize
.
STRING
})
self
.
Worker
.
hasMany
(
self
.
Task
)
self
.
Worker
.
sync
({
force
:
true
}).
success
(
function
()
{
self
.
Task
.
sync
({
force
:
true
}).
success
(
function
()
{
self
.
Worker
.
create
({
name
:
'worker'
}).
success
(
function
(
worker
)
{
self
.
Task
.
create
({
title
:
'homework'
}).
success
(
function
(
task
)
{
self
.
worker
=
worker
self
.
task
=
task
self
.
worker
.
setTasks
([
self
.
task
]).
success
(
function
()
{
done
()
})
})
})
})
})
})
it
(
'throws an error if included DaoFactory is not associated'
,
function
(
done
)
{
var
self
=
this
expect
(
function
()
{
self
.
Task
.
findAll
({
include
:
[
self
.
Worker
]
})
}).
to
.
throw
(
Error
,
'Worker is not associated to Task!'
)
done
()
})
it
(
'returns the associated tasks via worker.tasks'
,
function
(
done
)
{
this
.
Worker
.
findAll
({
where
:
{
name
:
'worker'
},
include
:
[
this
.
Task
]
}).
complete
(
function
(
err
,
workers
)
{
expect
(
err
).
to
.
be
.
null
expect
(
workers
).
to
.
exist
expect
(
workers
[
0
].
tasks
).
to
.
exist
expect
(
workers
[
0
].
tasks
[
0
].
title
).
to
.
equal
(
'homework'
)
done
()
})
})
})
describe
(
'hasMany with alias'
,
function
()
{
beforeEach
(
function
(
done
)
{
var
self
=
this
self
.
Task
=
self
.
sequelize
.
define
(
'Task'
,
{
title
:
Sequelize
.
STRING
})
self
.
Worker
=
self
.
sequelize
.
define
(
'Worker'
,
{
name
:
Sequelize
.
STRING
})
self
.
Worker
.
hasMany
(
self
.
Task
,
{
as
:
'ToDos'
})
self
.
Worker
.
sync
({
force
:
true
}).
success
(
function
()
{
self
.
Task
.
sync
({
force
:
true
}).
success
(
function
()
{
self
.
Worker
.
create
({
name
:
'worker'
}).
success
(
function
(
worker
)
{
self
.
Task
.
create
({
title
:
'homework'
}).
success
(
function
(
task
)
{
self
.
worker
=
worker
self
.
task
=
task
self
.
worker
.
setToDos
([
self
.
task
]).
success
(
function
()
{
done
()
})
})
})
})
})
})
it
(
'throws an error if included DaoFactory is not referenced by alias'
,
function
(
done
)
{
var
self
=
this
expect
(
function
()
{
self
.
Worker
.
findAll
({
include
:
[
self
.
Task
]
})
}).
to
.
throw
(
Error
,
'Task is not associated to Worker!'
)
done
()
})
it
(
'throws an error if alias is not associated'
,
function
(
done
)
{
var
self
=
this
expect
(
function
()
{
self
.
Worker
.
findAll
({
include
:
[
{
daoFactory
:
self
.
Task
,
as
:
'Work'
}
]
})
}).
to
.
throw
(
Error
,
'Task (Work) is not associated to Worker!'
)
done
()
})
it
(
'returns the associated task via worker.task'
,
function
(
done
)
{
this
.
Worker
.
findAll
({
where
:
{
name
:
'worker'
},
include
:
[
{
daoFactory
:
this
.
Task
,
as
:
'ToDos'
}
]
}).
complete
(
function
(
err
,
workers
)
{
expect
(
err
).
to
.
be
.
null
expect
(
workers
).
to
.
exist
expect
(
workers
[
0
].
toDos
).
to
.
exist
expect
(
workers
[
0
].
toDos
[
0
].
title
).
to
.
equal
(
'homework'
)
done
()
})
})
it
(
'returns the associated task via worker.task when daoFactory is aliased with model'
,
function
(
done
)
{
this
.
Worker
.
findAll
({
where
:
{
name
:
'worker'
},
include
:
[
{
daoFactory
:
this
.
Task
,
as
:
'ToDos'
}
]
}).
complete
(
function
(
err
,
workers
)
{
expect
(
workers
[
0
].
toDos
[
0
].
title
).
to
.
equal
(
'homework'
)
done
()
})
})
})
describe
(
'queryOptions'
,
function
()
{
beforeEach
(
function
(
done
)
{
var
self
=
this
this
.
User
.
create
({
username
:
'barfooz'
}).
success
(
function
(
user
)
{
self
.
user
=
user
done
()
})
})
it
(
"should return a DAO when queryOptions are not set"
,
function
(
done
)
{
var
self
=
this
this
.
User
.
findAll
({
where
:
{
username
:
'barfooz'
}}).
done
(
function
(
err
,
users
)
{
users
.
forEach
(
function
(
user
)
{
expect
(
user
).
to
.
be
.
instanceOf
(
self
.
User
.
DAO
)
})
done
()
})
})
it
(
"should return a DAO when raw is false"
,
function
(
done
)
{
var
self
=
this
this
.
User
.
findAll
({
where
:
{
username
:
'barfooz'
}},
{
raw
:
false
}).
done
(
function
(
err
,
users
)
{
users
.
forEach
(
function
(
user
)
{
expect
(
user
).
to
.
be
.
instanceOf
(
self
.
User
.
DAO
)
})
done
()
})
})
it
(
"should return raw data when raw is true"
,
function
(
done
)
{
var
self
=
this
this
.
User
.
findAll
({
where
:
{
username
:
'barfooz'
}},
{
raw
:
true
}).
done
(
function
(
err
,
users
)
{
users
.
forEach
(
function
(
user
)
{
expect
(
user
).
to
.
not
.
be
.
instanceOf
(
self
.
User
.
DAO
)
expect
(
users
[
0
]).
to
.
be
.
instanceOf
(
Object
)
})
done
()
})
})
})
})
describe
(
'normal findAll'
,
function
()
{
beforeEach
(
function
(
done
)
{
var
self
=
this
this
.
User
.
create
({
username
:
'user'
,
data
:
'foobar'
,
theDate
:
moment
().
toDate
()}).
success
(
function
(
user
)
{
self
.
User
.
create
({
username
:
'user2'
,
data
:
'bar'
,
theDate
:
moment
().
toDate
()}).
success
(
function
(
user2
){
self
.
users
=
[
user
].
concat
(
user2
)
done
()
})
})
})
it
(
"finds all entries"
,
function
(
done
)
{
this
.
User
.
all
().
on
(
'success'
,
function
(
users
)
{
expect
(
users
.
length
).
to
.
equal
(
2
)
done
()
})
})
it
(
'does not modify the passed arguments'
,
function
(
done
)
{
var
options
=
{
where
:
[
'username = ?'
,
'awesome'
]}
this
.
User
.
findAll
(
options
).
success
(
function
(
user
)
{
expect
(
options
).
to
.
deep
.
equal
({
where
:
[
'username = ?'
,
'awesome'
]})
done
()
})
})
it
(
"finds all users matching the passed conditions"
,
function
(
done
)
{
this
.
User
.
findAll
({
where
:
"id != "
+
this
.
users
[
1
].
id
}).
success
(
function
(
users
)
{
expect
(
users
.
length
).
to
.
equal
(
1
)
done
()
})
})
it
(
"can also handle array notation"
,
function
(
done
)
{
var
self
=
this
this
.
User
.
findAll
({
where
:
[
'id = ?'
,
this
.
users
[
1
].
id
]}).
success
(
function
(
users
)
{
expect
(
users
.
length
).
to
.
equal
(
1
)
expect
(
users
[
0
].
id
).
to
.
equal
(
self
.
users
[
1
].
id
)
done
()
})
})
it
(
"sorts the results via id in ascending order"
,
function
(
done
)
{
this
.
User
.
findAll
().
success
(
function
(
users
)
{
expect
(
users
.
length
).
to
.
equal
(
2
);
expect
(
users
[
0
].
id
).
to
.
be
.
below
(
users
[
1
].
id
)
done
()
})
})
it
(
"sorts the results via id in descending order"
,
function
(
done
)
{
this
.
User
.
findAll
({
order
:
"id DESC"
}).
success
(
function
(
users
)
{
expect
(
users
[
0
].
id
).
to
.
be
.
above
(
users
[
1
].
id
)
done
()
})
})
it
(
"sorts the results via a date column"
,
function
(
done
)
{
var
self
=
this
self
.
User
.
create
({
username
:
'user3'
,
data
:
'bar'
,
theDate
:
moment
().
add
(
'hours'
,
2
).
toDate
()}).
success
(
function
(){
self
.
User
.
findAll
({
order
:
[[
'theDate'
,
'DESC'
]]
}).
success
(
function
(
users
)
{
expect
(
users
[
0
].
id
).
to
.
be
.
above
(
users
[
2
].
id
)
done
()
})
})
})
it
(
"handles offset and limit"
,
function
(
done
)
{
var
self
=
this
this
.
User
.
bulkCreate
([{
username
:
'bobby'
},
{
username
:
'tables'
}]).
success
(
function
()
{
self
.
User
.
findAll
({
limit
:
2
,
offset
:
2
}).
success
(
function
(
users
)
{
expect
(
users
.
length
).
to
.
equal
(
2
)
expect
(
users
[
0
].
id
).
to
.
equal
(
3
)
done
()
done
()
})
})
})
})
})
})
it
(
'should allow us to find IDs using capital letters'
,
function
(
done
)
{
var
User
=
this
.
sequelize
.
define
(
'User'
+
config
.
rand
(),
{
ID
:
{
type
:
Sequelize
.
INTEGER
,
primaryKey
:
true
,
autoIncrement
:
true
},
Login
:
{
type
:
Sequelize
.
STRING
}
})
User
.
sync
({
force
:
true
}).
success
(
function
()
{
User
.
create
({
Login
:
'foo'
}).
success
(
function
()
{
User
.
findAll
({
ID
:
1
}).
success
(
function
(
user
)
{
expect
(
user
).
to
.
be
.
instanceof
(
Array
)
expect
(
user
).
to
.
have
.
length
(
1
)
done
()
})
})
})
})
})
})
})
})
...
@@ -4029,4 +1609,4 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
...
@@ -4029,4 +1609,4 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
})
})
})
})
})
})
})
})
\ No newline at end of file
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