2016-04-01 22:25:16 +00:00
|
|
|
// Copyright IBM Corp. 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
|
|
|
|
|
2016-08-22 19:55:22 +00:00
|
|
|
'use strict';
|
|
|
|
|
2018-12-07 16:13:48 +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() {
|
|
|
|
it('reports helpful error when connector init throws', function() {
|
2018-12-07 16:13:48 +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 16:13:48 +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 16:13:48 +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 16:13:48 +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 16:13:48 +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 16:13:48 +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 16:13:48 +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 16:13:48 +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 16:13:48 +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 16:13:48 +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 16:13:48 +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 16:13:48 +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();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
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');
|
|
|
|
});
|
|
|
|
});
|
2019-08-12 22:34:04 +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
|
|
|
});
|