1
0
mirror of https://github.com/node-red/node-red.git synced 2023-10-10 13:36:53 +02:00

Defining storage interface.

This commit is contained in:
zobalogh 2014-07-23 15:15:20 +01:00
parent b50b23318b
commit d58788f721
2 changed files with 164 additions and 16 deletions

View File

@ -14,21 +14,61 @@
* limitations under the License.
**/
var when = require('when');
var settings = require('../red').settings;
var storageModule;
var mod;
if (settings.storageModule) {
if (typeof settings.storageModule === "string") {
function moduleSelector(aSettings) {
var toReturn;
if (aSettings.storageModule) {
if (typeof aSettings.storageModule === "string") {
// TODO: allow storage modules to be specified by absolute path
mod = require("./"+settings.storageModule);
toReturn = require("./"+aSettings.storageModule);
} else {
mod = settings.storageModule;
toReturn = aSettings.storageModule;
}
} else {
mod = require("./localfilesystem");
toReturn = require("./localfilesystem");
}
return toReturn;
}
module.exports = mod;
var storageModuleInterface = {
init : function(settings) {
try {
storageModule = moduleSelector(settings);
} catch (e) {
return when.reject(e);
}
return storageModule.init(settings);
},
getFlows : function() {
return storageModule.getFlows();
},
saveFlows : function(flows) {
return storageModule.saveFlows(flows);
},
getCredentials : function() {
return storageModule.getCredentials();
},
saveCredentials : function(credentials) {
return storageModule.saveCredentials(credentials);
},
getAllFlows : function() {
return storageModule.getAllFlows();
},
getFlow : function(fn) {
return storageModule.getFlow(fn);
},
saveFlow : function(fn, data) {
return storageModule.saveFlow(fn, data);
},
getLibraryEntry : function(type, path) {
return storageModule.getLibraryEntry(type, path);
},
saveLibraryEntry : function(type, path, meta, body) {
return storageModule.saveLibraryEntry(type, path, meta, body);
}
}
module.exports = storageModuleInterface;

View File

@ -16,9 +16,117 @@
var should = require("should");
describe("red/storage/index", function() {
it('can be required without errors', function() {
var RED = require('../../../red/red');
RED.init({},{});
require("../../../red/storage/index");
it('can be required without errors', function(done) {
var storage = require("../../../red/storage/index");
done();
});
it('rejects the promise when settings suggest loading a bad module', function(done) {
var wrongModule = {
storageModule : "thisaintloading"
};
var storage = require("../../../red/storage/index");
storage.init(wrongModule).then( function() {
var one = 1;
var zero = 0;
try {
zero.should.equal(one, "The initialization promise should never get resolved");
} catch(err) {
done(err);
}
}).catch(function(e) {
done(); //successfully rejected promise
});
});
it('non-string storage module', function(done) {
var initSetsMeToTrue = false;
var moduleWithBooleanSettingInit = {
init : function() {
initSetsMeToTrue = true;
}
};
var setsBooleanModule = {
storageModule : moduleWithBooleanSettingInit
};
var storage = require("../../../red/storage/index");
storage.init(setsBooleanModule);
initSetsMeToTrue.should.be.true;
done();
});
it('respects storage interface', function(done) {
var calledFlagGetFlows = false;
var calledFlagGetCredentials = false;
var calledFlagGetAllFlows = false;
var calledInit = false;
var interfaceCheckerModule = {
init : function (settings) {
settings.should.be.an.Object;
calledInit = true;
},
getFlows : function() {
calledFlagGetFlows = true;
},
saveFlows : function (flows) {
flows.should.be.true;
},
getCredentials : function() {
calledFlagGetCredentials = true;
},
saveCredentials : function(credentials) {
credentials.should.be.true;
},
getAllFlows : function() {
calledFlagGetAllFlows = true;
},
getFlow : function(fn) {
fn.should.be.true;
},
saveFlow : function(fn, data) {
fn.should.be.true;
data.should.be.true;
},
getLibraryEntry : function(type, path) {
type.should.be.true;
path.should.be.true;
},
saveLibraryEntry : function(type, path, meta, body) {
type.should.be.true;
path.should.be.true;
meta.should.be.true;
body.should.be.true;
}
};
var moduleToLoad = {
storageModule : interfaceCheckerModule
};
var storage = require("../../../red/storage/index");
storage.init(moduleToLoad);
storage.getFlows();
storage.saveFlows(true);
storage.getCredentials();
storage.saveCredentials(true);
storage.getAllFlows();
storage.getFlow(true);
storage.saveFlow(true, true);
storage.getLibraryEntry(true, true);
storage.saveLibraryEntry(true, true, true, true);
calledInit.should.be.true;
calledFlagGetFlows.should.be.true;
calledFlagGetCredentials.should.be.true;
calledFlagGetAllFlows.should.be.true;
done();
});
});