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 6290d288
authored
Jul 16, 2013
by
Daniel Durante
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Finished moving DAO factory to Buster.
1 parent
7ea02763
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
310 additions
and
9 deletions
spec/associations/mixin.spec.js
spec/dao-factory.spec.js
spec/associations/mixin.spec.js
View file @
6290d28
...
...
@@ -18,6 +18,16 @@ describe(Helpers.getTestDialectTeaser("Mixin"), function() {
})
})
describe
(
'Mixin'
,
function
()
{
var
DAOFactory
=
require
(
"../../lib/dao-factory"
)
it
(
"adds the mixed-in functions to the dao"
,
function
()
{
expect
(
DAOFactory
.
prototype
.
hasOne
).
toBeDefined
()
expect
(
DAOFactory
.
prototype
.
hasMany
).
toBeDefined
()
expect
(
DAOFactory
.
prototype
.
belongsTo
).
toBeDefined
()
})
})
describe
(
'getAssociation'
,
function
()
{
it
(
'returns the respective part of the association for 1:1 associations'
,
function
()
{
var
User
=
this
.
sequelize
.
define
(
'User'
,
{})
...
...
spec/dao-factory.spec.js
View file @
6290d28
...
...
@@ -3,6 +3,7 @@ if(typeof require === 'function') {
,
Sequelize
=
require
(
"../index"
)
,
Helpers
=
require
(
'./buster-helpers'
)
,
_
=
require
(
'lodash'
)
,
moment
=
require
(
'moment'
)
,
dialect
=
Helpers
.
getTestDialect
()
}
...
...
@@ -742,6 +743,26 @@ describe(Helpers.getTestDialectTeaser("DAOFactory"), function() {
})
// - bulkCreate
describe
(
'update'
,
function
()
{
it
(
'allows sql logging of updated statements'
,
function
()
{
var
User
=
this
.
sequelize
.
define
(
'User'
,
{
name
:
Sequelize
.
STRING
,
bio
:
Sequelize
.
TEXT
},
{
paranoid
:
true
})
this
.
sequelize
.
sync
({
force
:
true
}).
success
(
function
()
{
User
.
create
({
name
:
'meg'
,
bio
:
'none'
}).
success
(
function
(
u
)
{
expect
(
u
).
toBeDefined
()
expect
(
u
).
not
.
toBe
(
null
)
u
.
setAttributes
({
name
:
'brian'
}).
on
(
'sql'
,
function
(
sql
)
{
expect
(
sql
).
toBeDefined
()
expect
(
sql
.
toUpperCase
().
indexOf
(
"UPDATE"
)).
toBeGreaterThan
(
-
1
)
done
()
})
})
})
})
it
(
'updates only values that match filter'
,
function
(
done
)
{
var
self
=
this
...
...
@@ -799,6 +820,45 @@ describe(Helpers.getTestDialectTeaser("DAOFactory"), function() {
})
// - update
describe
(
'destroy'
,
function
()
{
it
(
'deletes a record from the database if dao is not paranoid'
,
function
(
done
)
{
var
User
=
this
.
sequelize
.
define
(
'User'
,
{
name
:
Sequelize
.
STRING
,
bio
:
Sequelize
.
TEXT
})
this
.
sequelize
.
sync
({
force
:
true
}).
success
(
function
()
{
User
.
create
({
name
:
'hallo'
,
bio
:
'welt'
}).
success
(
function
(
u
)
{
User
.
all
().
success
(
function
(
users
)
{
expect
(
users
.
length
).
toEqual
(
1
)
u
.
destroy
().
success
(
function
()
{
User
.
all
().
success
(
function
(
users
)
{
expect
(
users
.
length
).
toEqual
(
0
)
done
()
})
})
})
})
})
})
it
(
'allows sql logging of delete statements'
,
function
(
done
)
{
var
User
=
this
.
sequelize
.
define
(
'User'
,
{
name
:
Sequelize
.
STRING
,
bio
:
Sequelize
.
TEXT
})
this
.
sequelize
.
sync
({
force
:
true
}).
success
(
function
()
{
User
.
create
({
name
:
'hallo'
,
bio
:
'welt'
}).
success
(
function
(
u
)
{
User
.
all
().
success
(
function
(
users
)
{
expect
(
users
.
length
).
toEqual
(
1
)
u
.
destroy
().
on
(
'sql'
,
function
(
sql
)
{
expect
(
sql
).
toBeDefined
()
expect
(
sql
.
toUpperCase
().
indexOf
(
"DELETE"
)).
toBeGreaterThan
(
-
1
)
done
()
})
})
})
})
})
it
(
'deletes values that match filter'
,
function
(
done
)
{
var
self
=
this
...
...
@@ -807,14 +867,11 @@ describe(Helpers.getTestDialectTeaser("DAOFactory"), function() {
{
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
).
toEqual
(
1
)
expect
(
users
[
0
].
username
).
toEqual
(
"Bob"
)
done
()
})
})
...
...
@@ -822,7 +879,6 @@ describe(Helpers.getTestDialectTeaser("DAOFactory"), function() {
})
it
(
'sets deletedAt to the current timestamp if paranoid is true'
,
function
(
done
)
{
var
self
=
this
,
User
=
this
.
sequelize
.
define
(
'ParanoidUser'
,
{
username
:
Sequelize
.
STRING
,
...
...
@@ -837,9 +893,7 @@ describe(Helpers.getTestDialectTeaser("DAOFactory"), function() {
{
username
:
'Bob'
,
secretValue
:
'43'
}]
User
.
sync
({
force
:
true
}).
success
(
function
()
{
User
.
bulkCreate
(
data
).
success
(
function
()
{
User
.
destroy
({
secretValue
:
'42'
})
.
success
(
function
()
{
User
.
findAll
({
order
:
'id'
}).
success
(
function
(
users
)
{
...
...
@@ -856,11 +910,8 @@ describe(Helpers.getTestDialectTeaser("DAOFactory"), function() {
})
})
})
})
})
})
// - destroy
describe
(
'special where conditions'
,
function
()
{
...
...
@@ -1860,8 +1911,248 @@ describe(Helpers.getTestDialectTeaser("DAOFactory"), function() {
})
})
// - describe: queryOptions
})
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
.
findAll
().
on
(
'success'
,
function
(
users
)
{
expect
(
users
.
length
).
toEqual
(
2
)
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
).
toEqual
(
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
).
toEqual
(
1
)
expect
(
users
[
0
].
id
).
toEqual
(
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
).
toEqual
(
2
);
expect
(
users
[
0
].
id
).
toBeLessThan
(
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
).
toBeGreaterThan
(
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
).
toBeGreaterThan
(
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
).
toEqual
(
2
)
expect
(
users
[
0
].
id
).
toEqual
(
3
)
done
()
})
})
})
})
})
//- describe: findAll
describe
(
'findAndCountAll'
,
function
()
{
beforeEach
(
function
(
done
)
{
var
self
=
this
this
.
User
.
bulkCreate
([
{
username
:
'user'
,
data
:
'foobar'
},
{
username
:
'user2'
,
data
:
'bar'
},
{
username
:
'bobby'
,
data
:
'foo'
}
]).
success
(
function
()
{
self
.
User
.
all
().
success
(
function
(
users
){
self
.
users
=
users
done
()
})
})
})
it
(
"handles where clause [only]"
,
function
(
done
)
{
this
.
User
.
findAndCountAll
({
where
:
"id != "
+
this
.
users
[
0
].
id
}).
success
(
function
(
info
)
{
expect
(
info
.
count
).
toEqual
(
2
)
expect
(
Array
.
isArray
(
info
.
rows
)).
toBeTruthy
()
expect
(
info
.
rows
.
length
).
toEqual
(
2
)
done
()
})
})
it
(
"handles where clause with ordering [only]"
,
function
(
done
)
{
this
.
User
.
findAndCountAll
({
where
:
"id != "
+
this
.
users
[
0
].
id
,
order
:
'id ASC'
}).
success
(
function
(
info
)
{
expect
(
info
.
count
).
toEqual
(
2
)
expect
(
Array
.
isArray
(
info
.
rows
)).
toBeTruthy
()
expect
(
info
.
rows
.
length
).
toEqual
(
2
)
done
()
})
})
it
(
"handles offset"
,
function
(
done
)
{
this
.
User
.
findAndCountAll
({
offset
:
1
}).
success
(
function
(
info
)
{
expect
(
info
.
count
).
toEqual
(
3
)
expect
(
Array
.
isArray
(
info
.
rows
)).
toBeTruthy
()
expect
(
info
.
rows
.
length
).
toEqual
(
2
)
done
()
})
})
it
(
"handles limit"
,
function
(
done
)
{
this
.
User
.
findAndCountAll
({
limit
:
1
}).
success
(
function
(
info
)
{
expect
(
info
.
count
).
toEqual
(
3
)
expect
(
Array
.
isArray
(
info
.
rows
)).
toBeTruthy
()
expect
(
info
.
rows
.
length
).
toEqual
(
1
)
done
()
})
})
it
(
"handles offset and limit"
,
function
(
done
)
{
this
.
User
.
findAndCountAll
({
offset
:
1
,
limit
:
1
}).
success
(
function
(
info
)
{
expect
(
info
.
count
).
toEqual
(
3
)
expect
(
Array
.
isArray
(
info
.
rows
)).
toBeTruthy
()
expect
(
info
.
rows
.
length
).
toEqual
(
1
)
done
()
})
})
})
describe
(
'all'
,
function
()
{
beforeEach
(
function
(
done
)
{
this
.
User
.
bulkCreate
([
{
username
:
'user'
,
data
:
'foobar'
},
{
username
:
'user2'
,
data
:
'bar'
}
]).
complete
(
done
)
})
it
(
"should return all users"
,
function
(
done
)
{
this
.
User
.
all
().
on
(
'success'
,
function
(
users
)
{
expect
(
users
.
length
).
toEqual
(
2
)
done
()
})
})
})
describe
(
'equals'
,
function
()
{
it
(
"correctly determines equality of objects"
,
function
(
done
)
{
this
.
User
.
create
({
username
:
'hallo'
,
data
:
'welt'
}).
success
(
function
(
u
)
{
expect
(
u
.
equals
(
u
)).
toBeTruthy
()
done
()
})
})
// sqlite can't handle multiple primary keys
if
(
dialect
!==
"sqlite"
)
{
it
(
"correctly determines equality with multiple primary keys"
,
function
(
done
)
{
var
userKeys
=
this
.
sequelize
.
define
(
'userkeys'
,
{
foo
:
{
type
:
Sequelize
.
STRING
,
primaryKey
:
true
},
bar
:
{
type
:
Sequelize
.
STRING
,
primaryKey
:
true
},
name
:
Sequelize
.
STRING
,
bio
:
Sequelize
.
TEXT
})
this
.
sequelize
.
sync
({
force
:
true
}).
success
(
function
()
{
userKeys
.
create
({
foo
:
'1'
,
bar
:
'2'
,
name
:
'hallo'
,
bio
:
'welt'
}).
success
(
function
(
u
)
{
expect
(
u
.
equals
(
u
)).
toBeTruthy
()
done
()
})
})
})
}
})
describe
(
'equalsOneOf'
,
function
()
{
// sqlite can't handle multiple primary keys
if
(
dialect
!==
"sqlite"
)
{
beforeEach
(
function
(
done
)
{
this
.
userKey
=
this
.
sequelize
.
define
(
'userKeys'
,
{
foo
:
{
type
:
Sequelize
.
STRING
,
primaryKey
:
true
},
bar
:
{
type
:
Sequelize
.
STRING
,
primaryKey
:
true
},
name
:
Sequelize
.
STRING
,
bio
:
Sequelize
.
TEXT
})
this
.
sequelize
.
sync
({
force
:
true
}).
success
(
done
)
})
it
(
'determines equality if one is matching'
,
function
(
done
)
{
this
.
userKey
.
create
({
foo
:
'1'
,
bar
:
'2'
,
name
:
'hallo'
,
bio
:
'welt'
}).
success
(
function
(
u
)
{
expect
(
u
.
equalsOneOf
([
u
,
{
a
:
1
}])).
toBeTruthy
()
done
()
})
})
it
(
"doesn't determine equality if none is matching"
,
function
(
done
)
{
this
.
userKey
.
create
({
foo
:
'1'
,
bar
:
'2'
,
name
:
'hallo'
,
bio
:
'welt'
}).
success
(
function
(
u
)
{
expect
(
u
.
equalsOneOf
([{
b
:
2
},
{
a
:
1
}])).
toBeFalsy
()
done
()
})
})
}
})
describe
(
'count'
,
function
()
{
it
(
'counts all created objects'
,
function
(
done
)
{
var
self
=
this
this
.
User
.
create
({
username
:
'user1'
}).
success
(
function
()
{
self
.
User
.
create
({
username
:
'user2'
}).
success
(
function
()
{
self
.
User
.
count
().
success
(
function
(
count
)
{
expect
(
count
).
toEqual
(
2
)
done
()
})
})
})
})
it
(
'allows sql logging'
,
function
(
done
)
{
this
.
User
.
count
().
on
(
'sql'
,
function
(
sql
)
{
expect
(
sql
).
toBeDefined
()
expect
(
sql
.
toUpperCase
().
indexOf
(
"SELECT"
)).
toBeGreaterThan
(
-
1
)
done
()
})
})
it
(
'filters object'
,
function
(
done
)
{
var
self
=
this
this
.
User
.
create
({
username
:
'user1'
}).
success
(
function
()
{
self
.
User
.
create
({
username
:
'foo'
}).
success
(
function
()
{
self
.
User
.
count
({
where
:
"username LIKE '%us%'"
}).
success
(
function
(
count
)
{
expect
(
count
).
toEqual
(
1
)
done
()
})
})
})
})
})
describe
(
'min'
,
function
()
{
before
(
function
(
done
)
{
this
.
UserWithAge
=
this
.
sequelize
.
define
(
'UserWithAge'
,
{
...
...
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