fix: add more tests to verify new DataSource()
This commit is contained in:
parent
54143dfa37
commit
4be2ea8afb
|
@ -354,10 +354,16 @@ DataSource.prototype.setup = function(dsName, settings) {
|
||||||
dsName = undefined;
|
dsName = undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (typeof dsName !== 'string') {
|
||||||
|
dsName = undefined;
|
||||||
|
}
|
||||||
|
|
||||||
if (typeof settings === 'object') {
|
if (typeof settings === 'object') {
|
||||||
if (settings.initialize) {
|
if (settings.initialize) {
|
||||||
// Settings is the resolved connector instance
|
// Settings is the resolved connector instance
|
||||||
connector = settings;
|
connector = settings;
|
||||||
|
// Set settings to undefined to avoid confusion
|
||||||
|
settings = undefined;
|
||||||
} else if (settings.connector) {
|
} else if (settings.connector) {
|
||||||
// Use `connector`
|
// Use `connector`
|
||||||
connector = settings.connector;
|
connector = settings.connector;
|
||||||
|
@ -381,10 +387,9 @@ DataSource.prototype.setup = function(dsName, settings) {
|
||||||
// setup('myDataSource', {name: 'anotherDataSource', connector: 'memory'});
|
// setup('myDataSource', {name: 'anotherDataSource', connector: 'memory'});
|
||||||
// ds.name -> 'myDataSource' and a warning will be issued
|
// ds.name -> 'myDataSource' and a warning will be issued
|
||||||
console.warn(
|
console.warn(
|
||||||
'A datasource has a name of %j while a name of %j is specified in ' +
|
'A datasource is created with name %j, which is different from the name in settings (%j). ' +
|
||||||
'its settings. Please adjust your configuration so these names match. ' +
|
'Please adjust your configuration to ensure these names match.',
|
||||||
'%j will be assigned as the actual datasource name.',
|
dsName, settings.name);
|
||||||
dsName, settings.name, dsName);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Disconnected by default
|
// Disconnected by default
|
||||||
|
@ -450,7 +455,7 @@ DataSource.prototype.setup = function(dsName, settings) {
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
if (err.message) {
|
if (err.message) {
|
||||||
err.message = 'Cannot initialize connector ' +
|
err.message = 'Cannot initialize connector ' +
|
||||||
JSON.stringify(connector.name || dsName) + ': ' +
|
JSON.stringify(connectorName) + ': ' +
|
||||||
err.message;
|
err.message;
|
||||||
}
|
}
|
||||||
throw err;
|
throw err;
|
||||||
|
|
|
@ -46,32 +46,56 @@ describe('DataSource', function() {
|
||||||
}).should.throw(/expected test error/);
|
}).should.throw(/expected test error/);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
/**
|
||||||
|
* new DataSource(dsName, settings) without settings.name
|
||||||
|
*/
|
||||||
it('should retain the name assigned to it', function() {
|
it('should retain the name assigned to it', function() {
|
||||||
var dataSource = new DataSource('ram', {
|
var dataSource = new DataSource('myDataSource', {
|
||||||
connector: 'memory',
|
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() {
|
it('should allow the name assigned to it to take precedence over the settings name', function() {
|
||||||
var dataSource = new DataSource('ram', {
|
var dataSource = new DataSource('myDataSource', {
|
||||||
name: 'temp',
|
name: 'defaultDataSource',
|
||||||
connector: 'memory',
|
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() {
|
it('should retain the name from the settings if no name is assigned', function() {
|
||||||
var dataSource = new DataSource({
|
var dataSource = new DataSource({
|
||||||
name: 'temp',
|
name: 'defaultDataSource',
|
||||||
connector: 'memory',
|
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() {
|
it('should use the connector name if no name is provided', function() {
|
||||||
var dataSource = new DataSource({
|
var dataSource = new DataSource({
|
||||||
connector: 'memory',
|
connector: 'memory',
|
||||||
|
@ -79,4 +103,38 @@ describe('DataSource', function() {
|
||||||
|
|
||||||
dataSource.name.should.equal('memory');
|
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);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue