2020-01-21 18:12:14 +00:00
|
|
|
// Copyright IBM Corp. 2016,2019. All Rights Reserved.
|
2016-04-01 22:25:16 +00:00
|
|
|
// Node module: loopback-datasource-juggler
|
|
|
|
// This file is licensed under the MIT License.
|
|
|
|
// License text available at https://opensource.org/licenses/MIT
|
|
|
|
|
2016-08-22 19:55:22 +00:00
|
|
|
'use strict';
|
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
const should = require('./init.js');
|
|
|
|
const DataSource = require('../lib/datasource.js').DataSource;
|
2016-03-02 11:32:11 +00:00
|
|
|
|
|
|
|
describe('DataSource', function() {
|
2019-05-10 12:56:02 +00:00
|
|
|
it('clones settings to prevent surprising changes in passed args', () => {
|
|
|
|
const config = {connector: 'memory'};
|
|
|
|
|
|
|
|
const ds = new DataSource(config);
|
|
|
|
ds.settings.extra = true;
|
|
|
|
|
|
|
|
config.should.eql({connector: 'memory'});
|
|
|
|
});
|
|
|
|
|
2016-03-02 11:32:11 +00:00
|
|
|
it('reports helpful error when connector init throws', function() {
|
2018-12-07 14:54:29 +00:00
|
|
|
const throwingConnector = {
|
2016-03-02 11:32:11 +00:00
|
|
|
name: 'loopback-connector-throwing',
|
|
|
|
initialize: function(ds, cb) {
|
|
|
|
throw new Error('expected test error');
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
(function() {
|
|
|
|
// this is what LoopBack does
|
|
|
|
return new DataSource({
|
|
|
|
name: 'dsname',
|
2016-04-01 11:48:17 +00:00
|
|
|
connector: throwingConnector,
|
2016-03-02 11:32:11 +00:00
|
|
|
});
|
|
|
|
}).should.throw(/loopback-connector-throwing/);
|
|
|
|
});
|
2017-01-31 09:31:54 +00:00
|
|
|
|
|
|
|
it('reports helpful error when connector init via short name throws', function() {
|
|
|
|
(function() {
|
|
|
|
// this is what LoopBack does
|
|
|
|
return new DataSource({
|
|
|
|
name: 'dsname',
|
|
|
|
connector: 'throwing',
|
|
|
|
});
|
|
|
|
}).should.throw(/expected test error/);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('reports helpful error when connector init via long name throws', function() {
|
|
|
|
(function() {
|
|
|
|
// this is what LoopBack does
|
|
|
|
return new DataSource({
|
|
|
|
name: 'dsname',
|
|
|
|
connector: 'loopback-connector-throwing',
|
|
|
|
});
|
|
|
|
}).should.throw(/expected test error/);
|
|
|
|
});
|
2018-01-30 21:57:42 +00:00
|
|
|
|
2018-02-09 21:16:04 +00:00
|
|
|
/**
|
|
|
|
* new DataSource(dsName, settings) without settings.name
|
|
|
|
*/
|
2018-01-30 21:57:42 +00:00
|
|
|
it('should retain the name assigned to it', function() {
|
2018-12-07 14:54:29 +00:00
|
|
|
const dataSource = new DataSource('myDataSource', {
|
2018-01-30 21:57:42 +00:00
|
|
|
connector: 'memory',
|
|
|
|
});
|
|
|
|
|
2018-02-09 21:16:04 +00:00
|
|
|
dataSource.name.should.equal('myDataSource');
|
2018-01-30 21:57:42 +00:00
|
|
|
});
|
|
|
|
|
2018-02-09 21:16:04 +00:00
|
|
|
/**
|
|
|
|
* new DataSource(dsName, settings)
|
|
|
|
*/
|
2018-01-30 21:57:42 +00:00
|
|
|
it('should allow the name assigned to it to take precedence over the settings name', function() {
|
2018-12-07 14:54:29 +00:00
|
|
|
const dataSource = new DataSource('myDataSource', {
|
2018-02-09 21:16:04 +00:00
|
|
|
name: 'defaultDataSource',
|
2018-01-30 21:57:42 +00:00
|
|
|
connector: 'memory',
|
|
|
|
});
|
|
|
|
|
2018-02-09 21:16:04 +00:00
|
|
|
dataSource.name.should.equal('myDataSource');
|
2018-01-30 21:57:42 +00:00
|
|
|
});
|
|
|
|
|
2018-02-09 21:16:04 +00:00
|
|
|
/**
|
|
|
|
* new DataSource(settings) with settings.name
|
|
|
|
*/
|
2018-01-30 21:57:42 +00:00
|
|
|
it('should retain the name from the settings if no name is assigned', function() {
|
2018-12-07 14:54:29 +00:00
|
|
|
const dataSource = new DataSource({
|
2018-02-09 21:16:04 +00:00
|
|
|
name: 'defaultDataSource',
|
2018-01-30 21:57:42 +00:00
|
|
|
connector: 'memory',
|
|
|
|
});
|
|
|
|
|
2018-02-09 21:16:04 +00:00
|
|
|
dataSource.name.should.equal('defaultDataSource');
|
2018-01-30 21:57:42 +00:00
|
|
|
});
|
|
|
|
|
2018-02-09 21:16:04 +00:00
|
|
|
/**
|
|
|
|
* new DataSource(undefined, settings)
|
|
|
|
*/
|
|
|
|
it('should retain the name from the settings if name is undefined', function() {
|
2018-12-07 14:54:29 +00:00
|
|
|
const dataSource = new DataSource(undefined, {
|
2018-02-09 21:16:04 +00:00
|
|
|
name: 'defaultDataSource',
|
|
|
|
connector: 'memory',
|
|
|
|
});
|
|
|
|
|
|
|
|
dataSource.name.should.equal('defaultDataSource');
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* new DataSource(settings) without settings.name
|
|
|
|
*/
|
2018-01-30 21:57:42 +00:00
|
|
|
it('should use the connector name if no name is provided', function() {
|
2018-12-07 14:54:29 +00:00
|
|
|
const dataSource = new DataSource({
|
2018-01-30 21:57:42 +00:00
|
|
|
connector: 'memory',
|
|
|
|
});
|
|
|
|
|
|
|
|
dataSource.name.should.equal('memory');
|
|
|
|
});
|
2018-02-09 21:16:04 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* new DataSource(connectorInstance)
|
|
|
|
*/
|
|
|
|
it('should accept resolved connector', function() {
|
2018-12-07 15:22:36 +00:00
|
|
|
const mockConnector = {
|
2018-02-09 21:16:04 +00:00
|
|
|
name: 'loopback-connector-mock',
|
|
|
|
initialize: function(ds, cb) {
|
|
|
|
ds.connector = mockConnector;
|
|
|
|
return cb(null);
|
|
|
|
},
|
|
|
|
};
|
2018-12-07 14:54:29 +00:00
|
|
|
const dataSource = new DataSource(mockConnector);
|
2018-02-09 21:16:04 +00:00
|
|
|
|
|
|
|
dataSource.name.should.equal('loopback-connector-mock');
|
|
|
|
dataSource.connector.should.equal(mockConnector);
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* new DataSource(dsName, connectorInstance)
|
|
|
|
*/
|
2018-06-12 07:13:32 +00:00
|
|
|
it('should accept dsName and resolved connector', function() {
|
2018-12-07 15:22:36 +00:00
|
|
|
const mockConnector = {
|
2018-02-09 21:16:04 +00:00
|
|
|
name: 'loopback-connector-mock',
|
|
|
|
initialize: function(ds, cb) {
|
|
|
|
ds.connector = mockConnector;
|
|
|
|
return cb(null);
|
|
|
|
},
|
|
|
|
};
|
2018-12-07 14:54:29 +00:00
|
|
|
const dataSource = new DataSource('myDataSource', mockConnector);
|
2018-02-09 21:16:04 +00:00
|
|
|
|
|
|
|
dataSource.name.should.equal('myDataSource');
|
|
|
|
dataSource.connector.should.equal(mockConnector);
|
|
|
|
});
|
2018-02-13 06:02:17 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* new DataSource(connectorInstance, settings)
|
|
|
|
*/
|
|
|
|
it('should accept resolved connector and settings', function() {
|
2018-12-07 15:22:36 +00:00
|
|
|
const mockConnector = {
|
2018-02-13 06:02:17 +00:00
|
|
|
name: 'loopback-connector-mock',
|
|
|
|
initialize: function(ds, cb) {
|
|
|
|
ds.connector = mockConnector;
|
|
|
|
return cb(null);
|
|
|
|
},
|
|
|
|
};
|
2018-12-07 14:54:29 +00:00
|
|
|
const dataSource = new DataSource(mockConnector, {name: 'myDataSource'});
|
2018-02-13 06:02:17 +00:00
|
|
|
|
|
|
|
dataSource.name.should.equal('myDataSource');
|
|
|
|
dataSource.connector.should.equal(mockConnector);
|
|
|
|
});
|
2018-04-16 11:28:17 +00:00
|
|
|
|
2018-05-22 17:06:50 +00:00
|
|
|
it('should set states correctly with eager connect', function(done) {
|
2018-12-07 15:22:36 +00:00
|
|
|
const mockConnector = {
|
2018-05-22 17:06:50 +00:00
|
|
|
name: 'loopback-connector-mock',
|
|
|
|
initialize: function(ds, cb) {
|
|
|
|
ds.connector = mockConnector;
|
|
|
|
this.connect(cb);
|
|
|
|
},
|
|
|
|
|
|
|
|
connect: function(cb) {
|
|
|
|
process.nextTick(function() {
|
|
|
|
cb(null);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
};
|
2018-12-07 14:54:29 +00:00
|
|
|
const dataSource = new DataSource(mockConnector);
|
2018-05-22 17:06:50 +00:00
|
|
|
// DataSource is instantiated
|
|
|
|
// connected: false, connecting: false, initialized: false
|
|
|
|
dataSource.connected.should.be.false();
|
|
|
|
dataSource.connecting.should.be.false();
|
|
|
|
dataSource.initialized.should.be.false();
|
|
|
|
|
|
|
|
dataSource.on('initialized', function() {
|
|
|
|
// DataSource is initialized with lazyConnect
|
|
|
|
// connected: false, connecting: false, initialized: true
|
|
|
|
dataSource.connected.should.be.false();
|
|
|
|
dataSource.connecting.should.be.false();
|
|
|
|
dataSource.initialized.should.be.true();
|
|
|
|
});
|
|
|
|
|
|
|
|
dataSource.on('connected', function() {
|
|
|
|
// DataSource is now connected
|
|
|
|
// connected: true, connecting: false
|
|
|
|
dataSource.connected.should.be.true();
|
|
|
|
dataSource.connecting.should.be.false();
|
|
|
|
});
|
|
|
|
|
|
|
|
// Call connect() in next tick so that we'll receive initialized event
|
|
|
|
// first
|
|
|
|
process.nextTick(function() {
|
|
|
|
// At this point, the datasource is already connected by
|
|
|
|
// connector's (mockConnector) initialize function
|
|
|
|
dataSource.connect(function() {
|
|
|
|
// DataSource is now connected
|
|
|
|
// connected: true, connecting: false
|
|
|
|
dataSource.connected.should.be.true();
|
|
|
|
dataSource.connecting.should.be.false();
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
// As the datasource is already connected, no connecting will happen
|
|
|
|
// connected: true, connecting: false
|
|
|
|
dataSource.connected.should.be.true();
|
|
|
|
dataSource.connecting.should.be.false();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should set states correctly with deferred connect', function(done) {
|
2018-12-07 15:22:36 +00:00
|
|
|
const mockConnector = {
|
2018-05-22 17:06:50 +00:00
|
|
|
name: 'loopback-connector-mock',
|
|
|
|
initialize: function(ds, cb) {
|
|
|
|
ds.connector = mockConnector;
|
|
|
|
// Explicitly call back with false to denote connection is not ready
|
|
|
|
process.nextTick(function() {
|
|
|
|
cb(null, false);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
connect: function(cb) {
|
|
|
|
process.nextTick(function() {
|
|
|
|
cb(null);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
};
|
2018-12-07 14:54:29 +00:00
|
|
|
const dataSource = new DataSource(mockConnector);
|
2018-05-22 17:06:50 +00:00
|
|
|
// DataSource is instantiated
|
|
|
|
// connected: false, connecting: false, initialized: false
|
|
|
|
dataSource.connected.should.be.false();
|
|
|
|
dataSource.connecting.should.be.false();
|
|
|
|
dataSource.initialized.should.be.false();
|
|
|
|
|
|
|
|
dataSource.on('initialized', function() {
|
|
|
|
// DataSource is initialized with lazyConnect
|
|
|
|
// connected: false, connecting: false, initialized: true
|
|
|
|
dataSource.connected.should.be.false();
|
|
|
|
dataSource.connecting.should.be.false();
|
|
|
|
dataSource.initialized.should.be.true();
|
|
|
|
});
|
|
|
|
|
|
|
|
dataSource.on('connected', function() {
|
|
|
|
// DataSource is now connected
|
|
|
|
// connected: true, connecting: false
|
|
|
|
dataSource.connected.should.be.true();
|
|
|
|
dataSource.connecting.should.be.false();
|
|
|
|
});
|
|
|
|
|
|
|
|
// Call connect() in next tick so that we'll receive initialized event
|
|
|
|
// first
|
|
|
|
process.nextTick(function() {
|
|
|
|
dataSource.connect(function() {
|
|
|
|
// DataSource is now connected
|
|
|
|
// connected: true, connecting: false
|
|
|
|
dataSource.connected.should.be.true();
|
|
|
|
dataSource.connecting.should.be.false();
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
// As the datasource is not connected, connecting will happen
|
|
|
|
// connected: false, connecting: true
|
|
|
|
dataSource.connected.should.be.false();
|
|
|
|
dataSource.connecting.should.be.true();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should set states correctly with lazyConnect = true', function(done) {
|
2018-12-07 15:22:36 +00:00
|
|
|
const mockConnector = {
|
2018-05-22 17:06:50 +00:00
|
|
|
name: 'loopback-connector-mock',
|
|
|
|
initialize: function(ds, cb) {
|
|
|
|
ds.connector = mockConnector;
|
|
|
|
process.nextTick(function() {
|
|
|
|
cb(null);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
connect: function(cb) {
|
|
|
|
process.nextTick(function() {
|
|
|
|
cb(null);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
};
|
2018-12-07 14:54:29 +00:00
|
|
|
const dataSource = new DataSource(mockConnector, {lazyConnect: true});
|
2018-05-22 17:06:50 +00:00
|
|
|
// DataSource is instantiated
|
|
|
|
// connected: false, connecting: false, initialized: false
|
|
|
|
dataSource.connected.should.be.false();
|
|
|
|
dataSource.connecting.should.be.false();
|
|
|
|
dataSource.initialized.should.be.false();
|
|
|
|
|
|
|
|
dataSource.on('initialized', function() {
|
|
|
|
// DataSource is initialized with lazyConnect
|
|
|
|
// connected: false, connecting: false, initialized: true
|
|
|
|
dataSource.connected.should.be.false();
|
|
|
|
dataSource.connecting.should.be.false();
|
|
|
|
dataSource.initialized.should.be.true();
|
|
|
|
});
|
|
|
|
|
|
|
|
dataSource.on('connected', function() {
|
|
|
|
// DataSource is now connected
|
|
|
|
// connected: true, connecting: false
|
|
|
|
dataSource.connected.should.be.true();
|
|
|
|
dataSource.connecting.should.be.false();
|
|
|
|
});
|
|
|
|
|
|
|
|
// Call connect() in next tick so that we'll receive initialized event
|
|
|
|
// first
|
|
|
|
process.nextTick(function() {
|
|
|
|
dataSource.connect(function() {
|
|
|
|
// DataSource is now connected
|
|
|
|
// connected: true, connecting: false
|
|
|
|
dataSource.connected.should.be.true();
|
|
|
|
dataSource.connecting.should.be.false();
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
// DataSource is now connecting
|
|
|
|
// connected: false, connecting: true
|
|
|
|
dataSource.connected.should.be.false();
|
|
|
|
dataSource.connecting.should.be.true();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-04-21 12:11:50 +00:00
|
|
|
it('provides stop() API calling disconnect', function(done) {
|
|
|
|
const mockConnector = {
|
|
|
|
name: 'loopback-connector-mock',
|
|
|
|
initialize: function(ds, cb) {
|
|
|
|
ds.connector = mockConnector;
|
|
|
|
process.nextTick(function() {
|
|
|
|
cb(null);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
const dataSource = new DataSource(mockConnector);
|
|
|
|
dataSource.on('connected', function() {
|
|
|
|
// DataSource is now connected
|
|
|
|
// connected: true, connecting: false
|
|
|
|
dataSource.connected.should.be.true();
|
|
|
|
dataSource.connecting.should.be.false();
|
|
|
|
|
|
|
|
dataSource.stop(() => {
|
|
|
|
dataSource.connected.should.be.false();
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-04-16 11:28:17 +00:00
|
|
|
describe('deleteModelByName()', () => {
|
|
|
|
it('removes the model from ModelBuilder registry', () => {
|
|
|
|
const ds = new DataSource('ds', {connector: 'memory'});
|
|
|
|
|
|
|
|
ds.createModel('TestModel');
|
|
|
|
Object.keys(ds.modelBuilder.models)
|
|
|
|
.should.containEql('TestModel');
|
|
|
|
Object.keys(ds.modelBuilder.definitions)
|
|
|
|
.should.containEql('TestModel');
|
|
|
|
|
|
|
|
ds.deleteModelByName('TestModel');
|
|
|
|
|
|
|
|
Object.keys(ds.modelBuilder.models)
|
|
|
|
.should.not.containEql('TestModel');
|
|
|
|
Object.keys(ds.modelBuilder.definitions)
|
|
|
|
.should.not.containEql('TestModel');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('removes the model from connector registry', () => {
|
|
|
|
const ds = new DataSource('ds', {connector: 'memory'});
|
|
|
|
|
|
|
|
ds.createModel('TestModel');
|
|
|
|
Object.keys(ds.connector._models)
|
|
|
|
.should.containEql('TestModel');
|
|
|
|
|
|
|
|
ds.deleteModelByName('TestModel');
|
|
|
|
|
|
|
|
Object.keys(ds.connector._models)
|
|
|
|
.should.not.containEql('TestModel');
|
|
|
|
});
|
|
|
|
});
|
2018-12-06 12:58:47 +00:00
|
|
|
|
|
|
|
describe('execute', () => {
|
|
|
|
let ds;
|
|
|
|
beforeEach(() => ds = new DataSource('ds', {connector: 'memory'}));
|
|
|
|
|
|
|
|
it('calls connnector to execute the command', async () => {
|
|
|
|
let called = 'not called';
|
|
|
|
ds.connector.execute = function(command, args, options, callback) {
|
|
|
|
called = {command, args, options};
|
|
|
|
callback(null, 'a-result');
|
|
|
|
};
|
|
|
|
|
|
|
|
const result = await ds.execute(
|
|
|
|
'command',
|
|
|
|
['arg1', 'arg2'],
|
2019-12-03 09:09:16 +00:00
|
|
|
{'a-flag': 'a-value'},
|
2018-12-06 12:58:47 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
result.should.be.equal('a-result');
|
|
|
|
called.should.be.eql({
|
|
|
|
command: 'command',
|
|
|
|
args: ['arg1', 'arg2'],
|
|
|
|
options: {'a-flag': 'a-value'},
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('supports shorthand version (cmd)', async () => {
|
|
|
|
let called = 'not called';
|
|
|
|
ds.connector.execute = function(command, args, options, callback) {
|
2020-08-03 12:23:50 +00:00
|
|
|
// copied from loopback-connector/lib/sql.js
|
|
|
|
if (typeof args === 'function' && options === undefined && callback === undefined) {
|
|
|
|
// execute(sql, callback)
|
|
|
|
options = {};
|
|
|
|
callback = args;
|
|
|
|
args = [];
|
|
|
|
}
|
|
|
|
|
2018-12-06 12:58:47 +00:00
|
|
|
called = {command, args, options};
|
|
|
|
callback(null, 'a-result');
|
|
|
|
};
|
|
|
|
|
|
|
|
const result = await ds.execute('command');
|
|
|
|
result.should.be.equal('a-result');
|
|
|
|
called.should.be.eql({
|
|
|
|
command: 'command',
|
|
|
|
args: [],
|
|
|
|
options: {},
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('supports shorthand version (cmd, args)', async () => {
|
|
|
|
let called = 'not called';
|
|
|
|
ds.connector.execute = function(command, args, options, callback) {
|
2020-08-03 12:23:50 +00:00
|
|
|
// copied from loopback-connector/lib/sql.js
|
|
|
|
if (typeof options === 'function' && callback === undefined) {
|
|
|
|
// execute(sql, params, callback)
|
|
|
|
callback = options;
|
|
|
|
options = {};
|
|
|
|
}
|
|
|
|
|
2018-12-06 12:58:47 +00:00
|
|
|
called = {command, args, options};
|
|
|
|
callback(null, 'a-result');
|
|
|
|
};
|
|
|
|
|
|
|
|
await ds.execute('command', ['arg1', 'arg2']);
|
|
|
|
called.should.be.eql({
|
|
|
|
command: 'command',
|
|
|
|
args: ['arg1', 'arg2'],
|
|
|
|
options: {},
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('converts multiple callbacks arguments into a promise resolved with an array', async () => {
|
2020-08-03 12:23:50 +00:00
|
|
|
ds.connector.execute = function() {
|
|
|
|
const callback = arguments[arguments.length - 1];
|
2018-12-06 12:58:47 +00:00
|
|
|
callback(null, 'result1', 'result2');
|
|
|
|
};
|
|
|
|
const result = await ds.execute('command');
|
|
|
|
result.should.eql(['result1', 'result2']);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('allows args as object', async () => {
|
|
|
|
let called = 'not called';
|
|
|
|
ds.connector.execute = function(command, args, options, callback) {
|
|
|
|
called = {command, args, options};
|
|
|
|
callback();
|
|
|
|
};
|
|
|
|
|
|
|
|
// See https://www.npmjs.com/package/loopback-connector-neo4j-graph
|
|
|
|
const command = 'MATCH (u:User {email: {email}}) RETURN u';
|
2020-08-03 12:23:50 +00:00
|
|
|
await ds.execute(command, {email: 'alice@example.com'}, {options: true});
|
2018-12-06 12:58:47 +00:00
|
|
|
called.should.be.eql({
|
|
|
|
command,
|
|
|
|
args: {email: 'alice@example.com'},
|
2020-08-03 12:23:50 +00:00
|
|
|
options: {options: true},
|
2018-12-06 12:58:47 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-08-03 12:23:50 +00:00
|
|
|
it('supports MongoDB version (collection, cmd, args, options)', async () => {
|
|
|
|
let called = 'not called';
|
|
|
|
ds.connector.execute = function(...params) {
|
|
|
|
const callback = params.pop();
|
|
|
|
called = params;
|
|
|
|
callback(null, 'a-result');
|
|
|
|
};
|
|
|
|
|
|
|
|
const result = await ds.execute(
|
|
|
|
'collection',
|
|
|
|
'command',
|
|
|
|
['arg1', 'arg2'],
|
|
|
|
{options: true},
|
|
|
|
);
|
|
|
|
|
|
|
|
result.should.equal('a-result');
|
|
|
|
called.should.be.eql([
|
|
|
|
'collection',
|
|
|
|
'command',
|
|
|
|
['arg1', 'arg2'],
|
|
|
|
{options: true},
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('supports free-form version (...params)', async () => {
|
|
|
|
let called = 'not called';
|
|
|
|
ds.connector.execute = function(...params) {
|
|
|
|
const callback = params.pop();
|
|
|
|
called = params;
|
|
|
|
callback(null, 'a-result');
|
|
|
|
};
|
|
|
|
|
|
|
|
const result = await ds.execute(
|
|
|
|
'arg1',
|
|
|
|
'arg2',
|
|
|
|
'arg3',
|
|
|
|
'arg4',
|
|
|
|
{options: true},
|
|
|
|
);
|
|
|
|
|
|
|
|
result.should.equal('a-result');
|
|
|
|
called.should.be.eql([
|
|
|
|
'arg1',
|
|
|
|
'arg2',
|
|
|
|
'arg3',
|
|
|
|
'arg4',
|
|
|
|
{options: true},
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
2018-12-06 12:58:47 +00:00
|
|
|
it('throws NOT_IMPLEMENTED when no connector is provided', () => {
|
|
|
|
ds.connector = undefined;
|
|
|
|
return ds.execute('command').should.be.rejectedWith({
|
|
|
|
code: 'NOT_IMPLEMENTED',
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('throws NOT_IMPLEMENTED for connectors not implementing execute', () => {
|
|
|
|
ds.connector.execute = undefined;
|
|
|
|
return ds.execute('command').should.be.rejectedWith({
|
|
|
|
code: 'NOT_IMPLEMENTED',
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2019-07-01 14:54:50 +00:00
|
|
|
|
|
|
|
describe('automigrate', () => {
|
|
|
|
it('reports connection errors (immediate connect)', async () => {
|
|
|
|
const dataSource = new DataSource({
|
|
|
|
connector: givenConnectorFailingOnConnect(),
|
|
|
|
});
|
|
|
|
dataSource.define('MyModel');
|
|
|
|
await dataSource.automigrate().should.be.rejectedWith(/test failure/);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('reports connection errors (lazy connect)', () => {
|
|
|
|
const dataSource = new DataSource({
|
|
|
|
connector: givenConnectorFailingOnConnect(),
|
|
|
|
lazyConnect: true,
|
|
|
|
});
|
|
|
|
dataSource.define('MyModel');
|
|
|
|
return dataSource.automigrate().should.be.rejectedWith(/test failure/);
|
|
|
|
});
|
|
|
|
|
|
|
|
function givenConnectorFailingOnConnect() {
|
|
|
|
return givenMockConnector({
|
|
|
|
connect: function(cb) {
|
|
|
|
process.nextTick(() => cb(new Error('test failure')));
|
|
|
|
},
|
|
|
|
automigrate: function(models, cb) {
|
|
|
|
cb(new Error('automigrate should not have been called'));
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('autoupdate', () => {
|
|
|
|
it('reports connection errors (immediate connect)', async () => {
|
|
|
|
const dataSource = new DataSource({
|
|
|
|
connector: givenConnectorFailingOnConnect(),
|
|
|
|
});
|
|
|
|
dataSource.define('MyModel');
|
|
|
|
await dataSource.autoupdate().should.be.rejectedWith(/test failure/);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('reports connection errors (lazy connect)', () => {
|
|
|
|
const dataSource = new DataSource({
|
|
|
|
connector: givenConnectorFailingOnConnect(),
|
|
|
|
lazyConnect: true,
|
|
|
|
});
|
|
|
|
dataSource.define('MyModel');
|
|
|
|
return dataSource.autoupdate().should.be.rejectedWith(/test failure/);
|
|
|
|
});
|
|
|
|
|
|
|
|
function givenConnectorFailingOnConnect() {
|
|
|
|
return givenMockConnector({
|
|
|
|
connect: function(cb) {
|
|
|
|
process.nextTick(() => cb(new Error('test failure')));
|
|
|
|
},
|
|
|
|
autoupdate: function(models, cb) {
|
|
|
|
cb(new Error('autoupdate should not have been called'));
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2019-07-18 14:34:27 +00:00
|
|
|
|
|
|
|
describe('deleteAllModels', () => {
|
|
|
|
it('removes all model definitions', () => {
|
|
|
|
const ds = new DataSource({connector: 'memory'});
|
|
|
|
ds.define('Category');
|
|
|
|
ds.define('Product');
|
|
|
|
|
|
|
|
Object.keys(ds.modelBuilder.definitions)
|
|
|
|
.should.deepEqual(['Category', 'Product']);
|
|
|
|
Object.keys(ds.modelBuilder.models)
|
|
|
|
.should.deepEqual(['Category', 'Product']);
|
|
|
|
Object.keys(ds.connector._models)
|
|
|
|
.should.deepEqual(['Category', 'Product']);
|
|
|
|
|
|
|
|
ds.deleteAllModels();
|
|
|
|
|
|
|
|
Object.keys(ds.modelBuilder.definitions).should.be.empty();
|
|
|
|
Object.keys(ds.modelBuilder.models).should.be.empty();
|
|
|
|
Object.keys(ds.connector._models).should.be.empty();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('preserves the connector instance', () => {
|
|
|
|
const ds = new DataSource({connector: 'memory'});
|
|
|
|
const connector = ds.connector;
|
|
|
|
ds.deleteAllModels();
|
|
|
|
ds.connector.should.equal(connector);
|
|
|
|
});
|
|
|
|
});
|
2019-08-01 20:54:44 +00:00
|
|
|
|
|
|
|
describe('getMaxOfflineRequests', () => {
|
|
|
|
let ds;
|
|
|
|
beforeEach(() => ds = new DataSource('ds', {connector: 'memory'}));
|
|
|
|
|
|
|
|
it('sets the default maximum number of event listeners to 16', () => {
|
|
|
|
ds.getMaxOfflineRequests().should.be.eql(16);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('uses provided number of listeners', () => {
|
|
|
|
ds.settings.maxOfflineRequests = 17;
|
|
|
|
ds.getMaxOfflineRequests().should.be.eql(17);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('throws an error if a non-number is provided for the max number of listeners', () => {
|
|
|
|
ds.settings.maxOfflineRequests = '17';
|
|
|
|
|
|
|
|
(function() {
|
|
|
|
return ds.getMaxOfflineRequests();
|
|
|
|
}).should.throw('maxOfflineRequests must be a number');
|
|
|
|
});
|
|
|
|
});
|
2016-03-02 11:32:11 +00:00
|
|
|
});
|
2019-07-01 14:54:50 +00:00
|
|
|
|
|
|
|
function givenMockConnector(props) {
|
|
|
|
const connector = {
|
|
|
|
name: 'loopback-connector-mock',
|
|
|
|
initialize: function(ds, cb) {
|
|
|
|
ds.connector = connector;
|
|
|
|
if (ds.settings.lazyConnect) {
|
|
|
|
cb(null, false);
|
|
|
|
} else {
|
|
|
|
connector.connect(cb);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
...props,
|
|
|
|
};
|
|
|
|
return connector;
|
|
|
|
}
|