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 76d386e1
authored
Apr 02, 2015
by
Ruben Bridgewater
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Refactor all find tests to use promises
1 parent
fd89ee73
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
185 additions
and
265 deletions
test/integration/model/find.test.js
test/integration/model/find.test.js
View file @
76d386e
...
...
@@ -19,7 +19,7 @@ chai.use(datetime);
chai
.
config
.
includeStack
=
true
;
describe
(
Support
.
getTestDialectTeaser
(
'Model'
),
function
()
{
beforeEach
(
function
(
done
)
{
beforeEach
(
function
()
{
this
.
User
=
this
.
sequelize
.
define
(
'User'
,
{
username
:
DataTypes
.
STRING
,
secretValue
:
DataTypes
.
STRING
,
...
...
@@ -29,32 +29,27 @@ describe(Support.getTestDialectTeaser('Model'), function() {
aBool
:
DataTypes
.
BOOLEAN
});
this
.
User
.
sync
({
force
:
true
}).
success
(
function
()
{
done
();
});
return
this
.
User
.
sync
({
force
:
true
});
});
describe
(
'find'
,
function
()
{
if
(
current
.
dialect
.
supports
.
transactions
)
{
it
(
'supports transactions'
,
function
(
done
)
{
Support
.
prepareTransactionTest
(
this
.
sequelize
,
function
(
sequelize
)
{
it
(
'supports transactions'
,
function
()
{
return
Support
.
prepareTransactionTest
(
this
.
sequelize
).
bind
({}).
then
(
function
(
sequelize
)
{
var
User
=
sequelize
.
define
(
'User'
,
{
username
:
Sequelize
.
STRING
});
User
.
sync
({
force
:
true
}).
success
(
function
()
{
sequelize
.
transaction
().
then
(
function
(
t
)
{
User
.
create
({
username
:
'foo'
},
{
transaction
:
t
}).
success
(
function
()
{
User
.
find
({
return
User
.
sync
({
force
:
true
}).
then
(
function
()
{
return
sequelize
.
transaction
().
then
(
function
(
t
)
{
return
User
.
create
({
username
:
'foo'
},
{
transaction
:
t
}).
then
(
function
()
{
return
User
.
find
({
where
:
{
username
:
'foo'
}
}).
success
(
function
(
user1
)
{
User
.
find
({
}).
then
(
function
(
user1
)
{
return
User
.
find
({
where
:
{
username
:
'foo'
}
},
{
transaction
:
t
}).
success
(
function
(
user2
)
{
},
{
transaction
:
t
}).
then
(
function
(
user2
)
{
expect
(
user1
).
to
.
be
.
null
;
expect
(
user2
).
to
.
not
.
be
.
null
;
t
.
rollback
().
success
(
function
()
{
done
();
});
return
t
.
rollback
();
});
});
});
...
...
@@ -65,9 +60,9 @@ describe(Support.getTestDialectTeaser('Model'), function() {
}
describe
(
'general / basic function'
,
function
()
{
beforeEach
(
function
(
done
)
{
beforeEach
(
function
()
{
var
self
=
this
;
this
.
User
.
create
({
username
:
'barfooz'
}).
success
(
function
(
user
)
{
return
this
.
User
.
create
({
username
:
'barfooz'
}).
then
(
function
(
user
)
{
self
.
UserPrimary
=
self
.
sequelize
.
define
(
'UserPrimary'
,
{
specialkey
:
{
type
:
DataTypes
.
STRING
,
...
...
@@ -75,10 +70,9 @@ describe(Support.getTestDialectTeaser('Model'), function() {
}
});
self
.
UserPrimary
.
sync
({
force
:
true
}).
success
(
function
()
{
self
.
UserPrimary
.
create
({
specialkey
:
'a string'
}).
success
(
function
()
{
return
self
.
UserPrimary
.
sync
({
force
:
true
}).
then
(
function
()
{
return
self
.
UserPrimary
.
create
({
specialkey
:
'a string'
}).
then
(
function
()
{
self
.
user
=
user
;
done
();
});
});
});
...
...
@@ -118,159 +112,144 @@ describe(Support.getTestDialectTeaser('Model'), function() {
});
}
it
(
'does not modify the passed arguments'
,
function
(
done
)
{
it
(
'does not modify the passed arguments'
,
function
()
{
var
options
=
{
where
:
[
'specialkey = ?'
,
'awesome'
]};
this
.
UserPrimary
.
find
(
options
).
success
(
function
()
{
return
this
.
UserPrimary
.
find
(
options
).
then
(
function
()
{
expect
(
options
).
to
.
deep
.
equal
({
where
:
[
'specialkey = ?'
,
'awesome'
]});
done
();
});
});
it
(
'treats questionmarks in an array'
,
function
(
done
)
{
this
.
UserPrimary
.
find
({
it
(
'treats questionmarks in an array'
,
function
()
{
return
this
.
UserPrimary
.
find
({
where
:
[
'specialkey = ?'
,
'awesome'
]
}).
on
(
'sql'
,
function
(
sql
)
{
expect
(
sql
).
to
.
contain
(
"WHERE specialkey = 'awesome'"
);
done
();
});
});
it
(
'doesn\'t throw an error when entering in a non integer value for a specified primary field'
,
function
(
done
)
{
this
.
UserPrimary
.
find
(
'a string'
).
success
(
function
(
user
)
{
it
(
'doesn\'t throw an error when entering in a non integer value for a specified primary field'
,
function
()
{
return
this
.
UserPrimary
.
find
(
'a string'
).
then
(
function
(
user
)
{
expect
(
user
.
specialkey
).
to
.
equal
(
'a string'
);
done
();
});
});
it
.
skip
(
'doesn\'t throw an error when entering in a non integer value'
,
function
(
done
)
{
this
.
User
.
find
(
'a string value'
).
success
(
function
(
user
)
{
it
.
skip
(
'doesn\'t throw an error when entering in a non integer value'
,
function
()
{
return
this
.
User
.
find
(
'a string value'
).
then
(
function
(
user
)
{
expect
(
user
).
to
.
be
.
null
;
done
();
});
});
it
(
'returns a single dao'
,
function
(
done
)
{
it
(
'returns a single dao'
,
function
()
{
var
self
=
this
;
this
.
User
.
find
(
this
.
user
.
id
).
success
(
function
(
user
)
{
return
this
.
User
.
find
(
this
.
user
.
id
).
then
(
function
(
user
)
{
expect
(
Array
.
isArray
(
user
)).
to
.
not
.
be
.
ok
;
expect
(
user
.
id
).
to
.
equal
(
self
.
user
.
id
);
expect
(
user
.
id
).
to
.
equal
(
1
);
done
();
});
});
it
(
'returns a single dao given a string id'
,
function
(
done
)
{
it
(
'returns a single dao given a string id'
,
function
()
{
var
self
=
this
;
this
.
User
.
find
(
this
.
user
.
id
+
''
).
success
(
function
(
user
)
{
return
this
.
User
.
find
(
this
.
user
.
id
+
''
).
then
(
function
(
user
)
{
expect
(
Array
.
isArray
(
user
)).
to
.
not
.
be
.
ok
;
expect
(
user
.
id
).
to
.
equal
(
self
.
user
.
id
);
expect
(
user
.
id
).
to
.
equal
(
1
);
done
();
});
});
it
(
'should make aliased attributes available'
,
function
(
done
)
{
this
.
User
.
find
({
it
(
'should make aliased attributes available'
,
function
()
{
return
this
.
User
.
find
({
where
:
{
id
:
1
},
attributes
:
[
'id'
,
[
'username'
,
'name'
]]
}).
success
(
function
(
user
)
{
}).
then
(
function
(
user
)
{
expect
(
user
.
dataValues
.
name
).
to
.
equal
(
'barfooz'
);
done
();
});
});
it
(
'should not try to convert boolean values if they are not selected'
,
function
(
done
)
{
it
(
'should not try to convert boolean values if they are not selected'
,
function
()
{
var
UserWithBoolean
=
this
.
sequelize
.
define
(
'UserBoolean'
,
{
active
:
Sequelize
.
BOOLEAN
});
UserWithBoolean
.
sync
({
force
:
true
}).
success
(
function
()
{
UserWithBoolean
.
create
({
active
:
true
}).
success
(
function
(
user
)
{
UserWithBoolean
.
find
({
where
:
{
id
:
user
.
id
},
attributes
:
[
'id'
]
}).
success
(
function
(
user
)
{
return
UserWithBoolean
.
sync
({
force
:
true
}).
then
(
function
()
{
return
UserWithBoolean
.
create
({
active
:
true
}).
then
(
function
(
user
)
{
return
UserWithBoolean
.
find
({
where
:
{
id
:
user
.
id
},
attributes
:
[
'id'
]
}).
then
(
function
(
user
)
{
expect
(
user
.
active
).
not
.
to
.
exist
;
done
();
});
});
});
});
it
(
'finds a specific user via where option'
,
function
(
done
)
{
this
.
User
.
find
({
where
:
{
username
:
'barfooz'
}
}).
success
(
function
(
user
)
{
it
(
'finds a specific user via where option'
,
function
()
{
return
this
.
User
.
find
({
where
:
{
username
:
'barfooz'
}
}).
then
(
function
(
user
)
{
expect
(
user
.
username
).
to
.
equal
(
'barfooz'
);
done
();
});
});
it
(
"doesn't find a user if conditions are not matching"
,
function
(
done
)
{
this
.
User
.
find
({
where
:
{
username
:
'foo'
}
}).
success
(
function
(
user
)
{
it
(
"doesn't find a user if conditions are not matching"
,
function
()
{
return
this
.
User
.
find
({
where
:
{
username
:
'foo'
}
}).
then
(
function
(
user
)
{
expect
(
user
).
to
.
be
.
null
;
done
();
});
});
it
(
'allows sql logging'
,
function
(
done
)
{
this
.
User
.
find
({
where
:
{
username
:
'foo'
}
}).
on
(
'sql'
,
function
(
sql
)
{
it
(
'allows sql logging'
,
function
()
{
return
this
.
User
.
find
({
where
:
{
username
:
'foo'
}
}).
on
(
'sql'
,
function
(
sql
)
{
expect
(
sql
).
to
.
exist
;
expect
(
sql
.
toUpperCase
().
indexOf
(
'SELECT'
)).
to
.
be
.
above
(
-
1
);
done
();
});
});
it
(
'ignores passed limit option'
,
function
(
done
)
{
this
.
User
.
find
({
limit
:
10
}).
success
(
function
(
user
)
{
it
(
'ignores passed limit option'
,
function
()
{
return
this
.
User
.
find
({
limit
:
10
}).
then
(
function
(
user
)
{
// it returns an object instead of an array
expect
(
Array
.
isArray
(
user
)).
to
.
not
.
be
.
ok
;
expect
(
user
.
dataValues
.
hasOwnProperty
(
'username'
)).
to
.
be
.
ok
;
done
();
});
});
it
(
'finds entries via primary keys'
,
function
(
done
)
{
it
(
'finds entries via primary keys'
,
function
()
{
var
self
=
this
,
UserPrimary
=
self
.
sequelize
.
define
(
'UserWithPrimaryKey'
,
{
identifier
:
{
type
:
Sequelize
.
STRING
,
primaryKey
:
true
},
name
:
Sequelize
.
STRING
});
UserPrimary
.
sync
({
force
:
true
}).
success
(
function
()
{
UserPrimary
.
create
({
return
UserPrimary
.
sync
({
force
:
true
}).
then
(
function
()
{
return
UserPrimary
.
create
({
identifier
:
'an identifier'
,
name
:
'John'
}).
success
(
function
(
u
)
{
}).
then
(
function
(
u
)
{
expect
(
u
.
id
).
not
.
to
.
exist
;
UserPrimary
.
find
(
'an identifier'
).
success
(
function
(
u2
)
{
return
UserPrimary
.
find
(
'an identifier'
).
then
(
function
(
u2
)
{
expect
(
u2
.
identifier
).
to
.
equal
(
'an identifier'
);
expect
(
u2
.
name
).
to
.
equal
(
'John'
);
done
();
});
});
});
});
it
(
'finds entries via a string primary key called id'
,
function
(
done
)
{
it
(
'finds entries via a string primary key called id'
,
function
()
{
var
self
=
this
,
UserPrimary
=
self
.
sequelize
.
define
(
'UserWithPrimaryKey'
,
{
id
:
{
type
:
Sequelize
.
STRING
,
primaryKey
:
true
},
name
:
Sequelize
.
STRING
});
UserPrimary
.
sync
({
force
:
true
}).
success
(
function
()
{
UserPrimary
.
create
({
return
UserPrimary
.
sync
({
force
:
true
}).
then
(
function
()
{
return
UserPrimary
.
create
({
id
:
'a string based id'
,
name
:
'Johnno'
}).
success
(
function
()
{
UserPrimary
.
find
(
'a string based id'
).
success
(
function
(
u2
)
{
}).
then
(
function
()
{
return
UserPrimary
.
find
(
'a string based id'
).
then
(
function
(
u2
)
{
expect
(
u2
.
id
).
to
.
equal
(
'a string based id'
);
expect
(
u2
.
name
).
to
.
equal
(
'Johnno'
);
done
();
});
});
});
});
it
(
'always honors ZERO as primary key'
,
function
(
_done
)
{
it
(
'always honors ZERO as primary key'
,
function
()
{
var
self
=
this
,
permutations
=
[
0
,
...
...
@@ -278,34 +257,34 @@ describe(Support.getTestDialectTeaser('Model'), function() {
{
where
:
{
id
:
0
}},
{
where
:
{
id
:
'0'
}}
]
,
done
=
_
.
after
(
2
*
permutations
.
length
,
_done
)
;
,
count
=
0
;
this
.
User
.
bulkCreate
([{
username
:
'jack'
},
{
username
:
'jack'
}]).
success
(
function
()
{
permutations
.
forEach
(
function
(
perm
)
{
self
.
User
.
find
(
perm
).
done
(
function
(
err
,
user
)
{
expect
(
err
).
to
.
be
.
null
;
return
this
.
User
.
bulkCreate
([{
username
:
'jack'
},
{
username
:
'jack'
}]).
then
(
function
()
{
return
this
.
sequelize
.
Promise
.
map
(
permutations
,
function
(
perm
)
{
return
self
.
User
.
find
(
perm
).
then
(
function
(
user
)
{
expect
(
user
).
to
.
be
.
null
;
done
()
;
count
++
;
}).
on
(
'sql'
,
function
(
s
)
{
expect
(
s
.
indexOf
(
0
)).
not
.
to
.
equal
(
-
1
);
done
()
;
count
++
;
});
});
}).
then
(
function
()
{
expect
(
count
).
to
.
be
.
equal
(
2
*
permutations
.
length
);
});
});
it
(
'should allow us to find IDs using capital letters'
,
function
(
done
)
{
it
(
'should allow us to find IDs using capital letters'
,
function
()
{
var
User
=
this
.
sequelize
.
define
(
'User'
+
config
.
rand
(),
{
ID
:
{
type
:
Sequelize
.
INTEGER
,
primaryKey
:
true
,
autoIncrement
:
true
},
Login
:
{
type
:
Sequelize
.
STRING
}
});
User
.
sync
({
force
:
true
}).
success
(
function
()
{
User
.
create
({
Login
:
'foo'
}).
success
(
function
()
{
User
.
find
(
1
).
success
(
function
(
user
)
{
return
User
.
sync
({
force
:
true
}).
then
(
function
()
{
return
User
.
create
({
Login
:
'foo'
}).
then
(
function
()
{
return
User
.
find
(
1
).
then
(
function
(
user
)
{
expect
(
user
).
to
.
exist
;
expect
(
user
.
ID
).
to
.
equal
(
1
);
done
();
});
});
});
...
...
@@ -313,91 +292,84 @@ describe(Support.getTestDialectTeaser('Model'), function() {
});
describe
(
'eager loading'
,
function
()
{
beforeEach
(
function
(
done
)
{
beforeEach
(
function
()
{
var
self
=
this
;
self
.
Task
=
self
.
sequelize
.
define
(
'Task'
,
{
title
:
Sequelize
.
STRING
});
self
.
Worker
=
self
.
sequelize
.
define
(
'Worker'
,
{
name
:
Sequelize
.
STRING
});
this
.
init
=
function
(
callback
)
{
self
.
sequelize
.
sync
({
force
:
true
}).
success
(
function
()
{
self
.
Worker
.
create
({
name
:
'worker'
}).
success
(
function
(
worker
)
{
self
.
Task
.
create
({
title
:
'homework'
}).
success
(
function
(
task
)
{
return
this
.
init
=
function
(
callback
)
{
return
self
.
sequelize
.
sync
({
force
:
true
}).
then
(
function
()
{
return
self
.
Worker
.
create
({
name
:
'worker'
}).
then
(
function
(
worker
)
{
return
self
.
Task
.
create
({
title
:
'homework'
}).
then
(
function
(
task
)
{
self
.
worker
=
worker
;
self
.
task
=
task
;
callback
();
return
callback
();
});
});
});
};
done
();
});
describe
(
'belongsTo'
,
function
()
{
describe
(
'generic'
,
function
()
{
it
(
'throws an error about unexpected input if include contains a non-object'
,
function
(
done
)
{
it
(
'throws an error about unexpected input if include contains a non-object'
,
function
()
{
var
self
=
this
;
self
.
Worker
.
find
({
include
:
[
1
]
}).
catch
(
function
(
err
)
{
return
self
.
Worker
.
find
({
include
:
[
1
]
}).
catch
(
function
(
err
)
{
expect
(
err
.
message
).
to
.
equal
(
'Include unexpected. Element has to be either a Model, an Association or an object.'
);
done
();
});
});
it
(
'throws an error if included DaoFactory is not associated'
,
function
(
done
)
{
it
(
'throws an error if included DaoFactory is not associated'
,
function
()
{
var
self
=
this
;
self
.
Worker
.
find
({
include
:
[
self
.
Task
]
}).
catch
(
function
(
err
)
{
return
self
.
Worker
.
find
({
include
:
[
self
.
Task
]
}).
catch
(
function
(
err
)
{
expect
(
err
.
message
).
to
.
equal
(
'Task is not associated to Worker!'
);
done
();
});
});
it
(
'returns the associated worker via task.worker'
,
function
(
done
)
{
it
(
'returns the associated worker via task.worker'
,
function
()
{
var
self
=
this
;
this
.
Task
.
belongsTo
(
this
.
Worker
);
this
.
init
(
function
()
{
self
.
task
.
setWorker
(
self
.
worker
).
success
(
function
()
{
self
.
Task
.
find
({
return
this
.
init
(
function
()
{
return
self
.
task
.
setWorker
(
self
.
worker
).
then
(
function
()
{
return
self
.
Task
.
find
({
where
:
{
title
:
'homework'
},
include
:
[
self
.
Worker
]
}).
complete
(
function
(
err
,
task
)
{
expect
(
err
).
to
.
be
.
null
;
}).
then
(
function
(
task
)
{
expect
(
task
).
to
.
exist
;
expect
(
task
.
Worker
).
to
.
exist
;
expect
(
task
.
Worker
.
name
).
to
.
equal
(
'worker'
);
done
();
});
});
});
});
});
it
(
'returns the private and public ip'
,
function
(
done
)
{
it
(
'returns the private and public ip'
,
function
()
{
var
self
=
Object
.
create
(
this
);
self
.
Domain
=
self
.
sequelize
.
define
(
'Domain'
,
{
ip
:
Sequelize
.
STRING
});
self
.
Environment
=
self
.
sequelize
.
define
(
'Environment'
,
{
name
:
Sequelize
.
STRING
});
self
.
Environment
.
belongsTo
(
self
.
Domain
,
{
as
:
'PrivateDomain'
,
foreignKey
:
'privateDomainId'
});
self
.
Environment
.
belongsTo
(
self
.
Domain
,
{
as
:
'PublicDomain'
,
foreignKey
:
'publicDomainId'
});
self
.
Domain
.
sync
({
force
:
true
}).
success
(
function
()
{
self
.
Environment
.
sync
({
force
:
true
}).
success
(
function
()
{
self
.
Domain
.
create
({
ip
:
'192.168.0.1'
}).
success
(
function
(
privateIp
)
{
self
.
Domain
.
create
({
ip
:
'91.65.189.19'
}).
success
(
function
(
publicIp
)
{
self
.
Environment
.
create
({
name
:
'environment'
}).
success
(
function
(
env
)
{
env
.
setPrivateDomain
(
privateIp
).
success
(
function
()
{
env
.
setPublicDomain
(
publicIp
).
success
(
function
()
{
self
.
Environment
.
find
({
return
self
.
Domain
.
sync
({
force
:
true
}).
then
(
function
()
{
return
self
.
Environment
.
sync
({
force
:
true
}).
then
(
function
()
{
return
self
.
Domain
.
create
({
ip
:
'192.168.0.1'
}).
then
(
function
(
privateIp
)
{
return
self
.
Domain
.
create
({
ip
:
'91.65.189.19'
}).
then
(
function
(
publicIp
)
{
return
self
.
Environment
.
create
({
name
:
'environment'
}).
then
(
function
(
env
)
{
return
env
.
setPrivateDomain
(
privateIp
).
then
(
function
()
{
return
env
.
setPublicDomain
(
publicIp
).
then
(
function
()
{
return
self
.
Environment
.
find
({
where
:
{
name
:
'environment'
},
include
:
[
{
daoFactory
:
self
.
Domain
,
as
:
'PrivateDomain'
},
{
daoFactory
:
self
.
Domain
,
as
:
'PublicDomain'
}
]
}).
complete
(
function
(
err
,
environment
)
{
expect
(
err
).
to
.
be
.
null
;
}).
then
(
function
(
environment
)
{
expect
(
environment
).
to
.
exist
;
expect
(
environment
.
PrivateDomain
).
to
.
exist
;
expect
(
environment
.
PrivateDomain
.
ip
).
to
.
equal
(
'192.168.0.1'
);
expect
(
environment
.
PublicDomain
).
to
.
exist
;
expect
(
environment
.
PublicDomain
.
ip
).
to
.
equal
(
'91.65.189.19'
);
done
();
});
});
});
...
...
@@ -408,7 +380,7 @@ describe(Support.getTestDialectTeaser('Model'), function() {
});
});
it
(
'eager loads with non-id primary keys'
,
function
(
done
)
{
it
(
'eager loads with non-id primary keys'
,
function
()
{
var
self
=
this
;
self
.
User
=
self
.
sequelize
.
define
(
'UserPKeagerbelong'
,
{
username
:
{
...
...
@@ -424,27 +396,25 @@ describe(Support.getTestDialectTeaser('Model'), function() {
});
self
.
User
.
belongsTo
(
self
.
Group
);
self
.
sequelize
.
sync
({
force
:
true
}).
success
(
function
()
{
self
.
Group
.
create
({
name
:
'people'
}).
success
(
function
()
{
self
.
User
.
create
({
username
:
'someone'
,
GroupPKeagerbelongName
:
'people'
}).
success
(
function
()
{
self
.
User
.
find
({
return
self
.
sequelize
.
sync
({
force
:
true
}).
then
(
function
()
{
return
self
.
Group
.
create
({
name
:
'people'
}).
then
(
function
()
{
return
self
.
User
.
create
({
username
:
'someone'
,
GroupPKeagerbelongName
:
'people'
}).
then
(
function
()
{
return
self
.
User
.
find
({
where
:
{
username
:
'someone'
},
include
:
[
self
.
Group
]
}).
complete
(
function
(
err
,
someUser
)
{
expect
(
err
).
to
.
be
.
null
;
}).
then
(
function
(
someUser
)
{
expect
(
someUser
).
to
.
exist
;
expect
(
someUser
.
username
).
to
.
equal
(
'someone'
);
expect
(
someUser
.
GroupPKeagerbelong
.
name
).
to
.
equal
(
'people'
);
done
();
});
});
});
});
});
it
(
'getting parent data in many to one relationship'
,
function
(
done
)
{
it
(
'getting parent data in many to one relationship'
,
function
()
{
var
User
=
this
.
sequelize
.
define
(
'User'
,
{
id
:
{
type
:
Sequelize
.
INTEGER
,
autoIncrement
:
true
,
primaryKey
:
true
},
username
:
{
type
:
Sequelize
.
STRING
}
...
...
@@ -459,20 +429,18 @@ describe(Support.getTestDialectTeaser('Model'), function() {
User
.
hasMany
(
Message
);
Message
.
belongsTo
(
User
,
{
foreignKey
:
'user_id'
});
this
.
sequelize
.
sync
({
force
:
true
}).
success
(
function
()
{
User
.
create
({
username
:
'test_testerson'
}).
success
(
function
(
user
)
{
Message
.
create
({
user_id
:
user
.
id
,
message
:
'hi there!'
}).
success
(
function
()
{
Message
.
create
({
user_id
:
user
.
id
,
message
:
'a second message'
}).
success
(
function
()
{
Message
.
findAll
({
return
this
.
sequelize
.
sync
({
force
:
true
}).
then
(
function
()
{
return
User
.
create
({
username
:
'test_testerson'
}).
then
(
function
(
user
)
{
return
Message
.
create
({
user_id
:
user
.
id
,
message
:
'hi there!'
}).
then
(
function
()
{
return
Message
.
create
({
user_id
:
user
.
id
,
message
:
'a second message'
}).
then
(
function
()
{
return
Message
.
findAll
({
where
:
{
user_id
:
user
.
id
},
attributes
:
[
'user_id'
,
'message'
],
include
:
[{
model
:
User
,
attributes
:
[
'username'
]
}]
}).
success
(
function
(
messages
)
{
}).
then
(
function
(
messages
)
{
expect
(
messages
.
length
).
to
.
equal
(
2
);
expect
(
messages
[
0
].
message
).
to
.
equal
(
'hi there!'
);
...
...
@@ -480,9 +448,6 @@ describe(Support.getTestDialectTeaser('Model'), function() {
expect
(
messages
[
1
].
message
).
to
.
equal
(
'a second message'
);
expect
(
messages
[
1
].
User
.
username
).
to
.
equal
(
'test_testerson'
);
done
();
});
});
});
...
...
@@ -490,58 +455,50 @@ describe(Support.getTestDialectTeaser('Model'), function() {
});
});
it
(
'allows mulitple assocations of the same model with different alias'
,
function
(
done
)
{
it
(
'allows mulitple assocations of the same model with different alias'
,
function
()
{
var
self
=
this
;
this
.
Worker
.
belongsTo
(
this
.
Task
,
{
as
:
'ToDo'
});
this
.
Worker
.
belongsTo
(
this
.
Task
,
{
as
:
'DoTo'
});
this
.
init
(
function
()
{
self
.
Worker
.
find
({
return
this
.
init
(
function
()
{
return
self
.
Worker
.
find
({
include
:
[
{
model
:
self
.
Task
,
as
:
'ToDo'
},
{
model
:
self
.
Task
,
as
:
'DoTo'
}
]
}).
success
(
function
()
{
// Just being able to include both shows that this test works, so no assertions needed
done
();
});
});
});
});
describe
(
'hasOne'
,
function
()
{
beforeEach
(
function
(
done
)
{
beforeEach
(
function
()
{
var
self
=
this
;
this
.
Worker
.
hasOne
(
this
.
Task
);
this
.
init
(
function
()
{
self
.
worker
.
setTask
(
self
.
task
).
success
(
function
()
{
done
();
});
return
this
.
init
(
function
()
{
return
self
.
worker
.
setTask
(
self
.
task
);
});
});
it
(
'throws an error if included DaoFactory is not associated'
,
function
(
done
)
{
it
(
'throws an error if included DaoFactory is not associated'
,
function
()
{
var
self
=
this
;
self
.
Task
.
find
({
include
:
[
self
.
Worker
]
}).
catch
(
function
(
err
)
{
return
self
.
Task
.
find
({
include
:
[
self
.
Worker
]
}).
catch
(
function
(
err
)
{
expect
(
err
.
message
).
to
.
equal
(
'Worker is not associated to Task!'
);
done
();
});
});
it
(
'returns the associated task via worker.task'
,
function
(
done
)
{
this
.
Worker
.
find
({
it
(
'returns the associated task via worker.task'
,
function
()
{
return
this
.
Worker
.
find
({
where
:
{
name
:
'worker'
},
include
:
[
this
.
Task
]
}).
complete
(
function
(
err
,
worker
)
{
expect
(
err
).
to
.
be
.
null
;
}).
then
(
function
(
worker
)
{
expect
(
worker
).
to
.
exist
;
expect
(
worker
.
Task
).
to
.
exist
;
expect
(
worker
.
Task
.
title
).
to
.
equal
(
'homework'
);
done
();
});
});
it
(
'eager loads with non-id primary keys'
,
function
(
done
)
{
it
(
'eager loads with non-id primary keys'
,
function
()
{
var
self
=
this
;
self
.
User
=
self
.
sequelize
.
define
(
'UserPKeagerone'
,
{
username
:
{
...
...
@@ -557,20 +514,18 @@ describe(Support.getTestDialectTeaser('Model'), function() {
});
self
.
Group
.
hasOne
(
self
.
User
);
self
.
sequelize
.
sync
({
force
:
true
}).
success
(
function
()
{
self
.
Group
.
create
({
name
:
'people'
}).
success
(
function
()
{
self
.
User
.
create
({
username
:
'someone'
,
GroupPKeageroneName
:
'people'
}).
success
(
function
()
{
self
.
Group
.
find
({
return
self
.
sequelize
.
sync
({
force
:
true
}).
then
(
function
()
{
return
self
.
Group
.
create
({
name
:
'people'
}).
then
(
function
()
{
return
self
.
User
.
create
({
username
:
'someone'
,
GroupPKeageroneName
:
'people'
}).
then
(
function
()
{
return
self
.
Group
.
find
({
where
:
{
name
:
'people'
},
include
:
[
self
.
User
]
}).
complete
(
function
(
err
,
someGroup
)
{
expect
(
err
).
to
.
be
.
null
;
}).
then
(
function
(
someGroup
)
{
expect
(
someGroup
).
to
.
exist
;
expect
(
someGroup
.
name
).
to
.
equal
(
'people'
);
expect
(
someGroup
.
UserPKeagerone
.
username
).
to
.
equal
(
'someone'
);
done
();
});
});
});
...
...
@@ -579,69 +534,59 @@ describe(Support.getTestDialectTeaser('Model'), function() {
});
describe
(
'hasOne with alias'
,
function
()
{
it
(
'throws an error if included DaoFactory is not referenced by alias'
,
function
(
done
)
{
it
(
'throws an error if included DaoFactory is not referenced by alias'
,
function
()
{
var
self
=
this
;
self
.
Worker
.
find
({
include
:
[
self
.
Task
]
}).
catch
(
function
(
err
)
{
return
self
.
Worker
.
find
({
include
:
[
self
.
Task
]
}).
catch
(
function
(
err
)
{
expect
(
err
.
message
).
to
.
equal
(
'Task is not associated to Worker!'
);
done
();
});
});
describe
(
'alias'
,
function
()
{
beforeEach
(
function
(
done
)
{
beforeEach
(
function
()
{
var
self
=
this
;
this
.
Worker
.
hasOne
(
this
.
Task
,
{
as
:
'ToDo'
});
this
.
init
(
function
()
{
self
.
worker
.
setToDo
(
self
.
task
).
success
(
function
()
{
done
();
});
return
this
.
init
(
function
()
{
return
self
.
worker
.
setToDo
(
self
.
task
);
});
});
it
(
'throws an error if alias is not associated'
,
function
(
done
)
{
it
(
'throws an error if alias is not associated'
,
function
()
{
var
self
=
this
;
self
.
Worker
.
find
({
include
:
[{
daoFactory
:
self
.
Task
,
as
:
'Work'
}]
}).
catch
(
function
(
err
)
{
return
self
.
Worker
.
find
({
include
:
[{
daoFactory
:
self
.
Task
,
as
:
'Work'
}]
}).
catch
(
function
(
err
)
{
expect
(
err
.
message
).
to
.
equal
(
'Task (Work) is not associated to Worker!'
);
done
();
});
});
it
(
'returns the associated task via worker.task'
,
function
(
done
)
{
this
.
Worker
.
find
({
it
(
'returns the associated task via worker.task'
,
function
()
{
return
this
.
Worker
.
find
({
where
:
{
name
:
'worker'
},
include
:
[{
daoFactory
:
this
.
Task
,
as
:
'ToDo'
}]
}).
complete
(
function
(
err
,
worker
)
{
expect
(
err
).
to
.
be
.
null
;
}).
then
(
function
(
worker
)
{
expect
(
worker
).
to
.
exist
;
expect
(
worker
.
ToDo
).
to
.
exist
;
expect
(
worker
.
ToDo
.
title
).
to
.
equal
(
'homework'
);
done
();
});
});
it
(
'returns the associated task via worker.task when daoFactory is aliased with model'
,
function
(
done
)
{
this
.
Worker
.
find
({
it
(
'returns the associated task via worker.task when daoFactory is aliased with model'
,
function
()
{
return
this
.
Worker
.
find
({
where
:
{
name
:
'worker'
},
include
:
[{
model
:
this
.
Task
,
as
:
'ToDo'
}]
}).
complete
(
function
(
err
,
worker
)
{
}).
then
(
function
(
worker
)
{
expect
(
worker
.
ToDo
.
title
).
to
.
equal
(
'homework'
);
done
();
});
});
it
(
'allows mulitple assocations of the same model with different alias'
,
function
(
done
)
{
it
(
'allows mulitple assocations of the same model with different alias'
,
function
()
{
var
self
=
this
;
this
.
Worker
.
hasOne
(
this
.
Task
,
{
as
:
'DoTo'
});
this
.
init
(
function
()
{
self
.
Worker
.
find
({
return
this
.
init
(
function
()
{
return
self
.
Worker
.
find
({
include
:
[
{
model
:
self
.
Task
,
as
:
'ToDo'
},
{
model
:
self
.
Task
,
as
:
'DoTo'
}
]
}).
success
(
function
()
{
// Just being able to include both shows that this test works, so no assertions needed
done
();
});
});
});
...
...
@@ -649,38 +594,33 @@ describe(Support.getTestDialectTeaser('Model'), function() {
});
describe
(
'hasMany'
,
function
()
{
beforeEach
(
function
(
done
)
{
beforeEach
(
function
()
{
var
self
=
this
;
this
.
Worker
.
hasMany
(
this
.
Task
);
this
.
init
(
function
()
{
self
.
worker
.
setTasks
([
self
.
task
]).
success
(
function
()
{
done
();
});
return
this
.
init
(
function
()
{
return
self
.
worker
.
setTasks
([
self
.
task
]);
});
});
it
(
'throws an error if included DaoFactory is not associated'
,
function
(
done
)
{
it
(
'throws an error if included DaoFactory is not associated'
,
function
()
{
var
self
=
this
;
self
.
Task
.
find
({
include
:
[
self
.
Worker
]
}).
catch
(
function
(
err
)
{
return
self
.
Task
.
find
({
include
:
[
self
.
Worker
]
}).
catch
(
function
(
err
)
{
expect
(
err
.
message
).
to
.
equal
(
'Worker is not associated to Task!'
);
done
();
});
});
it
(
'returns the associated tasks via worker.tasks'
,
function
(
done
)
{
this
.
Worker
.
find
({
it
(
'returns the associated tasks via worker.tasks'
,
function
()
{
return
this
.
Worker
.
find
({
where
:
{
name
:
'worker'
},
include
:
[
this
.
Task
]
}).
complete
(
function
(
err
,
worker
)
{
expect
(
err
).
to
.
be
.
null
;
}).
then
(
function
(
worker
)
{
expect
(
worker
).
to
.
exist
;
expect
(
worker
.
Tasks
).
to
.
exist
;
expect
(
worker
.
Tasks
[
0
].
title
).
to
.
equal
(
'homework'
);
done
();
});
});
it
(
'including two has many relations should not result in duplicate values'
,
function
(
done
)
{
it
(
'including two has many relations should not result in duplicate values'
,
function
()
{
var
self
=
this
;
self
.
Contact
=
self
.
sequelize
.
define
(
'Contact'
,
{
name
:
DataTypes
.
STRING
});
...
...
@@ -690,25 +630,22 @@ describe(Support.getTestDialectTeaser('Model'), function() {
self
.
Contact
.
hasMany
(
self
.
Photo
,
{
as
:
'Photos'
});
self
.
Contact
.
hasMany
(
self
.
PhoneNumber
);
self
.
sequelize
.
sync
({
force
:
true
}).
success
(
function
()
{
self
.
Contact
.
create
({
name
:
'Boris'
}).
success
(
function
(
someContact
)
{
self
.
Photo
.
create
({
img
:
'img.jpg'
}).
success
(
function
(
somePhoto
)
{
self
.
PhoneNumber
.
create
({
phone
:
'000000'
}).
success
(
function
(
somePhone1
)
{
self
.
PhoneNumber
.
create
({
phone
:
'111111'
}).
success
(
function
(
somePhone2
)
{
someContact
.
setPhotos
([
somePhoto
]).
complete
(
function
(
err
)
{
expect
(
err
).
to
.
be
.
null
;
someContact
.
setPhoneNumbers
([
somePhone1
,
somePhone2
]).
complete
(
function
()
{
return
self
.
sequelize
.
sync
({
force
:
true
}).
then
(
function
()
{
return
self
.
Contact
.
create
({
name
:
'Boris'
}).
then
(
function
(
someContact
)
{
return
self
.
Photo
.
create
({
img
:
'img.jpg'
}).
then
(
function
(
somePhoto
)
{
return
self
.
PhoneNumber
.
create
({
phone
:
'000000'
}).
then
(
function
(
somePhone1
)
{
return
self
.
PhoneNumber
.
create
({
phone
:
'111111'
}).
then
(
function
(
somePhone2
)
{
return
someContact
.
setPhotos
([
somePhoto
]).
then
(
function
()
{
return
someContact
.
setPhoneNumbers
([
somePhone1
,
somePhone2
]).
then
(
function
()
{
self
.
Contact
.
find
({
where
:
{
name
:
'Boris'
},
include
:
[
self
.
PhoneNumber
,
{
daoFactory
:
self
.
Photo
,
as
:
'Photos'
}]
}).
complete
(
function
(
err
,
fetchedContact
)
{
expect
(
err
).
to
.
be
.
null
;
}).
then
(
function
(
fetchedContact
)
{
expect
(
fetchedContact
).
to
.
exist
;
expect
(
fetchedContact
.
Photos
.
length
).
to
.
equal
(
1
);
expect
(
fetchedContact
.
PhoneNumbers
.
length
).
to
.
equal
(
2
);
done
();
});
});
});
...
...
@@ -719,7 +656,7 @@ describe(Support.getTestDialectTeaser('Model'), function() {
});
});
it
(
'eager loads with non-id primary keys'
,
function
(
done
)
{
it
(
'eager loads with non-id primary keys'
,
function
()
{
var
self
=
this
;
self
.
User
=
self
.
sequelize
.
define
(
'UserPKeagerone'
,
{
username
:
{
...
...
@@ -736,22 +673,19 @@ describe(Support.getTestDialectTeaser('Model'), function() {
self
.
Group
.
hasMany
(
self
.
User
);
self
.
User
.
hasMany
(
self
.
Group
);
self
.
sequelize
.
sync
({
force
:
true
}).
success
(
function
()
{
self
.
User
.
create
({
username
:
'someone'
}).
success
(
function
(
someUser
)
{
self
.
Group
.
create
({
name
:
'people'
}).
success
(
function
(
someGroup
)
{
someUser
.
setGroupPKeagerones
([
someGroup
]).
complete
(
function
(
err
)
{
expect
(
err
).
to
.
be
.
null
;
self
.
User
.
find
({
return
self
.
sequelize
.
sync
({
force
:
true
}).
then
(
function
()
{
return
self
.
User
.
create
({
username
:
'someone'
}).
then
(
function
(
someUser
)
{
return
self
.
Group
.
create
({
name
:
'people'
}).
then
(
function
(
someGroup
)
{
return
someUser
.
setGroupPKeagerones
([
someGroup
]).
then
(
function
()
{
return
self
.
User
.
find
({
where
:
{
username
:
'someone'
},
include
:
[
self
.
Group
]
}).
complete
(
function
(
err
,
someUser
)
{
expect
(
err
).
to
.
be
.
null
;
}).
then
(
function
(
someUser
)
{
expect
(
someUser
).
to
.
exist
;
expect
(
someUser
.
username
).
to
.
equal
(
'someone'
);
expect
(
someUser
.
GroupPKeagerones
[
0
].
name
).
to
.
equal
(
'people'
);
done
();
});
});
});
...
...
@@ -761,69 +695,59 @@ describe(Support.getTestDialectTeaser('Model'), function() {
});
describe
(
'hasMany with alias'
,
function
()
{
it
(
'throws an error if included DaoFactory is not referenced by alias'
,
function
(
done
)
{
it
(
'throws an error if included DaoFactory is not referenced by alias'
,
function
()
{
var
self
=
this
;
self
.
Worker
.
find
({
include
:
[
self
.
Task
]
}).
catch
(
function
(
err
)
{
return
self
.
Worker
.
find
({
include
:
[
self
.
Task
]
}).
catch
(
function
(
err
)
{
expect
(
err
.
message
).
to
.
equal
(
'Task is not associated to Worker!'
);
done
();
});
});
describe
(
'alias'
,
function
()
{
beforeEach
(
function
(
done
)
{
beforeEach
(
function
()
{
var
self
=
this
;
this
.
Worker
.
hasMany
(
this
.
Task
,
{
as
:
'ToDos'
});
this
.
init
(
function
()
{
self
.
worker
.
setToDos
([
self
.
task
]).
success
(
function
()
{
done
();
});
return
this
.
init
(
function
()
{
return
self
.
worker
.
setToDos
([
self
.
task
]);
});
});
it
(
'throws an error if alias is not associated'
,
function
(
done
)
{
it
(
'throws an error if alias is not associated'
,
function
()
{
var
self
=
this
;
self
.
Worker
.
find
({
include
:
[{
daoFactory
:
self
.
Task
,
as
:
'Work'
}]
}).
catch
(
function
(
err
)
{
return
self
.
Worker
.
find
({
include
:
[{
daoFactory
:
self
.
Task
,
as
:
'Work'
}]
}).
catch
(
function
(
err
)
{
expect
(
err
.
message
).
to
.
equal
(
'Task (Work) is not associated to Worker!'
);
done
();
});
});
it
(
'returns the associated task via worker.task'
,
function
(
done
)
{
this
.
Worker
.
find
({
it
(
'returns the associated task via worker.task'
,
function
()
{
return
this
.
Worker
.
find
({
where
:
{
name
:
'worker'
},
include
:
[{
daoFactory
:
this
.
Task
,
as
:
'ToDos'
}]
}).
complete
(
function
(
err
,
worker
)
{
expect
(
err
).
to
.
be
.
null
;
}).
then
(
function
(
worker
)
{
expect
(
worker
).
to
.
exist
;
expect
(
worker
.
ToDos
).
to
.
exist
;
expect
(
worker
.
ToDos
[
0
].
title
).
to
.
equal
(
'homework'
);
done
();
});
});
it
(
'returns the associated task via worker.task when daoFactory is aliased with model'
,
function
(
done
)
{
this
.
Worker
.
find
({
it
(
'returns the associated task via worker.task when daoFactory is aliased with model'
,
function
()
{
return
this
.
Worker
.
find
({
where
:
{
name
:
'worker'
},
include
:
[{
model
:
this
.
Task
,
as
:
'ToDos'
}]
}).
complete
(
function
(
err
,
worker
)
{
}).
then
(
function
(
worker
)
{
expect
(
worker
.
ToDos
[
0
].
title
).
to
.
equal
(
'homework'
);
done
();
});
});
it
(
'allows mulitple assocations of the same model with different alias'
,
function
(
done
)
{
it
(
'allows mulitple assocations of the same model with different alias'
,
function
()
{
var
self
=
this
;
this
.
Worker
.
hasMany
(
this
.
Task
,
{
as
:
'DoTos'
});
this
.
init
(
function
()
{
self
.
Worker
.
find
({
return
this
.
init
(
function
()
{
return
self
.
Worker
.
find
({
include
:
[
{
model
:
self
.
Task
,
as
:
'ToDos'
},
{
model
:
self
.
Task
,
as
:
'DoTos'
}
]
}).
success
(
function
()
{
// Just being able to include both shows that this test works, so no assertions needed
done
();
});
});
});
...
...
@@ -1004,36 +928,32 @@ describe(Support.getTestDialectTeaser('Model'), function() {
});
describe
(
'queryOptions'
,
function
()
{
beforeEach
(
function
(
done
)
{
beforeEach
(
function
()
{
var
self
=
this
;
this
.
User
.
create
({
username
:
'barfooz'
}).
success
(
function
(
user
)
{
return
this
.
User
.
create
({
username
:
'barfooz'
}).
then
(
function
(
user
)
{
self
.
user
=
user
;
done
();
});
});
it
(
'should return a DAO when queryOptions are not set'
,
function
(
done
)
{
it
(
'should return a DAO when queryOptions are not set'
,
function
()
{
var
self
=
this
;
this
.
User
.
find
({
where
:
{
username
:
'barfooz'
}}).
done
(
function
(
err
,
user
)
{
return
this
.
User
.
find
({
where
:
{
username
:
'barfooz'
}}).
then
(
function
(
user
)
{
expect
(
user
).
to
.
be
.
instanceOf
(
self
.
User
.
DAO
);
done
();
});
});
it
(
'should return a DAO when raw is false'
,
function
(
done
)
{
it
(
'should return a DAO when raw is false'
,
function
()
{
var
self
=
this
;
this
.
User
.
find
({
where
:
{
username
:
'barfooz'
}},
{
raw
:
false
}).
done
(
function
(
err
,
user
)
{
return
this
.
User
.
find
({
where
:
{
username
:
'barfooz'
}},
{
raw
:
false
}).
then
(
function
(
user
)
{
expect
(
user
).
to
.
be
.
instanceOf
(
self
.
User
.
DAO
);
done
();
});
});
it
(
'should return raw data when raw is true'
,
function
(
done
)
{
it
(
'should return raw data when raw is true'
,
function
()
{
var
self
=
this
;
this
.
User
.
find
({
where
:
{
username
:
'barfooz'
}},
{
raw
:
true
}).
done
(
function
(
err
,
user
)
{
return
this
.
User
.
find
({
where
:
{
username
:
'barfooz'
}},
{
raw
:
true
}).
then
(
function
(
user
)
{
expect
(
user
).
to
.
not
.
be
.
instanceOf
(
self
.
User
.
DAO
);
expect
(
user
).
to
.
be
.
instanceOf
(
Object
);
done
();
});
});
});
...
...
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