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-12-05 14:14:09 +00:00
|
|
|
|
|
|
|
/* global getSchema:false */
|
2018-12-07 16:13:48 +00:00
|
|
|
const async = require('async');
|
|
|
|
const should = require('./init.js');
|
|
|
|
let db, User, options, ModelWithForceId, whereCount = 0;
|
|
|
|
const j = require('../');
|
|
|
|
const ValidationError = j.ValidationError;
|
2015-11-04 10:05:24 +00:00
|
|
|
|
2018-12-07 16:13:48 +00:00
|
|
|
const INITIAL_NAME = 'Bert';
|
|
|
|
const NEW_NAME = 'Ernie';
|
|
|
|
const INVALID_DATA = {name: null};
|
|
|
|
const 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() {
|
|
|
|
before(function(done) {
|
2015-11-04 10:05:24 +00:00
|
|
|
db = getSchema();
|
2016-04-07 22:22:46 +00:00
|
|
|
ModelWithForceId = db.createModel(
|
2018-06-12 07:13:32 +00:00
|
|
|
'ModelWithForceId',
|
|
|
|
{name: String},
|
2018-07-16 06:46:25 +00:00
|
|
|
{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);
|
2018-12-07 16:13:48 +00:00
|
|
|
const 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',
|
2018-06-12 07:13:32 +00:00
|
|
|
function(done) {
|
|
|
|
ModelWithForceId.create({name: 'foo'}, function(err, created) {
|
|
|
|
if (err) return done(err);
|
|
|
|
created.replaceAttributes({name: 'bar'}, function(err, data) {
|
|
|
|
done(err);
|
|
|
|
});
|
2016-04-07 22:22:46 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should fail if you pass the Primary key in data object',
|
2018-06-12 07:13:32 +00:00
|
|
|
function(done) {
|
|
|
|
ModelWithForceId.create({name: 'foo'}, function(err, created) {
|
|
|
|
if (err) return done(err);
|
|
|
|
created.replaceAttributes({name: 'bar', id: 999},
|
|
|
|
function(err, data) {
|
|
|
|
should.exist(err);
|
|
|
|
done();
|
|
|
|
});
|
2016-04-07 22:22:46 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
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',
|
2018-06-12 07:13:32 +00:00
|
|
|
function(done) {
|
|
|
|
User.findOrCreate(getNewWhere(), INVALID_DATA, {validate: true},
|
|
|
|
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',
|
2018-06-12 07:13:32 +00:00
|
|
|
function(done) {
|
|
|
|
User.findOrCreate(getNewWhere(), INVALID_DATA, {validate: false},
|
|
|
|
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',
|
2018-06-12 07:13:32 +00:00
|
|
|
function(done) {
|
|
|
|
User.findOrCreate(getNewWhere(), VALID_DATA, {validate: true},
|
|
|
|
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',
|
2018-06-12 07:13:32 +00:00
|
|
|
function(done) {
|
|
|
|
User.findOrCreate(getNewWhere(), 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 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',
|
2018-06-12 07:13:32 +00:00
|
|
|
function(done) {
|
|
|
|
callUpdateOrCreateWithExistingUserId(null, {validate: true},
|
|
|
|
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',
|
2018-06-12 07:13:32 +00:00
|
|
|
function(done) {
|
|
|
|
callUpdateOrCreateWithExistingUserId(null, {validate: false},
|
|
|
|
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',
|
2018-06-12 07:13:32 +00:00
|
|
|
function(done) {
|
|
|
|
callUpdateOrCreateWithExistingUserId(NEW_NAME, {validate: true},
|
|
|
|
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',
|
2018-06-12 07:13:32 +00:00
|
|
|
function(done) {
|
|
|
|
callUpdateOrCreateWithExistingUserId(NEW_NAME, {validate: false},
|
|
|
|
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-12-17 16:21:04 +00:00
|
|
|
|
|
|
|
it('returns an error when trying to update the id property when forceId is set to true',
|
2018-06-12 07:13:32 +00:00
|
|
|
function(done) {
|
|
|
|
ModelWithForceId.create({name: 'foo'}, function(err, model) {
|
|
|
|
if (err) return done(err);
|
|
|
|
model.updateAttributes({id: 123}, function(err) {
|
|
|
|
err.should.be.instanceOf(Error);
|
|
|
|
err.message.should.eql('id cannot be updated from ' + model.id +
|
2016-12-17 16:21:04 +00:00
|
|
|
' to 123 when forceId is set to true');
|
2018-06-12 07:13:32 +00:00
|
|
|
done();
|
|
|
|
});
|
2016-12-17 16:21:04 +00:00
|
|
|
});
|
|
|
|
});
|
2015-11-04 10:05:24 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
describe('model setting: automaticValidation: false', function() {
|
|
|
|
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',
|
2018-06-12 07:13:32 +00:00
|
|
|
function(done) {
|
|
|
|
User.findOrCreate(getNewWhere(), INVALID_DATA, {validate: true},
|
|
|
|
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',
|
2018-06-12 07:13:32 +00:00
|
|
|
function(done) {
|
|
|
|
User.findOrCreate(getNewWhere(), INVALID_DATA, {validate: false},
|
|
|
|
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',
|
2018-06-12 07:13:32 +00:00
|
|
|
function(done) {
|
|
|
|
User.findOrCreate(getNewWhere(), VALID_DATA, {validate: true},
|
|
|
|
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',
|
2018-06-12 07:13:32 +00:00
|
|
|
function(done) {
|
|
|
|
User.findOrCreate(getNewWhere(), 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 findOrCreate with invalid data', function(done) {
|
2016-04-01 13:23:42 +00:00
|
|
|
User.findOrCreate(getNewWhere(), INVALID_DATA,
|
2018-06-12 07:13:32 +00:00
|
|
|
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',
|
2018-06-12 07:13:32 +00:00
|
|
|
function(done) {
|
|
|
|
callUpdateOrCreateWithExistingUserId(null, {validate: true},
|
|
|
|
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',
|
2018-06-12 07:13:32 +00:00
|
|
|
function(done) {
|
|
|
|
callUpdateOrCreateWithExistingUserId(null, {validate: false},
|
|
|
|
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',
|
2018-06-12 07:13:32 +00:00
|
|
|
function(done) {
|
|
|
|
callUpdateOrCreateWithExistingUserId(NEW_NAME, {validate: true},
|
|
|
|
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',
|
2018-06-12 07:13:32 +00:00
|
|
|
function(done) {
|
|
|
|
callUpdateOrCreateWithExistingUserId(NEW_NAME, {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 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() {
|
|
|
|
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',
|
2018-06-12 07:13:32 +00:00
|
|
|
function(done) {
|
|
|
|
User.findOrCreate(getNewWhere(), INVALID_DATA, {validate: true},
|
|
|
|
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',
|
2018-06-12 07:13:32 +00:00
|
|
|
function(done) {
|
|
|
|
User.findOrCreate(getNewWhere(), INVALID_DATA, {validate: false},
|
|
|
|
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',
|
2018-06-12 07:13:32 +00:00
|
|
|
function(done) {
|
|
|
|
User.findOrCreate(getNewWhere(), VALID_DATA, {validate: true},
|
|
|
|
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',
|
2018-06-12 07:13:32 +00:00
|
|
|
function(done) {
|
|
|
|
User.findOrCreate(getNewWhere(), 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 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',
|
2018-06-12 07:13:32 +00:00
|
|
|
function(done) {
|
|
|
|
callUpdateOrCreateWithExistingUserId(null, {validate: true},
|
|
|
|
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',
|
2018-06-12 07:13:32 +00:00
|
|
|
function(done) {
|
|
|
|
callUpdateOrCreateWithExistingUserId(null, {validate: false},
|
|
|
|
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',
|
2018-06-12 07:13:32 +00:00
|
|
|
function(done) {
|
|
|
|
callUpdateOrCreateWithExistingUserId(NEW_NAME, {validate: true},
|
|
|
|
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',
|
2018-06-12 07:13:32 +00:00
|
|
|
function(done) {
|
|
|
|
callUpdateOrCreateWithExistingUserId(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 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));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|