2016-04-01 22:25:16 +00:00
|
|
|
// Copyright IBM Corp. 2015,2016. All Rights Reserved.
|
|
|
|
// Node module: loopback-datasource-juggler
|
|
|
|
// This file is licensed under the MIT License.
|
|
|
|
// License text available at https://opensource.org/licenses/MIT
|
|
|
|
|
2015-11-04 10:05:24 +00:00
|
|
|
// This test written in mocha+should.js
|
2016-08-22 19:55:22 +00:00
|
|
|
'use strict';
|
2016-04-07 22:22:46 +00:00
|
|
|
var async = require('async');
|
2015-11-04 10:05:24 +00:00
|
|
|
var should = require('./init.js');
|
2016-04-07 22:22:46 +00:00
|
|
|
var db, User, options, ModelWithForceId, whereCount = 0;
|
2015-11-04 10:05:24 +00:00
|
|
|
var j = require('../');
|
|
|
|
var ValidationError = j.ValidationError;
|
|
|
|
|
|
|
|
var INITIAL_NAME = 'Bert';
|
|
|
|
var NEW_NAME = 'Ernie';
|
2016-08-19 17:46:59 +00:00
|
|
|
var INVALID_DATA = {name: null};
|
|
|
|
var VALID_DATA = {name: INITIAL_NAME};
|
2015-11-04 10:05:24 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
describe('optional-validation', function() {
|
2015-11-04 10:05:24 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
before(function(done) {
|
2015-11-04 10:05:24 +00:00
|
|
|
db = getSchema();
|
2016-04-07 22:22:46 +00:00
|
|
|
ModelWithForceId = db.createModel(
|
|
|
|
'ModelWithForceId',
|
2016-08-19 17:46:59 +00:00
|
|
|
{name: String},
|
|
|
|
{forceId: true});
|
2015-11-04 10:05:24 +00:00
|
|
|
User = db.define('User', {
|
2016-08-19 17:46:59 +00:00
|
|
|
seq: {type: Number, index: true},
|
|
|
|
name: {type: String, index: true, sort: true},
|
|
|
|
email: {type: String, index: true},
|
|
|
|
birthday: {type: Date, index: true},
|
|
|
|
role: {type: String, index: true},
|
|
|
|
order: {type: Number, index: true, sort: true},
|
|
|
|
vip: {type: Boolean},
|
|
|
|
}, {forceId: true, strict: true});
|
2016-04-07 22:22:46 +00:00
|
|
|
db.automigrate(['ModelWithForceId', 'User'], done);
|
2015-11-04 10:05:24 +00:00
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
beforeEach(function(done) {
|
|
|
|
User.destroyAll(function() {
|
2015-11-04 10:05:24 +00:00
|
|
|
delete User.validations;
|
|
|
|
User.validatesPresenceOf('name');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
function expectValidationError(done) {
|
2016-04-01 11:48:17 +00:00
|
|
|
return function(err, result) {
|
2015-11-04 10:05:24 +00:00
|
|
|
should.exist(err);
|
|
|
|
err.should.be.instanceOf(Error);
|
|
|
|
err.should.be.instanceOf(ValidationError);
|
|
|
|
done();
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function expectCreateSuccess(data, done) {
|
|
|
|
if (done === undefined && typeof data === 'function') {
|
|
|
|
done = data;
|
2016-08-19 17:46:59 +00:00
|
|
|
data = {name: INITIAL_NAME};
|
2015-11-04 10:05:24 +00:00
|
|
|
}
|
|
|
|
return function(err, instance) {
|
|
|
|
if (err) return done(err);
|
|
|
|
instance.should.be.instanceOf(User);
|
|
|
|
if (data.name) {
|
|
|
|
instance.name.should.eql(data.name || INITIAL_NAME);
|
|
|
|
} else {
|
|
|
|
should.not.exist(instance.name);
|
|
|
|
}
|
|
|
|
done();
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function expectChangeSuccess(data, done) {
|
|
|
|
if (done === undefined && typeof data === 'function') {
|
|
|
|
done = data;
|
2016-08-19 17:46:59 +00:00
|
|
|
data = {name: NEW_NAME};
|
2015-11-04 10:05:24 +00:00
|
|
|
}
|
|
|
|
return function(err, instance) {
|
|
|
|
if (err) return done(err);
|
|
|
|
instance.should.be.instanceOf(User);
|
|
|
|
if (data.name) {
|
|
|
|
instance.name.should.eql(data.name || NEW_NAME);
|
|
|
|
} else {
|
|
|
|
should.not.exist(instance.name);
|
|
|
|
}
|
|
|
|
done();
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function createUserAndChangeName(name, cb) {
|
2016-08-19 17:46:59 +00:00
|
|
|
User.create(VALID_DATA, {validate: true}, function(err, d) {
|
2015-11-04 10:05:24 +00:00
|
|
|
d.name = name;
|
|
|
|
cb(err, d);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function createUser(cb) {
|
2016-08-19 17:46:59 +00:00
|
|
|
User.create(VALID_DATA, {validate: true}, cb);
|
2015-11-04 10:05:24 +00:00
|
|
|
}
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
function callUpdateOrCreateWithExistingUserId(name, options, cb) {
|
2016-08-19 17:46:59 +00:00
|
|
|
User.create({'name': 'Groover'}, function(err, user) {
|
2015-11-04 10:05:24 +00:00
|
|
|
if (err) return cb(err);
|
2016-08-19 17:46:59 +00:00
|
|
|
var data = {name: name};
|
2015-11-04 10:05:24 +00:00
|
|
|
data.id = user.id;
|
|
|
|
User.updateOrCreate(data, options, cb);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function getNewWhere() {
|
2016-08-19 17:46:59 +00:00
|
|
|
return {name: 'DoesNotExist' + (whereCount++)};
|
2015-11-04 10:05:24 +00:00
|
|
|
}
|
|
|
|
|
2016-04-07 22:22:46 +00:00
|
|
|
describe('forceId', function() {
|
|
|
|
context('replaceAttributes', function() {
|
|
|
|
it('should not fail if you do not pass the Primary key in data object',
|
|
|
|
function(done) {
|
2016-08-19 17:46:59 +00:00
|
|
|
ModelWithForceId.create({name: 'foo'}, function(err, created) {
|
2016-04-07 22:22:46 +00:00
|
|
|
if (err) return done(err);
|
2016-08-19 17:46:59 +00:00
|
|
|
created.replaceAttributes({name: 'bar'}, function(err, data) {
|
2016-04-07 22:22:46 +00:00
|
|
|
done(err);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should fail if you pass the Primary key in data object',
|
|
|
|
function(done) {
|
2016-08-19 17:46:59 +00:00
|
|
|
ModelWithForceId.create({name: 'foo'}, function(err, created) {
|
2016-04-07 22:22:46 +00:00
|
|
|
if (err) return done(err);
|
2016-08-19 17:46:59 +00:00
|
|
|
created.replaceAttributes({name: 'bar', id: 999},
|
2016-04-07 22:22:46 +00:00
|
|
|
function(err, data) {
|
|
|
|
should.exist(err);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
describe('no model setting', function() {
|
2015-11-04 10:05:24 +00:00
|
|
|
|
|
|
|
describe('method create', function() {
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should throw on create with validate:true with invalid data', function(done) {
|
2016-08-19 17:46:59 +00:00
|
|
|
User.create(INVALID_DATA, {validate: true}, expectValidationError(done));
|
2015-11-04 10:05:24 +00:00
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should NOT throw on create with validate:false with invalid data', function(done) {
|
2016-08-19 17:46:59 +00:00
|
|
|
User.create(INVALID_DATA, {validate: false}, expectCreateSuccess(INVALID_DATA, done));
|
2015-11-04 10:05:24 +00:00
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should NOT throw on create with validate:true with valid data', function(done) {
|
2016-08-19 17:46:59 +00:00
|
|
|
User.create(VALID_DATA, {validate: true}, expectCreateSuccess(done));
|
2015-11-04 10:05:24 +00:00
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should NOT throw on create with validate:false with valid data', function(done) {
|
2016-08-19 17:46:59 +00:00
|
|
|
User.create(VALID_DATA, {validate: false}, expectCreateSuccess(done));
|
2015-11-04 10:05:24 +00:00
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should throw on create with invalid data', function(done) {
|
2015-11-04 10:05:24 +00:00
|
|
|
User.create(INVALID_DATA, expectValidationError(done));
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should NOT throw on create with valid data', function(done) {
|
2015-11-04 10:05:24 +00:00
|
|
|
User.create(VALID_DATA, expectCreateSuccess(done));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('method findOrCreate', function() {
|
2016-04-01 13:23:42 +00:00
|
|
|
it('should throw on findOrCreate with validate:true with invalid data',
|
|
|
|
function(done) {
|
2016-08-19 17:46:59 +00:00
|
|
|
User.findOrCreate(getNewWhere(), INVALID_DATA, {validate: true},
|
2016-04-01 13:23:42 +00:00
|
|
|
expectValidationError(done));
|
2015-11-04 10:05:24 +00:00
|
|
|
});
|
|
|
|
|
2016-04-01 13:23:42 +00:00
|
|
|
it('should NOT throw on findOrCreate with validate:false with invalid data',
|
|
|
|
function(done) {
|
2016-08-19 17:46:59 +00:00
|
|
|
User.findOrCreate(getNewWhere(), INVALID_DATA, {validate: false},
|
2016-04-01 13:23:42 +00:00
|
|
|
expectCreateSuccess(INVALID_DATA, done));
|
2015-11-04 10:05:24 +00:00
|
|
|
});
|
|
|
|
|
2016-04-01 13:23:42 +00:00
|
|
|
it('should NOT throw on findOrCreate with validate:true with valid data',
|
|
|
|
function(done) {
|
2016-08-19 17:46:59 +00:00
|
|
|
User.findOrCreate(getNewWhere(), VALID_DATA, {validate: true},
|
2016-04-01 13:23:42 +00:00
|
|
|
expectCreateSuccess(done));
|
2015-11-04 10:05:24 +00:00
|
|
|
});
|
|
|
|
|
2016-04-01 13:23:42 +00:00
|
|
|
it('should NOT throw on findOrCreate with validate:false with valid data',
|
|
|
|
function(done) {
|
2016-08-19 17:46:59 +00:00
|
|
|
User.findOrCreate(getNewWhere(), VALID_DATA, {validate: false},
|
2016-04-01 13:23:42 +00:00
|
|
|
expectCreateSuccess(done));
|
2015-11-04 10:05:24 +00:00
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should throw on findOrCreate with invalid data', function(done) {
|
2015-11-04 10:05:24 +00:00
|
|
|
User.findOrCreate(getNewWhere(), INVALID_DATA, expectValidationError(done));
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should NOT throw on findOrCreate with valid data', function(done) {
|
2015-11-04 10:05:24 +00:00
|
|
|
User.findOrCreate(getNewWhere(), VALID_DATA, expectCreateSuccess(done));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('method updateOrCreate on existing data', function() {
|
2016-04-01 13:23:42 +00:00
|
|
|
it('should throw on updateOrCreate(id) with validate:true with invalid data',
|
|
|
|
function(done) {
|
2016-08-19 17:46:59 +00:00
|
|
|
callUpdateOrCreateWithExistingUserId(null, {validate: true},
|
2016-04-01 13:23:42 +00:00
|
|
|
expectValidationError(done));
|
2015-11-04 10:05:24 +00:00
|
|
|
});
|
|
|
|
|
2016-04-01 13:23:42 +00:00
|
|
|
it('should NOT throw on updateOrCreate(id) with validate:false with invalid data',
|
|
|
|
function(done) {
|
2016-08-19 17:46:59 +00:00
|
|
|
callUpdateOrCreateWithExistingUserId(null, {validate: false},
|
2016-04-01 13:23:42 +00:00
|
|
|
expectChangeSuccess(INVALID_DATA, done));
|
2015-11-04 10:05:24 +00:00
|
|
|
});
|
|
|
|
|
2016-04-01 13:23:42 +00:00
|
|
|
it('should NOT throw on updateOrCreate(id) with validate:true with valid data',
|
|
|
|
function(done) {
|
2016-08-19 17:46:59 +00:00
|
|
|
callUpdateOrCreateWithExistingUserId(NEW_NAME, {validate: true},
|
2016-04-01 13:23:42 +00:00
|
|
|
expectChangeSuccess(done));
|
2015-11-04 10:05:24 +00:00
|
|
|
});
|
|
|
|
|
2016-04-01 13:23:42 +00:00
|
|
|
it('should NOT throw on updateOrCreate(id) with validate:false with valid data',
|
|
|
|
function(done) {
|
2016-08-19 17:46:59 +00:00
|
|
|
callUpdateOrCreateWithExistingUserId(NEW_NAME, {validate: false},
|
2016-04-01 13:23:42 +00:00
|
|
|
expectChangeSuccess(done));
|
2015-11-04 10:05:24 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// backwards compatible with validateUpsert
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should NOT throw on updateOrCreate(id) with invalid data', function(done) {
|
2015-11-04 10:05:24 +00:00
|
|
|
callUpdateOrCreateWithExistingUserId(null, expectChangeSuccess(INVALID_DATA, done));
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should NOT throw on updateOrCreate(id) with valid data', function(done) {
|
2015-11-04 10:05:24 +00:00
|
|
|
callUpdateOrCreateWithExistingUserId(NEW_NAME, expectChangeSuccess(done));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('method save', function() {
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should throw on save with {validate:true} with invalid data', function(done) {
|
|
|
|
createUserAndChangeName(null, function(err, d) {
|
2016-08-19 17:46:59 +00:00
|
|
|
d.save({validate: true}, expectValidationError(done));
|
2015-11-04 10:05:24 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should NOT throw on save with {validate:false} with invalid data', function(done) {
|
|
|
|
createUserAndChangeName(null, function(err, d) {
|
2016-08-19 17:46:59 +00:00
|
|
|
d.save({validate: false}, expectChangeSuccess(INVALID_DATA, done));
|
2015-11-04 10:05:24 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should NOT throw on save with {validate:true} with valid data', function(done) {
|
|
|
|
createUserAndChangeName(NEW_NAME, function(err, d) {
|
2016-08-19 17:46:59 +00:00
|
|
|
d.save({validate: true}, expectChangeSuccess(done));
|
2015-11-04 10:05:24 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should NOT throw on save with {validate:false} with valid data', function(done) {
|
|
|
|
createUserAndChangeName(NEW_NAME, function(err, d) {
|
2016-08-19 17:46:59 +00:00
|
|
|
d.save({validate: false}, expectChangeSuccess(done));
|
2015-11-04 10:05:24 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should throw on save(cb) with invalid data', function(done) {
|
|
|
|
createUserAndChangeName(null, function(err, d) {
|
2015-11-04 10:05:24 +00:00
|
|
|
d.save(expectValidationError(done));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should NOT throw on save(cb) with valid data', function(done) {
|
|
|
|
createUserAndChangeName(NEW_NAME, function(err, d) {
|
2015-11-04 10:05:24 +00:00
|
|
|
d.save(expectChangeSuccess(done));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('method updateAttributes', function() {
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should throw on updateAttributes with {validate:true} with invalid data', function(done) {
|
|
|
|
createUser(function(err, d) {
|
2016-08-19 17:46:59 +00:00
|
|
|
d.updateAttributes(INVALID_DATA, {validate: true}, expectValidationError(done));
|
2015-11-04 10:05:24 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should NOT throw on updateAttributes with {validate:false} with invalid data', function(done) {
|
|
|
|
createUser(function(err, d) {
|
2016-08-19 17:46:59 +00:00
|
|
|
d.updateAttributes(INVALID_DATA, {validate: false}, expectChangeSuccess(INVALID_DATA, done));
|
2015-11-04 10:05:24 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should NOT throw on updateAttributes with {validate:true} with valid data', function(done) {
|
|
|
|
createUser(function(err, d) {
|
2016-08-19 17:46:59 +00:00
|
|
|
d.updateAttributes({'name': NEW_NAME}, {validate: true}, expectChangeSuccess(done));
|
2015-11-04 10:05:24 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should NOT throw on updateAttributes with {validate:false} with valid data', function(done) {
|
|
|
|
createUser(function(err, d) {
|
2016-08-19 17:46:59 +00:00
|
|
|
d.updateAttributes({'name': NEW_NAME}, {validate: false}, expectChangeSuccess(done));
|
2015-11-04 10:05:24 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should throw on updateAttributes(cb) with invalid data', function(done) {
|
|
|
|
createUser(function(err, d) {
|
2015-11-04 10:05:24 +00:00
|
|
|
d.updateAttributes(INVALID_DATA, expectValidationError(done));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should NOT throw on updateAttributes(cb) with valid data', function(done) {
|
|
|
|
createUser(function(err, d) {
|
2016-08-19 17:46:59 +00:00
|
|
|
d.updateAttributes({'name': NEW_NAME}, expectChangeSuccess(done));
|
2015-11-04 10:05:24 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
describe('model setting: automaticValidation: false', function() {
|
2015-11-04 10:05:24 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
before(function(done) {
|
2015-11-04 10:05:24 +00:00
|
|
|
User.settings.automaticValidation = false;
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('method create', function() {
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should throw on create with validate:true with invalid data', function(done) {
|
2016-08-19 17:46:59 +00:00
|
|
|
User.create(INVALID_DATA, {validate: true}, expectValidationError(done));
|
2015-11-04 10:05:24 +00:00
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should NOT throw on create with validate:false with invalid data', function(done) {
|
2016-08-19 17:46:59 +00:00
|
|
|
User.create(INVALID_DATA, {validate: false}, expectCreateSuccess(INVALID_DATA, done));
|
2015-11-04 10:05:24 +00:00
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should NOT throw on create with validate:true with valid data', function(done) {
|
2016-08-19 17:46:59 +00:00
|
|
|
User.create(VALID_DATA, {validate: true}, expectCreateSuccess(done));
|
2015-11-04 10:05:24 +00:00
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should NOT throw on create with validate:false with valid data', function(done) {
|
2016-08-19 17:46:59 +00:00
|
|
|
User.create(VALID_DATA, {validate: false}, expectCreateSuccess(done));
|
2015-11-04 10:05:24 +00:00
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should NOT throw on create with invalid data', function(done) {
|
2015-11-04 10:05:24 +00:00
|
|
|
User.create(INVALID_DATA, expectCreateSuccess(INVALID_DATA, done));
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should NOT throw on create with valid data', function(done) {
|
2015-11-04 10:05:24 +00:00
|
|
|
User.create(VALID_DATA, expectCreateSuccess(done));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('method findOrCreate', function() {
|
2016-04-01 13:23:42 +00:00
|
|
|
it('should throw on findOrCreate with validate:true with invalid data',
|
|
|
|
function(done) {
|
2016-08-19 17:46:59 +00:00
|
|
|
User.findOrCreate(getNewWhere(), INVALID_DATA, {validate: true},
|
2016-04-01 13:23:42 +00:00
|
|
|
expectValidationError(done));
|
2015-11-04 10:05:24 +00:00
|
|
|
});
|
|
|
|
|
2016-04-01 13:23:42 +00:00
|
|
|
it('should NOT throw on findOrCreate with validate:false with invalid data',
|
|
|
|
function(done) {
|
2016-08-19 17:46:59 +00:00
|
|
|
User.findOrCreate(getNewWhere(), INVALID_DATA, {validate: false},
|
2016-04-01 13:23:42 +00:00
|
|
|
expectCreateSuccess(INVALID_DATA, done));
|
2015-11-04 10:05:24 +00:00
|
|
|
});
|
|
|
|
|
2016-04-01 13:23:42 +00:00
|
|
|
it('should NOT throw on findOrCreate with validate:true with valid data',
|
|
|
|
function(done) {
|
2016-08-19 17:46:59 +00:00
|
|
|
User.findOrCreate(getNewWhere(), VALID_DATA, {validate: true},
|
2016-04-01 13:23:42 +00:00
|
|
|
expectCreateSuccess(done));
|
2015-11-04 10:05:24 +00:00
|
|
|
});
|
|
|
|
|
2016-04-01 13:23:42 +00:00
|
|
|
it('should NOT throw on findOrCreate with validate:false with valid data',
|
|
|
|
function(done) {
|
2016-08-19 17:46:59 +00:00
|
|
|
User.findOrCreate(getNewWhere(), VALID_DATA, {validate: false},
|
2016-04-01 13:23:42 +00:00
|
|
|
expectCreateSuccess(done));
|
2015-11-04 10:05:24 +00:00
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should NOT throw on findOrCreate with invalid data', function(done) {
|
2016-04-01 13:23:42 +00:00
|
|
|
User.findOrCreate(getNewWhere(), INVALID_DATA,
|
|
|
|
expectCreateSuccess(INVALID_DATA, done));
|
2015-11-04 10:05:24 +00:00
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should NOT throw on findOrCreate with valid data', function(done) {
|
2015-11-04 10:05:24 +00:00
|
|
|
User.findOrCreate(getNewWhere(), VALID_DATA, expectCreateSuccess(done));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('method updateOrCreate on existing data', function() {
|
2016-04-01 13:23:42 +00:00
|
|
|
it('should throw on updateOrCreate(id) with validate:true with invalid data',
|
|
|
|
function(done) {
|
2016-08-19 17:46:59 +00:00
|
|
|
callUpdateOrCreateWithExistingUserId(null, {validate: true},
|
2016-04-01 13:23:42 +00:00
|
|
|
expectValidationError(done));
|
2015-11-04 10:05:24 +00:00
|
|
|
});
|
|
|
|
|
2016-04-01 13:23:42 +00:00
|
|
|
it('should NOT throw on updateOrCreate(id) with validate:false with invalid data',
|
|
|
|
function(done) {
|
2016-08-19 17:46:59 +00:00
|
|
|
callUpdateOrCreateWithExistingUserId(null, {validate: false},
|
2016-04-01 13:23:42 +00:00
|
|
|
expectChangeSuccess(INVALID_DATA, done));
|
2015-11-04 10:05:24 +00:00
|
|
|
});
|
|
|
|
|
2016-04-01 13:23:42 +00:00
|
|
|
it('should NOT throw on updateOrCreate(id) with validate:true with valid data',
|
|
|
|
function(done) {
|
2016-08-19 17:46:59 +00:00
|
|
|
callUpdateOrCreateWithExistingUserId(NEW_NAME, {validate: true},
|
2016-04-01 13:23:42 +00:00
|
|
|
expectChangeSuccess(done));
|
2015-11-04 10:05:24 +00:00
|
|
|
});
|
|
|
|
|
2016-04-01 13:23:42 +00:00
|
|
|
it('should NOT throw on updateOrCreate(id) with validate:false with valid data',
|
|
|
|
function(done) {
|
2016-08-19 17:46:59 +00:00
|
|
|
callUpdateOrCreateWithExistingUserId(NEW_NAME, {validate: false},
|
2016-04-01 13:23:42 +00:00
|
|
|
expectChangeSuccess(done));
|
2015-11-04 10:05:24 +00:00
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should NOT throw on updateOrCreate(id) with invalid data', function(done) {
|
2015-11-04 10:05:24 +00:00
|
|
|
callUpdateOrCreateWithExistingUserId(null, expectChangeSuccess(INVALID_DATA, done));
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should NOT throw on updateOrCreate(id) with valid data', function(done) {
|
2015-11-04 10:05:24 +00:00
|
|
|
callUpdateOrCreateWithExistingUserId(NEW_NAME, expectChangeSuccess(done));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('method save', function() {
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should throw on save with {validate:true} with invalid data', function(done) {
|
|
|
|
createUserAndChangeName(null, function(err, d) {
|
2016-08-19 17:46:59 +00:00
|
|
|
d.save({validate: true}, expectValidationError(done));
|
2015-11-04 10:05:24 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should NOT throw on save with {validate:false} with invalid data', function(done) {
|
|
|
|
createUserAndChangeName(null, function(err, d) {
|
2016-08-19 17:46:59 +00:00
|
|
|
d.save({validate: false}, expectChangeSuccess(INVALID_DATA, done));
|
2015-11-04 10:05:24 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should NOT throw on save with {validate:true} with valid data', function(done) {
|
|
|
|
createUserAndChangeName(NEW_NAME, function(err, d) {
|
2016-08-19 17:46:59 +00:00
|
|
|
d.save({validate: true}, expectChangeSuccess(done));
|
2015-11-04 10:05:24 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should NOT throw on save with {validate:false} with valid data', function(done) {
|
|
|
|
createUserAndChangeName(NEW_NAME, function(err, d) {
|
2016-08-19 17:46:59 +00:00
|
|
|
d.save({validate: false}, expectChangeSuccess(done));
|
2015-11-04 10:05:24 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should NOT throw on save(cb) with invalid data', function(done) {
|
|
|
|
createUserAndChangeName(null, function(err, d) {
|
2015-11-04 10:05:24 +00:00
|
|
|
d.save(expectChangeSuccess(INVALID_DATA, done));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should NOT throw on save(cb) with valid data', function(done) {
|
|
|
|
createUserAndChangeName(NEW_NAME, function(err, d) {
|
2015-11-04 10:05:24 +00:00
|
|
|
d.save(expectChangeSuccess(done));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
describe('model setting: automaticValidation: true', function() {
|
2015-11-04 10:05:24 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
before(function(done) {
|
2015-11-04 10:05:24 +00:00
|
|
|
User.settings.automaticValidation = true;
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('method create', function() {
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should throw on create with validate:true with invalid data', function(done) {
|
2016-08-19 17:46:59 +00:00
|
|
|
User.create(INVALID_DATA, {validate: true}, expectValidationError(done));
|
2015-11-04 10:05:24 +00:00
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should NOT throw on create with validate:false with invalid data', function(done) {
|
2016-08-19 17:46:59 +00:00
|
|
|
User.create(INVALID_DATA, {validate: false}, expectCreateSuccess(INVALID_DATA, done));
|
2015-11-04 10:05:24 +00:00
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should NOT throw on create with validate:true with valid data', function(done) {
|
2016-08-19 17:46:59 +00:00
|
|
|
User.create(VALID_DATA, {validate: true}, expectCreateSuccess(done));
|
2015-11-04 10:05:24 +00:00
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should NOT throw on create with validate:false with valid data', function(done) {
|
2016-08-19 17:46:59 +00:00
|
|
|
User.create(VALID_DATA, {validate: false}, expectCreateSuccess(done));
|
2015-11-04 10:05:24 +00:00
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should throw on create with invalid data', function(done) {
|
2015-11-04 10:05:24 +00:00
|
|
|
User.create(INVALID_DATA, expectValidationError(done));
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should NOT throw on create with valid data', function(done) {
|
2015-11-04 10:05:24 +00:00
|
|
|
User.create(VALID_DATA, expectCreateSuccess(done));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('method findOrCreate', function() {
|
2016-04-01 13:23:42 +00:00
|
|
|
it('should throw on findOrCreate with validate:true with invalid data',
|
|
|
|
function(done) {
|
2016-08-19 17:46:59 +00:00
|
|
|
User.findOrCreate(getNewWhere(), INVALID_DATA, {validate: true},
|
2016-04-01 13:23:42 +00:00
|
|
|
expectValidationError(done));
|
2015-11-04 10:05:24 +00:00
|
|
|
});
|
|
|
|
|
2016-04-01 13:23:42 +00:00
|
|
|
it('should NOT throw on findOrCreate with validate:false with invalid data',
|
|
|
|
function(done) {
|
2016-08-19 17:46:59 +00:00
|
|
|
User.findOrCreate(getNewWhere(), INVALID_DATA, {validate: false},
|
2016-04-01 13:23:42 +00:00
|
|
|
expectCreateSuccess(INVALID_DATA, done));
|
2015-11-04 10:05:24 +00:00
|
|
|
});
|
|
|
|
|
2016-04-01 13:23:42 +00:00
|
|
|
it('should NOT throw on findOrCreate with validate:true with valid data',
|
|
|
|
function(done) {
|
2016-08-19 17:46:59 +00:00
|
|
|
User.findOrCreate(getNewWhere(), VALID_DATA, {validate: true},
|
2016-04-01 13:23:42 +00:00
|
|
|
expectCreateSuccess(done));
|
2015-11-04 10:05:24 +00:00
|
|
|
});
|
|
|
|
|
2016-04-01 13:23:42 +00:00
|
|
|
it('should NOT throw on findOrCreate with validate:false with valid data',
|
|
|
|
function(done) {
|
2016-08-19 17:46:59 +00:00
|
|
|
User.findOrCreate(getNewWhere(), VALID_DATA, {validate: false},
|
2016-04-01 13:23:42 +00:00
|
|
|
expectCreateSuccess(done));
|
2015-11-04 10:05:24 +00:00
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should throw on findOrCreate with invalid data', function(done) {
|
2015-11-04 10:05:24 +00:00
|
|
|
User.findOrCreate(getNewWhere(), INVALID_DATA, expectValidationError(done));
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should NOT throw on findOrCreate with valid data', function(done) {
|
2015-11-04 10:05:24 +00:00
|
|
|
User.findOrCreate(getNewWhere(), VALID_DATA, expectCreateSuccess(done));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('method updateOrCreate on existing data', function() {
|
2016-04-01 13:23:42 +00:00
|
|
|
it('should throw on updateOrCreate(id) with validate:true with invalid data',
|
|
|
|
function(done) {
|
2016-08-19 17:46:59 +00:00
|
|
|
callUpdateOrCreateWithExistingUserId(null, {validate: true},
|
2016-04-01 13:23:42 +00:00
|
|
|
expectValidationError(done));
|
2015-11-04 10:05:24 +00:00
|
|
|
});
|
|
|
|
|
2016-04-01 13:23:42 +00:00
|
|
|
it('should NOT throw on updateOrCreate(id) with validate:false with invalid data',
|
|
|
|
function(done) {
|
2016-08-19 17:46:59 +00:00
|
|
|
callUpdateOrCreateWithExistingUserId(null, {validate: false},
|
2016-04-01 13:23:42 +00:00
|
|
|
expectChangeSuccess(INVALID_DATA, done));
|
2015-11-04 10:05:24 +00:00
|
|
|
});
|
|
|
|
|
2016-04-01 13:23:42 +00:00
|
|
|
it('should NOT throw on updateOrCreate(id) with validate:true with valid data',
|
|
|
|
function(done) {
|
2016-08-19 17:46:59 +00:00
|
|
|
callUpdateOrCreateWithExistingUserId(NEW_NAME, {validate: true},
|
2016-04-01 13:23:42 +00:00
|
|
|
expectChangeSuccess(done));
|
2015-11-04 10:05:24 +00:00
|
|
|
});
|
|
|
|
|
2016-04-01 13:23:42 +00:00
|
|
|
it('should NOT throw on updateOrCreate(id) with validate:false with valid data',
|
|
|
|
function(done) {
|
2016-08-19 17:46:59 +00:00
|
|
|
callUpdateOrCreateWithExistingUserId(NEW_NAME, {validate: false},
|
2016-04-01 13:23:42 +00:00
|
|
|
expectChangeSuccess(done));
|
2015-11-04 10:05:24 +00:00
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should throw on updateOrCreate(id) with invalid data', function(done) {
|
2015-11-04 10:05:24 +00:00
|
|
|
callUpdateOrCreateWithExistingUserId(null, expectValidationError(done));
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should NOT throw on updateOrCreate(id) with valid data', function(done) {
|
2015-11-04 10:05:24 +00:00
|
|
|
callUpdateOrCreateWithExistingUserId(NEW_NAME, expectChangeSuccess(done));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('method save', function() {
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should throw on save with {validate:true} with invalid data', function(done) {
|
|
|
|
createUserAndChangeName(null, function(err, d) {
|
2015-11-04 10:05:24 +00:00
|
|
|
d.save(options, expectValidationError(done));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should NOT throw on save with {validate:false} with invalid data', function(done) {
|
|
|
|
createUserAndChangeName(null, function(err, d) {
|
2016-08-19 17:46:59 +00:00
|
|
|
d.save({validate: false}, expectChangeSuccess(INVALID_DATA, done));
|
2015-11-04 10:05:24 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should NOT throw on save with {validate:true} with valid data', function(done) {
|
|
|
|
createUserAndChangeName(NEW_NAME, function(err, d) {
|
2016-08-19 17:46:59 +00:00
|
|
|
d.save({validate: true}, expectChangeSuccess(done));
|
2015-11-04 10:05:24 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should NOT throw on save with {validate:false} with valid data', function(done) {
|
|
|
|
createUserAndChangeName(NEW_NAME, function(err, d) {
|
2016-08-19 17:46:59 +00:00
|
|
|
d.save({validate: false}, expectChangeSuccess(done));
|
2015-11-04 10:05:24 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should throw on save(cb) with invalid data', function(done) {
|
|
|
|
createUserAndChangeName(null, function(err, d) {
|
2015-11-04 10:05:24 +00:00
|
|
|
d.save(expectValidationError(done));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should NOT throw on save(cb) with valid data', function(done) {
|
|
|
|
createUserAndChangeName(NEW_NAME, function(err, d) {
|
2015-11-04 10:05:24 +00:00
|
|
|
d.save(expectChangeSuccess(done));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|