2014-01-28 22:32:13 +00:00
|
|
|
var async = require('async');
|
2014-05-03 01:12:24 +00:00
|
|
|
var describe = require('./describe');
|
|
|
|
var loopback = require('../../');
|
2014-01-28 22:32:13 +00:00
|
|
|
var ACL = loopback.ACL;
|
|
|
|
var Change = loopback.Change;
|
2014-05-03 01:12:24 +00:00
|
|
|
var DataModel = loopback.DataModel;
|
2014-05-03 03:04:06 +00:00
|
|
|
var RemoteObjects = require('strong-remoting');
|
2014-05-03 01:12:24 +00:00
|
|
|
|
|
|
|
module.exports = function defineModelTestsWithDataSource(options) {
|
2014-05-03 03:04:06 +00:00
|
|
|
|
|
|
|
describe('Model Tests', function() {
|
|
|
|
|
2014-05-03 01:12:24 +00:00
|
|
|
var User, dataSource;
|
|
|
|
|
|
|
|
if(options.beforeEach) {
|
|
|
|
beforeEach(options.beforeEach);
|
|
|
|
}
|
|
|
|
|
|
|
|
beforeEach(function() {
|
|
|
|
var test = this;
|
|
|
|
|
|
|
|
// setup a model / datasource
|
|
|
|
dataSource = this.dataSource || loopback.createDataSource(options.dataSource);
|
2013-12-07 01:04:47 +00:00
|
|
|
|
2014-05-03 01:12:24 +00:00
|
|
|
var extend = DataModel.extend;
|
2013-06-07 19:57:51 +00:00
|
|
|
|
2014-05-03 01:12:24 +00:00
|
|
|
// create model hook
|
|
|
|
DataModel.extend = function() {
|
|
|
|
var extendedModel = extend.apply(DataModel, arguments);
|
2013-06-12 22:44:38 +00:00
|
|
|
|
2014-05-03 01:12:24 +00:00
|
|
|
if(options.onDefine) {
|
2014-05-03 03:04:06 +00:00
|
|
|
options.onDefine.call(test, extendedModel);
|
2014-05-03 01:12:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return extendedModel;
|
|
|
|
}
|
|
|
|
|
|
|
|
User = DataModel.extend('user', {
|
2013-06-12 22:44:38 +00:00
|
|
|
'first': String,
|
|
|
|
'last': String,
|
|
|
|
'age': Number,
|
|
|
|
'password': String,
|
|
|
|
'gender': String,
|
|
|
|
'domain': String,
|
|
|
|
'email': String
|
2014-04-30 00:17:49 +00:00
|
|
|
}, {
|
|
|
|
trackChanges: true
|
2013-06-12 22:44:38 +00:00
|
|
|
});
|
2014-05-03 01:12:24 +00:00
|
|
|
|
2014-05-03 04:19:14 +00:00
|
|
|
// enable destroy all for testing
|
|
|
|
User.destroyAll.shared = true;
|
2014-05-03 01:12:24 +00:00
|
|
|
User.attachTo(dataSource);
|
2013-06-24 19:26:46 +00:00
|
|
|
});
|
2013-06-12 22:44:38 +00:00
|
|
|
|
2013-06-07 19:57:51 +00:00
|
|
|
describe('Model.validatesPresenceOf(properties...)', function() {
|
2013-07-16 20:46:33 +00:00
|
|
|
it("Require a model to include a property to be considered valid", function() {
|
2013-06-07 19:57:51 +00:00
|
|
|
User.validatesPresenceOf('first', 'last', 'age');
|
2013-06-12 22:44:38 +00:00
|
|
|
var joe = new User({first: 'joe'});
|
|
|
|
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
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('Model.validatesLengthOf(property, options)', function() {
|
2013-07-16 20:46:33 +00:00
|
|
|
it("Require a property length to be within a specified range", function() {
|
2013-06-07 19:57:51 +00:00
|
|
|
User.validatesLengthOf('password', {min: 5, message: {min: 'Password is too short'}});
|
2013-06-12 22:44:38 +00:00
|
|
|
var joe = new User({password: '1234'});
|
|
|
|
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
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('Model.validatesInclusionOf(property, options)', function() {
|
2013-07-16 20:46:33 +00:00
|
|
|
it("Require a value for `property` to be in the specified array", function() {
|
2013-06-07 19:57:51 +00:00
|
|
|
User.validatesInclusionOf('gender', {in: ['male', 'female']});
|
2013-06-12 22:44:38 +00:00
|
|
|
var foo = new User({gender: 'bar'});
|
|
|
|
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
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('Model.validatesExclusionOf(property, options)', function() {
|
2013-07-16 20:46:33 +00:00
|
|
|
it("Require a value for `property` to not exist in the specified array", function() {
|
2013-06-07 19:57:51 +00:00
|
|
|
User.validatesExclusionOf('domain', {in: ['www', 'billing', 'admin']});
|
2013-06-12 22:44:38 +00:00
|
|
|
var foo = new User({domain: 'www'});
|
|
|
|
var bar = new User({domain: 'billing'});
|
|
|
|
var bat = new User({domain: 'admin'});
|
|
|
|
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
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('Model.validatesNumericalityOf(property, options)', function() {
|
2013-07-16 20:46:33 +00:00
|
|
|
it("Require a value for `property` to be a specific type of `Number`", function() {
|
2013-06-07 19:57:51 +00:00
|
|
|
User.validatesNumericalityOf('age', {int: true});
|
2013-06-12 22:44:38 +00:00
|
|
|
var joe = new User({age: 10.2});
|
|
|
|
assert(joe.isValid() === false);
|
|
|
|
var bob = new User({age: 0});
|
|
|
|
assert(bob.isValid() === true);
|
|
|
|
assert(joe.errors.age, 'model should have an age error');
|
2013-06-07 19:57:51 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('myModel.isValid()', function() {
|
2013-07-16 20:46:33 +00:00
|
|
|
it("Validate the model instance", function() {
|
2013-06-12 22:44:38 +00:00
|
|
|
User.validatesNumericalityOf('age', {int: true});
|
|
|
|
var user = new User({first: 'joe', age: 'flarg'})
|
|
|
|
var valid = user.isValid();
|
|
|
|
assert(valid === false);
|
|
|
|
assert(user.errors.age, 'model should have age error');
|
2013-06-07 19:57:51 +00:00
|
|
|
});
|
2013-06-21 23:41:33 +00:00
|
|
|
|
2013-07-16 20:41:17 +00:00
|
|
|
it('Asynchronously validate the model', function(done) {
|
2013-06-21 23:41:33 +00:00
|
|
|
User.validatesNumericalityOf('age', {int: true});
|
2014-05-03 03:04:06 +00:00
|
|
|
var user = new User({first: 'joe', age: 'flarg'});
|
2013-06-21 23:41:33 +00:00
|
|
|
user.isValid(function (valid) {
|
|
|
|
assert(valid === false);
|
|
|
|
assert(user.errors.age, 'model should have age error');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2013-06-07 19:57:51 +00:00
|
|
|
});
|
|
|
|
|
2014-05-03 03:07:59 +00:00
|
|
|
describe('Model.create([data], [callback])', function() {
|
2013-07-16 20:46:33 +00:00
|
|
|
it("Create an instance of Model with given data and save to the attached data source", function(done) {
|
2013-06-07 19:57:51 +00:00
|
|
|
User.create({first: 'Joe', last: 'Bob'}, function(err, user) {
|
2013-06-12 22:44:38 +00:00
|
|
|
assert(user instanceof User);
|
|
|
|
done();
|
2013-06-07 19:57:51 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('model.save([options], [callback])', function() {
|
2013-07-16 20:46:33 +00:00
|
|
|
it("Save an instance of a Model to the attached data source", function(done) {
|
2013-06-07 19:57:51 +00:00
|
|
|
var joe = new User({first: 'Joe', last: 'Bob'});
|
|
|
|
joe.save(function(err, user) {
|
2013-06-12 22:44:38 +00:00
|
|
|
assert(user.id);
|
|
|
|
assert(!err);
|
|
|
|
assert(!user.errors);
|
|
|
|
done();
|
2013-06-07 19:57:51 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-05-03 03:07:59 +00:00
|
|
|
describe('model.updateAttributes(data, [callback])', function() {
|
2013-07-16 20:46:33 +00:00
|
|
|
it("Save specified attributes to the attached data source", function(done) {
|
2013-06-12 22:44:38 +00:00
|
|
|
User.create({first: 'joe', age: 100}, function (err, user) {
|
|
|
|
assert(!err);
|
|
|
|
assert.equal(user.first, 'joe');
|
|
|
|
|
|
|
|
user.updateAttributes({
|
|
|
|
first: 'updatedFirst',
|
|
|
|
last: 'updatedLast'
|
|
|
|
}, function (err, updatedUser) {
|
|
|
|
assert(!err);
|
|
|
|
assert.equal(updatedUser.first, 'updatedFirst');
|
|
|
|
assert.equal(updatedUser.last, 'updatedLast');
|
|
|
|
assert.equal(updatedUser.age, 100);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2013-06-07 19:57:51 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2013-06-12 22:44:38 +00:00
|
|
|
describe('Model.upsert(data, callback)', function() {
|
2013-06-07 19:57:51 +00:00
|
|
|
it("Update when record with id=data.id found, insert otherwise", function(done) {
|
2013-06-12 22:44:38 +00:00
|
|
|
User.upsert({first: 'joe', id: 7}, function (err, user) {
|
|
|
|
assert(!err);
|
|
|
|
assert.equal(user.first, 'joe');
|
|
|
|
|
|
|
|
User.upsert({first: 'bob', id: 7}, function (err, updatedUser) {
|
|
|
|
assert(!err);
|
|
|
|
assert.equal(updatedUser.first, 'bob');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2013-06-07 19:57:51 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('model.destroy([callback])', function() {
|
2013-07-16 20:46:33 +00:00
|
|
|
it("Remove a model from the attached data source", function(done) {
|
2013-06-12 22:44:38 +00:00
|
|
|
User.create({first: 'joe', last: 'bob'}, function (err, user) {
|
2013-06-24 19:26:46 +00:00
|
|
|
User.findById(user.id, function (err, foundUser) {
|
2013-06-12 22:44:38 +00:00
|
|
|
assert.equal(user.id, foundUser.id);
|
|
|
|
foundUser.destroy(function () {
|
2013-06-24 19:26:46 +00:00
|
|
|
User.findById(user.id, function (err, notFound) {
|
2013-06-12 22:44:38 +00:00
|
|
|
assert.equal(notFound, null);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2013-06-07 19:57:51 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-05-19 22:56:26 +00:00
|
|
|
describe('Model.removeById(id, [callback])', function () {
|
2014-02-12 00:01:51 +00:00
|
|
|
it("Delete a model instance from the attached data source", function (done) {
|
|
|
|
User.create({first: 'joe', last: 'bob'}, function (err, user) {
|
2014-05-19 22:56:26 +00:00
|
|
|
User.removeById(user.id, function (err) {
|
2014-02-12 00:01:51 +00:00
|
|
|
User.findById(user.id, function (err, notFound) {
|
2014-05-03 03:04:06 +00:00
|
|
|
assert.equal(notFound, null);
|
|
|
|
done();
|
2014-02-12 00:01:51 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2013-07-22 17:00:07 +00:00
|
|
|
|
2013-06-24 19:26:46 +00:00
|
|
|
describe('Model.findById(id, callback)', function() {
|
2013-07-16 20:46:33 +00:00
|
|
|
it("Find an instance by id", function(done) {
|
2013-06-12 22:44:38 +00:00
|
|
|
User.create({first: 'michael', last: 'jordan', id: 23}, function () {
|
2013-06-24 19:26:46 +00:00
|
|
|
User.findById(23, function (err, user) {
|
2013-06-12 22:44:38 +00:00
|
|
|
assert.equal(user.id, 23);
|
|
|
|
assert.equal(user.first, 'michael');
|
|
|
|
assert.equal(user.last, 'jordan');
|
|
|
|
done();
|
|
|
|
});
|
2013-06-07 19:57:51 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('Model.count([query], callback)', function() {
|
|
|
|
it("Query count of Model instances in data source", function(done) {
|
2013-06-12 22:44:38 +00:00
|
|
|
(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'})
|
|
|
|
.on('done', function () {
|
|
|
|
User.count({age: {gt: 99}}, function (err, count) {
|
|
|
|
assert.equal(count, 2);
|
|
|
|
done();
|
|
|
|
});
|
2013-06-07 19:57:51 +00:00
|
|
|
});
|
2013-06-12 22:44:38 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-05-03 03:04:06 +00:00
|
|
|
});
|
2013-11-21 15:17:19 +00:00
|
|
|
|
2013-06-07 19:57:51 +00:00
|
|
|
|
2014-05-03 01:12:24 +00:00
|
|
|
}
|