loopback-datasource-juggler/test/validations_test.coffee

114 lines
3.0 KiB
CoffeeScript
Raw Normal View History

2011-10-10 13:22:51 +00:00
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
2011-10-11 19:52:03 +00:00
pendingPeriod: Number
createdByAdmin: Boolean
createdByScript: Boolean
updatedAt: Date
2011-10-10 13:22:51 +00:00
validAttributes =
name: 'Anatoliy'
email: 'email@example.com'
state: ''
age: 26
gender: 'male'
domain: '1602'
2011-10-11 19:52:03 +00:00
createdByAdmin: false
createdByScript: true
2011-10-10 13:22:51 +00:00
2011-11-19 08:58:49 +00:00
getValidAttributes = ->
name: 'Anatoliy'
email: 'email@example.com'
state: ''
age: 26
gender: 'male'
domain: '1602'
createdByAdmin: false
createdByScript: true
2011-10-10 13:22:51 +00:00
it 'should validate presence', (test) ->
2011-10-11 19:52:03 +00:00
User.validatesPresenceOf 'email', 'name'
2011-10-10 13:22:51 +00:00
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()
2011-10-11 19:52:03 +00:00
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
2011-10-10 13:22:51 +00:00
test.done()
2013-01-21 17:48:10 +00:00
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.save ->
dmk = new Airport code: 'DMK', city: 'Bangkok'
dmk.isValid (valid) ->
test.ok valid
dmk.save ->
2013-01-21 20:48:12 +00:00
dmk.updateAttributes city: 'Bangkok, Don Muang', (err) ->
test.ok !err
dmk.save ->
dmk.code = 'BKK'
2013-01-21 17:48:10 +00:00
dmk.isValid (valid) ->
2013-01-21 20:48:12 +00:00
test.ok !valid
dmk.code = 'DMK'
dmk.isValid (valid) ->
test.ok valid
test.done()