2016-04-01 22:25:16 +00:00
|
|
|
// Copyright IBM Corp. 2014,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
|
|
|
|
|
2014-08-06 11:26:47 +00:00
|
|
|
// This test written in mocha+should.js
|
2016-08-22 19:55:22 +00:00
|
|
|
'use strict';
|
2018-12-07 14:54:29 +00:00
|
|
|
const should = require('./init.js');
|
2014-08-06 11:26:47 +00:00
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
const jdb = require('../');
|
|
|
|
const ModelBuilder = jdb.ModelBuilder;
|
|
|
|
const DataSource = jdb.DataSource;
|
|
|
|
const Memory = require('../lib/connectors/memory');
|
2014-08-06 11:26:47 +00:00
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
const modelBuilder = new ModelBuilder();
|
|
|
|
const mixins = modelBuilder.mixins;
|
2014-08-08 08:20:57 +00:00
|
|
|
|
|
|
|
function timestamps(Model, options) {
|
2016-08-19 17:46:59 +00:00
|
|
|
Model.defineProperty('createdAt', {type: Date});
|
|
|
|
Model.defineProperty('updatedAt', {type: Date});
|
2014-08-08 08:20:57 +00:00
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
const originalBeforeSave = Model.beforeSave;
|
2014-08-08 08:20:57 +00:00
|
|
|
Model.beforeSave = function(next, data) {
|
|
|
|
Model.applyTimestamps(data, this.isNewRecord());
|
|
|
|
if (data.createdAt) {
|
|
|
|
this.createdAt = data.createdAt;
|
|
|
|
}
|
|
|
|
if (data.updatedAt) {
|
|
|
|
this.updatedAt = data.updatedAt;
|
|
|
|
}
|
|
|
|
if (originalBeforeSave) {
|
|
|
|
originalBeforeSave.apply(this, arguments);
|
|
|
|
} else {
|
|
|
|
next();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Model.applyTimestamps = function(data, creation) {
|
|
|
|
data.updatedAt = new Date();
|
|
|
|
if (creation) {
|
|
|
|
data.createdAt = data.updatedAt;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
mixins.define('TimeStamp', timestamps);
|
2014-08-06 11:26:47 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
describe('Model class', function() {
|
2014-08-09 08:58:03 +00:00
|
|
|
it('should define mixins', function() {
|
2014-08-06 11:26:47 +00:00
|
|
|
mixins.define('Example', function(Model, options) {
|
|
|
|
Model.prototype.example = function() {
|
|
|
|
return options;
|
|
|
|
};
|
|
|
|
});
|
2014-08-09 08:58:03 +00:00
|
|
|
mixins.define('Demo', function(Model, options) {
|
2014-08-17 10:29:04 +00:00
|
|
|
Model.demoMixin = options.value;
|
|
|
|
});
|
|
|
|
mixins.define('Multi', function(Model, options) {
|
|
|
|
Model.multiMixin = Model.multiMixin || {};
|
|
|
|
Model.multiMixin[options.key] = options.value;
|
2014-08-09 08:58:03 +00:00
|
|
|
});
|
2014-08-06 11:26:47 +00:00
|
|
|
});
|
2015-03-11 08:02:00 +00:00
|
|
|
|
2014-08-06 11:26:47 +00:00
|
|
|
it('should apply a mixin class', function() {
|
2018-12-07 14:54:29 +00:00
|
|
|
const Address = modelBuilder.define('Address', {
|
2016-08-19 17:46:59 +00:00
|
|
|
street: {type: 'string', required: true},
|
|
|
|
city: {type: 'string', required: true},
|
2014-08-06 11:26:47 +00:00
|
|
|
});
|
2014-08-08 08:20:57 +00:00
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
const memory = new DataSource('mem', {connector: Memory}, modelBuilder);
|
|
|
|
const Item = memory.createModel('Item', {name: 'string'}, {
|
2016-08-19 17:46:59 +00:00
|
|
|
mixins: {Address: true},
|
2014-08-08 08:20:57 +00:00
|
|
|
});
|
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
const properties = Item.definition.properties;
|
2015-03-11 08:02:00 +00:00
|
|
|
|
2016-08-19 17:46:59 +00:00
|
|
|
properties.street.should.eql({type: String, required: true});
|
|
|
|
properties.city.should.eql({type: String, required: true});
|
2014-08-06 11:26:47 +00:00
|
|
|
});
|
2015-03-11 08:02:00 +00:00
|
|
|
|
2016-05-24 17:58:21 +00:00
|
|
|
it('should fail to apply an undefined mixin class', function() {
|
2018-12-07 14:54:29 +00:00
|
|
|
const memory = new DataSource('mem', {connector: Memory}, modelBuilder);
|
2016-05-24 17:58:21 +00:00
|
|
|
function applyMixin() {
|
2016-08-19 17:46:59 +00:00
|
|
|
memory.createModel('Item', {name: 'string'}, {
|
|
|
|
mixins: {UndefinedMixin: true},
|
2016-05-24 17:58:21 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
should.throws(applyMixin, 'failed to apply undefined mixin class');
|
|
|
|
});
|
|
|
|
|
2014-08-06 11:26:47 +00:00
|
|
|
it('should apply mixins', function(done) {
|
2018-12-07 14:54:29 +00:00
|
|
|
const memory = new DataSource('mem', {connector: Memory}, modelBuilder);
|
|
|
|
const Item = memory.createModel('Item', {name: 'string'}, {
|
2015-03-11 08:02:00 +00:00
|
|
|
mixins: {
|
2016-05-24 17:58:21 +00:00
|
|
|
TimeStamp: true,
|
2016-08-19 17:46:59 +00:00
|
|
|
Demo: {value: true},
|
2014-08-17 10:29:04 +00:00
|
|
|
Multi: [
|
2016-08-19 17:46:59 +00:00
|
|
|
{key: 'foo', value: 'bar'},
|
|
|
|
{key: 'fox', value: 'baz'},
|
2016-04-01 11:48:17 +00:00
|
|
|
],
|
|
|
|
},
|
2014-08-06 11:26:47 +00:00
|
|
|
});
|
2015-03-11 08:02:00 +00:00
|
|
|
|
2016-08-19 17:46:59 +00:00
|
|
|
Item.mixin('Example', {foo: 'bar'});
|
2015-03-11 08:02:00 +00:00
|
|
|
|
2014-08-09 08:58:03 +00:00
|
|
|
Item.demoMixin.should.be.true;
|
2015-03-11 08:02:00 +00:00
|
|
|
|
2014-08-17 10:29:04 +00:00
|
|
|
Item.multiMixin.foo.should.equal('bar');
|
|
|
|
Item.multiMixin.fox.should.equal('baz');
|
2015-03-11 08:02:00 +00:00
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
const properties = Item.definition.properties;
|
2016-08-19 17:46:59 +00:00
|
|
|
properties.createdAt.should.eql({type: Date});
|
|
|
|
properties.updatedAt.should.eql({type: Date});
|
2015-03-11 08:02:00 +00:00
|
|
|
|
2016-08-19 17:46:59 +00:00
|
|
|
Item.create({name: 'Item 1'}, function(err, inst) {
|
2014-08-06 11:26:47 +00:00
|
|
|
inst.createdAt.should.be.a.date;
|
|
|
|
inst.updatedAt.should.be.a.date;
|
2016-08-19 17:46:59 +00:00
|
|
|
inst.example().should.eql({foo: 'bar'});
|
2014-08-06 11:26:47 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2015-03-11 08:02:00 +00:00
|
|
|
|
2016-05-24 17:58:21 +00:00
|
|
|
it('should fail to apply undefined mixin', function() {
|
2018-12-07 14:54:29 +00:00
|
|
|
const memory = new DataSource('mem', {connector: Memory}, modelBuilder);
|
|
|
|
const Item = memory.createModel('Item', {name: 'string'});
|
2016-05-24 17:58:21 +00:00
|
|
|
|
|
|
|
function applyMixin() {
|
2016-08-19 17:46:59 +00:00
|
|
|
Item.mixin('UndefinedMixin', {foo: 'bar'});
|
2016-05-24 17:58:21 +00:00
|
|
|
}
|
|
|
|
should.throws(applyMixin, 'failed to apply undefined mixin');
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
describe('#mixin()', function() {
|
2018-12-07 14:54:29 +00:00
|
|
|
let Person, Author, Address;
|
2015-03-11 08:02:00 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
beforeEach(function() {
|
2015-03-11 08:02:00 +00:00
|
|
|
Address = modelBuilder.define('Address', {
|
2016-08-19 17:46:59 +00:00
|
|
|
street: {type: 'string', required: true},
|
|
|
|
city: {type: 'string', required: true},
|
2015-03-11 08:02:00 +00:00
|
|
|
});
|
2018-12-07 14:54:29 +00:00
|
|
|
const memory = new DataSource('mem', {connector: Memory}, modelBuilder);
|
2016-08-19 17:46:59 +00:00
|
|
|
Person = memory.createModel('Person', {name: 'string'});
|
|
|
|
Author = memory.createModel('Author', {name: 'string'});
|
2015-03-11 08:02:00 +00:00
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should register mixin class into _mixins', function() {
|
2015-03-11 08:02:00 +00:00
|
|
|
Person.mixin(Address);
|
|
|
|
Person._mixins.should.containEql(Address);
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should NOT share mixins registry', function() {
|
2015-03-11 08:02:00 +00:00
|
|
|
Person.mixin(Address);
|
|
|
|
Author._mixins.should.not.containEql(Address);
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should able to mixin same class', function() {
|
2015-03-11 08:02:00 +00:00
|
|
|
Person.mixin(Address);
|
|
|
|
Author.mixin(Address);
|
|
|
|
Author._mixins.should.containEql(Address);
|
|
|
|
});
|
|
|
|
});
|
2014-08-06 11:26:47 +00:00
|
|
|
});
|