loopback/test/util/model-tests.js

271 lines
9.1 KiB
JavaScript
Raw Normal View History

2016-05-03 22:50:21 +00:00
// Copyright IBM Corp. 2014,2016. All Rights Reserved.
// Node module: loopback
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT
'use strict';
var assert = require('assert');
2014-01-28 22:32:13 +00:00
var async = require('async');
var describe = require('./describe');
var loopback = require('../../');
2014-01-28 22:32:13 +00:00
var ACL = loopback.ACL;
var Change = loopback.Change;
2014-06-05 07:45:09 +00:00
var PersistedModel = loopback.PersistedModel;
2014-05-03 03:04:06 +00:00
var RemoteObjects = require('strong-remoting');
var TaskEmitter = require('strong-task-emitter');
module.exports = function defineModelTestsWithDataSource(options) {
2014-11-21 02:35:36 +00:00
describe('Model Tests', function() {
var User, dataSource;
2014-11-21 02:35:36 +00:00
if (options.beforeEach) {
beforeEach(options.beforeEach);
}
2014-11-21 02:35:36 +00:00
beforeEach(function() {
var test = this;
2014-11-21 02:35:36 +00:00
// setup a model / datasource
dataSource = this.dataSource || loopback.createDataSource(options.dataSource);
2013-12-07 01:04:47 +00:00
2014-11-21 02:35:36 +00:00
var extend = PersistedModel.extend;
2013-06-07 19:57:51 +00:00
2014-11-21 02:35:36 +00:00
// create model hook
PersistedModel.extend = function() {
var extendedModel = extend.apply(PersistedModel, arguments);
2013-06-12 22:44:38 +00:00
2014-11-21 02:35:36 +00:00
if (options.onDefine) {
options.onDefine.call(test, extendedModel);
}
2014-11-21 02:35:36 +00:00
return extendedModel;
};
User = PersistedModel.extend('UtilUser', {
id: {id: true, type: String, defaultFn: 'guid'},
2014-11-21 02:35:36 +00:00
'first': String,
'last': String,
'age': Number,
'password': String,
'gender': String,
'domain': String,
'email': String,
2014-11-21 02:35:36 +00:00
}, {
trackChanges: options.trackChanges !== false,
enableRemoteReplication: options.enableRemoteReplication,
2014-11-21 02:35:36 +00:00
});
2013-06-12 22:44:38 +00:00
2014-11-21 02:35:36 +00:00
User.attachTo(dataSource);
User.handleChangeError = function(err) {
console.warn('WARNING: unhandled change-tracking error');
console.warn(err);
};
2013-06-07 19:57:51 +00:00
});
2014-11-21 02:35:36 +00:00
describe('Model.validatesPresenceOf(properties...)', function() {
it('Require a model to include a property to be considered valid', function() {
User.validatesPresenceOf('first', 'last', 'age');
var joe = new User({first: 'joe'});
2014-11-21 02:35:36 +00:00
assert(joe.isValid() === false, 'model should not validate');
assert(joe.errors.last, 'should have a missing last error');
assert(joe.errors.age, 'should have a missing age error');
});
2013-06-07 19:57:51 +00:00
});
2014-11-21 02:35:36 +00:00
describe('Model.validatesLengthOf(property, options)', function() {
it('Require a property length to be within a specified range', function() {
User.validatesLengthOf('password', {min: 5, message: {min: 'Password is too short'}});
var joe = new User({password: '1234'});
2014-11-21 02:35:36 +00:00
assert(joe.isValid() === false, 'model should not be valid');
assert(joe.errors.password, 'should have password error');
});
2013-06-07 19:57:51 +00:00
});
2014-11-21 02:35:36 +00:00
describe('Model.validatesInclusionOf(property, options)', function() {
it('Require a value for `property` to be in the specified array', function() {
User.validatesInclusionOf('gender', {in: ['male', 'female']});
var foo = new User({gender: 'bar'});
2014-11-21 02:35:36 +00:00
assert(foo.isValid() === false, 'model should not be valid');
assert(foo.errors.gender, 'should have gender error');
});
2013-06-07 19:57:51 +00:00
});
2014-11-21 02:35:36 +00:00
describe('Model.validatesExclusionOf(property, options)', function() {
it('Require a value for `property` to not exist in the specified array', function() {
User.validatesExclusionOf('domain', {in: ['www', 'billing', 'admin']});
var foo = new User({domain: 'www'});
var bar = new User({domain: 'billing'});
var bat = new User({domain: 'admin'});
2014-11-21 02:35:36 +00:00
assert(foo.isValid() === false);
assert(bar.isValid() === false);
assert(bat.isValid() === false);
assert(foo.errors.domain, 'model should have a domain error');
assert(bat.errors.domain, 'model should have a domain error');
assert(bat.errors.domain, 'model should have a domain error');
});
2013-06-07 19:57:51 +00:00
});
2014-11-21 02:35:36 +00:00
describe('Model.validatesNumericalityOf(property, options)', function() {
it('Require a value for `property` to be a specific type of `Number`', function() {
User.validatesNumericalityOf('age', {int: true});
var joe = new User({age: 10.2});
2014-11-21 02:35:36 +00:00
assert(joe.isValid() === false);
var bob = new User({age: 0});
2014-11-21 02:35:36 +00:00
assert(bob.isValid() === true);
assert(joe.errors.age, 'model should have an age error');
});
2013-06-07 19:57:51 +00:00
});
2014-11-21 02:35:36 +00:00
describe('myModel.isValid()', function() {
it('Validate the model instance', function() {
User.validatesNumericalityOf('age', {int: true});
var user = new User({first: 'joe', age: 'flarg'});
2014-11-21 02:35:36 +00:00
var valid = user.isValid();
2013-06-21 23:41:33 +00:00
assert(valid === false);
assert(user.errors.age, 'model should have age error');
});
2013-06-07 19:57:51 +00:00
2014-11-21 02:35:36 +00:00
it('Asynchronously validate the model', function(done) {
User.validatesNumericalityOf('age', {int: true});
var user = new User({first: 'joe', age: 'flarg'});
2014-11-21 02:35:36 +00:00
user.isValid(function(valid) {
assert(valid === false);
assert(user.errors.age, 'model should have age error');
2014-11-21 02:35:36 +00:00
done();
});
2013-06-07 19:57:51 +00:00
});
});
2014-11-21 02:35:36 +00:00
describe('Model.create([data], [callback])', function() {
it('Create an instance of Model with given data and save to the attached data source',
function(done) {
User.create({first: 'Joe', last: 'Bob'}, function(err, user) {
2014-11-21 02:35:36 +00:00
assert(user instanceof User);
2014-11-21 02:35:36 +00:00
done();
});
2013-06-07 19:57:51 +00:00
});
});
2014-11-21 02:35:36 +00:00
describe('model.save([options], [callback])', function() {
it('Save an instance of a Model to the attached data source', function(done) {
var joe = new User({first: 'Joe', last: 'Bob'});
2014-11-21 02:35:36 +00:00
joe.save(function(err, user) {
assert(user.id);
2013-06-12 22:44:38 +00:00
assert(!err);
2014-11-21 02:35:36 +00:00
assert(!user.errors);
2013-06-12 22:44:38 +00:00
done();
});
});
2013-06-07 19:57:51 +00:00
});
2014-11-21 02:35:36 +00:00
describe('model.updateAttributes(data, [callback])', function() {
it('Save specified attributes to the attached data source', function(done) {
User.create({first: 'joe', age: 100}, function(err, user) {
2013-06-12 22:44:38 +00:00
assert(!err);
2014-11-21 02:35:36 +00:00
assert.equal(user.first, 'joe');
user.updateAttributes({
first: 'updatedFirst',
last: 'updatedLast',
2014-11-21 02:35:36 +00:00
}, function(err, updatedUser) {
assert(!err);
assert.equal(updatedUser.first, 'updatedFirst');
assert.equal(updatedUser.last, 'updatedLast');
assert.equal(updatedUser.age, 100);
2014-11-21 02:35:36 +00:00
done();
});
2013-06-12 22:44:38 +00:00
});
});
2013-06-07 19:57:51 +00:00
});
2014-11-21 02:35:36 +00:00
describe('Model.upsert(data, callback)', function() {
it('Update when record with id=data.id found, insert otherwise', function(done) {
User.upsert({first: 'joe', id: 7}, function(err, user) {
2014-11-21 02:35:36 +00:00
assert(!err);
assert.equal(user.first, 'joe');
User.upsert({first: 'bob', id: 7}, function(err, updatedUser) {
2014-11-21 02:35:36 +00:00
assert(!err);
assert.equal(updatedUser.first, 'bob');
2014-11-21 02:35:36 +00:00
done();
2013-06-12 22:44:38 +00:00
});
});
2013-06-07 19:57:51 +00:00
});
});
2014-11-21 02:35:36 +00:00
describe('model.destroy([callback])', function() {
it('Remove a model from the attached data source', function(done) {
User.create({first: 'joe', last: 'bob'}, function(err, user) {
2014-11-21 02:35:36 +00:00
User.findById(user.id, function(err, foundUser) {
if (err) return done(err);
2014-11-21 02:35:36 +00:00
assert.equal(user.id, foundUser.id);
User.deleteById(foundUser.id, function(err) {
if (err) return done(err);
User.find({where: {id: user.id}}, function(err, found) {
if (err) return done(err);
assert.equal(found.length, 0);
2014-11-21 02:35:36 +00:00
done();
2014-02-12 00:01:51 +00:00
});
2014-11-21 02:35:36 +00:00
});
2014-02-12 00:01:51 +00:00
});
2014-11-21 02:35:36 +00:00
});
2014-02-12 00:01:51 +00:00
});
2014-11-21 02:35:36 +00:00
});
2013-07-22 17:00:07 +00:00
2014-11-21 02:35:36 +00:00
describe('Model.deleteById(id, [callback])', function() {
it('Delete a model instance from the attached data source', function(done) {
User.create({first: 'joe', last: 'bob'}, function(err, user) {
2014-11-21 02:35:36 +00:00
User.deleteById(user.id, function(err) {
User.findById(user.id, function(err, notFound) {
assert.equal(notFound, null);
2014-11-21 02:35:36 +00:00
done();
});
});
2013-06-12 22:44:38 +00:00
});
2013-06-07 19:57:51 +00:00
});
});
2014-11-21 02:35:36 +00:00
describe('Model.findById(id, callback)', function() {
it('Find an instance by id', function(done) {
User.create({first: 'michael', last: 'jordan', id: 23}, function() {
2014-11-21 02:35:36 +00:00
User.findById(23, function(err, user) {
assert.equal(user.id, 23);
assert.equal(user.first, 'michael');
assert.equal(user.last, 'jordan');
2013-06-12 22:44:38 +00:00
done();
});
2013-06-07 19:57:51 +00:00
});
2014-11-21 02:35:36 +00:00
});
2013-06-12 22:44:38 +00:00
});
2014-11-21 02:35:36 +00:00
describe('Model.count([query], callback)', function() {
it('Query count of Model instances in data source', function(done) {
(new TaskEmitter())
.task(User, 'create', {first: 'jill', age: 100})
.task(User, 'create', {first: 'bob', age: 200})
.task(User, 'create', {first: 'jan'})
.task(User, 'create', {first: 'sam'})
.task(User, 'create', {first: 'suzy'})
2014-11-21 02:35:36 +00:00
.on('done', function() {
User.count({age: {gt: 99}}, function(err, count) {
2014-11-21 02:35:36 +00:00
assert.equal(count, 2);
2014-11-21 02:35:36 +00:00
done();
});
});
});
});
});
};