loopback-datasource-juggler/test/validations.test.js

203 lines
5.1 KiB
JavaScript
Raw Normal View History

2013-04-06 10:57:12 +00:00
// This test written in mocha+should.js
var should = require('./init.js');
2013-03-24 22:21:18 +00:00
var j = require('../'), db, User;
var ValidationError = j.ValidationError;
2013-03-24 22:21:18 +00:00
function getValidAttributes() {
2014-01-24 17:09:53 +00:00
return {
name: 'Anatoliy',
email: 'email@example.com',
state: '',
age: 26,
gender: 'male',
createdByAdmin: false,
createdByScript: true
};
2013-03-24 22:21:18 +00:00
}
2014-01-24 17:09:53 +00:00
describe('validations', function () {
before(function (done) {
db = getSchema();
User = db.define('User', {
email: String,
name: String,
password: String,
state: String,
age: Number,
gender: String,
domain: String,
pendingPeriod: Number,
createdByAdmin: Boolean,
createdByScript: Boolean,
updatedAt: Date
});
2014-01-24 17:09:53 +00:00
db.automigrate(done);
});
2014-01-24 17:09:53 +00:00
beforeEach(function (done) {
User.destroyAll(function () {
delete User._validations;
done();
2013-03-26 00:41:00 +00:00
});
2014-01-24 17:09:53 +00:00
});
2013-03-26 00:41:00 +00:00
2014-01-24 17:09:53 +00:00
after(function () {
db.disconnect();
});
2014-01-24 17:09:53 +00:00
describe('commons', function () {
2013-04-01 16:08:22 +00:00
2014-01-24 17:09:53 +00:00
describe('skipping', function () {
2013-04-01 16:08:22 +00:00
2014-01-24 17:09:53 +00:00
it('should allow to skip using if: attribute', function () {
User.validatesPresenceOf('pendingPeriod', {if: 'createdByAdmin'});
var user = new User;
user.createdByAdmin = true;
user.isValid().should.be.false;
user.errors.pendingPeriod.should.eql(['can\'t be blank']);
user.pendingPeriod = 1
user.isValid().should.be.true;
});
2013-04-01 16:08:22 +00:00
2014-01-24 17:09:53 +00:00
});
2013-04-01 16:08:22 +00:00
2014-01-24 17:09:53 +00:00
describe('lifecycle', function () {
2013-04-01 16:10:46 +00:00
2014-01-24 17:09:53 +00:00
it('should work on create', function (done) {
delete User._validations;
User.validatesPresenceOf('name');
User.create(function (e, u) {
should.exist(e);
User.create({name: 'Valid'}, function (e, d) {
should.not.exist(e);
done();
});
});
});
it('should work on update', function (done) {
delete User._validations;
User.validatesPresenceOf('name');
User.create({name: 'Valid'}, function (e, d) {
d.updateAttribute('name', null, function (e) {
should.exist(e);
e.should.be.instanceOf(Error);
e.should.be.instanceOf(ValidationError);
d.updateAttribute('name', 'Vasiliy', function (e) {
should.not.exist(e);
done();
2013-04-04 15:31:07 +00:00
});
2014-01-24 17:09:53 +00:00
})
});
});
it('should return error code', function (done) {
delete User._validations;
User.validatesPresenceOf('name');
User.create(function (e, u) {
should.exist(e);
e.details.codes.name.should.eql(['presence']);
done();
});
});
2013-04-04 15:31:07 +00:00
2014-01-24 17:09:53 +00:00
it('should allow to modify error after validation', function (done) {
User.afterValidate = function (next) {
next();
};
done();
});
2013-04-30 18:47:35 +00:00
});
2014-01-24 17:09:53 +00:00
});
2014-01-24 17:09:53 +00:00
describe('presence', function () {
2013-03-24 22:21:18 +00:00
2014-01-24 17:09:53 +00:00
it('should validate presence', function () {
User.validatesPresenceOf('name', 'email');
var u = new User;
u.isValid().should.not.be.true;
u.name = 1;
u.email = 2;
u.isValid().should.be.true;
});
2013-03-24 22:21:18 +00:00
2014-01-24 17:09:53 +00:00
it('should skip validation by property (if/unless)', function () {
User.validatesPresenceOf('domain', {unless: 'createdByScript'});
2013-03-24 22:21:18 +00:00
2014-01-24 17:09:53 +00:00
var user = new User(getValidAttributes())
user.isValid().should.be.true;
2013-03-24 22:21:18 +00:00
2014-01-24 17:09:53 +00:00
user.createdByScript = false;
user.isValid().should.be.false;
user.errors.domain.should.eql(['can\'t be blank']);
2013-03-24 22:21:18 +00:00
2014-01-24 17:09:53 +00:00
user.domain = 'domain';
user.isValid().should.be.true;
2013-03-24 22:21:18 +00:00
});
2014-01-24 17:09:53 +00:00
});
describe('uniqueness', function () {
it('should validate uniqueness', function (done) {
User.validatesUniquenessOf('email');
var u = new User({email: 'hey'});
Boolean(u.isValid(function (valid) {
valid.should.be.true;
u.save(function () {
var u2 = new User({email: 'hey'});
u2.isValid(function (valid) {
valid.should.be.false;
done();
});
2013-03-24 22:21:18 +00:00
});
2014-01-24 17:09:53 +00:00
})).should.be.false;
});
2013-03-24 22:21:18 +00:00
2014-01-24 17:09:53 +00:00
it('should handle same object modification', function (done) {
User.validatesUniquenessOf('email');
var u = new User({email: 'hey'});
Boolean(u.isValid(function (valid) {
valid.should.be.true;
u.save(function () {
u.name = 'Goghi';
u.isValid(function (valid) {
valid.should.be.true;
u.save(done);
});
2013-03-24 22:21:18 +00:00
});
2014-01-24 17:09:53 +00:00
// async validations always falsy when called as sync
})).should.not.be.ok;
2013-03-24 22:21:18 +00:00
});
2014-01-24 17:09:53 +00:00
});
2013-03-24 22:21:18 +00:00
2014-01-24 17:09:53 +00:00
describe('format', function () {
it('should validate format');
it('should overwrite default blank message with custom format message');
});
2013-03-24 22:21:18 +00:00
2014-01-24 17:09:53 +00:00
describe('numericality', function () {
it('should validate numericality');
});
2013-03-24 22:21:18 +00:00
2014-01-24 17:09:53 +00:00
describe('inclusion', function () {
it('should validate inclusion');
});
2013-03-24 22:21:18 +00:00
2014-01-24 17:09:53 +00:00
describe('exclusion', function () {
it('should validate exclusion');
});
2013-03-24 22:21:18 +00:00
2014-01-24 17:09:53 +00:00
describe('length', function () {
it('should validate length');
});
describe('custom', function () {
it('should validate using custom sync validation');
it('should validate using custom async validation');
});
2013-03-24 22:21:18 +00:00
});