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 870572b6
authored
Dec 07, 2014
by
Mick Hansen
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
refactor model/attribute/types test into seperate file
1 parent
c7fde175
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
145 additions
and
125 deletions
test/model/attributes.test.js
test/model/attributes/field.test.js
test/model/attributes/types.test.js
test/model/attributes.test.js
View file @
870572b
...
@@ -16,128 +16,6 @@ chai.config.includeStack = true;
...
@@ -16,128 +16,6 @@ chai.config.includeStack = true;
describe
(
Support
.
getTestDialectTeaser
(
"Model"
),
function
()
{
describe
(
Support
.
getTestDialectTeaser
(
"Model"
),
function
()
{
describe
(
'attributes'
,
function
()
{
describe
(
'attributes'
,
function
()
{
describe
(
'types'
,
function
()
{
describe
(
'VIRTUAL'
,
function
()
{
beforeEach
(
function
()
{
this
.
User
=
this
.
sequelize
.
define
(
'user'
,
{
storage
:
Sequelize
.
STRING
,
field1
:
{
type
:
Sequelize
.
VIRTUAL
,
set
:
function
(
val
)
{
this
.
setDataValue
(
'storage'
,
val
);
this
.
setDataValue
(
'field1'
,
val
);
},
get
:
function
()
{
return
this
.
getDataValue
(
'field1'
);
}
},
field2
:
{
type
:
Sequelize
.
VIRTUAL
,
get
:
function
()
{
return
42
;
}
},
virtualWithDefault
:
{
type
:
Sequelize
.
VIRTUAL
,
defaultValue
:
'cake'
}
},
{
timestamps
:
false
});
this
.
Task
=
this
.
sequelize
.
define
(
'task'
,
{});
this
.
Project
=
this
.
sequelize
.
define
(
'project'
,
{});
this
.
Task
.
belongsTo
(
this
.
User
);
this
.
Project
.
hasMany
(
this
.
User
);
this
.
User
.
hasMany
(
this
.
Project
);
this
.
sqlAssert
=
function
(
sql
)
{
expect
(
sql
.
indexOf
(
'field1'
)).
to
.
equal
(
-
1
);
expect
(
sql
.
indexOf
(
'field2'
)).
to
.
equal
(
-
1
);
};
return
this
.
sequelize
.
sync
({
force
:
true
});
});
it
(
'should not be ignored in dataValues get'
,
function
()
{
var
user
=
this
.
User
.
build
({
field1
:
'field1_value'
,
field2
:
'field2_value'
});
expect
(
user
.
get
()).
to
.
deep
.
equal
({
storage
:
'field1_value'
,
field1
:
'field1_value'
,
virtualWithDefault
:
'cake'
,
field2
:
42
,
id
:
null
});
});
it
(
'should be ignored in table creation'
,
function
()
{
return
this
.
sequelize
.
getQueryInterface
().
describeTable
(
this
.
User
.
tableName
).
then
(
function
(
fields
)
{
expect
(
Object
.
keys
(
fields
).
length
).
to
.
equal
(
2
);
});
});
it
(
'should be ignored in find, findAll and includes'
,
function
()
{
return
Promise
.
all
([
this
.
User
.
find
().
on
(
'sql'
,
this
.
sqlAssert
),
this
.
User
.
findAll
().
on
(
'sql'
,
this
.
sqlAssert
),
this
.
Task
.
findAll
({
include
:
[
this
.
User
]
}).
on
(
'sql'
,
this
.
sqlAssert
),
this
.
Project
.
findAll
({
include
:
[
this
.
User
]
}).
on
(
'sql'
,
this
.
sqlAssert
)
]);
});
it
(
"should allow me to store selected values"
,
function
()
{
var
Post
=
this
.
sequelize
.
define
(
'Post'
,
{
text
:
Sequelize
.
TEXT
,
someBoolean
:
{
type
:
Sequelize
.
VIRTUAL
}
});
return
this
.
sequelize
.
sync
({
force
:
true
}).
then
(
function
()
{
return
Post
.
bulkCreate
([{
text
:
'text1'
},{
text
:
'text2'
}]);
}).
then
(
function
()
{
return
Post
.
find
({
attributes
:
[
'id'
,
'text'
,
Sequelize
.
literal
(
'EXISTS(SELECT 1) AS "someBoolean"'
)]
});
}).
then
(
function
(
post
)
{
expect
(
post
.
get
(
'someBoolean'
)).
to
.
be
.
ok
;
expect
(
post
.
get
().
someBoolean
).
to
.
be
.
ok
;
});
});
it
(
'should be ignored in create and updateAttributes'
,
function
()
{
return
this
.
User
.
create
({
field1
:
'something'
}).
then
(
function
(
user
)
{
// We already verified that the virtual is not added to the table definition, so if this succeeds, were good
expect
(
user
.
virtualWithDefault
).
to
.
equal
(
'cake'
);
expect
(
user
.
storage
).
to
.
equal
(
'something'
);
return
user
.
updateAttributes
({
field1
:
'something else'
});
}).
then
(
function
(
user
)
{
expect
(
user
.
virtualWithDefault
).
to
.
equal
(
'cake'
);
expect
(
user
.
storage
).
to
.
equal
(
'something else'
);
});
});
it
(
'should be ignored in bulkCreate and and bulkUpdate'
,
function
()
{
var
self
=
this
;
return
this
.
User
.
bulkCreate
([{
field1
:
'something'
}]).
on
(
'sql'
,
this
.
sqlAssert
).
then
(
function
()
{
return
self
.
User
.
findAll
();
}).
then
(
function
(
users
)
{
expect
(
users
[
0
].
storage
).
to
.
equal
(
'something'
);
});
});
});
});
describe
(
'set'
,
function
()
{
describe
(
'set'
,
function
()
{
it
(
'should only be called once when used on a join model called with an association getter'
,
function
()
{
it
(
'should only be called once when used on a join model called with an association getter'
,
function
()
{
var
self
=
this
;
var
self
=
this
;
...
...
test/model/attributes/field.test.js
View file @
870572b
...
@@ -3,11 +3,11 @@
...
@@ -3,11 +3,11 @@
/* jshint camelcase: false */
/* jshint camelcase: false */
/* jshint expr: true */
/* jshint expr: true */
var
chai
=
require
(
'chai'
)
var
chai
=
require
(
'chai'
)
,
Sequelize
=
require
(
'../../index'
)
,
Sequelize
=
require
(
'../../
../
index'
)
,
Promise
=
Sequelize
.
Promise
,
Promise
=
Sequelize
.
Promise
,
expect
=
chai
.
expect
,
expect
=
chai
.
expect
,
Support
=
require
(
__dirname
+
'/../support'
)
,
Support
=
require
(
__dirname
+
'/../
../
support'
)
,
DataTypes
=
require
(
__dirname
+
"/../../lib/data-types"
)
,
DataTypes
=
require
(
__dirname
+
"/../../
../
lib/data-types"
)
,
dialect
=
Support
.
getTestDialect
()
,
dialect
=
Support
.
getTestDialect
()
,
datetime
=
require
(
'chai-datetime'
);
,
datetime
=
require
(
'chai-datetime'
);
...
...
test/model/attributes/types.test.js
0 → 100644
View file @
870572b
"use strict"
;
/* jshint camelcase: false */
/* jshint expr: true */
var
chai
=
require
(
'chai'
)
,
Sequelize
=
require
(
'../../../index'
)
,
Promise
=
Sequelize
.
Promise
,
expect
=
chai
.
expect
,
Support
=
require
(
__dirname
+
'/../../support'
)
,
DataTypes
=
require
(
__dirname
+
"/../../../lib/data-types"
)
,
dialect
=
Support
.
getTestDialect
()
,
datetime
=
require
(
'chai-datetime'
);
chai
.
use
(
datetime
);
chai
.
config
.
includeStack
=
true
;
describe
(
Support
.
getTestDialectTeaser
(
"Model"
),
function
()
{
describe
(
'attributes'
,
function
()
{
describe
(
'types'
,
function
()
{
describe
(
'VIRTUAL'
,
function
()
{
beforeEach
(
function
()
{
this
.
User
=
this
.
sequelize
.
define
(
'user'
,
{
storage
:
Sequelize
.
STRING
,
field1
:
{
type
:
Sequelize
.
VIRTUAL
,
set
:
function
(
val
)
{
this
.
setDataValue
(
'storage'
,
val
);
this
.
setDataValue
(
'field1'
,
val
);
},
get
:
function
()
{
return
this
.
getDataValue
(
'field1'
);
}
},
field2
:
{
type
:
Sequelize
.
VIRTUAL
,
get
:
function
()
{
return
42
;
}
},
virtualWithDefault
:
{
type
:
Sequelize
.
VIRTUAL
,
defaultValue
:
'cake'
}
},
{
timestamps
:
false
});
this
.
Task
=
this
.
sequelize
.
define
(
'task'
,
{});
this
.
Project
=
this
.
sequelize
.
define
(
'project'
,
{});
this
.
Task
.
belongsTo
(
this
.
User
);
this
.
Project
.
hasMany
(
this
.
User
);
this
.
User
.
hasMany
(
this
.
Project
);
this
.
sqlAssert
=
function
(
sql
)
{
expect
(
sql
.
indexOf
(
'field1'
)).
to
.
equal
(
-
1
);
expect
(
sql
.
indexOf
(
'field2'
)).
to
.
equal
(
-
1
);
};
return
this
.
sequelize
.
sync
({
force
:
true
});
});
it
(
'should not be ignored in dataValues get'
,
function
()
{
var
user
=
this
.
User
.
build
({
field1
:
'field1_value'
,
field2
:
'field2_value'
});
expect
(
user
.
get
()).
to
.
deep
.
equal
({
storage
:
'field1_value'
,
field1
:
'field1_value'
,
virtualWithDefault
:
'cake'
,
field2
:
42
,
id
:
null
});
});
it
(
'should be ignored in table creation'
,
function
()
{
return
this
.
sequelize
.
getQueryInterface
().
describeTable
(
this
.
User
.
tableName
).
then
(
function
(
fields
)
{
expect
(
Object
.
keys
(
fields
).
length
).
to
.
equal
(
2
);
});
});
it
(
'should be ignored in find, findAll and includes'
,
function
()
{
return
Promise
.
all
([
this
.
User
.
find
().
on
(
'sql'
,
this
.
sqlAssert
),
this
.
User
.
findAll
().
on
(
'sql'
,
this
.
sqlAssert
),
this
.
Task
.
findAll
({
include
:
[
this
.
User
]
}).
on
(
'sql'
,
this
.
sqlAssert
),
this
.
Project
.
findAll
({
include
:
[
this
.
User
]
}).
on
(
'sql'
,
this
.
sqlAssert
)
]);
});
it
(
"should allow me to store selected values"
,
function
()
{
var
Post
=
this
.
sequelize
.
define
(
'Post'
,
{
text
:
Sequelize
.
TEXT
,
someBoolean
:
{
type
:
Sequelize
.
VIRTUAL
}
});
return
this
.
sequelize
.
sync
({
force
:
true
}).
then
(
function
()
{
return
Post
.
bulkCreate
([{
text
:
'text1'
},{
text
:
'text2'
}]);
}).
then
(
function
()
{
return
Post
.
find
({
attributes
:
[
'id'
,
'text'
,
Sequelize
.
literal
(
'EXISTS(SELECT 1) AS "someBoolean"'
)]
});
}).
then
(
function
(
post
)
{
expect
(
post
.
get
(
'someBoolean'
)).
to
.
be
.
ok
;
expect
(
post
.
get
().
someBoolean
).
to
.
be
.
ok
;
});
});
it
(
'should be ignored in create and updateAttributes'
,
function
()
{
return
this
.
User
.
create
({
field1
:
'something'
}).
then
(
function
(
user
)
{
// We already verified that the virtual is not added to the table definition, so if this succeeds, were good
expect
(
user
.
virtualWithDefault
).
to
.
equal
(
'cake'
);
expect
(
user
.
storage
).
to
.
equal
(
'something'
);
return
user
.
updateAttributes
({
field1
:
'something else'
});
}).
then
(
function
(
user
)
{
expect
(
user
.
virtualWithDefault
).
to
.
equal
(
'cake'
);
expect
(
user
.
storage
).
to
.
equal
(
'something else'
);
});
});
it
(
'should be ignored in bulkCreate and and bulkUpdate'
,
function
()
{
var
self
=
this
;
return
this
.
User
.
bulkCreate
([{
field1
:
'something'
}]).
on
(
'sql'
,
this
.
sqlAssert
).
then
(
function
()
{
return
self
.
User
.
findAll
();
}).
then
(
function
(
users
)
{
expect
(
users
[
0
].
storage
).
to
.
equal
(
'something'
);
});
});
});
});
});
});
\ No newline at end of file
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