Add unit test for localfilestorage

Stage 2 of #62
This commit is contained in:
Nicholas O'Leary 2013-11-10 22:19:01 +00:00
parent 89dc15567d
commit 29734dd994
4 changed files with 261 additions and 6 deletions

View File

@ -24,6 +24,7 @@
"when": "~2.6.0",
"mqtt": "~0.3.3",
"ws": "~0.4.31",
"fs-extra": "~0.8.1",
"mustache": "~0.7.2",
"cron":"1.x",
"ntwitter":"0.5.0",

View File

@ -27,8 +27,6 @@ var userDir;
var libDir;
var libFlowsDir;
// TODO: Make user data directory relocatable
function listFiles(dir) {
var dirs = {};
var files = [];
@ -93,7 +91,7 @@ function getFileBody(root,path) {
var thisRead = fs.readSync(fd,buffer,0,length);
read += thisRead;
if (scanning) {
var data = remaining+buffer.toString();
var data = remaining+buffer.slice(0,thisRead).toString();
var parts = data.split("\n");
remaining = parts.splice(-1)[0];
for (var i=0;i<parts.length;i+=1) {
@ -151,7 +149,6 @@ var localfilesystem = {
flowsFile = 'flows_'+require('os').hostname()+'.json';
flowsFullPath = fspath.join(userDir,flowsFile);
}
credentialsFile = fspath.join(userDir,"credentials.json");
libDir = fspath.join(userDir,"lib");
@ -293,7 +290,6 @@ var localfilesystem = {
fs.lstat(rootPath,function(err,stats) {
if (err) {
console.log(err);
defer.reject(err);
} else if (stats.isFile()) {
defer.resolve(getFileBody(root,path));

View File

@ -26,7 +26,7 @@ describe('Node', function() {
n.should.have.property('id','123');
n.should.have.property('type','abc');
n.should.not.have.property('name');
n.wires.should.have.a.length(2);
n.wires.should.have.length(2);
});
});

View File

@ -0,0 +1,258 @@
var should = require("should");
var fs = require('fs-extra');
var path = require('path');
var localfilesystem = require("../red/storage/localfilesystem");
describe('LocalFileSystem', function() {
var userDir = path.join(__dirname,".testUserHome");
var testFlow = [{"type":"tab","id":"d8be2a6d.2741d8","label":"Sheet 1"}];
beforeEach(function() {
if (fs.existsSync(userDir)) {
fs.removeSync(userDir)
}
fs.mkdirSync(userDir);
});
afterEach(function() {
fs.removeSync(userDir);
});
it('should initialise the user directory',function() {
localfilesystem.init({userDir:userDir});
fs.existsSync(path.join(userDir,"lib")).should.be.true;
fs.existsSync(path.join(userDir,"lib",'flows')).should.be.true;
});
it('should handle missing flow file',function(done) {
localfilesystem.init({userDir:userDir});
var flowFile = 'flows_'+require('os').hostname()+'.json';
var flowFilePath = path.join(userDir,flowFile);
fs.existsSync(flowFilePath).should.be.false;
localfilesystem.getFlows().then(function(flows) {
flows.should.eql([]);
done();
}).otherwise(function(err) {
done(err);
});
});
it('should save flows to the default file',function(done) {
localfilesystem.init({userDir:userDir});
var flowFile = 'flows_'+require('os').hostname()+'.json';
var flowFilePath = path.join(userDir,flowFile);
fs.existsSync(flowFilePath).should.be.false;
localfilesystem.saveFlows(testFlow).then(function() {
fs.existsSync(flowFilePath).should.be.true;
localfilesystem.getFlows().then(function(flows) {
flows.should.eql(testFlow);
done();
}).otherwise(function(err) {
done(err);
});
}).otherwise(function(err) {
done(err);
});
});
it('should save flows to the specified file',function(done) {
var defaultFlowFile = 'flows_'+require('os').hostname()+'.json';
var defaultFlowFilePath = path.join(userDir,defaultFlowFile);
var flowFile = 'test.json';
var flowFilePath = path.join(userDir,flowFile);
localfilesystem.init({userDir:userDir, flowFile:flowFilePath});
fs.existsSync(defaultFlowFilePath).should.be.false;
fs.existsSync(flowFilePath).should.be.false;
localfilesystem.saveFlows(testFlow).then(function() {
fs.existsSync(defaultFlowFilePath).should.be.false;
fs.existsSync(flowFilePath).should.be.true;
localfilesystem.getFlows().then(function(flows) {
flows.should.eql(testFlow);
done();
}).otherwise(function(err) {
done(err);
});
}).otherwise(function(err) {
done(err);
});
});
it('should handle missing credentials', function(done) {
var credFile = path.join(userDir,"credentials.json");
localfilesystem.init({userDir:userDir});
fs.existsSync(credFile).should.be.false;
localfilesystem.getCredentials().then(function(creds) {
creds.should.eql({});
done();
}).otherwise(function(err) {
done(err);
});
});
it('should handle credentials', function(done) {
var credFile = path.join(userDir,"credentials.json");
localfilesystem.init({userDir:userDir});
fs.existsSync(credFile).should.be.false;
var credentials = {"abc":{"type":"creds"}};
localfilesystem.saveCredentials(credentials).then(function() {
fs.existsSync(credFile).should.be.true;
localfilesystem.getCredentials().then(function(creds) {
creds.should.eql(credentials);
done();
}).otherwise(function(err) {
done(err);
});
}).otherwise(function(err) {
done(err);
});
});
it('should return an empty list of library flows',function(done) {
localfilesystem.init({userDir:userDir});
localfilesystem.getAllFlows().then(function(flows) {
flows.should.eql({});
done();
}).otherwise(function(err) {
done(err);
});
});
it('should return a valid list of library flows',function(done) {
localfilesystem.init({userDir:userDir});
var flowLib = path.join(userDir,"lib","flows");
fs.closeSync(fs.openSync(path.join(flowLib,"A.json"),"w"));
fs.closeSync(fs.openSync(path.join(flowLib,"B.json"),"w"));
fs.mkdirSync(path.join(flowLib,"C"));
fs.closeSync(fs.openSync(path.join(flowLib,"C","D.json"),"w"));
var testFlowsList = {"d":{"C":{"f":["D"]}},"f":["A","B"]};
localfilesystem.getAllFlows().then(function(flows) {
flows.should.eql(testFlowsList);
done();
}).otherwise(function(err) {
done(err);
});
});
it('should fail a non-existent flow', function(done) {
localfilesystem.init({userDir:userDir});
localfilesystem.getFlow("a/b/c.json").then(function(flow) {
should.fail(flow,"No flow","Flow found");
}).otherwise(function(err) {
// err should be null, so this will pass
done(err);
});
});
it('should return a flow',function(done) {
localfilesystem.init({userDir:userDir});
var testflowString = JSON.stringify(testFlow);
localfilesystem.saveFlow("a/b/c/d.json",testflowString).then(function() {
localfilesystem.getFlow("a/b/c/d.json").then(function(flow) {
flow.should.eql(testflowString);
done();
}).otherwise(function(err) {
done(err);
});
}).otherwise(function(err) {
done(err);
});
});
it('should return an empty list of library objects',function(done) {
localfilesystem.init({userDir:userDir});
localfilesystem.getLibraryEntry('object','').then(function(flows) {
flows.should.eql({});
done();
}).otherwise(function(err) {
done(err);
});
});
it('should return an error for a non-existent library object',function(done) {
localfilesystem.init({userDir:userDir});
localfilesystem.getLibraryEntry('object','A/B').then(function(flows) {
should.fail(null,null,"non-existent flow");
}).otherwise(function(err) {
should.exist(err);
done();
});
});
function createObjectLibrary() {
var objLib = path.join(userDir,"lib","object");
fs.mkdirSync(objLib);
fs.mkdirSync(path.join(objLib,"A"));
fs.mkdirSync(path.join(objLib,"B"));
fs.mkdirSync(path.join(objLib,"B","C"));
fs.writeFileSync(path.join(objLib,"file1.js"),"// abc: def\n// not a metaline \n\n Hi",'utf8');
fs.writeFileSync(path.join(objLib,"B","file2.js"),"// ghi: jkl\n// not a metaline \n\n Hi",'utf8');
}
it('should return a directory listing of library objects',function(done) {
localfilesystem.init({userDir:userDir});
createObjectLibrary();
localfilesystem.getLibraryEntry('object','').then(function(flows) {
flows.should.eql([ 'A', 'B', { abc: 'def', fn: 'file1.js' } ]);
localfilesystem.getLibraryEntry('object','B').then(function(flows) {
flows.should.eql([ 'C', { ghi: 'jkl', fn: 'file2.js' } ]);
localfilesystem.getLibraryEntry('object','B/C').then(function(flows) {
flows.should.eql([]);
done();
}).otherwise(function(err) {
done(err);
});
}).otherwise(function(err) {
done(err);
});
}).otherwise(function(err) {
done(err);
});
});
it('should return a library object',function(done) {
localfilesystem.init({userDir:userDir});
createObjectLibrary();
localfilesystem.getLibraryEntry('object','B/file2.js').then(function(body) {
body.should.eql("// not a metaline \n\n Hi");
done();
}).otherwise(function(err) {
done(err);
});
});
it('should return a newly saved library object',function(done) {
localfilesystem.init({userDir:userDir});
createObjectLibrary();
localfilesystem.getLibraryEntry('object','B').then(function(flows) {
flows.should.eql([ 'C', { ghi: 'jkl', fn: 'file2.js' } ]);
localfilesystem.saveLibraryEntry('object','B/D/file3.js',{mno:'pqr'},"// another non meta line\n\n Hi There").then(function() {
localfilesystem.getLibraryEntry('object','B/D').then(function(flows) {
flows.should.eql([ { mno: 'pqr', fn: 'file3.js' } ]);
localfilesystem.getLibraryEntry('object','B/D/file3.js').then(function(body) {
body.should.eql("// another non meta line\n\n Hi There");
done();
}).otherwise(function(err) {
done(err);
});
}).otherwise(function(err) {
done(err);
});
}).otherwise(function(err) {
done(err);
});
}).otherwise(function(err) {
done(err);
});
});
});