2016-05-03 22:50:21 +00:00
|
|
|
// Copyright IBM Corp. 2013,2016. All Rights Reserved.
|
|
|
|
// Node module: loopback
|
|
|
|
// This file is licensed under the MIT License.
|
|
|
|
// License text available at https://opensource.org/licenses/MIT
|
|
|
|
|
2016-11-15 21:46:23 +00:00
|
|
|
'use strict';
|
|
|
|
var assert = require('assert');
|
|
|
|
var loopback = require('../');
|
|
|
|
|
2013-06-07 19:57:51 +00:00
|
|
|
describe('DataSource', function() {
|
2013-06-11 18:07:49 +00:00
|
|
|
var memory;
|
2014-11-21 01:46:21 +00:00
|
|
|
|
2014-11-21 02:35:36 +00:00
|
|
|
beforeEach(function() {
|
2013-07-16 17:49:25 +00:00
|
|
|
memory = loopback.createDataSource({
|
2016-04-01 09:14:26 +00:00
|
|
|
connector: loopback.Memory,
|
2013-06-11 18:07:49 +00:00
|
|
|
});
|
2014-11-21 01:46:21 +00:00
|
|
|
|
2013-07-12 19:40:36 +00:00
|
|
|
assertValidDataSource(memory);
|
2013-06-11 18:07:49 +00:00
|
|
|
});
|
2013-06-07 19:57:51 +00:00
|
|
|
|
2013-06-08 00:37:30 +00:00
|
|
|
describe('dataSource.createModel(name, properties, settings)', function() {
|
2014-11-21 01:46:21 +00:00
|
|
|
it('Define a model and attach it to a `DataSource`', function() {
|
2016-11-15 21:46:23 +00:00
|
|
|
var Color = memory.createModel('color', {name: String});
|
2013-06-24 19:26:46 +00:00
|
|
|
assert.isFunc(Color, 'find');
|
|
|
|
assert.isFunc(Color, 'findById');
|
|
|
|
assert.isFunc(Color, 'findOne');
|
2013-06-11 16:01:44 +00:00
|
|
|
assert.isFunc(Color, 'create');
|
|
|
|
assert.isFunc(Color, 'updateOrCreate');
|
2016-09-02 12:40:47 +00:00
|
|
|
assert.isFunc(Color, 'upsertWithWhere');
|
2013-06-11 16:01:44 +00:00
|
|
|
assert.isFunc(Color, 'upsert');
|
|
|
|
assert.isFunc(Color, 'findOrCreate');
|
|
|
|
assert.isFunc(Color, 'exists');
|
|
|
|
assert.isFunc(Color, 'destroyAll');
|
|
|
|
assert.isFunc(Color, 'count');
|
|
|
|
assert.isFunc(Color, 'include');
|
|
|
|
assert.isFunc(Color, 'hasMany');
|
|
|
|
assert.isFunc(Color, 'belongsTo');
|
|
|
|
assert.isFunc(Color, 'hasAndBelongsToMany');
|
|
|
|
assert.isFunc(Color.prototype, 'save');
|
|
|
|
assert.isFunc(Color.prototype, 'isNewRecord');
|
|
|
|
assert.isFunc(Color.prototype, 'destroy');
|
|
|
|
assert.isFunc(Color.prototype, 'updateAttribute');
|
|
|
|
assert.isFunc(Color.prototype, 'updateAttributes');
|
2014-11-21 01:46:21 +00:00
|
|
|
assert.isFunc(Color.prototype, 'reload');
|
2013-06-07 19:57:51 +00:00
|
|
|
});
|
2014-07-20 18:52:12 +00:00
|
|
|
|
2014-11-21 01:46:21 +00:00
|
|
|
it('should honor settings.base', function() {
|
2014-07-20 18:52:12 +00:00
|
|
|
var Base = memory.createModel('base');
|
2016-11-15 21:46:23 +00:00
|
|
|
var Color = memory.createModel('color', {name: String}, {base: Base});
|
2014-07-22 17:57:42 +00:00
|
|
|
assert(Color.prototype instanceof Base);
|
|
|
|
assert.equal(Color.base, Base);
|
2014-07-20 18:52:12 +00:00
|
|
|
});
|
|
|
|
|
2014-11-21 01:46:21 +00:00
|
|
|
it('should use loopback.PersistedModel as the base for DBs', function() {
|
2016-11-15 21:46:23 +00:00
|
|
|
var Color = memory.createModel('color', {name: String});
|
2014-07-22 17:57:42 +00:00
|
|
|
assert(Color.prototype instanceof loopback.PersistedModel);
|
|
|
|
assert.equal(Color.base, loopback.PersistedModel);
|
2014-07-20 18:52:12 +00:00
|
|
|
});
|
|
|
|
|
2014-11-21 01:46:21 +00:00
|
|
|
it('should use loopback.Model as the base for non DBs', function() {
|
2014-07-20 18:52:12 +00:00
|
|
|
// Mock up a non-DB connector
|
|
|
|
var Connector = function() {
|
|
|
|
};
|
|
|
|
Connector.prototype.getTypes = function() {
|
|
|
|
return ['rest'];
|
|
|
|
};
|
|
|
|
|
|
|
|
var ds = loopback.createDataSource({
|
2016-04-01 09:14:26 +00:00
|
|
|
connector: new Connector(),
|
2014-07-20 18:52:12 +00:00
|
|
|
});
|
|
|
|
|
2016-11-15 21:46:23 +00:00
|
|
|
var Color = ds.createModel('color', {name: String});
|
2015-04-01 21:50:36 +00:00
|
|
|
assert(Color.prototype instanceof Color.registry.getModel('Model'));
|
|
|
|
assert.equal(Color.base.modelName, 'PersistedModel');
|
2014-07-20 18:52:12 +00:00
|
|
|
});
|
2013-06-07 19:57:51 +00:00
|
|
|
});
|
|
|
|
|
2014-06-05 07:45:09 +00:00
|
|
|
describe.skip('PersistedModel Methods', function() {
|
2014-11-21 01:46:21 +00:00
|
|
|
it('List the enabled and disabled methods', function() {
|
2014-06-05 07:45:09 +00:00
|
|
|
var TestModel = loopback.PersistedModel.extend('TestPersistedModel');
|
2014-05-03 04:19:14 +00:00
|
|
|
TestModel.attachTo(loopback.memory());
|
2014-11-21 01:46:21 +00:00
|
|
|
|
2013-06-12 22:44:38 +00:00
|
|
|
// assert the defaults
|
|
|
|
// - true: the method should be remote enabled
|
|
|
|
// - false: the method should not be remote enabled
|
2014-06-21 06:48:46 +00:00
|
|
|
// -
|
2013-06-11 18:07:49 +00:00
|
|
|
existsAndShared('_forDB', false);
|
|
|
|
existsAndShared('create', true);
|
2013-08-16 23:39:13 +00:00
|
|
|
existsAndShared('updateOrCreate', true);
|
2016-09-02 12:40:47 +00:00
|
|
|
existsAndShared('upsertWithWhere', true);
|
2013-08-16 23:39:13 +00:00
|
|
|
existsAndShared('upsert', true);
|
2013-06-11 18:07:49 +00:00
|
|
|
existsAndShared('findOrCreate', false);
|
|
|
|
existsAndShared('exists', true);
|
|
|
|
existsAndShared('find', true);
|
|
|
|
existsAndShared('findOne', true);
|
|
|
|
existsAndShared('destroyAll', false);
|
|
|
|
existsAndShared('count', true);
|
|
|
|
existsAndShared('include', false);
|
|
|
|
existsAndShared('hasMany', false);
|
|
|
|
existsAndShared('belongsTo', false);
|
|
|
|
existsAndShared('hasAndBelongsToMany', false);
|
2013-08-16 23:39:13 +00:00
|
|
|
existsAndShared('save', false);
|
2013-06-11 18:07:49 +00:00
|
|
|
existsAndShared('isNewRecord', false);
|
|
|
|
existsAndShared('_adapter', false);
|
2013-08-16 23:39:13 +00:00
|
|
|
existsAndShared('destroyById', true);
|
|
|
|
existsAndShared('destroy', false);
|
2013-06-11 18:07:49 +00:00
|
|
|
existsAndShared('updateAttributes', true);
|
2014-06-21 06:48:46 +00:00
|
|
|
existsAndShared('updateAll', true);
|
2013-08-28 17:39:01 +00:00
|
|
|
existsAndShared('reload', false);
|
2014-11-21 01:46:21 +00:00
|
|
|
|
2014-05-19 22:56:26 +00:00
|
|
|
function existsAndShared(Model, name, isRemoteEnabled, isProto) {
|
|
|
|
var scope = isProto ? Model.prototype : Model;
|
2014-05-03 04:19:14 +00:00
|
|
|
var fn = scope[name];
|
2014-05-19 22:56:26 +00:00
|
|
|
var actuallyEnabled = Model.getRemoteMethod(name);
|
2014-05-03 04:19:14 +00:00
|
|
|
assert(fn, name + ' should be defined!');
|
2016-04-01 09:14:26 +00:00
|
|
|
assert(actuallyEnabled === isRemoteEnabled,
|
|
|
|
name + ' ' + (isRemoteEnabled ? 'should' : 'should not') +
|
|
|
|
' be remote enabled');
|
2013-06-07 19:57:51 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2013-08-16 23:39:13 +00:00
|
|
|
});
|
2016-11-15 21:46:23 +00:00
|
|
|
|
|
|
|
var assertValidDataSource = function(dataSource) {
|
|
|
|
// has methods
|
|
|
|
assert.isFunc(dataSource, 'createModel');
|
|
|
|
assert.isFunc(dataSource, 'discoverModelDefinitions');
|
|
|
|
assert.isFunc(dataSource, 'discoverSchema');
|
|
|
|
assert.isFunc(dataSource, 'enableRemote');
|
|
|
|
assert.isFunc(dataSource, 'disableRemote');
|
|
|
|
assert.isFunc(dataSource, 'defineOperation');
|
|
|
|
assert.isFunc(dataSource, 'operations');
|
|
|
|
};
|
|
|
|
|
|
|
|
assert.isFunc = function(obj, name) {
|
|
|
|
assert(obj, 'cannot assert function ' + name + ' on object that doesnt exist');
|
|
|
|
assert(typeof obj[name] === 'function', name + ' is not a function');
|
|
|
|
};
|