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 e713fba7
authored
Jul 16, 2013
by
Daniel Durante
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Converted all of Jasmine associations to Buster.
1 parent
34b680d8
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
608 additions
and
714 deletions
spec-jasmine/associations/belongs-to.spec.js
spec-jasmine/associations/has-many.spec.js
spec-jasmine/associations/has-one.spec.js
spec/associations/belongs-to.spec.js
spec/associations/has-many.spec.js
spec/associations/has-one.spec.js
spec-jasmine/associations/belongs-to.spec.js
deleted
100644 → 0
View file @
34b680d
var
config
=
require
(
"../config/config"
)
,
Sequelize
=
require
(
"../../index"
)
,
sequelize
=
new
Sequelize
(
config
.
mysql
.
database
,
config
.
mysql
.
username
,
config
.
mysql
.
password
,
{
pool
:
config
.
mysql
.
pool
,
logging
:
false
,
host
:
config
.
mysql
.
host
,
port
:
config
.
mysql
.
port
})
,
Helpers
=
new
(
require
(
"../config/helpers"
))(
sequelize
)
describe
(
'BelongsTo'
,
function
()
{
var
User
=
null
,
Task
=
null
var
setup
=
function
()
{
User
=
sequelize
.
define
(
'User'
,
{
username
:
Sequelize
.
STRING
,
enabled
:
{
type
:
Sequelize
.
BOOLEAN
,
defaultValue
:
true
}})
Task
=
sequelize
.
define
(
'Task'
,
{
title
:
Sequelize
.
STRING
})
}
beforeEach
(
function
()
{
Helpers
.
dropAllTables
();
setup
()
})
afterEach
(
function
()
{
Helpers
.
dropAllTables
()
})
it
(
'adds the foreign key'
,
function
()
{
Task
.
belongsTo
(
User
)
expect
(
Task
.
attributes
[
'UserId'
]).
toEqual
(
"INTEGER"
)
})
it
(
"underscores the foreign key"
,
function
()
{
Task
=
sequelize
.
define
(
'Task'
,
{
title
:
Sequelize
.
STRING
},
{
underscored
:
true
})
Task
.
belongsTo
(
User
)
expect
(
Task
.
attributes
[
'user_id'
]).
toEqual
(
"INTEGER"
)
})
it
(
"uses the passed foreign key"
,
function
()
{
Task
.
belongsTo
(
User
,
{
foreignKey
:
'person_id'
})
expect
(
Task
.
attributes
[
'person_id'
]).
toEqual
(
"INTEGER"
)
})
it
(
"defines getters and setters"
,
function
()
{
Task
.
belongsTo
(
User
)
var
task
=
Task
.
build
({
title
:
'asd'
})
expect
(
task
.
setUser
).
toBeDefined
()
expect
(
task
.
getUser
).
toBeDefined
()
})
it
(
"aliases the getters and setters according to the passed 'as' option"
,
function
()
{
Task
.
belongsTo
(
User
,
{
as
:
'Person'
})
var
task
=
Task
.
build
({
title
:
'asd'
})
expect
(
task
.
setPerson
).
toBeDefined
()
expect
(
task
.
getPerson
).
toBeDefined
()
})
it
(
"aliases associations to the same table according to the passed 'as' option"
,
function
()
{
Task
.
belongsTo
(
User
,
{
as
:
'Poster'
})
Task
.
belongsTo
(
User
,
{
as
:
'Owner'
})
var
task
=
Task
.
build
({
title
:
'asd'
})
expect
(
task
.
getPoster
).
toBeDefined
()
expect
(
task
.
setPoster
).
toBeDefined
()
expect
(
task
.
getOwner
).
toBeDefined
()
expect
(
task
.
setOwner
).
toBeDefined
()
})
it
(
"intializes the foreign key with null"
,
function
()
{
Task
.
belongsTo
(
User
)
var
task
=
Task
.
build
({
title
:
'asd'
})
expect
(
task
[
'UserId'
]).
not
.
toBeDefined
();
})
it
(
"sets and gets the correct objects"
,
function
()
{
Task
.
belongsTo
(
User
,
{
as
:
'User'
})
Helpers
.
async
(
function
(
done
)
{
User
.
sync
({
force
:
true
}).
success
(
function
()
{
Task
.
sync
({
force
:
true
}).
success
(
done
)
})
})
Helpers
.
async
(
function
(
done
)
{
User
.
create
({
username
:
'asd'
}).
success
(
function
(
u
)
{
Task
.
create
({
title
:
'a task'
}).
success
(
function
(
t
)
{
t
.
setUser
(
u
).
success
(
function
()
{
t
.
getUser
().
success
(
function
(
user
)
{
expect
(
user
.
username
).
toEqual
(
'asd'
)
done
()
})
})
})
})
})
})
it
(
'extends the id where param with the supplied where params'
,
function
()
{
Task
.
belongsTo
(
User
,
{
as
:
'User'
})
Helpers
.
async
(
function
(
done
)
{
User
.
sync
({
force
:
true
}).
success
(
function
()
{
Task
.
sync
({
force
:
true
}).
success
(
done
)
})
})
Helpers
.
async
(
function
(
done
)
{
User
.
create
({
username
:
'asd'
,
enabled
:
false
}).
success
(
function
(
u
)
{
Task
.
create
({
title
:
'a task'
}).
success
(
function
(
t
)
{
t
.
setUser
(
u
).
success
(
function
()
{
t
.
getUser
({
where
:
{
enabled
:
true
}}).
success
(
function
(
user
)
{
expect
(
user
).
toEqual
(
null
)
done
()
})
})
})
})
})
})
it
(
"handles self associations"
,
function
()
{
Helpers
.
async
(
function
(
done
)
{
var
Person
=
sequelize
.
define
(
'Person'
,
{
name
:
Sequelize
.
STRING
})
Person
.
belongsTo
(
Person
,
{
as
:
'Mother'
,
foreignKey
:
'MotherId'
})
Person
.
belongsTo
(
Person
,
{
as
:
'Father'
,
foreignKey
:
'FatherId'
})
Person
.
sync
({
force
:
true
}).
success
(
function
()
{
var
p
=
Person
.
build
()
expect
(
p
.
setFather
).
toBeDefined
()
expect
(
p
.
setMother
).
toBeDefined
()
done
()
})
})
})
it
(
"sets the foreign key in self associations"
,
function
()
{
var
Person
=
sequelize
.
define
(
'Person'
,
{
name
:
Sequelize
.
STRING
})
Person
.
belongsTo
(
Person
,
{
as
:
'Mother'
})
expect
(
Person
.
associations
.
MotherPersons
.
options
.
foreignKey
).
toEqual
(
'MotherId'
)
})
})
spec-jasmine/associations/has-many.spec.js
deleted
100644 → 0
View file @
34b680d
var
config
=
require
(
"../config/config"
)
,
Sequelize
=
require
(
"../../index"
)
,
sequelize
=
new
Sequelize
(
config
.
mysql
.
database
,
config
.
mysql
.
username
,
config
.
mysql
.
password
,
{
pool
:
config
.
mysql
.
pool
,
logging
:
false
,
host
:
config
.
mysql
.
host
,
port
:
config
.
mysql
.
port
})
,
Helpers
=
new
(
require
(
"../config/helpers"
))(
sequelize
)
describe
(
'HasMany'
,
function
()
{
var
User
=
null
,
Task
=
null
,
sequelize
=
null
,
Helpers
=
null
var
setup
=
function
()
{
sequelize
=
new
Sequelize
(
config
.
mysql
.
database
,
config
.
mysql
.
username
,
config
.
mysql
.
password
,
{
pool
:
config
.
mysql
.
pool
,
logging
:
false
,
host
:
config
.
mysql
.
host
,
port
:
config
.
mysql
.
port
})
Helpers
=
new
(
require
(
"../config/helpers"
))(
sequelize
)
Helpers
.
dropAllTables
()
User
=
sequelize
.
define
(
'User'
,
{
username
:
Sequelize
.
STRING
})
Task
=
sequelize
.
define
(
'Task'
,
{
title
:
Sequelize
.
STRING
})
}
beforeEach
(
function
()
{
setup
()
})
afterEach
(
function
()
{
Helpers
.
dropAllTables
()
})
describe
(
'mono-directional'
,
function
()
{
it
(
"adds the foreign key"
,
function
()
{
User
.
hasMany
(
Task
)
expect
(
Task
.
attributes
.
UserId
).
toEqual
(
"INTEGER"
)
})
it
(
'adds the foreign key with underscore'
,
function
()
{
User
=
sequelize
.
define
(
'User'
,
{
username
:
Sequelize
.
STRING
})
Task
=
sequelize
.
define
(
'Task'
,
{
title
:
Sequelize
.
STRING
},
{
underscored
:
true
})
Task
.
hasMany
(
User
)
expect
(
User
.
attributes
.
task_id
).
toBeDefined
()
})
it
(
'uses the passed foreign key'
,
function
()
{
User
.
hasMany
(
Task
,
{
foreignKey
:
'person_id'
})
expect
(
Task
.
attributes
.
person_id
).
toEqual
(
"INTEGER"
)
})
it
(
'defines getters and setters'
,
function
()
{
User
.
hasMany
(
Task
)
var
u
=
User
.
build
({
username
:
'asd'
})
expect
(
u
.
setTasks
).
toBeDefined
()
expect
(
u
.
getTasks
).
toBeDefined
()
})
it
(
"defines getters and setters according to the 'as' option"
,
function
()
{
User
.
hasMany
(
Task
,
{
as
:
'Tasks'
})
var
u
=
User
.
build
({
username
:
'asd'
})
expect
(
u
.
setTasks
).
toBeDefined
()
expect
(
u
.
getTasks
).
toBeDefined
()
})
it
(
"sets and gets associated objects"
,
function
()
{
var
user
,
task1
,
task2
;
User
.
hasMany
(
Task
,
{
as
:
'Tasks'
})
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
(
_user
)
{
Task
.
create
({
title
:
'task1'
}).
success
(
function
(
_task1
)
{
Task
.
create
({
title
:
'task2'
}).
success
(
function
(
_task2
)
{
user
=
_user
task1
=
_task1
task2
=
_task2
done
()
})
})
})
})
Helpers
.
async
(
function
(
done
)
{
user
.
setTasks
([
task1
,
task2
]).
success
(
function
()
{
user
.
getTasks
().
success
(
function
(
tasks
)
{
expect
(
tasks
.
length
).
toEqual
(
2
)
user
.
getTasks
({
attributes
:
[
'title'
]}).
success
(
function
(
tasks
)
{
expect
(
tasks
[
0
].
selectedValues
.
title
).
toEqual
(
'task1'
)
expect
(
tasks
[
0
].
selectedValues
.
id
).
toEqual
(
null
)
done
()
})
})
})
})
})
it
(
"should allow selfAssociation to be single linked (only one DAO is created)"
,
function
()
{
var
oldLength
=
sequelize
.
daoFactoryManager
.
daos
.
length
;
var
Comment
=
sequelize
.
define
(
'Comment'
,
{
content
:
Sequelize
.
STRING
})
Comment
.
belongsTo
(
Comment
,
{
as
:
"Parent"
});
Comment
.
hasMany
(
Comment
,
{
as
:
'Children'
,
foreignKey
:
"ParentId"
,
useJunctionTable
:
false
})
expect
(
sequelize
.
daoFactoryManager
.
daos
.
length
).
toEqual
(
oldLength
+
1
)
Helpers
.
async
(
function
(
done
)
{
Comment
.
sync
({
force
:
true
}).
success
(
function
()
{
done
()
})
})
var
parent
Helpers
.
async
(
function
(
done
)
{
Comment
.
create
({
content
:
'parentComment'
}).
success
(
function
(
p
)
{
parent
=
p
done
()
})
})
Helpers
.
async
(
function
(
done
)
{
Comment
.
create
({
content
:
'child1'
}).
success
(
function
(
child1
)
{
child1
.
setParent
(
parent
).
success
(
function
()
{
done
()
})
})
})
Helpers
.
async
(
function
(
done
)
{
Comment
.
create
({
content
:
'child2'
}).
success
(
function
(
child2
)
{
child2
.
setParent
(
parent
).
success
(
function
()
{
done
()
})
})
})
Helpers
.
async
(
function
(
done
)
{
Comment
.
find
({
where
:
{
content
:
'parentComment'
}}).
success
(
function
(
parent
)
{
parent
.
getChildren
().
success
(
function
(
children
)
{
expect
(
children
.
length
).
toEqual
(
2
)
done
()
})
})
})
})
it
(
"should still use many to many for selfAssociation by default (two DAOs are created)"
,
function
()
{
Helpers
.
async
(
function
(
done
)
{
var
oldLength
=
sequelize
.
daoFactoryManager
.
daos
.
length
;
var
Comment
=
sequelize
.
define
(
'Comment'
,
{
content
:
Sequelize
.
STRING
})
Comment
.
belongsTo
(
Comment
,
{
as
:
"Parent"
})
Comment
.
hasMany
(
Comment
,
{
as
:
'Children'
})
expect
(
sequelize
.
daoFactoryManager
.
daos
.
length
).
toEqual
(
oldLength
+
2
)
done
();
})
})
})
describe
(
'bi-directional'
,
function
()
{
it
(
'adds the foreign key'
,
function
()
{
Task
.
hasMany
(
User
)
User
.
hasMany
(
Task
)
expect
(
Task
.
attributes
.
UserId
).
toBeUndefined
()
expect
(
User
.
attributes
.
UserId
).
toBeUndefined
()
var
daos
=
sequelize
.
daoFactoryManager
.
daos
.
filter
(
function
(
dao
)
{
return
(
dao
.
tableName
==
(
Task
.
tableName
+
User
.
tableName
))
})
daos
.
forEach
(
function
(
dao
)
{
expect
(
dao
.
attributes
.
UserId
).
toBeDefined
()
expect
(
dao
.
attributes
.
TaskId
).
toBeDefined
()
})
})
it
(
"adds the foreign key with underscores"
,
function
()
{
User
=
sequelize
.
define
(
'User'
,
{
username
:
Sequelize
.
STRING
},
{
underscored
:
true
})
Task
=
sequelize
.
define
(
'Task'
,
{
title
:
Sequelize
.
STRING
})
Task
.
hasMany
(
User
)
User
.
hasMany
(
Task
)
expect
(
Task
.
attributes
.
user_id
).
toBeUndefined
()
expect
(
User
.
attributes
.
user_id
).
toBeUndefined
()
var
daos
=
sequelize
.
daoFactoryManager
.
daos
.
filter
(
function
(
dao
)
{
return
(
dao
.
tableName
==
(
Task
.
tableName
+
User
.
tableName
))
})
daos
.
forEach
(
function
(
dao
)
{
expect
(
dao
.
attributes
.
user_id
).
toBeDefined
()
expect
(
dao
.
attributes
.
TaskId
).
toBeDefined
()
})
})
it
(
"uses the passed foreign keys"
,
function
()
{
User
.
hasMany
(
Task
,
{
foreignKey
:
'person_id'
})
Task
.
hasMany
(
User
,
{
foreignKey
:
'work_item_id'
})
var
daos
=
sequelize
.
daoFactoryManager
.
daos
.
filter
(
function
(
dao
)
{
return
(
dao
.
tableName
==
(
Task
.
tableName
+
User
.
tableName
))
})
daos
.
forEach
(
function
(
dao
)
{
expect
(
dao
.
attributes
.
person_id
).
toBeDefined
()
expect
(
dao
.
attributes
.
work_item_id
).
toBeDefined
()
})
})
it
(
"defines getters and setters"
,
function
()
{
User
.
hasMany
(
Task
)
Task
.
hasMany
(
User
)
var
u
=
User
.
build
({
username
:
'asd'
})
expect
(
u
.
setTasks
).
toBeDefined
()
expect
(
u
.
getTasks
).
toBeDefined
()
var
t
=
Task
.
build
({
title
:
'foobar'
})
expect
(
t
.
setUsers
).
toBeDefined
()
expect
(
t
.
getUsers
).
toBeDefined
()
})
it
(
"defines getters and setters according to the 'as' option"
,
function
()
{
User
.
hasMany
(
Task
,
{
as
:
'Tasks'
})
Task
.
hasMany
(
User
,
{
as
:
'Users'
})
var
u
=
User
.
build
({
username
:
'asd'
})
expect
(
u
.
setTasks
).
toBeDefined
()
expect
(
u
.
getTasks
).
toBeDefined
()
var
t
=
Task
.
build
({
title
:
'asd'
})
expect
(
t
.
setUsers
).
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 daos 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
daoNames
=
sequelize
.
daoFactoryManager
.
daos
.
map
(
function
(
dao
)
{
return
dao
.
tableName
})
,
expectation
=
[
"Persons"
,
"ChildrenPersons"
,
"CoWorkersPersons"
,
"FriendsPersons"
]
expectation
.
forEach
(
function
(
ex
)
{
expect
(
daoNames
.
indexOf
(
ex
)
>
-
1
).
toBeTruthy
()
})
done
()
})
})
})
it
(
"allows join table to be specified"
,
function
()
{
Helpers
.
async
(
function
(
done
)
{
var
Child
=
sequelize
.
define
(
'Child'
,
{
name
:
Sequelize
.
STRING
},
{
underscore
:
true
,
freezeTableName
:
true
})
var
Parent
=
sequelize
.
define
(
'Parent'
,
{
name
:
Sequelize
.
STRING
},
{
underscore
:
true
,
freezeTableName
:
true
})
var
ParentJoin
=
sequelize
.
define
(
'ParentRelationship'
,
{
parent_id
:
Sequelize
.
INTEGER
,
child_id
:
Sequelize
.
INTEGER
},
{
underscore
:
true
,
freezeTableName
:
true
})
Parent
.
hasMany
(
Child
,
{
as
:
'Children'
,
foreignKey
:
'child_id'
,
joinTableName
:
'ParentRelationship'
})
Child
.
hasMany
(
Parent
,
{
as
:
'Parents'
,
foreignKey
:
'parent_id'
,
joinTableName
:
'ParentRelationship'
})
var
parents
=
[]
ParentJoin
.
sync
({
force
:
true
}).
success
(
function
()
{
Parent
.
sync
({
force
:
true
}).
success
(
function
()
{
Child
.
sync
({
force
:
true
}).
success
(
function
()
{
Parent
.
create
({
name
:
'mom'
}).
success
(
function
(
mom
)
{
parents
.
push
(
mom
)
Parent
.
create
({
name
:
'dad'
}).
success
(
function
(
dad
)
{
parents
.
push
(
dad
)
Child
.
create
({
name
:
'baby'
}).
success
(
function
(
baby
)
{
baby
.
setParents
(
parents
).
success
(
function
(){
parents
[
0
].
getChildren
().
success
(
function
(
children
){
expect
(
children
).
not
.
toBe
(
null
)
expect
(
children
.
length
).
toBeDefined
()
expect
(
children
.
length
).
toEqual
(
1
)
expect
(
children
[
0
]).
toBeDefined
()
expect
(
children
[
0
].
name
).
toEqual
(
'baby'
)
done
()
})
})
})
})
})
})
})
})
})
})
it
(
"allows join table to be mapped and specified"
,
function
()
{
var
User
=
sequelize
.
define
(
'User'
,
{
name
:
Sequelize
.
STRING
},
{
underscore
:
true
,
freezeTableName
:
true
})
var
Company
=
sequelize
.
define
(
'Company'
,
{
name
:
Sequelize
.
STRING
},
{
underscore
:
true
,
freezeTableName
:
true
})
var
CompanyAccess
=
sequelize
.
define
(
'CompanyAccess'
,
{
company_id
:
Sequelize
.
INTEGER
,
user_id
:
Sequelize
.
INTEGER
,
permission
:
Sequelize
.
STRING
},
{
underscore
:
true
,
freezeTableName
:
true
})
CompanyAccess
.
belongsTo
(
User
,
{
as
:
'User'
,
foreignKey
:
'user_id'
})
CompanyAccess
.
belongsTo
(
Company
,
{
as
:
'Company'
,
foreignKey
:
'company_id'
})
User
.
hasMany
(
Company
,
{
as
:
'Companies'
,
foreignKey
:
'user_id'
,
joinTableName
:
'CompanyAccess'
})
Company
.
hasMany
(
User
,
{
as
:
'Users'
,
foreignKey
:
'company_id'
,
joinTableName
:
'CompanyAccess'
})
Helpers
.
async
(
function
(
done
)
{
var
companies
=
[]
CompanyAccess
.
sync
({
force
:
true
}).
success
(
function
()
{
User
.
sync
({
force
:
true
}).
success
(
function
()
{
Company
.
sync
({
force
:
true
}).
success
(
function
()
{
Company
.
create
({
name
:
'IBM'
}).
success
(
function
(
ibm
)
{
companies
.
push
(
ibm
)
Company
.
create
({
name
:
'EA'
}).
success
(
function
(
ea
)
{
companies
.
push
(
ea
)
User
.
create
({
name
:
'joe@ibm.com'
}).
success
(
function
(
joe
)
{
joe
.
setCompanies
(
companies
).
success
(
function
(){
User
.
find
({
where
:
{
name
:
'joe@ibm.com'
}}).
success
(
function
(
joe
)
{
expect
(
joe
).
not
.
toEqual
(
null
)
joe
.
getCompanies
().
success
(
function
(
comps
)
{
expect
(
comps
).
not
.
toEqual
(
null
)
expect
(
comps
.
length
).
toEqual
(
2
)
done
()
})
})
})
})
})
})
})
})
})
})
})
it
(
"gets and sets the connector daos"
,
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
()
})
})
})
})
})
})
})
})
spec-jasmine/associations/has-one.spec.js
deleted
100644 → 0
View file @
34b680d
var
config
=
require
(
"../config/config"
)
,
Sequelize
=
require
(
"../../index"
)
,
sequelize
=
new
Sequelize
(
config
.
mysql
.
database
,
config
.
mysql
.
username
,
config
.
mysql
.
password
,
{
pool
:
config
.
mysql
.
pool
,
logging
:
false
,
host
:
config
.
mysql
.
host
,
port
:
config
.
mysql
.
port
})
,
Helpers
=
new
(
require
(
"../config/helpers"
))(
sequelize
)
describe
(
'HasOne'
,
function
()
{
var
User
=
null
,
Task
=
null
var
setup
=
function
()
{
User
=
sequelize
.
define
(
'User'
,
{
username
:
Sequelize
.
STRING
})
Task
=
sequelize
.
define
(
'Task'
,
{
title
:
Sequelize
.
STRING
})
}
beforeEach
(
function
()
{
Helpers
.
dropAllTables
();
setup
()
})
afterEach
(
function
()
{
Helpers
.
dropAllTables
()
})
it
(
"adds the foreign key"
,
function
()
{
User
.
hasOne
(
Task
)
expect
(
Task
.
attributes
.
UserId
).
toEqual
(
"INTEGER"
)
})
it
(
"adds an underscored foreign key"
,
function
()
{
User
=
sequelize
.
define
(
'User'
,
{
username
:
Sequelize
.
STRING
},
{
underscored
:
true
})
Task
=
sequelize
.
define
(
'Task'
,
{
title
:
Sequelize
.
STRING
})
User
.
hasOne
(
Task
)
expect
(
Task
.
attributes
.
user_id
).
toEqual
(
"INTEGER"
)
})
it
(
"uses the passed foreign key"
,
function
()
{
User
=
sequelize
.
define
(
'User'
,
{
username
:
Sequelize
.
STRING
},
{
underscored
:
true
})
Task
=
sequelize
.
define
(
'Task'
,
{
title
:
Sequelize
.
STRING
})
User
.
hasOne
(
Task
,
{
foreignKey
:
'person_id'
})
expect
(
Task
.
attributes
.
person_id
).
toEqual
(
"INTEGER"
)
})
it
(
"defines the getter and the setter"
,
function
()
{
User
.
hasOne
(
Task
)
var
u
=
User
.
build
({
username
:
'asd'
})
expect
(
u
.
setTask
).
toBeDefined
()
expect
(
u
.
getTask
).
toBeDefined
()
})
it
(
"defined the getter and the setter according to the passed 'as' option"
,
function
()
{
User
.
hasOne
(
Task
,
{
as
:
'Work'
})
var
u
=
User
.
build
({
username
:
'asd'
})
expect
(
u
.
setWork
).
toBeDefined
()
expect
(
u
.
getWork
).
toBeDefined
()
})
it
(
"aliases associations to the same table according to the passed 'as' option"
,
function
()
{
User
.
hasOne
(
Task
,
{
as
:
'Work'
});
User
.
hasOne
(
Task
,
{
as
:
'Play'
});
var
u
=
User
.
build
({
username
:
'asd'
})
expect
(
u
.
getWork
).
toBeDefined
()
expect
(
u
.
setWork
).
toBeDefined
()
expect
(
u
.
getPlay
).
toBeDefined
()
expect
(
u
.
setPlay
).
toBeDefined
()
})
it
(
"gets and sets the correct objects"
,
function
()
{
var
user
,
task
;
User
.
hasOne
(
Task
,
{
as
:
'Task'
})
Helpers
.
async
(
function
(
done
)
{
User
.
sync
({
force
:
true
}).
success
(
function
()
{
Task
.
sync
({
force
:
true
}).
success
(
function
()
{
User
.
create
({
username
:
'name'
}).
success
(
function
(
_user
)
{
Task
.
create
({
title
:
'snafu'
}).
success
(
function
(
_task
)
{
user
=
_user
task
=
_task
done
()
})
})
})
})
})
Helpers
.
async
(
function
(
done
)
{
user
.
setTask
(
task
).
on
(
'success'
,
function
()
{
user
.
getTask
().
on
(
'success'
,
function
(
task2
)
{
expect
(
task
.
title
).
toEqual
(
task2
.
title
)
user
.
getTask
({
attributes
:
[
'title'
]}).
on
(
'success'
,
function
(
task2
)
{
expect
(
task2
.
selectedValues
.
title
).
toEqual
(
'snafu'
)
expect
(
task2
.
selectedValues
.
id
).
toEqual
(
null
)
done
()
})
})
})
})
})
it
(
"unsets unassociated objects"
,
function
()
{
var
user
,
task1
,
task2
;
User
.
hasOne
(
Task
,
{
as
:
'Task'
})
Helpers
.
async
(
function
(
done
)
{
User
.
sync
({
force
:
true
}).
success
(
function
()
{
Task
.
sync
({
force
:
true
}).
success
(
function
()
{
User
.
create
({
username
:
'name'
}).
success
(
function
(
_user
)
{
Task
.
create
({
title
:
'snafu'
}).
success
(
function
(
_task1
)
{
Task
.
create
({
title
:
'another task'
}).
success
(
function
(
_task2
)
{
user
=
_user
task1
=
_task1
task2
=
_task2
done
()
})
})
})
})
})
})
Helpers
.
async
(
function
(
done
)
{
user
.
setTask
(
task1
).
success
(
function
()
{
user
.
getTask
().
success
(
function
(
_task
)
{
expect
(
task1
.
title
).
toEqual
(
_task
.
title
)
user
.
setTask
(
task2
).
success
(
function
()
{
user
.
getTask
().
success
(
function
(
_task2
)
{
expect
(
task2
.
title
).
toEqual
(
task2
.
title
)
done
()
})
})
})
})
})
})
it
(
"sets self associations"
,
function
()
{
Helpers
.
async
(
function
(
done
)
{
var
Person
=
sequelize
.
define
(
'Person'
,
{
name
:
Sequelize
.
STRING
})
Person
.
hasOne
(
Person
,
{
as
:
'Mother'
,
foreignKey
:
'MotherId'
})
Person
.
hasOne
(
Person
,
{
as
:
'Father'
,
foreignKey
:
'FatherId'
})
Person
.
sync
({
force
:
true
}).
success
(
function
()
{
var
p
=
Person
.
build
()
expect
(
p
.
setFather
).
toBeDefined
()
expect
(
p
.
setMother
).
toBeDefined
()
done
()
})
})
})
it
(
"automatically sets the foreign key on self associations"
,
function
()
{
var
Person
=
sequelize
.
define
(
'Person'
,
{
name
:
Sequelize
.
STRING
})
Person
.
hasOne
(
Person
,
{
as
:
'Mother'
})
expect
(
Person
.
associations
.
MotherPersons
.
options
.
foreignKey
).
toEqual
(
'MotherId'
)
})
})
spec/associations/belongs-to.spec.js
View file @
e713fba
/* jshint camelcase: false */
if
(
typeof
require
===
'function'
)
{
if
(
typeof
require
===
'function'
)
{
const
buster
=
require
(
"buster"
)
const
buster
=
require
(
"buster"
)
,
Helpers
=
require
(
'../buster-helpers'
)
,
Helpers
=
require
(
'../buster-helpers'
)
...
@@ -19,6 +20,137 @@ describe(Helpers.getTestDialectTeaser("BelongsTo"), function() {
...
@@ -19,6 +20,137 @@ describe(Helpers.getTestDialectTeaser("BelongsTo"), function() {
})
})
})
})
describe
(
'general usage'
,
function
()
{
before
(
function
(
done
)
{
this
.
User
=
this
.
sequelize
.
define
(
'User'
,
{
username
:
Helpers
.
Sequelize
.
STRING
,
enabled
:
{
type
:
Helpers
.
Sequelize
.
BOOLEAN
,
defaultValue
:
true
}
})
this
.
Task
=
this
.
sequelize
.
define
(
'Task'
,
{
title
:
Helpers
.
Sequelize
.
STRING
})
this
.
sequelize
.
sync
({
force
:
true
}).
success
(
done
)
})
it
(
'adds the foreign key'
,
function
(
done
)
{
this
.
Task
.
belongsTo
(
this
.
User
)
expect
(
this
.
Task
.
attributes
.
UserId
).
toEqual
(
"INTEGER"
)
done
()
})
it
(
"underscores the foreign key"
,
function
(
done
)
{
var
Task
=
this
.
sequelize
.
define
(
'Task'
,
{
title
:
Sequelize
.
STRING
},
{
underscored
:
true
})
Task
.
belongsTo
(
this
.
User
)
expect
(
Task
.
attributes
.
user_id
).
toEqual
(
"INTEGER"
)
done
()
})
it
(
"uses the passed foreign key"
,
function
(
done
)
{
this
.
Task
.
belongsTo
(
this
.
User
,
{
foreignKey
:
'person_id'
})
expect
(
this
.
Task
.
attributes
.
person_id
).
toEqual
(
"INTEGER"
)
done
()
})
it
(
"defines getters and setters"
,
function
(
done
)
{
this
.
Task
.
belongsTo
(
this
.
User
)
var
task
=
this
.
Task
.
build
({
title
:
'asd'
})
expect
(
task
.
setUser
).
toBeDefined
()
expect
(
task
.
getUser
).
toBeDefined
()
done
()
})
it
(
"aliases the getters and setters according to the passed 'as' option"
,
function
(
done
)
{
this
.
Task
.
belongsTo
(
this
.
User
,
{
as
:
'Person'
})
var
task
=
this
.
Task
.
build
({
title
:
'asd'
})
expect
(
task
.
setPerson
).
toBeDefined
()
expect
(
task
.
getPerson
).
toBeDefined
()
done
()
})
it
(
"aliases associations to the same table according to the passed 'as' option"
,
function
(
done
)
{
this
.
Task
.
belongsTo
(
this
.
User
,
{
as
:
'Poster'
})
this
.
Task
.
belongsTo
(
this
.
User
,
{
as
:
'Owner'
})
var
task
=
this
.
Task
.
build
({
title
:
'asd'
})
expect
(
task
.
getPoster
).
toBeDefined
()
expect
(
task
.
setPoster
).
toBeDefined
()
expect
(
task
.
getOwner
).
toBeDefined
()
expect
(
task
.
setOwner
).
toBeDefined
()
done
()
})
it
(
"intializes the foreign key with null"
,
function
(
done
)
{
this
.
Task
.
belongsTo
(
this
.
User
)
var
task
=
this
.
Task
.
build
({
title
:
'asd'
})
expect
(
task
.
UserId
).
not
.
toBeDefined
();
done
()
})
it
(
"sets and gets the correct objects"
,
function
(
done
)
{
var
self
=
this
this
.
Task
.
belongsTo
(
this
.
User
,
{
as
:
'User'
})
this
.
sequelize
.
sync
({
force
:
true
}).
success
(
function
()
{
self
.
User
.
create
({
username
:
'asd'
}).
success
(
function
(
u
)
{
self
.
Task
.
create
({
title
:
'a task'
}).
success
(
function
(
t
)
{
t
.
setUser
(
u
).
success
(
function
()
{
t
.
getUser
().
success
(
function
(
user
)
{
expect
(
user
.
username
).
toEqual
(
'asd'
)
done
()
})
})
})
})
})
})
it
(
'extends the id where param with the supplied where params'
,
function
(
done
)
{
var
self
=
this
this
.
Task
.
belongsTo
(
this
.
User
,
{
as
:
'User'
})
this
.
sequelize
.
sync
({
force
:
true
}).
success
(
function
()
{
self
.
User
.
create
({
username
:
'asd'
,
enabled
:
false
}).
success
(
function
(
u
)
{
self
.
Task
.
create
({
title
:
'a task'
}).
success
(
function
(
t
)
{
t
.
setUser
(
u
).
success
(
function
()
{
t
.
getUser
({
where
:
{
enabled
:
true
}}).
success
(
function
(
user
)
{
expect
(
user
).
toEqual
(
null
)
done
()
})
})
})
})
})
})
it
(
"handles self associations"
,
function
(
done
)
{
var
Person
=
this
.
sequelize
.
define
(
'Person'
,
{
name
:
Helpers
.
Sequelize
.
STRING
})
Person
.
belongsTo
(
Person
,
{
as
:
'Mother'
,
foreignKey
:
'MotherId'
})
Person
.
belongsTo
(
Person
,
{
as
:
'Father'
,
foreignKey
:
'FatherId'
})
Person
.
sync
({
force
:
true
}).
success
(
function
()
{
var
p
=
Person
.
build
()
expect
(
p
.
setFather
).
toBeDefined
()
expect
(
p
.
setMother
).
toBeDefined
()
done
()
})
})
it
(
"sets the foreign key in self associations"
,
function
(
done
)
{
var
Person
=
this
.
sequelize
.
define
(
'Person'
,
{
name
:
Helpers
.
Sequelize
.
STRING
})
Person
.
belongsTo
(
Person
,
{
as
:
'Mother'
})
expect
(
Person
.
associations
.
MotherPersons
.
options
.
foreignKey
).
toEqual
(
'MotherId'
)
done
()
})
})
describe
(
'setAssociation'
,
function
()
{
describe
(
'setAssociation'
,
function
()
{
it
(
'clears the association if null is passed'
,
function
(
done
)
{
it
(
'clears the association if null is passed'
,
function
(
done
)
{
var
User
=
this
.
sequelize
.
define
(
'UserXYZ'
,
{
username
:
Sequelize
.
STRING
})
var
User
=
this
.
sequelize
.
define
(
'UserXYZ'
,
{
username
:
Sequelize
.
STRING
})
...
...
spec/associations/has-many.spec.js
View file @
e713fba
/* jshint camelcase: false */
if
(
typeof
require
===
'function'
)
{
if
(
typeof
require
===
'function'
)
{
const
buster
=
require
(
"buster"
)
const
buster
=
require
(
"buster"
)
,
Helpers
=
require
(
'../buster-helpers'
)
,
Helpers
=
require
(
'../buster-helpers'
)
...
@@ -11,7 +12,6 @@ buster.spec.expose()
...
@@ -11,7 +12,6 @@ buster.spec.expose()
buster
.
testRunner
.
timeout
=
500
buster
.
testRunner
.
timeout
=
500
describe
(
Helpers
.
getTestDialectTeaser
(
"HasMany"
),
function
()
{
describe
(
Helpers
.
getTestDialectTeaser
(
"HasMany"
),
function
()
{
before
(
function
(
done
)
{
before
(
function
(
done
)
{
var
self
=
this
var
self
=
this
...
@@ -22,6 +22,350 @@ describe(Helpers.getTestDialectTeaser("HasMany"), function() {
...
@@ -22,6 +22,350 @@ describe(Helpers.getTestDialectTeaser("HasMany"), function() {
})
})
})
})
describe
(
'general usage'
,
function
()
{
before
(
function
(
done
)
{
this
.
User
=
this
.
sequelize
.
define
(
'User'
,
{
username
:
Helpers
.
Sequelize
.
STRING
})
this
.
Task
=
this
.
sequelize
.
define
(
'Task'
,
{
title
:
Helpers
.
Sequelize
.
STRING
})
this
.
sequelize
.
sync
({
force
:
true
}).
success
(
done
)
})
describe
(
'mono-directional'
,
function
()
{
it
(
"adds the foreign key"
,
function
(
done
)
{
this
.
User
.
hasMany
(
this
.
Task
)
expect
(
this
.
Task
.
attributes
.
UserId
).
toEqual
(
"INTEGER"
)
done
()
})
it
(
'adds the foreign key with underscore'
,
function
(
done
)
{
var
User
=
this
.
sequelize
.
define
(
'User'
,
{
username
:
Helpers
.
Sequelize
.
STRING
})
,
Task
=
this
.
sequelize
.
define
(
'Task'
,
{
title
:
Helpers
.
Sequelize
.
STRING
},
{
underscored
:
true
})
Task
.
hasMany
(
User
)
expect
(
User
.
attributes
.
task_id
).
toBeDefined
()
done
()
})
it
(
'uses the passed foreign key'
,
function
(
done
)
{
this
.
User
.
hasMany
(
this
.
Task
,
{
foreignKey
:
'person_id'
})
expect
(
this
.
Task
.
attributes
.
person_id
).
toEqual
(
"INTEGER"
)
done
()
})
it
(
'defines getters and setters'
,
function
(
done
)
{
this
.
User
.
hasMany
(
this
.
Task
)
var
u
=
this
.
User
.
build
({
username
:
'asd'
})
expect
(
u
.
setTasks
).
toBeDefined
()
expect
(
u
.
getTasks
).
toBeDefined
()
done
()
})
it
(
"defines getters and setters according to the 'as' option"
,
function
(
done
)
{
this
.
User
.
hasMany
(
this
.
Task
,
{
as
:
'Tasks'
})
var
u
=
this
.
User
.
build
({
username
:
'asd'
})
expect
(
u
.
setTasks
).
toBeDefined
()
expect
(
u
.
getTasks
).
toBeDefined
()
done
()
})
it
(
"sets and gets associated objects"
,
function
(
done
)
{
var
self
=
this
this
.
User
.
hasMany
(
this
.
Task
,
{
as
:
'Tasks'
})
this
.
sequelize
.
sync
({
force
:
true
}).
success
(
function
()
{
self
.
User
.
create
({
username
:
'name'
}).
success
(
function
(
user
)
{
self
.
Task
.
create
({
title
:
'task1'
}).
success
(
function
(
task1
)
{
self
.
Task
.
create
({
title
:
'task2'
}).
success
(
function
(
task2
)
{
user
.
setTasks
([
task1
,
task2
]).
success
(
function
()
{
user
.
getTasks
().
success
(
function
(
tasks
)
{
expect
(
tasks
.
length
).
toEqual
(
2
)
user
.
getTasks
({
attributes
:
[
'title'
]}).
success
(
function
(
tasks
)
{
expect
(
tasks
[
0
].
selectedValues
.
title
).
toEqual
(
'task1'
)
expect
(
tasks
[
0
].
selectedValues
.
id
).
not
.
toBeDefined
()
done
()
})
})
})
})
})
})
})
})
})
it
(
"should allow selfAssociation to be single linked (only one DAO is created)"
,
function
(
done
)
{
var
oldLength
=
this
.
sequelize
.
daoFactoryManager
.
daos
.
length
,
Comment
=
this
.
sequelize
.
define
(
'Comment'
,
{
content
:
Helpers
.
Sequelize
.
STRING
})
Comment
.
belongsTo
(
Comment
,
{
as
:
"Parent"
});
Comment
.
hasMany
(
Comment
,
{
as
:
'Children'
,
foreignKey
:
"ParentId"
,
useJunctionTable
:
false
})
expect
(
this
.
sequelize
.
daoFactoryManager
.
daos
.
length
).
toEqual
(
oldLength
+
1
)
Comment
.
sync
({
force
:
true
}).
success
(
function
()
{
Comment
.
create
({
content
:
'parentComment'
}).
success
(
function
(
parent
)
{
Comment
.
create
({
content
:
'child1'
}).
success
(
function
(
child1
)
{
child1
.
setParent
(
parent
).
success
(
function
()
{
Comment
.
create
({
content
:
'child2'
}).
success
(
function
(
child2
)
{
child2
.
setParent
(
parent
).
success
(
function
()
{
Comment
.
find
({
where
:
{
content
:
'parentComment'
}}).
success
(
function
(
parent
)
{
parent
.
getChildren
().
success
(
function
(
children
)
{
expect
(
children
.
length
).
toEqual
(
2
)
done
()
})
})
})
})
})
})
})
})
})
it
(
"should still use many to many for selfAssociation by default (two DAOs are created)"
,
function
(
done
)
{
var
oldLength
=
this
.
sequelize
.
daoFactoryManager
.
daos
.
length
,
Comment
=
this
.
sequelize
.
define
(
'Comment'
,
{
content
:
Sequelize
.
STRING
})
Comment
.
belongsTo
(
Comment
,
{
as
:
"Parent"
})
Comment
.
hasMany
(
Comment
,
{
as
:
'Children'
})
expect
(
this
.
sequelize
.
daoFactoryManager
.
daos
.
length
).
toEqual
(
oldLength
+
2
)
done
();
})
describe
(
'bi-directional'
,
function
()
{
it
(
'adds the foreign key'
,
function
(
done
)
{
var
self
=
this
this
.
Task
.
hasMany
(
this
.
User
)
this
.
User
.
hasMany
(
this
.
Task
)
expect
(
this
.
Task
.
attributes
.
UserId
).
not
.
toBeDefined
()
expect
(
this
.
User
.
attributes
.
UserId
).
not
.
toBeDefined
()
var
daos
=
this
.
sequelize
.
daoFactoryManager
.
daos
.
filter
(
function
(
dao
)
{
return
(
dao
.
tableName
==
(
self
.
Task
.
tableName
+
self
.
User
.
tableName
))
})
daos
.
forEach
(
function
(
dao
)
{
expect
(
dao
.
attributes
.
UserId
).
toBeDefined
()
expect
(
dao
.
attributes
.
TaskId
).
toBeDefined
()
})
done
()
})
it
(
"adds the foreign key with underscores"
,
function
(
done
)
{
var
User
=
this
.
sequelize
.
define
(
'User'
,
{
username
:
Helpers
.
Sequelize
.
STRING
},
{
underscored
:
true
})
,
Task
=
this
.
sequelize
.
define
(
'Task'
,
{
title
:
Helpers
.
Sequelize
.
STRING
})
Task
.
hasMany
(
User
)
User
.
hasMany
(
Task
)
expect
(
Task
.
attributes
.
user_id
).
not
.
toBeDefined
()
expect
(
User
.
attributes
.
user_id
).
not
.
toBeDefined
()
var
daos
=
this
.
sequelize
.
daoFactoryManager
.
daos
.
filter
(
function
(
dao
)
{
return
(
dao
.
tableName
==
(
Task
.
tableName
+
User
.
tableName
))
})
daos
.
forEach
(
function
(
dao
)
{
expect
(
dao
.
attributes
.
user_id
).
toBeDefined
()
expect
(
dao
.
attributes
.
TaskId
).
toBeDefined
()
})
done
()
})
it
(
"uses the passed foreign keys"
,
function
(
done
)
{
var
self
=
this
this
.
User
.
hasMany
(
this
.
Task
,
{
foreignKey
:
'person_id'
})
this
.
Task
.
hasMany
(
this
.
User
,
{
foreignKey
:
'work_item_id'
})
var
daos
=
this
.
sequelize
.
daoFactoryManager
.
daos
.
filter
(
function
(
dao
)
{
return
(
dao
.
tableName
==
(
self
.
Task
.
tableName
+
self
.
User
.
tableName
))
})
daos
.
forEach
(
function
(
dao
)
{
expect
(
dao
.
attributes
.
person_id
).
toBeDefined
()
expect
(
dao
.
attributes
.
work_item_id
).
toBeDefined
()
})
done
()
})
it
(
"defines getters and setters"
,
function
(
done
)
{
this
.
User
.
hasMany
(
this
.
Task
)
this
.
Task
.
hasMany
(
this
.
User
)
var
u
=
this
.
User
.
build
({
username
:
'asd'
})
expect
(
u
.
setTasks
).
toBeDefined
()
expect
(
u
.
getTasks
).
toBeDefined
()
var
t
=
this
.
Task
.
build
({
title
:
'foobar'
})
expect
(
t
.
setUsers
).
toBeDefined
()
expect
(
t
.
getUsers
).
toBeDefined
()
done
()
})
it
(
"defines getters and setters according to the 'as' option"
,
function
(
done
)
{
this
.
User
.
hasMany
(
this
.
Task
,
{
as
:
'Tasks'
})
this
.
Task
.
hasMany
(
this
.
User
,
{
as
:
'Users'
})
var
u
=
this
.
User
.
build
({
username
:
'asd'
})
expect
(
u
.
setTasks
).
toBeDefined
()
expect
(
u
.
getTasks
).
toBeDefined
()
var
t
=
this
.
Task
.
build
({
title
:
'asd'
})
expect
(
t
.
setUsers
).
toBeDefined
()
expect
(
t
.
getUsers
).
toBeDefined
()
done
()
})
it
(
"sets and gets the corrected associated objects"
,
function
(
done
)
{
var
self
=
this
var
users
=
[]
,
tasks
=
[]
this
.
User
.
hasMany
(
this
.
Task
,
{
as
:
'Tasks'
})
this
.
Task
.
hasMany
(
this
.
User
,
{
as
:
'Users'
})
this
.
sequelize
.
sync
({
force
:
true
}).
success
(
function
()
{
self
.
User
.
create
({
username
:
'name'
}).
success
(
function
(
user1
)
{
self
.
User
.
create
({
username
:
'name2'
}).
success
(
function
(
user2
)
{
self
.
Task
.
create
({
title
:
'task1'
}).
success
(
function
(
task1
)
{
self
.
Task
.
create
({
title
:
'task2'
}).
success
(
function
(
task2
)
{
users
.
push
(
user1
)
users
.
push
(
user2
)
tasks
.
push
(
task1
)
tasks
.
push
(
task2
)
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 daos name"
,
function
(
done
)
{
var
self
=
this
,
Person
=
this
.
sequelize
.
define
(
'Person'
,
{
name
:
Helpers
.
Sequelize
.
STRING
})
Person
.
hasMany
(
Person
,
{
as
:
'Children'
})
Person
.
hasMany
(
Person
,
{
as
:
'Friends'
})
Person
.
hasMany
(
Person
,
{
as
:
'CoWorkers'
})
Person
.
sync
({
force
:
true
}).
success
(
function
()
{
var
daoNames
=
self
.
sequelize
.
daoFactoryManager
.
daos
.
map
(
function
(
dao
)
{
return
dao
.
tableName
})
,
expectation
=
[
"Persons"
,
"ChildrenPersons"
,
"CoWorkersPersons"
,
"FriendsPersons"
]
expectation
.
forEach
(
function
(
ex
)
{
expect
(
daoNames
.
indexOf
(
ex
)
>
-
1
).
toBeTruthy
()
})
done
()
})
})
it
(
"allows join table to be specified"
,
function
(
done
)
{
var
Child
=
this
.
sequelize
.
define
(
'Child'
,
{
name
:
Helpers
.
Sequelize
.
STRING
},
{
underscore
:
true
,
freezeTableName
:
true
})
,
Parent
=
this
.
sequelize
.
define
(
'Parent'
,
{
name
:
Helpers
.
Sequelize
.
STRING
},
{
underscore
:
true
,
freezeTableName
:
true
})
,
ParentJoin
=
this
.
sequelize
.
define
(
'ParentRelationship'
,
{
parent_id
:
Helpers
.
Sequelize
.
INTEGER
,
child_id
:
Helpers
.
Sequelize
.
INTEGER
},
{
underscore
:
true
,
freezeTableName
:
true
})
,
parents
=
[]
Parent
.
hasMany
(
Child
,
{
as
:
'Children'
,
foreignKey
:
'child_id'
,
joinTableName
:
'ParentRelationship'
})
Child
.
hasMany
(
Parent
,
{
as
:
'Parents'
,
foreignKey
:
'parent_id'
,
joinTableName
:
'ParentRelationship'
})
this
.
sequelize
.
sync
({
force
:
true
}).
success
(
function
()
{
Parent
.
create
({
name
:
'mom'
}).
success
(
function
(
mom
)
{
parents
.
push
(
mom
)
Parent
.
create
({
name
:
'dad'
}).
success
(
function
(
dad
)
{
parents
.
push
(
dad
)
Child
.
create
({
name
:
'baby'
}).
success
(
function
(
baby
)
{
baby
.
setParents
(
parents
).
success
(
function
(){
parents
[
0
].
getChildren
().
success
(
function
(
children
){
expect
(
children
).
not
.
toBe
(
null
)
expect
(
children
.
length
).
toBeDefined
()
expect
(
children
.
length
).
toEqual
(
1
)
expect
(
children
[
0
]).
toBeDefined
()
expect
(
children
[
0
].
name
).
toEqual
(
'baby'
)
done
()
})
})
})
})
})
})
})
it
(
"allows join table to be mapped and specified"
,
function
(
done
)
{
var
User
=
this
.
sequelize
.
define
(
'User'
,
{
name
:
Helpers
.
Sequelize
.
STRING
},
{
underscore
:
true
,
freezeTableName
:
true
})
,
Company
=
this
.
sequelize
.
define
(
'Company'
,
{
name
:
Helpers
.
Sequelize
.
STRING
},
{
underscore
:
true
,
freezeTableName
:
true
})
,
CompanyAccess
=
this
.
sequelize
.
define
(
'CompanyAccess'
,
{
company_id
:
Helpers
.
Sequelize
.
INTEGER
,
user_id
:
Helpers
.
Sequelize
.
INTEGER
,
permission
:
Helpers
.
Sequelize
.
STRING
},
{
underscore
:
true
,
freezeTableName
:
true
})
,
companies
=
[]
CompanyAccess
.
belongsTo
(
User
,
{
as
:
'User'
,
foreignKey
:
'user_id'
})
CompanyAccess
.
belongsTo
(
Company
,
{
as
:
'Company'
,
foreignKey
:
'company_id'
})
User
.
hasMany
(
Company
,
{
as
:
'Companies'
,
foreignKey
:
'user_id'
,
joinTableName
:
'CompanyAccess'
})
Company
.
hasMany
(
User
,
{
as
:
'Users'
,
foreignKey
:
'company_id'
,
joinTableName
:
'CompanyAccess'
})
this
.
sequelize
.
sync
({
force
:
true
}).
success
(
function
()
{
Company
.
create
({
name
:
'IBM'
}).
success
(
function
(
ibm
)
{
companies
.
push
(
ibm
)
Company
.
create
({
name
:
'EA'
}).
success
(
function
(
ea
)
{
companies
.
push
(
ea
)
User
.
create
({
name
:
'joe@ibm.com'
}).
success
(
function
(
joe
)
{
joe
.
setCompanies
(
companies
).
success
(
function
(){
User
.
find
({
where
:
{
name
:
'joe@ibm.com'
}}).
success
(
function
(
joe
)
{
expect
(
joe
).
not
.
toEqual
(
null
)
joe
.
getCompanies
().
success
(
function
(
comps
)
{
expect
(
comps
).
not
.
toEqual
(
null
)
expect
(
comps
.
length
).
toEqual
(
2
)
done
()
})
})
})
})
})
})
})
})
it
(
"gets and sets the connector daos"
,
function
(
done
)
{
var
Person
=
this
.
sequelize
.
define
(
'Person'
,
{
name
:
Helpers
.
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
()
})
})
})
})
})
})
})
describe
(
'(1:N)'
,
function
()
{
describe
(
'(1:N)'
,
function
()
{
describe
(
'hasSingle'
,
function
()
{
describe
(
'hasSingle'
,
function
()
{
before
(
function
(
done
)
{
before
(
function
(
done
)
{
...
...
spec/associations/has-one.spec.js
View file @
e713fba
/* jshint camelcase: false */
if
(
typeof
require
===
'function'
)
{
if
(
typeof
require
===
'function'
)
{
const
buster
=
require
(
"buster"
)
const
buster
=
require
(
"buster"
)
,
Sequelize
=
require
(
"../../index"
)
,
Sequelize
=
require
(
"../../index"
)
...
@@ -19,6 +20,136 @@ describe(Helpers.getTestDialectTeaser("HasOne"), function() {
...
@@ -19,6 +20,136 @@ describe(Helpers.getTestDialectTeaser("HasOne"), function() {
})
})
})
})
describe
(
'general usage'
,
function
()
{
before
(
function
(
done
)
{
this
.
User
=
this
.
sequelize
.
define
(
'User'
,
{
username
:
Helpers
.
Sequelize
.
STRING
})
this
.
Task
=
this
.
sequelize
.
define
(
'Task'
,
{
title
:
Helpers
.
Sequelize
.
STRING
})
this
.
sequelize
.
sync
({
force
:
true
}).
success
(
done
)
})
it
(
"adds the foreign key"
,
function
(
done
)
{
this
.
User
.
hasOne
(
this
.
Task
)
expect
(
this
.
Task
.
attributes
.
UserId
).
toEqual
(
"INTEGER"
)
done
()
})
it
(
"adds an underscored foreign key"
,
function
(
done
)
{
var
User
=
this
.
sequelize
.
define
(
'User'
,
{
username
:
Helpers
.
Sequelize
.
STRING
},
{
underscored
:
true
})
,
Task
=
this
.
sequelize
.
define
(
'Task'
,
{
title
:
Helpers
.
Sequelize
.
STRING
})
User
.
hasOne
(
Task
)
expect
(
Task
.
attributes
.
user_id
).
toEqual
(
"INTEGER"
)
done
()
})
it
(
"uses the passed foreign key"
,
function
(
done
)
{
var
User
=
this
.
sequelize
.
define
(
'User'
,
{
username
:
Helpers
.
Sequelize
.
STRING
},
{
underscored
:
true
})
,
Task
=
this
.
sequelize
.
define
(
'Task'
,
{
title
:
Helpers
.
Sequelize
.
STRING
})
User
.
hasOne
(
Task
,
{
foreignKey
:
'person_id'
})
expect
(
Task
.
attributes
.
person_id
).
toEqual
(
"INTEGER"
)
done
()
})
it
(
"defines the getter and the setter"
,
function
(
done
)
{
this
.
User
.
hasOne
(
this
.
Task
)
var
u
=
this
.
User
.
build
({
username
:
'asd'
})
expect
(
u
.
setTask
).
toBeDefined
()
expect
(
u
.
getTask
).
toBeDefined
()
done
()
})
it
(
"defined the getter and the setter according to the passed 'as' option"
,
function
(
done
)
{
this
.
User
.
hasOne
(
this
.
Task
,
{
as
:
'Work'
})
var
u
=
this
.
User
.
build
({
username
:
'asd'
})
expect
(
u
.
setWork
).
toBeDefined
()
expect
(
u
.
getWork
).
toBeDefined
()
done
()
})
it
(
"aliases associations to the same table according to the passed 'as' option"
,
function
(
done
)
{
this
.
User
.
hasOne
(
this
.
Task
,
{
as
:
'Work'
});
this
.
User
.
hasOne
(
this
.
Task
,
{
as
:
'Play'
});
var
u
=
this
.
User
.
build
({
username
:
'asd'
})
expect
(
u
.
getWork
).
toBeDefined
()
expect
(
u
.
setWork
).
toBeDefined
()
expect
(
u
.
getPlay
).
toBeDefined
()
expect
(
u
.
setPlay
).
toBeDefined
()
done
()
})
it
(
"gets and sets the correct objects"
,
function
(
done
)
{
var
self
=
this
this
.
User
.
hasOne
(
this
.
Task
,
{
as
:
'Task'
})
this
.
sequelize
.
sync
({
force
:
true
}).
success
(
function
()
{
self
.
User
.
create
({
username
:
'name'
}).
success
(
function
(
user
)
{
self
.
Task
.
create
({
title
:
'snafu'
}).
success
(
function
(
task
)
{
user
.
setTask
(
task
).
on
(
'success'
,
function
()
{
user
.
getTask
().
on
(
'success'
,
function
(
task2
)
{
expect
(
task
.
title
).
toEqual
(
task2
.
title
)
user
.
getTask
({
attributes
:
[
'title'
]}).
on
(
'success'
,
function
(
task2
)
{
expect
(
task2
.
selectedValues
.
title
).
toEqual
(
'snafu'
)
expect
(
task2
.
selectedValues
.
id
).
not
.
toBeDefined
()
done
()
})
})
})
})
})
})
})
it
(
"unsets unassociated objects"
,
function
(
done
)
{
var
self
=
this
this
.
User
.
hasOne
(
this
.
Task
,
{
as
:
'Task'
})
this
.
sequelize
.
sync
({
force
:
true
}).
success
(
function
()
{
self
.
User
.
create
({
username
:
'name'
}).
success
(
function
(
user
)
{
self
.
Task
.
create
({
title
:
'snafu'
}).
success
(
function
(
task1
)
{
self
.
Task
.
create
({
title
:
'another task'
}).
success
(
function
(
task2
)
{
user
.
setTask
(
task1
).
success
(
function
()
{
user
.
getTask
().
success
(
function
(
_task
)
{
expect
(
task1
.
title
).
toEqual
(
_task
.
title
)
user
.
setTask
(
task2
).
success
(
function
()
{
user
.
getTask
().
success
(
function
(
_task2
)
{
expect
(
task2
.
title
).
toEqual
(
_task2
.
title
)
done
()
})
})
})
})
})
})
})
})
})
it
(
"sets self associations"
,
function
(
done
)
{
var
Person
=
this
.
sequelize
.
define
(
'Person'
,
{
name
:
Helpers
.
Sequelize
.
STRING
})
Person
.
hasOne
(
Person
,
{
as
:
'Mother'
,
foreignKey
:
'MotherId'
})
Person
.
hasOne
(
Person
,
{
as
:
'Father'
,
foreignKey
:
'FatherId'
})
Person
.
sync
({
force
:
true
}).
success
(
function
()
{
var
p
=
Person
.
build
()
expect
(
p
.
setFather
).
toBeDefined
()
expect
(
p
.
setMother
).
toBeDefined
()
done
()
})
})
it
(
"automatically sets the foreign key on self associations"
,
function
(
done
)
{
var
Person
=
this
.
sequelize
.
define
(
'Person'
,
{
name
:
Helpers
.
Sequelize
.
STRING
})
Person
.
hasOne
(
Person
,
{
as
:
'Mother'
})
expect
(
Person
.
associations
.
MotherPersons
.
options
.
foreignKey
).
toEqual
(
'MotherId'
)
done
()
})
})
describe
(
'getAssocation'
,
function
()
{
describe
(
'getAssocation'
,
function
()
{
it
(
'should be able to handle a where object that\'s a first class citizen.'
,
function
(
done
)
{
it
(
'should be able to handle a where object that\'s a first class citizen.'
,
function
(
done
)
{
var
User
=
this
.
sequelize
.
define
(
'UserXYZ'
,
{
username
:
Sequelize
.
STRING
})
var
User
=
this
.
sequelize
.
define
(
'UserXYZ'
,
{
username
:
Sequelize
.
STRING
})
...
@@ -70,7 +201,6 @@ describe(Helpers.getTestDialectTeaser("HasOne"), function() {
...
@@ -70,7 +201,6 @@ describe(Helpers.getTestDialectTeaser("HasOne"), function() {
})
})
describe
(
"Foreign key constraints"
,
function
()
{
describe
(
"Foreign key constraints"
,
function
()
{
it
(
"are not enabled by default"
,
function
(
done
)
{
it
(
"are not enabled by default"
,
function
(
done
)
{
var
Task
=
this
.
sequelize
.
define
(
'Task'
,
{
title
:
Sequelize
.
STRING
})
var
Task
=
this
.
sequelize
.
define
(
'Task'
,
{
title
:
Sequelize
.
STRING
})
,
User
=
this
.
sequelize
.
define
(
'User'
,
{
username
:
Sequelize
.
STRING
})
,
User
=
this
.
sequelize
.
define
(
'User'
,
{
username
:
Sequelize
.
STRING
})
...
...
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