From 4be2ea8afb03b727e9f988f716201e4e2d9424d7 Mon Sep 17 00:00:00 2001 From: Raymond Feng Date: Fri, 9 Feb 2018 13:16:04 -0800 Subject: [PATCH] fix: add more tests to verify new DataSource() --- lib/datasource.js | 15 ++++++--- test/datasource.test.js | 72 +++++++++++++++++++++++++++++++++++++---- 2 files changed, 75 insertions(+), 12 deletions(-) diff --git a/lib/datasource.js b/lib/datasource.js index c4f8ddf4..1c87400f 100644 --- a/lib/datasource.js +++ b/lib/datasource.js @@ -354,10 +354,16 @@ DataSource.prototype.setup = function(dsName, settings) { dsName = undefined; } + if (typeof dsName !== 'string') { + dsName = undefined; + } + if (typeof settings === 'object') { if (settings.initialize) { // Settings is the resolved connector instance connector = settings; + // Set settings to undefined to avoid confusion + settings = undefined; } else if (settings.connector) { // Use `connector` connector = settings.connector; @@ -381,10 +387,9 @@ DataSource.prototype.setup = function(dsName, settings) { // setup('myDataSource', {name: 'anotherDataSource', connector: 'memory'}); // ds.name -> 'myDataSource' and a warning will be issued console.warn( - 'A datasource has a name of %j while a name of %j is specified in ' + - 'its settings. Please adjust your configuration so these names match. ' + - '%j will be assigned as the actual datasource name.', - dsName, settings.name, dsName); + 'A datasource is created with name %j, which is different from the name in settings (%j). ' + + 'Please adjust your configuration to ensure these names match.', + dsName, settings.name); } // Disconnected by default @@ -450,7 +455,7 @@ DataSource.prototype.setup = function(dsName, settings) { } catch (err) { if (err.message) { err.message = 'Cannot initialize connector ' + - JSON.stringify(connector.name || dsName) + ': ' + + JSON.stringify(connectorName) + ': ' + err.message; } throw err; diff --git a/test/datasource.test.js b/test/datasource.test.js index cc644d3d..5cb74e0c 100644 --- a/test/datasource.test.js +++ b/test/datasource.test.js @@ -46,32 +46,56 @@ describe('DataSource', function() { }).should.throw(/expected test error/); }); + /** + * new DataSource(dsName, settings) without settings.name + */ it('should retain the name assigned to it', function() { - var dataSource = new DataSource('ram', { + var dataSource = new DataSource('myDataSource', { connector: 'memory', }); - dataSource.name.should.equal('ram'); + dataSource.name.should.equal('myDataSource'); }); + /** + * new DataSource(dsName, settings) + */ it('should allow the name assigned to it to take precedence over the settings name', function() { - var dataSource = new DataSource('ram', { - name: 'temp', + var dataSource = new DataSource('myDataSource', { + name: 'defaultDataSource', connector: 'memory', }); - dataSource.name.should.equal('ram'); + dataSource.name.should.equal('myDataSource'); }); + /** + * new DataSource(settings) with settings.name + */ it('should retain the name from the settings if no name is assigned', function() { var dataSource = new DataSource({ - name: 'temp', + name: 'defaultDataSource', connector: 'memory', }); - dataSource.name.should.equal('temp'); + dataSource.name.should.equal('defaultDataSource'); }); + /** + * 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 + */ it('should use the connector name if no name is provided', function() { var dataSource = new DataSource({ connector: 'memory', @@ -79,4 +103,38 @@ describe('DataSource', function() { dataSource.name.should.equal('memory'); }); + + /** + * 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); + }); });