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 c6e964cb
authored
Dec 18, 2011
by
sdepold
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
moved specs
1 parent
eeb147a8
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
108 additions
and
252 deletions
spec/associations/has-many.spec.js
test/Model/has-many.js
spec/associations/has-many.spec.js
View file @
c6e964c
...
@@ -60,6 +60,8 @@ describe('BelongsTo', function() {
...
@@ -60,6 +60,8 @@ describe('BelongsTo', function() {
})
})
it
(
"sets and gets associated objects"
,
function
()
{
it
(
"sets and gets associated objects"
,
function
()
{
var
user
,
task1
,
task2
;
User
.
hasMany
(
Task
,
{
as
:
'Tasks'
})
User
.
hasMany
(
Task
,
{
as
:
'Tasks'
})
Helpers
.
async
(
function
(
done
)
{
Helpers
.
async
(
function
(
done
)
{
...
@@ -69,21 +71,26 @@ describe('BelongsTo', function() {
...
@@ -69,21 +71,26 @@ describe('BelongsTo', function() {
})
})
Helpers
.
async
(
function
(
done
)
{
Helpers
.
async
(
function
(
done
)
{
User
.
create
({
username
:
'name'
}).
success
(
function
(
user
)
{
User
.
create
({
username
:
'name'
}).
success
(
function
(
_user
)
{
Task
.
create
({
title
:
'task1'
}).
success
(
function
(
task1
)
{
Task
.
create
({
title
:
'task1'
}).
success
(
function
(
_task1
)
{
Task
.
create
({
title
:
'task2'
}).
success
(
function
(
task2
)
{
Task
.
create
({
title
:
'task2'
}).
success
(
function
(
_task2
)
{
user
=
_user
user
.
setTasks
([
task1
,
task2
]).
success
(
function
()
{
task1
=
_task1
user
.
getTasks
().
success
(
function
(
tasks
)
{
task2
=
_task2
expect
(
tasks
.
length
).
toEqual
(
2
)
done
()
done
()
})
})
})
})
})
})
})
})
})
})
Helpers
.
async
(
function
(
done
)
{
user
.
setTasks
([
task1
,
task2
]).
success
(
function
()
{
user
.
getTasks
().
success
(
function
(
tasks
)
{
expect
(
tasks
.
length
).
toEqual
(
2
)
done
()
})
})
})
})
})
})
})
...
@@ -164,5 +171,95 @@ describe('BelongsTo', function() {
...
@@ -164,5 +171,95 @@ describe('BelongsTo', function() {
expect
(
t
.
setUsers
).
toBeDefined
()
expect
(
t
.
setUsers
).
toBeDefined
()
expect
(
t
.
getUsers
).
toBeDefined
()
expect
(
t
.
getUsers
).
toBeDefined
()
})
})
it
(
"sets and gets the corrected associated objects"
,
function
()
{
var
users
=
[]
,
tasks
=
[]
User
.
hasMany
(
Task
,
{
as
:
'Tasks'
})
Task
.
hasMany
(
User
,
{
as
:
'Users'
})
Helpers
.
async
(
function
(
done
)
{
User
.
sync
({
force
:
true
}).
success
(
function
()
{
Task
.
sync
({
force
:
true
}).
success
(
done
)
})
})
Helpers
.
async
(
function
(
done
)
{
User
.
create
({
username
:
'name'
}).
success
(
function
(
user1
)
{
User
.
create
({
username
:
'name2'
}).
success
(
function
(
user2
)
{
Task
.
create
({
title
:
'task1'
}).
success
(
function
(
task1
)
{
Task
.
create
({
title
:
'task2'
}).
success
(
function
(
task2
)
{
users
.
push
(
user1
)
users
.
push
(
user2
)
tasks
.
push
(
task1
)
tasks
.
push
(
task2
)
done
()
})
})
})
})
})
Helpers
.
async
(
function
(
done
)
{
users
[
0
].
setTasks
(
tasks
).
success
(
function
()
{
users
[
0
].
getTasks
().
success
(
function
(
_tasks
)
{
expect
(
_tasks
.
length
).
toEqual
(
2
)
tasks
[
1
].
setUsers
(
users
).
success
(
function
()
{
tasks
[
1
].
getUsers
().
success
(
function
(
_users
)
{
expect
(
users
.
length
).
toEqual
(
2
)
done
()
})
})
})
})
})
})
})
it
(
"build the connector models name"
,
function
()
{
Helpers
.
async
(
function
(
done
)
{
var
Person
=
sequelize
.
define
(
'Person'
,
{
name
:
Sequelize
.
STRING
})
Person
.
hasMany
(
Person
,
{
as
:
'Children'
})
Person
.
hasMany
(
Person
,
{
as
:
'Friends'
})
Person
.
hasMany
(
Person
,
{
as
:
'CoWorkers'
})
Person
.
sync
({
force
:
true
}).
success
(
function
()
{
var
modelNames
=
sequelize
.
modelManager
.
models
.
map
(
function
(
model
)
{
return
model
.
tableName
})
,
expectation
=
[
"Persons"
,
"ChildrenPersons"
,
"CoWorkersPersons"
,
"FriendsPersons"
]
expectation
.
forEach
(
function
(
ex
)
{
expect
(
modelNames
.
indexOf
(
ex
)
>
-
1
).
toBeTruthy
()
})
done
()
})
})
})
it
(
"gets and sets the connector models"
,
function
()
{
Helpers
.
async
(
function
(
done
)
{
var
Person
=
sequelize
.
define
(
'Person'
,
{
name
:
Sequelize
.
STRING
})
Person
.
hasMany
(
Person
,
{
as
:
'Children'
})
Person
.
hasMany
(
Person
,
{
as
:
'Friends'
})
Person
.
hasMany
(
Person
,
{
as
:
'CoWorkers'
})
Person
.
sync
({
force
:
true
}).
success
(
function
()
{
Person
.
create
({
name
:
'foobar'
}).
success
(
function
(
person
)
{
Person
.
create
({
name
:
'friend'
}).
success
(
function
(
friend
)
{
person
.
setFriends
([
friend
]).
success
(
function
()
{
person
.
getFriends
().
success
(
function
(
friends
)
{
expect
(
friends
.
length
).
toEqual
(
1
)
expect
(
friends
[
0
].
name
).
toEqual
(
'friend'
)
done
()
})
})
})
})
})
})
})
})
})
})
test/Model/has-many.js
deleted
100644 → 0
View file @
eeb147a
var
assert
=
require
(
"assert"
)
,
config
=
require
(
"./../config"
)
,
Sequelize
=
require
(
"./../../index"
)
,
sequelize
=
new
Sequelize
(
config
.
database
,
config
.
username
,
config
.
password
,
{
logging
:
false
,
define
:
{
charset
:
'latin1'
}})
module
.
exports
=
{
'it should correctly add the foreign id - monodirectional'
:
function
()
{
var
num
=
config
.
rand
()
var
User
=
sequelize
.
define
(
'User'
+
num
,
{
username
:
Sequelize
.
STRING
})
var
Task
=
sequelize
.
define
(
'Task'
+
num
,
{
title
:
Sequelize
.
STRING
})
User
.
hasMany
(
Task
)
assert
.
eql
(
Task
.
attributes
[
'User'
+
num
+
'Id'
],
"INT"
)
},
'it should correctly add the foreign ids - bidirectional'
:
function
(
exit
)
{
var
num
=
config
.
rand
()
var
User
=
sequelize
.
define
(
'User'
+
num
,
{
username
:
Sequelize
.
STRING
})
var
Task
=
sequelize
.
define
(
'Task'
+
num
,
{
title
:
Sequelize
.
STRING
})
Task
.
hasMany
(
User
)
User
.
hasMany
(
Task
)
assert
.
isUndefined
(
Task
.
attributes
[
'User'
+
num
+
'Id'
])
assert
.
isUndefined
(
User
.
attributes
[
'User'
+
num
+
'Id'
])
sequelize
.
modelManager
.
models
.
forEach
(
function
(
model
)
{
if
(
model
.
tableName
==
(
Task
.
tableName
+
User
.
tableName
))
{
assert
.
isDefined
(
model
.
attributes
[
'User'
+
num
+
'Id'
])
assert
.
isDefined
(
model
.
attributes
[
'Task'
+
num
+
'Id'
])
exit
(
function
(){})
}
})
},
'it should correctly add the foreign id with underscore - monodirectional'
:
function
()
{
var
num
=
config
.
rand
()
var
User
=
sequelize
.
define
(
'User'
+
num
,
{
username
:
Sequelize
.
STRING
})
var
Task
=
sequelize
.
define
(
'Task'
+
num
,
{
title
:
Sequelize
.
STRING
},
{
underscored
:
true
})
Task
.
hasMany
(
User
)
assert
.
isDefined
(
User
.
attributes
[
'task'
+
num
+
'_id'
])
},
'it should correctly add the foreign id with underscore - bidirectional'
:
function
()
{
var
num
=
config
.
rand
()
var
User
=
sequelize
.
define
(
'User'
+
num
,
{
username
:
Sequelize
.
STRING
},
{
underscored
:
true
})
var
Task
=
sequelize
.
define
(
'Task'
+
num
,
{
title
:
Sequelize
.
STRING
})
Task
.
hasMany
(
User
)
User
.
hasMany
(
Task
)
assert
.
isUndefined
(
Task
.
attributes
[
'user'
+
num
+
'_id'
])
assert
.
isUndefined
(
User
.
attributes
[
'user'
+
num
+
'_id'
])
sequelize
.
modelManager
.
models
.
forEach
(
function
(
model
)
{
if
(
model
.
tableName
==
(
Task
.
tableName
+
User
.
tableName
))
{
assert
.
isDefined
(
model
.
attributes
[
'user'
+
num
+
'_id'
])
assert
.
isDefined
(
model
.
attributes
[
'Task'
+
num
+
'Id'
])
}
})
},
'it should correctly add the foreign id when defining the foreignkey as option - monodirectional'
:
function
()
{
var
num
=
config
.
rand
()
var
User
=
sequelize
.
define
(
'User'
+
num
,
{
username
:
Sequelize
.
STRING
},
{
underscored
:
true
})
var
Task
=
sequelize
.
define
(
'Task'
+
num
,
{
title
:
Sequelize
.
STRING
})
User
.
hasMany
(
Task
,
{
foreignKey
:
'person_id'
})
assert
.
eql
(
Task
.
attributes
.
person_id
,
"INT"
)
},
'it should correctly add the foreign id when defining the foreignkey as option - bidirectional'
:
function
()
{
var
num
=
config
.
rand
()
var
User
=
sequelize
.
define
(
'User'
+
num
,
{
username
:
Sequelize
.
STRING
},
{
underscored
:
true
})
var
Task
=
sequelize
.
define
(
'Task'
+
num
,
{
title
:
Sequelize
.
STRING
})
User
.
hasMany
(
Task
,
{
foreignKey
:
'person_id'
})
Task
.
hasMany
(
User
,
{
foreignKey
:
'work_item_id'
})
sequelize
.
modelManager
.
models
.
forEach
(
function
(
model
)
{
if
(
model
.
tableName
==
(
Task
.
tableName
+
User
.
tableName
))
{
assert
.
isDefined
(
model
.
attributes
.
person_id
)
assert
.
isDefined
(
model
.
attributes
.
work_item_id
)
}
})
},
'it should define getter and setter - monodirectional'
:
function
()
{
var
num
=
config
.
rand
()
var
User
=
sequelize
.
define
(
'User'
+
num
,
{
username
:
Sequelize
.
STRING
})
var
Task
=
sequelize
.
define
(
'Task'
+
num
,
{
title
:
Sequelize
.
STRING
})
User
.
hasMany
(
Task
)
var
u
=
User
.
build
({
username
:
'asd'
})
assert
.
isDefined
(
u
[
'setTask'
+
num
+
"s"
])
assert
.
isDefined
(
u
[
'getTask'
+
num
+
"s"
])
},
'it should define getter and setter - bidirectional'
:
function
()
{
var
num
=
config
.
rand
()
var
User
=
sequelize
.
define
(
'User'
+
num
,
{
username
:
Sequelize
.
STRING
})
var
Task
=
sequelize
.
define
(
'Task'
+
num
,
{
title
:
Sequelize
.
STRING
})
User
.
hasMany
(
Task
)
Task
.
hasMany
(
User
)
var
u
=
User
.
build
({
username
:
'asd'
})
assert
.
isDefined
(
u
[
'setTask'
+
num
+
"s"
])
assert
.
isDefined
(
u
[
'getTask'
+
num
+
"s"
])
var
t
=
Task
.
build
({
title
:
'foobar'
})
assert
.
isDefined
(
t
[
'setUser'
+
num
+
's'
])
assert
.
isDefined
(
t
[
'getUser'
+
num
+
's'
])
},
'it should define getter and setter according to as option - monodirectional'
:
function
()
{
var
num
=
config
.
rand
()
var
User
=
sequelize
.
define
(
'User'
+
num
,
{
username
:
Sequelize
.
STRING
})
var
Task
=
sequelize
.
define
(
'Task'
+
num
,
{
title
:
Sequelize
.
STRING
})
User
.
hasMany
(
Task
,
{
as
:
'Tasks'
})
var
u
=
User
.
build
({
username
:
'asd'
})
assert
.
isDefined
(
u
.
setTasks
)
assert
.
isDefined
(
u
.
getTasks
)
},
'it should define getter and setter according to as option - bidirectional'
:
function
()
{
var
num
=
config
.
rand
()
var
User
=
sequelize
.
define
(
'User'
+
num
,
{
username
:
Sequelize
.
STRING
})
var
Task
=
sequelize
.
define
(
'Task'
+
num
,
{
title
:
Sequelize
.
STRING
})
User
.
hasMany
(
Task
,
{
as
:
'Tasks'
})
Task
.
hasMany
(
User
,
{
as
:
'Users'
})
var
u
=
User
.
build
({
username
:
'asd'
})
assert
.
isDefined
(
u
.
setTasks
)
assert
.
isDefined
(
u
.
getTasks
)
var
t
=
Task
.
build
({
title
:
'asd'
})
assert
.
isDefined
(
t
.
setUsers
)
assert
.
isDefined
(
t
.
getUsers
)
},
'it should set and get the correct objects - monodirectional'
:
function
(
exit
)
{
var
User
=
sequelize
.
define
(
'User'
+
config
.
rand
(),
{
username
:
Sequelize
.
STRING
})
var
Task
=
sequelize
.
define
(
'Task'
+
config
.
rand
(),
{
title
:
Sequelize
.
STRING
})
User
.
hasMany
(
Task
,
{
as
:
'Tasks'
})
User
.
sync
({
force
:
true
}).
on
(
'success'
,
function
()
{
Task
.
sync
({
force
:
true
}).
on
(
'success'
,
function
()
{
User
.
create
({
username
:
'name'
}).
on
(
'success'
,
function
(
user
)
{
Task
.
create
({
title
:
'task1'
}).
on
(
'success'
,
function
(
task1
)
{
Task
.
create
({
title
:
'task2'
}).
on
(
'success'
,
function
(
task2
)
{
user
.
setTasks
([
task1
,
task2
]).
on
(
'success'
,
function
()
{
user
.
getTasks
().
on
(
'success'
,
function
(
tasks
)
{
assert
.
eql
(
tasks
.
length
,
2
)
exit
(
function
(){})
})
})
})
})
})
})
})
},
'it should set and get the correct objects - bidirectional'
:
function
(
exit
)
{
var
User
=
sequelize
.
define
(
'User'
+
config
.
rand
(),
{
username
:
Sequelize
.
STRING
})
var
Task
=
sequelize
.
define
(
'Task'
+
config
.
rand
(),
{
title
:
Sequelize
.
STRING
})
User
.
hasMany
(
Task
,
{
as
:
'Tasks'
})
Task
.
hasMany
(
User
,
{
as
:
'Users'
})
User
.
sync
({
force
:
true
}).
on
(
'success'
,
function
()
{
Task
.
sync
({
force
:
true
}).
on
(
'success'
,
function
()
{
User
.
create
({
username
:
'name'
}).
on
(
'success'
,
function
(
user1
)
{
User
.
create
({
username
:
'name2'
}).
on
(
'success'
,
function
(
user2
)
{
Task
.
create
({
title
:
'task1'
}).
on
(
'success'
,
function
(
task1
)
{
Task
.
create
({
title
:
'task2'
}).
on
(
'success'
,
function
(
task2
)
{
user1
.
setTasks
([
task1
,
task2
]).
on
(
'success'
,
function
()
{
user1
.
getTasks
().
on
(
'success'
,
function
(
tasks
)
{
assert
.
eql
(
tasks
.
length
,
2
)
task2
.
setUsers
([
user1
,
user2
]).
on
(
'success'
,
function
()
{
task2
.
getUsers
().
on
(
'success'
,
function
(
users
)
{
assert
.
eql
(
users
.
length
,
2
)
exit
(
function
(){})
})
})
})
})
})
})
})
})
})
})
},
'it should correctly build the connector model names'
:
function
(
exit
){
var
num
=
config
.
rand
()
,
Person
=
sequelize
.
define
(
'Person'
+
num
,
{
name
:
Sequelize
.
STRING
})
Person
.
hasMany
(
Person
,
{
as
:
'Children'
})
Person
.
hasMany
(
Person
,
{
as
:
'Friends'
})
Person
.
hasMany
(
Person
,
{
as
:
'CoWorkers'
})
Person
.
sync
({
force
:
true
}).
on
(
'success'
,
function
()
{
var
modelNames
=
sequelize
.
modelManager
.
models
.
map
(
function
(
model
)
{
return
model
.
tableName
})
,
expectation
=
[
"Person"
+
num
+
"s"
,
"ChildrenPerson"
+
num
+
"s"
,
"CoWorkersPerson"
+
num
+
"s"
,
"FriendsPerson"
+
num
+
"s"
]
expectation
.
forEach
(
function
(
ex
)
{
assert
.
eql
(
modelNames
.
indexOf
(
ex
)
>
-
1
,
true
)
})
exit
(
function
(){})
})
},
'it should correctly get and set the connected models'
:
function
(
exit
)
{
var
num
=
config
.
rand
()
,
Person
=
sequelize
.
define
(
'Person'
+
num
,
{
name
:
Sequelize
.
STRING
})
Person
.
hasMany
(
Person
,
{
as
:
'Children'
})
Person
.
hasMany
(
Person
,
{
as
:
'Friends'
})
Person
.
hasMany
(
Person
,
{
as
:
'CoWorkers'
})
Person
.
sync
({
force
:
true
}).
on
(
'success'
,
function
()
{
Person
.
create
({
name
:
'foobar'
}).
on
(
'success'
,
function
(
person
)
{
Person
.
create
({
name
:
'friend'
}).
on
(
'success'
,
function
(
friend
)
{
person
.
setFriends
([
friend
]).
on
(
'success'
,
function
()
{
person
.
getFriends
().
on
(
'success'
,
function
(
friends
)
{
assert
.
eql
(
friends
.
length
,
1
)
assert
.
eql
(
friends
[
0
].
name
,
'friend'
)
exit
(
function
(){})
})
})
})
})
})
}
}
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