Rewrite validations in mocha

This commit is contained in:
Anatoliy Chakkaev 2013-03-25 02:21:18 +04:00
parent 6a9f460b18
commit 716318bedd
2 changed files with 123 additions and 113 deletions

123
test/validations.test.js Normal file
View File

@ -0,0 +1,123 @@
var j = require('../'), db, User;
function getValidAttributes() {
return {
name: 'Anatoliy',
email: 'email@example.com',
state: '',
age: 26,
gender: 'male',
createdByAdmin: false,
createdByScript: true
};
}
describe('validations', function() {
beforeEach(function() {
db = new j.Schema('memory');
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
});
});
describe('presence', function() {
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;
});
it('should skip validation by property (if/unless)', function() {
User.validatesPresenceOf('pendingPeriod', {if: 'createdByAdmin'});
User.validatesPresenceOf('domain', {unless: 'createdByScript'});
var user = new User(getValidAttributes())
user.isValid().should.be.true;
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;
user.createdByScript = false;
user.isValid().should.be.false;
user.errors.domain.should.eql(['can\'t be blank']);
user.domain = 'domain';
user.isValid().should.be.true;
});
});
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();
});
});
})).should.be.false;
});
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 = 'hey';
u.save(done);
});
})).should.be.false;
});
});
describe('format', function() {
it('should validate format');
it('should overwrite default blank message with custom format message');
});
describe('numericality', function() {
it('should validate numericality');
});
describe('inclusion', function() {
it('should validate inclusion');
});
describe('exclusion', function() {
it('should validate exclusion');
});
describe('length', function() {
it('should validate length');
});
describe('custom', function() {
it('should validate using custom sync validation');
it('should validate using custom async validation');
});
});

View File

@ -1,113 +0,0 @@
juggling = require('../index')
Schema = juggling.Schema
AbstractClass = juggling.AbstractClass
Validatable = juggling.Validatable
require('./spec_helper').init module.exports
schema = new Schema 'memory'
User = schema.define 'User',
email: String
name: String
password: String
state: String
age: Number
gender: String
domain: String
pendingPeriod: Number
createdByAdmin: Boolean
createdByScript: Boolean
updatedAt: Date
validAttributes =
name: 'Anatoliy'
email: 'email@example.com'
state: ''
age: 26
gender: 'male'
domain: '1602'
createdByAdmin: false
createdByScript: true
getValidAttributes = ->
name: 'Anatoliy'
email: 'email@example.com'
state: ''
age: 26
gender: 'male'
domain: '1602'
createdByAdmin: false
createdByScript: true
it 'should validate presence', (test) ->
User.validatesPresenceOf 'email', 'name'
user = new User
test.ok not user.isValid(), 'User is not valid'
test.ok user.errors.email, 'Attr email in errors'
test.ok user.errors.name, 'Attr name in errors'
user.name = 'Anatoliy'
test.ok not user.isValid(), 'User is still not valid'
test.ok user.errors.email, 'Attr email still in errors'
test.ok not user.errors.name, 'Attr name valid'
user.email = 'anatoliy@localhost'
test.ok user.isValid(), 'User is valid'
test.ok not user.errors, 'No errors'
test.ok not user.errors.email, 'Attr email valid'
test.ok not user.errors.name, 'Attr name valid'
test.done()
it 'should allow to skip validations', (test) ->
User.validatesPresenceOf 'pendingPeriod', if: 'createdByAdmin'
User.validatesLengthOf 'domain', is: 2, unless: 'createdByScript'
user = new User validAttributes
test.ok user.isValid()
user.createdByAdmin = true
test.ok not user.isValid()
test.ok user.errors.pendingPeriod.length
user.pendingPeriod = 1
test.ok user.isValid()
user.createdByScript = false
test.ok not user.isValid()
test.ok user.errors.domain.length
user.domain = '12'
test.ok user.isValid()
User.validatesLengthOf 'domain', is: 3, unless: -> @domain != 'xyz'
test.ok user.isValid()
user.domain = 'xyz'
test.ok not user.isValid() # is: 3 passed, but is: 2 failed
test.done()
it 'should validate uniqueness', (test) ->
Airport = schema.define 'Airport', code: String, city: String
Airport.validatesUniquenessOf 'code'
bkk = new Airport code: 'BKK', city: 'Bangkok'
bkk.isValid (valid) ->
test.ok valid
bkk.updateAttribute 'code', 'BKK', ->
dmk = new Airport code: 'DMK', city: 'Bangkok'
dmk.isValid (valid) ->
test.ok valid
dmk.save ->
dmk.updateAttributes city: 'Bangkok, Don Muang', (err) ->
test.ok !err
dmk.save ->
dmk.code = 'BKK'
dmk.isValid (valid) ->
test.ok !valid
dmk.code = 'DMK'
dmk.isValid (valid) ->
test.ok valid
test.done()