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';
|
|
|
|
|
2016-03-02 11:32:11 +00:00
|
|
|
var should = require('./init.js');
|
|
|
|
var DataSource = require('../lib/datasource.js').DataSource;
|
|
|
|
|
|
|
|
describe('DataSource', function() {
|
|
|
|
it('reports helpful error when connector init throws', function() {
|
|
|
|
var throwingConnector = {
|
|
|
|
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-02-09 21:16:04 +00:00
|
|
|
var 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-02-09 21:16:04 +00:00
|
|
|
var dataSource = new DataSource('myDataSource', {
|
|
|
|
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() {
|
|
|
|
var 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() {
|
|
|
|
var dataSource = new DataSource(undefined, {
|
|
|
|
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() {
|
|
|
|
var dataSource = new DataSource({
|
|
|
|
connector: 'memory',
|
|
|
|
});
|
|
|
|
|
|
|
|
dataSource.name.should.equal('memory');
|
|
|
|
});
|
2018-02-09 21:16:04 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* new DataSource(connectorInstance)
|
|
|
|
*/
|
|
|
|
it('should accept resolved connector', function() {
|
|
|
|
var mockConnector = {
|
|
|
|
name: 'loopback-connector-mock',
|
|
|
|
initialize: function(ds, cb) {
|
|
|
|
ds.connector = mockConnector;
|
|
|
|
return cb(null);
|
|
|
|
},
|
|
|
|
};
|
|
|
|
var dataSource = new DataSource(mockConnector);
|
|
|
|
|
|
|
|
dataSource.name.should.equal('loopback-connector-mock');
|
|
|
|
dataSource.connector.should.equal(mockConnector);
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* new DataSource(dsName, connectorInstance)
|
|
|
|
*/
|
|
|
|
it('should accept resolved connector', function() {
|
|
|
|
var mockConnector = {
|
|
|
|
name: 'loopback-connector-mock',
|
|
|
|
initialize: function(ds, cb) {
|
|
|
|
ds.connector = mockConnector;
|
|
|
|
return cb(null);
|
|
|
|
},
|
|
|
|
};
|
|
|
|
var dataSource = new DataSource('myDataSource', mockConnector);
|
|
|
|
|
|
|
|
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() {
|
|
|
|
var mockConnector = {
|
|
|
|
name: 'loopback-connector-mock',
|
|
|
|
initialize: function(ds, cb) {
|
|
|
|
ds.connector = mockConnector;
|
|
|
|
return cb(null);
|
|
|
|
},
|
|
|
|
};
|
|
|
|
var dataSource = new DataSource(mockConnector, {name: 'myDataSource'});
|
|
|
|
|
|
|
|
dataSource.name.should.equal('myDataSource');
|
|
|
|
dataSource.connector.should.equal(mockConnector);
|
|
|
|
});
|
2016-03-02 11:32:11 +00:00
|
|
|
});
|