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 38826cac
authored
Dec 18, 2011
by
sdepold
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
moved specs
1 parent
c6e964cb
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
148 additions
and
123 deletions
spec/associations/has-many.spec.js
spec/associations/has-one.spec.js
test/Model/has-one.js
spec/associations/has-many.spec.js
View file @
38826ca
...
...
@@ -3,7 +3,7 @@ var config = require("../config/config")
,
sequelize
=
new
Sequelize
(
config
.
database
,
config
.
username
,
config
.
password
,
{
logging
:
false
})
,
Helpers
=
new
(
require
(
"../config/helpers"
))(
sequelize
)
describe
(
'
BelongsTo
'
,
function
()
{
describe
(
'
HasMany
'
,
function
()
{
var
User
=
null
,
Task
=
null
,
sequelize
=
null
...
...
spec/associations/has-one.spec.js
0 → 100644
View file @
38826ca
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
(
'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
(
"INT"
)
})
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
(
"INT"
)
})
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
(
"INT"
)
})
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
(
"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
)
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'
)
})
})
test/Model/has-one.js
deleted
100644 → 0
View file @
c6e964c
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
})
User
.
hasOne
(
Task
)
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
},
{
underscored
:
true
})
var
Task
=
sequelize
.
define
(
'Task'
+
num
,
{
title
:
Sequelize
.
STRING
})
User
.
hasOne
(
Task
)
assert
.
eql
(
Task
.
attributes
[
'user'
+
num
+
'_id'
],
"INT"
)
},
'it should correctly add the foreign id when defining the foreignkey as option'
:
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
.
hasOne
(
Task
,
{
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
})
User
.
hasOne
(
Task
)
var
u
=
User
.
build
({
username
:
'asd'
})
assert
.
isDefined
(
u
[
'setTask'
+
num
])
assert
.
isDefined
(
u
[
'getTask'
+
num
])
},
'it should define getter and setter according to as option'
:
function
()
{
var
num
=
config
.
rand
()
var
User
=
sequelize
.
define
(
'User'
+
num
,
{
username
:
Sequelize
.
STRING
})
var
Task
=
sequelize
.
define
(
'Task'
+
num
,
{
title
:
Sequelize
.
STRING
})
User
.
hasOne
(
Task
,
{
as
:
'Task'
})
var
u
=
User
.
build
({
username
:
'asd'
})
assert
.
isDefined
(
u
.
setTask
)
assert
.
isDefined
(
u
.
getTask
)
},
'it should set and get the correct objects'
:
function
(
exit
)
{
var
User
=
sequelize
.
define
(
'User'
+
config
.
rand
(),
{
username
:
Sequelize
.
STRING
})
var
Task
=
sequelize
.
define
(
'Task'
+
config
.
rand
(),
{
title
:
Sequelize
.
STRING
})
User
.
hasOne
(
Task
,
{
as
:
'Task'
})
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
:
'snafu'
}).
on
(
'success'
,
function
(
task
)
{
user
.
setTask
(
task
).
on
(
'success'
,
function
()
{
user
.
getTask
().
on
(
'success'
,
function
(
task2
)
{
assert
.
eql
(
task
.
title
,
task2
.
title
)
exit
(
function
(){})
})
})
})
})
})
})
},
'it should correctly unset the obsolete objects'
:
function
(
exit
)
{
var
User
=
sequelize
.
define
(
'User'
+
config
.
rand
(),
{
username
:
Sequelize
.
STRING
})
var
Task
=
sequelize
.
define
(
'Task'
+
config
.
rand
(),
{
title
:
Sequelize
.
STRING
})
User
.
hasOne
(
Task
,
{
as
:
'Task'
})
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
:
'snafu'
}).
on
(
'success'
,
function
(
task
)
{
Task
.
create
({
title
:
'another task'
}).
on
(
'success'
,
function
(
task2
)
{
user
.
setTask
(
task
).
on
(
'success'
,
function
()
{
user
.
getTask
().
on
(
'success'
,
function
(
_task
)
{
assert
.
eql
(
task
.
title
,
_task
.
title
)
user
.
setTask
(
task2
).
on
(
'success'
,
function
()
{
user
.
getTask
().
on
(
'success'
,
function
(
_task2
)
{
assert
.
eql
(
task2
.
title
,
_task2
.
title
)
exit
(
function
(){})
})
})
})
})
})
})
})
})
})
},
'it should correctly associate with itself'
:
function
(
exit
)
{
var
Person
=
sequelize
.
define
(
'Person'
+
config
.
rand
(),
{
name
:
Sequelize
.
STRING
})
Person
.
hasOne
(
Person
,
{
as
:
'Mother'
,
foreignKey
:
'MotherId'
})
Person
.
hasOne
(
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
.
hasOne
(
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