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 a85bd251
authored
Dec 15, 2011
by
sdepold
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
moved specs
1 parent
a65ce474
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
101 additions
and
128 deletions
spec/associations/belongs-to.spec.js
test/Model/belongs-to.js
spec/associations/belongs-to.spec.js
0 → 100644
View file @
a85bd25
var
config
=
require
(
"../config/config"
)
,
Sequelize
=
require
(
"../../index"
)
,
sequelize
=
new
Sequelize
(
config
.
database
,
config
.
username
,
config
.
password
,
{
logging
:
false
})
,
Helpers
=
new
(
require
(
"../config/helpers"
))(
sequelize
)
describe
(
'BelongsTo'
,
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
()
{
Task
.
belongsTo
(
User
)
expect
(
Task
.
attributes
[
'UserId'
]).
toEqual
(
"INT"
)
})
it
(
"underscores the foreign key"
,
function
()
{
Task
=
sequelize
.
define
(
'Task'
,
{
title
:
Sequelize
.
STRING
},
{
underscored
:
true
})
Task
.
belongsTo
(
User
)
expect
(
Task
.
attributes
[
'user_id'
]).
toEqual
(
"INT"
)
})
it
(
"uses the passed foreign key"
,
function
()
{
Task
.
belongsTo
(
User
,
{
foreignKey
:
'person_id'
})
expect
(
Task
.
attributes
[
'person_id'
]).
toEqual
(
"INT"
)
})
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
(
"intializes the foreign key with null"
,
function
()
{
Task
.
belongsTo
(
User
)
var
task
=
Task
.
build
({
title
:
'asd'
})
expect
(
task
[
'UserId'
]).
toBeNull
()
})
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
(
"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'
)
})
})
test/Model/belongs-to.js
deleted
100644 → 0
View file @
a65ce47
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'
:
function
()
{
var
num
=
config
.
rand
()
var
User
=
sequelize
.
define
(
'User'
+
num
,
{
username
:
Sequelize
.
STRING
})
var
Task
=
sequelize
.
define
(
'Task'
+
num
,
{
title
:
Sequelize
.
STRING
})
Task
.
belongsTo
(
User
)
assert
.
eql
(
Task
.
attributes
[
'User'
+
num
+
'Id'
],
"INT"
)
},
'it should correctly add the foreign id with underscore'
:
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
.
belongsTo
(
User
)
assert
.
eql
(
Task
.
attributes
[
'user'
+
num
+
'_id'
],
"INT"
)
},
'it should correctly add the foreign id if foreignKey is passed'
:
function
()
{
var
User
=
sequelize
.
define
(
'User'
+
config
.
rand
(),
{
username
:
Sequelize
.
STRING
})
var
Task
=
sequelize
.
define
(
'Task'
+
config
.
rand
(),
{
title
:
Sequelize
.
STRING
})
Task
.
belongsTo
(
User
,
{
foreignKey
:
'person_id'
})
assert
.
eql
(
Task
.
attributes
[
'person_id'
],
"INT"
)
},
'it should define getter and setter'
:
function
()
{
var
num
=
config
.
rand
()
var
User
=
sequelize
.
define
(
'User'
+
num
,
{
username
:
Sequelize
.
STRING
})
var
Task
=
sequelize
.
define
(
'Task'
+
num
,
{
title
:
Sequelize
.
STRING
})
Task
.
belongsTo
(
User
)
var
t
=
Task
.
build
({
title
:
'asd'
})
assert
.
isDefined
(
t
[
'setUser'
+
num
])
assert
.
isDefined
(
t
[
'getUser'
+
num
])
},
'it should define getter and setter according to passed as option'
:
function
()
{
var
User
=
sequelize
.
define
(
'User'
+
config
.
rand
(),
{
username
:
Sequelize
.
STRING
})
var
Task
=
sequelize
.
define
(
'Task'
+
config
.
rand
(),
{
title
:
Sequelize
.
STRING
})
Task
.
belongsTo
(
User
,
{
as
:
'Person'
})
var
t
=
Task
.
build
({
title
:
'asd'
})
assert
.
isDefined
(
t
.
setPerson
)
assert
.
isDefined
(
t
.
getPerson
)
},
'it should set the foreign id to null'
:
function
()
{
var
num
=
config
.
rand
()
var
User
=
sequelize
.
define
(
'User'
+
num
,
{
username
:
Sequelize
.
STRING
})
var
Task
=
sequelize
.
define
(
'Task'
+
num
,
{
title
:
Sequelize
.
STRING
})
Task
.
belongsTo
(
User
)
var
t
=
Task
.
build
({
title
:
'asd'
})
assert
.
isNull
(
t
[
'User'
+
num
+
'Id'
])
},
'it should set and get the correct object'
:
function
(
exit
)
{
var
User
=
sequelize
.
define
(
'User'
+
config
.
rand
(),
{
username
:
Sequelize
.
STRING
})
var
Task
=
sequelize
.
define
(
'Task'
+
config
.
rand
(),
{
title
:
Sequelize
.
STRING
})
Task
.
belongsTo
(
User
,
{
as
:
'User'
})
User
.
sync
({
force
:
true
}).
on
(
'success'
,
function
()
{
Task
.
sync
({
force
:
true
}).
on
(
'success'
,
function
()
{
User
.
create
({
username
:
'asd'
}).
on
(
'success'
,
function
(
u
)
{
Task
.
create
({
title
:
'a task'
}).
on
(
'success'
,
function
(
t
)
{
t
.
setUser
(
u
).
on
(
'success'
,
function
()
{
t
.
getUser
().
on
(
'success'
,
function
(
user
)
{
assert
.
eql
(
user
.
username
,
'asd'
)
exit
(
function
(){})
})
})
})
})
})
})
},
'it should correctly delete associations'
:
function
(
exit
)
{
var
User
=
sequelize
.
define
(
'User'
+
config
.
rand
(),
{
username
:
Sequelize
.
STRING
})
var
Task
=
sequelize
.
define
(
'Task'
+
config
.
rand
(),
{
title
:
Sequelize
.
STRING
})
Task
.
belongsTo
(
User
,
{
as
:
'User'
})
User
.
sync
({
force
:
true
}).
on
(
'success'
,
function
()
{
Task
.
sync
({
force
:
true
}).
on
(
'success'
,
function
()
{
User
.
create
({
username
:
'asd'
}).
on
(
'success'
,
function
(
u
)
{
Task
.
create
({
title
:
'a task'
}).
on
(
'success'
,
function
(
t
)
{
t
.
setUser
(
u
).
on
(
'success'
,
function
()
{
t
.
getUser
().
on
(
'success'
,
function
(
user
)
{
assert
.
eql
(
user
.
username
,
'asd'
)
t
.
setUser
(
null
).
on
(
'success'
,
function
()
{
t
.
getUser
().
on
(
'success'
,
function
(
user
)
{
assert
.
isNull
(
user
)
exit
(
function
(){})
})
})
})
})
})
})
})
})
},
'it should correctly handle self associations'
:
function
(
exit
)
{
var
Person
=
sequelize
.
define
(
'Person'
+
config
.
rand
(),
{
name
:
Sequelize
.
STRING
})
Person
.
belongsTo
(
Person
,
{
as
:
'Mother'
,
foreignKey
:
'MotherId'
})
Person
.
belongsTo
(
Person
,
{
as
:
'Father'
,
foreignKey
:
'FatherId'
})
Person
.
sync
({
force
:
true
}).
on
(
'success'
,
function
()
{
var
p
=
Person
.
build
()
assert
.
isDefined
(
p
.
setFather
)
assert
.
isDefined
(
p
.
setMother
)
exit
(
function
(){})
})
},
'it should automatically set the foreignKey if it is a self association'
:
function
()
{
var
num
=
config
.
rand
()
var
Person
=
sequelize
.
define
(
'Person'
+
num
,
{
name
:
Sequelize
.
STRING
})
Person
.
belongsTo
(
Person
,
{
as
:
'Mother'
})
assert
.
eql
(
Person
.
associations
[
"MotherPerson"
+
num
+
"s"
].
options
.
foreignKey
,
'MotherId'
)
}
}
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