Make the code testable following the review comments

This commit is contained in:
Raymond Feng 2014-01-13 11:06:02 -08:00
parent acd02bf482
commit 2a50388894
2 changed files with 58 additions and 29 deletions

View File

@ -165,40 +165,50 @@ DataSource.prototype._setupConnector = function () {
} }
}; };
// List possible connector module names
function connectorModuleNames(name) {
var names = [name]; // Check the name as is
if (!name.match(/^\//)) {
names.push('./connectors/' + name); // Check built-in connectors
if (name.indexOf('loopback-connector-') !== 0) {
names.push('loopback-connector-' + name); // Try loopback-connector-<name>
}
}
return names;
}
// testable with DI
function tryModules(names, loader) {
var mod;
loader = loader || require;
for(var m =0; m<names.length; m++) {
try {
mod = loader(names[m]);
} catch (e) {
/* ignore */
}
if (mod) {
break;
}
}
return mod;
}
/*! /*!
* Resolve a connector by name * Resolve a connector by name
* @param name The connector name * @param name The connector name
* @returns {*} * @returns {*}
* @private * @private
*/ */
DataSource._resolveConnector = function(name) { DataSource._resolveConnector = function(name, loader) {
var connector = null; var names = connectorModuleNames(name);
var connector = tryModules(names , loader);
var error = null; var error = null;
if (name.match(/^\//)) {
// try absolute path
connector = require(name);
} else if (existsSync(__dirname + '/connectors/' + name + '.js')) {
// try built-in connector
connector = require('./connectors/' + name);
} else {
// try foreign connector
try {
connector = require(name);
} catch(e) {
try {
if(name.indexOf('loopback-connector-') === -1) {
name = 'loopback-connector-' + name;
connector = require(name);
}
} catch (e) {
// ignore
}
if(!connector) { if(!connector) {
error = '\nWARNING: LoopBack connector "' + name error = '\nWARNING: LoopBack connector "' + name
+ '" is not installed,\nso your models would not work, to fix run:\n\n npm install ' + '" is not installed at any of the locations ' + names
+ name, '\n'; + '. To fix, run:\n\n npm install '
} + name + '\n';
}
} }
return { return {
connector: connector, connector: connector,

View File

@ -445,6 +445,13 @@ describe('Load models with base', function () {
}); });
describe('DataSource constructor', function() { describe('DataSource constructor', function() {
// Mocked require
var loader = function(name) {
return {
name: name
}
};
it('should resolve connector by path', function() { it('should resolve connector by path', function() {
var connector = DataSource._resolveConnector(__dirname + '/../lib/connectors/memory'); var connector = DataSource._resolveConnector(__dirname + '/../lib/connectors/memory');
assert(connector.connector); assert(connector.connector);
@ -454,16 +461,28 @@ describe('DataSource constructor', function() {
assert(connector.connector); assert(connector.connector);
}); });
it('should try to resolve connector by module name starts with loopback-connector-', function() { it('should try to resolve connector by module name starts with loopback-connector-', function() {
var connector = DataSource._resolveConnector('loopback-connector-xyz', loader);
assert(connector.connector);
});
it('should try to resolve connector by short module name', function() {
var connector = DataSource._resolveConnector('xyz', loader);
assert(connector.connector);
});
it('should try to resolve connector by full module name', function() {
var connector = DataSource._resolveConnector('loopback-xyz', loader);
assert(connector.connector);
});
it('should fail to resolve connector by module name starts with loopback-connector-', function() {
var connector = DataSource._resolveConnector('loopback-connector-xyz'); var connector = DataSource._resolveConnector('loopback-connector-xyz');
assert(!connector.connector); assert(!connector.connector);
assert(connector.error.indexOf('loopback-connector-xyz') !== -1); assert(connector.error.indexOf('loopback-connector-xyz') !== -1);
}); });
it('should try to resolve connector by short module name', function() { it('should fail to resolve connector by short module name', function() {
var connector = DataSource._resolveConnector('xyz'); var connector = DataSource._resolveConnector('xyz');
assert(!connector.connector); assert(!connector.connector);
assert(connector.error.indexOf('loopback-connector-xyz') !== -1); assert(connector.error.indexOf('loopback-connector-xyz') !== -1);
}); });
it('should try to resolve connector by full module name', function() { it('should fail to resolve connector by full module name', function() {
var connector = DataSource._resolveConnector('loopback-xyz'); var connector = DataSource._resolveConnector('loopback-xyz');
assert(!connector.connector); assert(!connector.connector);
assert(connector.error.indexOf('loopback-connector-loopback-xyz') !== -1); assert(connector.error.indexOf('loopback-connector-loopback-xyz') !== -1);