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 bcef931f
authored
Apr 01, 2015
by
Ruben Bridgewater
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Refactor all instance validations tests to use promises
1 parent
5d4c8228
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
101 additions
and
174 deletions
test/integration/instance.validations.test.js
test/integration/instance.validations.test.js
View file @
bcef931
...
@@ -171,7 +171,7 @@ describe(Support.getTestDialectTeaser('InstanceValidator'), function() {
...
@@ -171,7 +171,7 @@ describe(Support.getTestDialectTeaser('InstanceValidator'), function() {
var
applyFailTest
=
function
applyFailTest
(
validatorDetails
,
i
,
validator
)
{
var
applyFailTest
=
function
applyFailTest
(
validatorDetails
,
i
,
validator
)
{
var
failingValue
=
validatorDetails
.
fail
[
i
];
var
failingValue
=
validatorDetails
.
fail
[
i
];
it
(
'correctly specifies an instance as invalid using a value of "'
+
failingValue
+
'" for the validation "'
+
validator
+
'"'
,
function
(
done
)
{
it
(
'correctly specifies an instance as invalid using a value of "'
+
failingValue
+
'" for the validation "'
+
validator
+
'"'
,
function
()
{
var
validations
=
{}
var
validations
=
{}
,
message
=
validator
+
'('
+
failingValue
+
')'
;
,
message
=
validator
+
'('
+
failingValue
+
')'
;
...
@@ -192,17 +192,16 @@ describe(Support.getTestDialectTeaser('InstanceValidator'), function() {
...
@@ -192,17 +192,16 @@ describe(Support.getTestDialectTeaser('InstanceValidator'), function() {
var
failingUser
=
UserFail
.
build
({
name
:
failingValue
});
var
failingUser
=
UserFail
.
build
({
name
:
failingValue
});
failingUser
.
validate
().
done
(
function
(
err
,
_errors
)
{
return
failingUser
.
validate
().
then
(
function
(
_errors
)
{
expect
(
_errors
).
not
.
to
.
be
.
null
;
expect
(
_errors
).
not
.
to
.
be
.
null
;
expect
(
_errors
).
to
.
be
.
an
.
instanceOf
(
Error
);
expect
(
_errors
).
to
.
be
.
an
.
instanceOf
(
Error
);
expect
(
_errors
.
get
(
'name'
)[
0
].
message
).
to
.
equal
(
message
);
expect
(
_errors
.
get
(
'name'
)[
0
].
message
).
to
.
equal
(
message
);
done
();
});
});
});
});
}
}
,
applyPassTest
=
function
applyPassTest
(
validatorDetails
,
j
,
validator
)
{
,
applyPassTest
=
function
applyPassTest
(
validatorDetails
,
j
,
validator
)
{
var
succeedingValue
=
validatorDetails
.
pass
[
j
];
var
succeedingValue
=
validatorDetails
.
pass
[
j
];
it
(
'correctly specifies an instance as valid using a value of "'
+
succeedingValue
+
'" for the validation "'
+
validator
+
'"'
,
function
(
done
)
{
it
(
'correctly specifies an instance as valid using a value of "'
+
succeedingValue
+
'" for the validation "'
+
validator
+
'"'
,
function
()
{
var
validations
=
{};
var
validations
=
{};
if
(
validatorDetails
.
hasOwnProperty
(
'spec'
))
{
if
(
validatorDetails
.
hasOwnProperty
(
'spec'
))
{
...
@@ -220,12 +219,10 @@ describe(Support.getTestDialectTeaser('InstanceValidator'), function() {
...
@@ -220,12 +219,10 @@ describe(Support.getTestDialectTeaser('InstanceValidator'), function() {
}
}
});
});
var
successfulUser
=
UserSuccess
.
build
({
name
:
succeedingValue
});
var
successfulUser
=
UserSuccess
.
build
({
name
:
succeedingValue
});
successfulUser
.
validate
().
success
(
function
(
errors
)
{
return
successfulUser
.
validate
().
then
(
function
(
errors
)
{
expect
(
errors
).
to
.
be
.
undefined
;
expect
(
errors
).
to
.
be
.
undefined
;
done
();
}).
catch
(
function
(
err
)
{
}).
error
(
function
(
err
)
{
expect
(
err
).
to
.
deep
.
equal
({});
expect
(
err
).
to
.
deep
.
equal
({});
done
();
});
});
});
});
};
};
...
@@ -251,7 +248,7 @@ describe(Support.getTestDialectTeaser('InstanceValidator'), function() {
...
@@ -251,7 +248,7 @@ describe(Support.getTestDialectTeaser('InstanceValidator'), function() {
}
}
describe
(
'#update'
,
function
()
{
describe
(
'#update'
,
function
()
{
it
(
'should allow us to update specific columns without tripping the validations'
,
function
(
done
)
{
it
(
'should allow us to update specific columns without tripping the validations'
,
function
()
{
var
User
=
this
.
sequelize
.
define
(
'model'
,
{
var
User
=
this
.
sequelize
.
define
(
'model'
,
{
username
:
Sequelize
.
STRING
,
username
:
Sequelize
.
STRING
,
email
:
{
email
:
{
...
@@ -265,22 +262,21 @@ describe(Support.getTestDialectTeaser('InstanceValidator'), function() {
...
@@ -265,22 +262,21 @@ describe(Support.getTestDialectTeaser('InstanceValidator'), function() {
}
}
});
});
User
.
sync
({
force
:
true
}).
success
(
function
()
{
return
User
.
sync
({
force
:
true
}).
then
(
function
()
{
User
.
create
({
username
:
'bob'
,
email
:
'hello@world.com'
}).
success
(
function
(
user
)
{
return
User
.
create
({
username
:
'bob'
,
email
:
'hello@world.com'
}).
then
(
function
(
user
)
{
User
return
User
.
update
({
username
:
'toni'
},
{
where
:
{
id
:
user
.
id
}})
.
update
({
username
:
'toni'
},
{
where
:
{
id
:
user
.
id
}})
.
error
(
function
(
err
)
{
console
.
log
(
err
);
})
.
catch
(
function
(
err
)
{
console
.
log
(
err
);
})
.
success
(
function
()
{
.
then
(
function
()
{
User
.
find
(
1
).
success
(
function
(
user
)
{
return
User
.
find
(
1
).
then
(
function
(
user
)
{
expect
(
user
.
username
).
to
.
equal
(
'toni'
);
expect
(
user
.
username
).
to
.
equal
(
'toni'
);
done
();
});
});
});
});
});
});
});
});
});
});
it
(
'should be able to emit an error upon updating when a validation has failed from an instance'
,
function
(
done
)
{
it
(
'should be able to emit an error upon updating when a validation has failed from an instance'
,
function
()
{
var
Model
=
this
.
sequelize
.
define
(
'model'
,
{
var
Model
=
this
.
sequelize
.
define
(
'model'
,
{
name
:
{
name
:
{
type
:
Sequelize
.
STRING
,
type
:
Sequelize
.
STRING
,
...
@@ -291,18 +287,17 @@ describe(Support.getTestDialectTeaser('InstanceValidator'), function() {
...
@@ -291,18 +287,17 @@ describe(Support.getTestDialectTeaser('InstanceValidator'), function() {
}
}
});
});
Model
.
sync
({
force
:
true
}).
success
(
function
()
{
return
Model
.
sync
({
force
:
true
}).
then
(
function
()
{
Model
.
create
({
name
:
'World'
}).
success
(
function
(
model
)
{
return
Model
.
create
({
name
:
'World'
}).
then
(
function
(
model
)
{
model
.
updateAttributes
({
name
:
''
}).
error
(
function
(
err
)
{
return
model
.
updateAttributes
({
name
:
''
}).
catch
(
function
(
err
)
{
expect
(
err
).
to
.
be
.
an
.
instanceOf
(
Error
);
expect
(
err
).
to
.
be
.
an
.
instanceOf
(
Error
);
expect
(
err
.
get
(
'name'
)[
0
].
message
).
to
.
equal
(
'Validation notEmpty failed'
);
expect
(
err
.
get
(
'name'
)[
0
].
message
).
to
.
equal
(
'Validation notEmpty failed'
);
done
();
});
});
});
});
});
});
});
});
it
(
'should be able to emit an error upon updating when a validation has failed from the factory'
,
function
(
done
)
{
it
(
'should be able to emit an error upon updating when a validation has failed from the factory'
,
function
()
{
var
Model
=
this
.
sequelize
.
define
(
'model'
,
{
var
Model
=
this
.
sequelize
.
define
(
'model'
,
{
name
:
{
name
:
{
type
:
Sequelize
.
STRING
,
type
:
Sequelize
.
STRING
,
...
@@ -313,12 +308,11 @@ describe(Support.getTestDialectTeaser('InstanceValidator'), function() {
...
@@ -313,12 +308,11 @@ describe(Support.getTestDialectTeaser('InstanceValidator'), function() {
}
}
});
});
Model
.
sync
({
force
:
true
}).
success
(
function
()
{
return
Model
.
sync
({
force
:
true
}).
then
(
function
()
{
Model
.
create
({
name
:
'World'
}).
success
(
function
()
{
return
Model
.
create
({
name
:
'World'
}).
then
(
function
()
{
Model
.
update
({
name
:
''
},
{
where
:
{
id
:
1
}}).
error
(
function
(
err
)
{
return
Model
.
update
({
name
:
''
},
{
where
:
{
id
:
1
}}).
catch
(
function
(
err
)
{
expect
(
err
).
to
.
be
.
an
.
instanceOf
(
Error
);
expect
(
err
).
to
.
be
.
an
.
instanceOf
(
Error
);
expect
(
err
.
get
(
'name'
)[
0
].
message
).
to
.
equal
(
'Validation notEmpty failed'
);
expect
(
err
.
get
(
'name'
)[
0
].
message
).
to
.
equal
(
'Validation notEmpty failed'
);
done
();
});
});
});
});
});
});
...
@@ -327,7 +321,7 @@ describe(Support.getTestDialectTeaser('InstanceValidator'), function() {
...
@@ -327,7 +321,7 @@ describe(Support.getTestDialectTeaser('InstanceValidator'), function() {
describe
(
'#create'
,
function
()
{
describe
(
'#create'
,
function
()
{
describe
(
'generic'
,
function
()
{
describe
(
'generic'
,
function
()
{
beforeEach
(
function
(
done
)
{
beforeEach
(
function
()
{
var
self
=
this
;
var
self
=
this
;
var
Project
=
this
.
sequelize
.
define
(
'Project'
,
{
var
Project
=
this
.
sequelize
.
define
(
'Project'
,
{
...
@@ -348,29 +342,26 @@ describe(Support.getTestDialectTeaser('InstanceValidator'), function() {
...
@@ -348,29 +342,26 @@ describe(Support.getTestDialectTeaser('InstanceValidator'), function() {
Project
.
hasOne
(
Task
);
Project
.
hasOne
(
Task
);
Task
.
belongsTo
(
Project
);
Task
.
belongsTo
(
Project
);
this
.
sequelize
.
sync
({
force
:
true
}).
success
(
function
()
{
return
this
.
sequelize
.
sync
({
force
:
true
}).
then
(
function
()
{
self
.
Project
=
Project
;
self
.
Project
=
Project
;
self
.
Task
=
Task
;
self
.
Task
=
Task
;
done
();
});
});
});
});
it
(
'correctly throws an error using create method '
,
function
(
done
)
{
it
(
'correctly throws an error using create method '
,
function
()
{
this
.
Project
.
create
({
name
:
'nope'
}).
error
(
function
(
err
)
{
return
this
.
Project
.
create
({
name
:
'nope'
}).
catch
(
function
(
err
)
{
expect
(
err
).
to
.
have
.
ownProperty
(
'name'
);
expect
(
err
).
to
.
have
.
ownProperty
(
'name'
);
done
();
});
});
});
});
it
(
'correctly validates using create method '
,
function
(
done
)
{
it
(
'correctly validates using create method '
,
function
()
{
var
self
=
this
;
var
self
=
this
;
this
.
Project
.
create
({}).
success
(
function
(
project
)
{
return
this
.
Project
.
create
({}).
then
(
function
(
project
)
{
self
.
Task
.
create
({
something
:
1
}).
success
(
function
(
task
)
{
return
self
.
Task
.
create
({
something
:
1
}).
then
(
function
(
task
)
{
project
.
setTask
(
task
).
success
(
function
(
task
)
{
return
project
.
setTask
(
task
).
then
(
function
(
task
)
{
expect
(
task
.
ProjectId
).
to
.
not
.
be
.
null
;
expect
(
task
.
ProjectId
).
to
.
not
.
be
.
null
;
task
.
setProject
(
project
).
success
(
function
(
project
)
{
return
task
.
setProject
(
project
).
then
(
function
(
project
)
{
expect
(
project
.
ProjectId
).
to
.
not
.
be
.
null
;
expect
(
project
.
ProjectId
).
to
.
not
.
be
.
null
;
done
();
});
});
});
});
});
});
...
@@ -379,7 +370,7 @@ describe(Support.getTestDialectTeaser('InstanceValidator'), function() {
...
@@ -379,7 +370,7 @@ describe(Support.getTestDialectTeaser('InstanceValidator'), function() {
});
});
describe
(
'explicitly validating primary/auto incremented columns'
,
function
()
{
describe
(
'explicitly validating primary/auto incremented columns'
,
function
()
{
it
(
'should emit an error when we try to enter in a string for the id key without validation arguments'
,
function
(
done
)
{
it
(
'should emit an error when we try to enter in a string for the id key without validation arguments'
,
function
()
{
var
User
=
this
.
sequelize
.
define
(
'UserId'
,
{
var
User
=
this
.
sequelize
.
define
(
'UserId'
,
{
id
:
{
id
:
{
type
:
Sequelize
.
INTEGER
,
type
:
Sequelize
.
INTEGER
,
...
@@ -391,16 +382,15 @@ describe(Support.getTestDialectTeaser('InstanceValidator'), function() {
...
@@ -391,16 +382,15 @@ describe(Support.getTestDialectTeaser('InstanceValidator'), function() {
}
}
});
});
User
.
sync
({
force
:
true
}).
success
(
function
()
{
return
User
.
sync
({
force
:
true
}).
then
(
function
()
{
User
.
create
({
id
:
'helloworld'
}).
error
(
function
(
err
)
{
return
User
.
create
({
id
:
'helloworld'
}).
catch
(
function
(
err
)
{
expect
(
err
).
to
.
be
.
an
.
instanceOf
(
Error
);
expect
(
err
).
to
.
be
.
an
.
instanceOf
(
Error
);
expect
(
err
.
get
(
'id'
)[
0
].
message
).
to
.
equal
(
'Validation isInt failed'
);
expect
(
err
.
get
(
'id'
)[
0
].
message
).
to
.
equal
(
'Validation isInt failed'
);
done
();
});
});
});
});
});
});
it
(
'should emit an error when we try to enter in a string for an auto increment key (not named id)'
,
function
(
done
)
{
it
(
'should emit an error when we try to enter in a string for an auto increment key (not named id)'
,
function
()
{
var
User
=
this
.
sequelize
.
define
(
'UserId'
,
{
var
User
=
this
.
sequelize
.
define
(
'UserId'
,
{
username
:
{
username
:
{
type
:
Sequelize
.
INTEGER
,
type
:
Sequelize
.
INTEGER
,
...
@@ -412,17 +402,16 @@ describe(Support.getTestDialectTeaser('InstanceValidator'), function() {
...
@@ -412,17 +402,16 @@ describe(Support.getTestDialectTeaser('InstanceValidator'), function() {
}
}
});
});
User
.
sync
({
force
:
true
}).
success
(
function
()
{
return
User
.
sync
({
force
:
true
}).
then
(
function
()
{
User
.
create
({
username
:
'helloworldhelloworld'
}).
error
(
function
(
err
)
{
return
User
.
create
({
username
:
'helloworldhelloworld'
}).
catch
(
function
(
err
)
{
expect
(
err
).
to
.
be
.
an
.
instanceOf
(
Error
);
expect
(
err
).
to
.
be
.
an
.
instanceOf
(
Error
);
expect
(
err
.
get
(
'username'
)[
0
].
message
).
to
.
equal
(
'Username must be an integer!'
);
expect
(
err
.
get
(
'username'
)[
0
].
message
).
to
.
equal
(
'Username must be an integer!'
);
done
();
});
});
});
});
});
});
describe
(
"primaryKey with the name as id with arguments for it's validation"
,
function
()
{
describe
(
"primaryKey with the name as id with arguments for it's validation"
,
function
()
{
beforeEach
(
function
(
done
)
{
beforeEach
(
function
()
{
this
.
User
=
this
.
sequelize
.
define
(
'UserId'
,
{
this
.
User
=
this
.
sequelize
.
define
(
'UserId'
,
{
id
:
{
id
:
{
type
:
Sequelize
.
INTEGER
,
type
:
Sequelize
.
INTEGER
,
...
@@ -434,41 +423,36 @@ describe(Support.getTestDialectTeaser('InstanceValidator'), function() {
...
@@ -434,41 +423,36 @@ describe(Support.getTestDialectTeaser('InstanceValidator'), function() {
}
}
});
});
this
.
User
.
sync
({
force
:
true
}).
success
(
function
()
{
return
this
.
User
.
sync
({
force
:
true
});
done
();
});
});
});
it
(
'should emit an error when we try to enter in a string for the id key with validation arguments'
,
function
(
done
)
{
it
(
'should emit an error when we try to enter in a string for the id key with validation arguments'
,
function
()
{
this
.
User
.
create
({
id
:
'helloworld'
}).
error
(
function
(
err
)
{
return
this
.
User
.
create
({
id
:
'helloworld'
}).
catch
(
function
(
err
)
{
expect
(
err
).
to
.
be
.
an
.
instanceOf
(
Error
);
expect
(
err
).
to
.
be
.
an
.
instanceOf
(
Error
);
expect
(
err
.
get
(
'id'
)[
0
].
message
).
to
.
equal
(
'ID must be an integer!'
);
expect
(
err
.
get
(
'id'
)[
0
].
message
).
to
.
equal
(
'ID must be an integer!'
);
done
();
});
});
});
});
it
(
'should emit an error when we try to enter in a string for an auto increment key through .build().validate()'
,
function
(
done
)
{
it
(
'should emit an error when we try to enter in a string for an auto increment key through .build().validate()'
,
function
()
{
var
user
=
this
.
User
.
build
({
id
:
'helloworld'
});
var
user
=
this
.
User
.
build
({
id
:
'helloworld'
});
user
.
validate
().
success
(
function
(
err
)
{
return
user
.
validate
().
then
(
function
(
err
)
{
expect
(
err
).
to
.
be
.
an
.
instanceOf
(
Error
);
expect
(
err
).
to
.
be
.
an
.
instanceOf
(
Error
);
expect
(
err
.
get
(
'id'
)[
0
].
message
).
to
.
equal
(
'ID must be an integer!'
);
expect
(
err
.
get
(
'id'
)[
0
].
message
).
to
.
equal
(
'ID must be an integer!'
);
done
();
});
});
});
});
it
(
'should emit an error when we try to .save()'
,
function
(
done
)
{
it
(
'should emit an error when we try to .save()'
,
function
()
{
var
user
=
this
.
User
.
build
({
id
:
'helloworld'
});
var
user
=
this
.
User
.
build
({
id
:
'helloworld'
});
user
.
save
().
error
(
function
(
err
)
{
return
user
.
save
().
catch
(
function
(
err
)
{
expect
(
err
).
to
.
be
.
an
.
instanceOf
(
Error
);
expect
(
err
).
to
.
be
.
an
.
instanceOf
(
Error
);
expect
(
err
.
get
(
'id'
)[
0
].
message
).
to
.
equal
(
'ID must be an integer!'
);
expect
(
err
.
get
(
'id'
)[
0
].
message
).
to
.
equal
(
'ID must be an integer!'
);
done
();
});
});
});
});
});
});
});
});
describe
(
'Pass all paths when validating'
,
function
()
{
describe
(
'Pass all paths when validating'
,
function
()
{
beforeEach
(
function
(
done
)
{
beforeEach
(
function
()
{
var
self
=
this
;
var
self
=
this
;
var
Project
=
this
.
sequelize
.
define
(
'Project'
,
{
var
Project
=
this
.
sequelize
.
define
(
'Project'
,
{
name
:
{
name
:
{
...
@@ -496,27 +480,25 @@ describe(Support.getTestDialectTeaser('InstanceValidator'), function() {
...
@@ -496,27 +480,25 @@ describe(Support.getTestDialectTeaser('InstanceValidator'), function() {
Project
.
hasOne
(
Task
);
Project
.
hasOne
(
Task
);
Task
.
belongsTo
(
Project
);
Task
.
belongsTo
(
Project
);
Project
.
sync
({
force
:
true
}).
success
(
function
()
{
return
Project
.
sync
({
force
:
true
}).
then
(
function
()
{
Task
.
sync
({
force
:
true
}).
success
(
function
()
{
return
Task
.
sync
({
force
:
true
}).
then
(
function
()
{
self
.
Project
=
Project
;
self
.
Project
=
Project
;
self
.
Task
=
Task
;
self
.
Task
=
Task
;
done
();
});
});
});
});
});
});
it
(
'produce 3 errors'
,
function
(
done
)
{
it
(
'produce 3 errors'
,
function
()
{
this
.
Project
.
create
({}).
error
(
function
(
err
)
{
return
this
.
Project
.
create
({}).
catch
(
function
(
err
)
{
expect
(
err
).
to
.
be
.
an
.
instanceOf
(
Error
);
expect
(
err
).
to
.
be
.
an
.
instanceOf
(
Error
);
delete
err
.
stack
;
// longStackTraces
delete
err
.
stack
;
// longStackTraces
expect
(
Object
.
keys
(
err
)).
to
.
have
.
length
(
3
);
expect
(
Object
.
keys
(
err
)).
to
.
have
.
length
(
3
);
done
();
});
});
});
});
});
});
});
});
it
(
'correctly validates using custom validation methods'
,
function
(
done
)
{
it
(
'correctly validates using custom validation methods'
,
function
()
{
var
User
=
this
.
sequelize
.
define
(
'User'
+
config
.
rand
(),
{
var
User
=
this
.
sequelize
.
define
(
'User'
+
config
.
rand
(),
{
name
:
{
name
:
{
type
:
Sequelize
.
STRING
,
type
:
Sequelize
.
STRING
,
...
@@ -534,20 +516,18 @@ describe(Support.getTestDialectTeaser('InstanceValidator'), function() {
...
@@ -534,20 +516,18 @@ describe(Support.getTestDialectTeaser('InstanceValidator'), function() {
var
failingUser
=
User
.
build
({
name
:
'3'
});
var
failingUser
=
User
.
build
({
name
:
'3'
});
failingUser
.
validate
().
success
(
function
(
error
)
{
return
failingUser
.
validate
().
then
(
function
(
error
)
{
expect
(
error
).
to
.
be
.
an
.
instanceOf
(
Error
);
expect
(
error
).
to
.
be
.
an
.
instanceOf
(
Error
);
expect
(
error
.
get
(
'name'
)[
0
].
message
).
to
.
equal
(
"name should equal '2'"
);
expect
(
error
.
get
(
'name'
)[
0
].
message
).
to
.
equal
(
"name should equal '2'"
);
var
successfulUser
=
User
.
build
({
name
:
'2'
});
var
successfulUser
=
User
.
build
({
name
:
'2'
});
successfulUser
.
validate
().
success
(
function
(
err
)
{
return
successfulUser
.
validate
().
then
(
function
(
err
)
{
expect
(
err
).
not
.
to
.
be
.
defined
;
expect
(
err
).
not
.
to
.
be
.
defined
;
done
();
});
});
});
});
});
});
it
(
'supports promises with custom validation methods'
,
function
(
done
)
{
it
(
'supports promises with custom validation methods'
,
function
()
{
var
self
=
this
var
self
=
this
,
User
=
this
.
sequelize
.
define
(
'User'
+
config
.
rand
(),
{
,
User
=
this
.
sequelize
.
define
(
'User'
+
config
.
rand
(),
{
name
:
{
name
:
{
...
@@ -565,20 +545,19 @@ describe(Support.getTestDialectTeaser('InstanceValidator'), function() {
...
@@ -565,20 +545,19 @@ describe(Support.getTestDialectTeaser('InstanceValidator'), function() {
}
}
});
});
User
.
sync
().
success
(
function
()
{
return
User
.
sync
().
then
(
function
()
{
User
.
build
({
name
:
'error'
}).
validate
().
success
(
function
(
error
)
{
return
User
.
build
({
name
:
'error'
}).
validate
().
then
(
function
(
error
)
{
expect
(
error
).
to
.
be
.
an
.
instanceOf
(
self
.
sequelize
.
ValidationError
);
expect
(
error
).
to
.
be
.
an
.
instanceOf
(
self
.
sequelize
.
ValidationError
);
expect
(
error
.
get
(
'name'
)[
0
].
message
).
to
.
equal
(
'Invalid username'
);
expect
(
error
.
get
(
'name'
)[
0
].
message
).
to
.
equal
(
'Invalid username'
);
User
.
build
({
name
:
'no error'
}).
validate
().
success
(
function
(
errors
)
{
return
User
.
build
({
name
:
'no error'
}).
validate
().
then
(
function
(
errors
)
{
expect
(
errors
).
not
.
to
.
be
.
defined
;
expect
(
errors
).
not
.
to
.
be
.
defined
;
done
();
});
});
});
});
});
});
});
});
it
(
'skips other validations if allowNull is true and the value is null'
,
function
(
done
)
{
it
(
'skips other validations if allowNull is true and the value is null'
,
function
()
{
var
User
=
this
.
sequelize
.
define
(
'User'
+
config
.
rand
(),
{
var
User
=
this
.
sequelize
.
define
(
'User'
+
config
.
rand
(),
{
age
:
{
age
:
{
type
:
Sequelize
.
INTEGER
,
type
:
Sequelize
.
INTEGER
,
...
@@ -589,62 +568,23 @@ describe(Support.getTestDialectTeaser('InstanceValidator'), function() {
...
@@ -589,62 +568,23 @@ describe(Support.getTestDialectTeaser('InstanceValidator'), function() {
}
}
});
});
User
return
User
.
build
({
age
:
-
1
})
.
build
({
age
:
-
1
})
.
validate
()
.
validate
()
.
success
(
function
(
error
)
{
.
then
(
function
(
error
)
{
expect
(
error
).
not
.
to
.
be
.
null
;
expect
(
error
).
not
.
to
.
be
.
null
;
expect
(
error
).
to
.
be
.
an
.
instanceOf
(
Error
);
expect
(
error
).
to
.
be
.
an
.
instanceOf
(
Error
);
expect
(
error
.
get
(
'age'
)[
0
].
message
).
to
.
equal
(
'must be positive'
);
expect
(
error
.
get
(
'age'
)[
0
].
message
).
to
.
equal
(
'must be positive'
);
User
.
build
({
age
:
null
}).
validate
().
success
(
function
()
{
// TODO: This does not test anything
User
.
build
({
age
:
1
}).
validate
().
success
(
function
()
{
// Check what the original intention was
done
();
return
User
.
build
({
age
:
null
}).
validate
().
then
(
function
()
{
}
);
return
User
.
build
({
age
:
1
}).
validate
(
);
});
});
});
});
});
});
it
(
'validates a model with custom model-wide validation methods'
,
function
(
done
)
{
it
(
'validates a model with custom model-wide validation methods'
,
function
()
{
var
Foo
=
this
.
sequelize
.
define
(
'Foo'
+
config
.
rand
(),
{
field1
:
{
type
:
Sequelize
.
INTEGER
,
allowNull
:
true
},
field2
:
{
type
:
Sequelize
.
INTEGER
,
allowNull
:
true
}
},
{
validate
:
{
xnor
:
function
(
done
)
{
if
((
this
.
field1
===
null
)
===
(
this
.
field2
===
null
))
{
done
(
'xnor failed'
);
}
else
{
done
();
}
}
}
});
Foo
.
build
({
field1
:
null
,
field2
:
null
})
.
validate
()
.
success
(
function
(
error
)
{
expect
(
error
).
not
.
to
.
be
.
null
;
expect
(
error
).
to
.
be
.
an
.
instanceOf
(
Error
);
expect
(
error
.
get
(
'xnor'
)[
0
].
message
).
to
.
equal
(
'xnor failed'
);
Foo
.
build
({
field1
:
33
,
field2
:
null
})
.
validate
()
.
success
(
function
(
errors
)
{
expect
(
errors
).
not
.
exist
;
done
();
});
});
});
it
(
'validates a model with no async callback'
,
function
(
done
)
{
var
Foo
=
this
.
sequelize
.
define
(
'Foo'
+
config
.
rand
(),
{
var
Foo
=
this
.
sequelize
.
define
(
'Foo'
+
config
.
rand
(),
{
field1
:
{
field1
:
{
type
:
Sequelize
.
INTEGER
,
type
:
Sequelize
.
INTEGER
,
...
@@ -664,25 +604,23 @@ describe(Support.getTestDialectTeaser('InstanceValidator'), function() {
...
@@ -664,25 +604,23 @@ describe(Support.getTestDialectTeaser('InstanceValidator'), function() {
}
}
});
});
Foo
return
Foo
.
build
({
field1
:
null
,
field2
:
null
})
.
build
({
field1
:
null
,
field2
:
null
})
.
validate
()
.
validate
()
.
success
(
function
(
error
)
{
.
then
(
function
(
error
)
{
expect
(
error
).
not
.
to
.
be
.
null
;
expect
(
error
).
not
.
to
.
be
.
null
;
expect
(
error
).
to
.
be
.
an
.
instanceOf
(
Error
);
expect
(
error
).
to
.
be
.
an
.
instanceOf
(
Error
);
expect
(
error
.
get
(
'xnor'
)[
0
].
message
).
to
.
equal
(
'xnor failed'
);
expect
(
error
.
get
(
'xnor'
)[
0
].
message
).
to
.
equal
(
'xnor failed'
);
return
Foo
Foo
.
build
({
field1
:
33
,
field2
:
null
})
.
build
({
field1
:
33
,
field2
:
null
})
.
validate
()
.
validate
()
.
success
(
function
(
errors
)
{
.
then
(
function
(
errors
)
{
expect
(
errors
).
not
.
exist
;
expect
(
errors
).
not
.
exist
;
done
();
});
});
});
});
});
});
it
(
'validates model with a validator whose arg is an Array successfully twice in a row'
,
function
(
done
)
{
it
(
'validates model with a validator whose arg is an Array successfully twice in a row'
,
function
()
{
var
Foo
=
this
.
sequelize
.
define
(
'Foo'
+
config
.
rand
(),
{
var
Foo
=
this
.
sequelize
.
define
(
'Foo'
+
config
.
rand
(),
{
bar
:
{
bar
:
{
type
:
Sequelize
.
STRING
,
type
:
Sequelize
.
STRING
,
...
@@ -693,11 +631,10 @@ describe(Support.getTestDialectTeaser('InstanceValidator'), function() {
...
@@ -693,11 +631,10 @@ describe(Support.getTestDialectTeaser('InstanceValidator'), function() {
}),
foo
;
}),
foo
;
foo
=
Foo
.
build
({
bar
:
'a'
});
foo
=
Foo
.
build
({
bar
:
'a'
});
foo
.
validate
().
success
(
function
(
errors
)
{
return
foo
.
validate
().
then
(
function
(
errors
)
{
expect
(
errors
).
not
.
to
.
exist
;
expect
(
errors
).
not
.
to
.
exist
;
foo
.
validate
().
success
(
function
(
errors
)
{
return
foo
.
validate
().
then
(
function
(
errors
)
{
expect
(
errors
).
not
.
to
.
exist
;
expect
(
errors
).
not
.
to
.
exist
;
done
();
});
});
});
});
});
});
...
@@ -717,14 +654,14 @@ describe(Support.getTestDialectTeaser('InstanceValidator'), function() {
...
@@ -717,14 +654,14 @@ describe(Support.getTestDialectTeaser('InstanceValidator'), function() {
var
failingBar
=
Bar
.
build
({
field
:
'value3'
});
var
failingBar
=
Bar
.
build
({
field
:
'value3'
});
failingBar
.
validate
().
success
(
function
(
errors
)
{
return
failingBar
.
validate
().
then
(
function
(
errors
)
{
expect
(
errors
).
not
.
to
.
be
.
null
;
expect
(
errors
).
not
.
to
.
be
.
null
;
expect
(
errors
.
get
(
'field'
)).
to
.
have
.
length
(
1
);
expect
(
errors
.
get
(
'field'
)).
to
.
have
.
length
(
1
);
expect
(
errors
.
get
(
'field'
)[
0
].
message
).
to
.
equal
(
'Validation isIn failed'
);
expect
(
errors
.
get
(
'field'
)[
0
].
message
).
to
.
equal
(
'Validation isIn failed'
);
});
});
});
});
it
(
'skips validations for the given fields'
,
function
(
done
)
{
it
(
'skips validations for the given fields'
,
function
()
{
var
values
=
[
'value1'
,
'value2'
];
var
values
=
[
'value1'
,
'value2'
];
var
Bar
=
this
.
sequelize
.
define
(
'Bar'
+
config
.
rand
(),
{
var
Bar
=
this
.
sequelize
.
define
(
'Bar'
+
config
.
rand
(),
{
...
@@ -739,9 +676,8 @@ describe(Support.getTestDialectTeaser('InstanceValidator'), function() {
...
@@ -739,9 +676,8 @@ describe(Support.getTestDialectTeaser('InstanceValidator'), function() {
var
failingBar
=
Bar
.
build
({
field
:
'value3'
});
var
failingBar
=
Bar
.
build
({
field
:
'value3'
});
failingBar
.
validate
({
skip
:
[
'field'
]
}).
success
(
function
(
errors
)
{
return
failingBar
.
validate
({
skip
:
[
'field'
]
}).
then
(
function
(
errors
)
{
expect
(
errors
).
not
.
to
.
exist
;
expect
(
errors
).
not
.
to
.
exist
;
done
();
});
});
});
});
...
@@ -764,7 +700,7 @@ describe(Support.getTestDialectTeaser('InstanceValidator'), function() {
...
@@ -764,7 +700,7 @@ describe(Support.getTestDialectTeaser('InstanceValidator'), function() {
});
});
});
});
it
(
'raises an error if saving a different value into an immutable field'
,
function
(
done
)
{
it
(
'raises an error if saving a different value into an immutable field'
,
function
()
{
var
User
=
this
.
sequelize
.
define
(
'User'
,
{
var
User
=
this
.
sequelize
.
define
(
'User'
,
{
name
:
{
name
:
{
type
:
Sequelize
.
STRING
,
type
:
Sequelize
.
STRING
,
...
@@ -774,22 +710,21 @@ describe(Support.getTestDialectTeaser('InstanceValidator'), function() {
...
@@ -774,22 +710,21 @@ describe(Support.getTestDialectTeaser('InstanceValidator'), function() {
}
}
});
});
User
.
sync
({
force
:
true
}).
success
(
function
()
{
return
User
.
sync
({
force
:
true
}).
then
(
function
()
{
User
.
create
({
name
:
'RedCat'
}).
success
(
function
(
user
)
{
return
User
.
create
({
name
:
'RedCat'
}).
then
(
function
(
user
)
{
expect
(
user
.
getDataValue
(
'name'
)).
to
.
equal
(
'RedCat'
);
expect
(
user
.
getDataValue
(
'name'
)).
to
.
equal
(
'RedCat'
);
user
.
setDataValue
(
'name'
,
'YellowCat'
);
user
.
setDataValue
(
'name'
,
'YellowCat'
);
user
.
save
()
return
user
.
save
()
.
done
(
function
(
errors
)
{
.
catch
(
function
(
errors
)
{
expect
(
errors
).
to
.
not
.
be
.
null
;
expect
(
errors
).
to
.
not
.
be
.
null
;
expect
(
errors
).
to
.
be
.
an
.
instanceOf
(
Error
);
expect
(
errors
).
to
.
be
.
an
.
instanceOf
(
Error
);
expect
(
errors
.
get
(
'name'
)[
0
].
message
).
to
.
eql
(
'Validation isImmutable failed'
);
expect
(
errors
.
get
(
'name'
)[
0
].
message
).
to
.
eql
(
'Validation isImmutable failed'
);
done
();
});
});
});
});
});
});
});
});
it
(
'allows setting an immutable field if the record is unsaved'
,
function
(
done
)
{
it
(
'allows setting an immutable field if the record is unsaved'
,
function
()
{
var
User
=
this
.
sequelize
.
define
(
'User'
,
{
var
User
=
this
.
sequelize
.
define
(
'User'
,
{
name
:
{
name
:
{
type
:
Sequelize
.
STRING
,
type
:
Sequelize
.
STRING
,
...
@@ -803,114 +738,106 @@ describe(Support.getTestDialectTeaser('InstanceValidator'), function() {
...
@@ -803,114 +738,106 @@ describe(Support.getTestDialectTeaser('InstanceValidator'), function() {
expect
(
user
.
getDataValue
(
'name'
)).
to
.
equal
(
'RedCat'
);
expect
(
user
.
getDataValue
(
'name'
)).
to
.
equal
(
'RedCat'
);
user
.
setDataValue
(
'name'
,
'YellowCat'
);
user
.
setDataValue
(
'name'
,
'YellowCat'
);
user
.
validate
().
success
(
function
(
errors
)
{
return
user
.
validate
().
then
(
function
(
errors
)
{
expect
(
errors
).
not
.
to
.
be
.
ok
;
expect
(
errors
).
not
.
to
.
be
.
ok
;
done
();
});
});
});
});
it
(
'raises an error for array on a STRING'
,
function
(
done
)
{
it
(
'raises an error for array on a STRING'
,
function
()
{
var
User
=
this
.
sequelize
.
define
(
'User'
,
{
var
User
=
this
.
sequelize
.
define
(
'User'
,
{
'email'
:
{
'email'
:
{
type
:
Sequelize
.
STRING
type
:
Sequelize
.
STRING
}
}
});
});
User
.
build
({
return
User
.
build
({
email
:
[
'iama'
,
'dummy.com'
]
email
:
[
'iama'
,
'dummy.com'
]
}).
validate
().
success
(
function
(
errors
)
{
}).
validate
().
then
(
function
(
errors
)
{
expect
(
errors
).
to
.
be
.
an
.
instanceof
(
Sequelize
.
ValidationError
);
expect
(
errors
).
to
.
be
.
an
.
instanceof
(
Sequelize
.
ValidationError
);
done
();
});
});
});
});
it
(
'raises an error for array on a STRING(20)'
,
function
(
done
)
{
it
(
'raises an error for array on a STRING(20)'
,
function
()
{
var
User
=
this
.
sequelize
.
define
(
'User'
,
{
var
User
=
this
.
sequelize
.
define
(
'User'
,
{
'email'
:
{
'email'
:
{
type
:
Sequelize
.
STRING
(
20
)
type
:
Sequelize
.
STRING
(
20
)
}
}
});
});
User
.
build
({
return
User
.
build
({
email
:
[
'iama'
,
'dummy.com'
]
email
:
[
'iama'
,
'dummy.com'
]
}).
validate
().
success
(
function
(
errors
)
{
}).
validate
().
then
(
function
(
errors
)
{
expect
(
errors
).
to
.
be
.
an
.
instanceof
(
Sequelize
.
ValidationError
);
expect
(
errors
).
to
.
be
.
an
.
instanceof
(
Sequelize
.
ValidationError
);
done
();
});
});
});
});
it
(
'raises an error for array on a TEXT'
,
function
(
done
)
{
it
(
'raises an error for array on a TEXT'
,
function
()
{
var
User
=
this
.
sequelize
.
define
(
'User'
,
{
var
User
=
this
.
sequelize
.
define
(
'User'
,
{
'email'
:
{
'email'
:
{
type
:
Sequelize
.
TEXT
type
:
Sequelize
.
TEXT
}
}
});
});
User
.
build
({
return
User
.
build
({
email
:
[
'iama'
,
'dummy.com'
]
email
:
[
'iama'
,
'dummy.com'
]
}).
validate
().
success
(
function
(
errors
)
{
}).
validate
().
then
(
function
(
errors
)
{
expect
(
errors
).
to
.
be
.
an
.
instanceof
(
Sequelize
.
ValidationError
);
expect
(
errors
).
to
.
be
.
an
.
instanceof
(
Sequelize
.
ValidationError
);
done
();
});
});
});
});
it
(
'raises an error for {} on a STRING'
,
function
(
done
)
{
it
(
'raises an error for {} on a STRING'
,
function
()
{
var
User
=
this
.
sequelize
.
define
(
'User'
,
{
var
User
=
this
.
sequelize
.
define
(
'User'
,
{
'email'
:
{
'email'
:
{
type
:
Sequelize
.
STRING
type
:
Sequelize
.
STRING
}
}
});
});
User
.
build
({
return
User
.
build
({
email
:
{
lol
:
true
}
email
:
{
lol
:
true
}
}).
validate
().
success
(
function
(
errors
)
{
}).
validate
().
then
(
function
(
errors
)
{
expect
(
errors
).
to
.
be
.
an
.
instanceof
(
Sequelize
.
ValidationError
);
expect
(
errors
).
to
.
be
.
an
.
instanceof
(
Sequelize
.
ValidationError
);
done
();
});
});
});
});
it
(
'raises an error for {} on a STRING(20)'
,
function
(
done
)
{
it
(
'raises an error for {} on a STRING(20)'
,
function
()
{
var
User
=
this
.
sequelize
.
define
(
'User'
,
{
var
User
=
this
.
sequelize
.
define
(
'User'
,
{
'email'
:
{
'email'
:
{
type
:
Sequelize
.
STRING
(
20
)
type
:
Sequelize
.
STRING
(
20
)
}
}
});
});
User
.
build
({
return
User
.
build
({
email
:
{
lol
:
true
}
email
:
{
lol
:
true
}
}).
validate
().
success
(
function
(
errors
)
{
}).
validate
().
then
(
function
(
errors
)
{
expect
(
errors
).
to
.
be
.
an
.
instanceof
(
Sequelize
.
ValidationError
);
expect
(
errors
).
to
.
be
.
an
.
instanceof
(
Sequelize
.
ValidationError
);
done
();
});
});
});
});
it
(
'raises an error for {} on a TEXT'
,
function
(
done
)
{
it
(
'raises an error for {} on a TEXT'
,
function
()
{
var
User
=
this
.
sequelize
.
define
(
'User'
,
{
var
User
=
this
.
sequelize
.
define
(
'User'
,
{
'email'
:
{
'email'
:
{
type
:
Sequelize
.
TEXT
type
:
Sequelize
.
TEXT
}
}
});
});
User
.
build
({
return
User
.
build
({
email
:
{
lol
:
true
}
email
:
{
lol
:
true
}
}).
validate
().
success
(
function
(
errors
)
{
}).
validate
().
then
(
function
(
errors
)
{
expect
(
errors
).
to
.
be
.
an
.
instanceof
(
Sequelize
.
ValidationError
);
expect
(
errors
).
to
.
be
.
an
.
instanceof
(
Sequelize
.
ValidationError
);
done
();
});
});
});
});
it
(
'does not raise an error for null on a STRING (where null is allowed)'
,
function
(
done
)
{
it
(
'does not raise an error for null on a STRING (where null is allowed)'
,
function
()
{
var
User
=
this
.
sequelize
.
define
(
'User'
,
{
var
User
=
this
.
sequelize
.
define
(
'User'
,
{
'email'
:
{
'email'
:
{
type
:
Sequelize
.
STRING
type
:
Sequelize
.
STRING
}
}
});
});
User
.
build
({
return
User
.
build
({
email
:
null
email
:
null
}).
validate
().
success
(
function
(
errors
)
{
}).
validate
().
then
(
function
(
errors
)
{
expect
(
errors
).
not
.
to
.
be
.
ok
;
expect
(
errors
).
not
.
to
.
be
.
ok
;
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