mirror of
https://github.com/node-red/node-red.git
synced 2025-03-01 10:36:34 +00:00
update external context
- Implement `delete` function - Swap default easily - Change memory context as a plugin - Update localfilesystem plugin - Change file/folder structure
This commit is contained in:
committed by
HirokiUchikawa
parent
b4b70a988e
commit
e33ec0cf50
@@ -1,222 +0,0 @@
|
||||
/**
|
||||
* Copyright JS Foundation and other contributors, http://js.foundation
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
**/
|
||||
var should = require("should");
|
||||
var sinon = require('sinon');
|
||||
var external = require("../../../../../red/runtime/nodes/context/external");
|
||||
|
||||
describe("external", function() {
|
||||
var stubModule = {
|
||||
module: {
|
||||
init: function(){},
|
||||
get: function(){},
|
||||
set: function(){},
|
||||
keys: function(){},
|
||||
run: function(){},
|
||||
close: function(){}
|
||||
},
|
||||
config: {}
|
||||
};
|
||||
describe('#init()', function() {
|
||||
it('should load bundle module as default', function() {
|
||||
external.init({
|
||||
contextStorage:{
|
||||
default:{
|
||||
module: "./localfilesystem",
|
||||
config:{}
|
||||
}
|
||||
}});
|
||||
external.hasContextStorage("default").should.be.true();
|
||||
});
|
||||
|
||||
it('should load bundle module as localfile', function() {
|
||||
external.init({
|
||||
contextStorage:{
|
||||
localfile:{
|
||||
module: "./localfilesystem",
|
||||
config:{}
|
||||
}
|
||||
}});
|
||||
external.hasContextStorage("localfile").should.be.true();
|
||||
});
|
||||
|
||||
it('should not load non-existent module', function() {
|
||||
external.init({
|
||||
contextStorage:{
|
||||
default:{
|
||||
module: "non-existent-module",
|
||||
config:{}
|
||||
}
|
||||
|
||||
}})
|
||||
external.hasContextStorage("default").should.be.false();
|
||||
});
|
||||
|
||||
it('should load multiple modules', function() {
|
||||
external.init({
|
||||
contextStorage:{
|
||||
default:{
|
||||
module: "./localfilesystem",
|
||||
config:{}
|
||||
},
|
||||
test:{
|
||||
module: {
|
||||
init: function() {
|
||||
return true;
|
||||
}
|
||||
},
|
||||
config:{}
|
||||
}
|
||||
}
|
||||
});
|
||||
external.hasContextStorage("default").should.be.true();
|
||||
external.hasContextStorage("test").should.be.true();
|
||||
});
|
||||
|
||||
it('should load multiple modules without non-existent module', function() {
|
||||
external.init({
|
||||
contextStorage:{
|
||||
nonexist:{
|
||||
module: "non-existent-module",
|
||||
config:{}
|
||||
},
|
||||
default:{
|
||||
module: "./localfilesystem",
|
||||
config:{}
|
||||
},
|
||||
test:{
|
||||
module: {
|
||||
init: function() {
|
||||
return true;
|
||||
}
|
||||
},
|
||||
config:{}
|
||||
}
|
||||
}
|
||||
});
|
||||
external.hasContextStorage("nonexist").should.be.false();
|
||||
external.hasContextStorage("default").should.be.true();
|
||||
external.hasContextStorage("test").should.be.true();
|
||||
});
|
||||
});
|
||||
|
||||
// describe('#get()', function() {
|
||||
// });
|
||||
|
||||
// describe('#set()', function() {
|
||||
// });
|
||||
|
||||
// describe('#keys()', function() {
|
||||
// });
|
||||
|
||||
// describe('#hasContextStorage()', function() {
|
||||
// });
|
||||
|
||||
describe('#canUse()', function() {
|
||||
it('should return true if specified module is loaded', function() {
|
||||
external.init({
|
||||
contextStorage:{
|
||||
localfilesystem:{
|
||||
module: {name:"test",init: function(){return true}},
|
||||
config: {}
|
||||
}
|
||||
}
|
||||
});
|
||||
external.canUse("$localfilesystem").should.be.true();
|
||||
external.canUse("$localfilesystem.foo").should.be.true();
|
||||
});
|
||||
it('should return false if specified module is not loaded', function() {
|
||||
external.init({
|
||||
contextStorage:{
|
||||
localfilesystem:{
|
||||
module: {name:"test",init: function(){return true}},
|
||||
config: {}
|
||||
}
|
||||
}
|
||||
});
|
||||
external.canUse("$file").should.be.false();
|
||||
external.canUse("$file.foo").should.be.false();
|
||||
});
|
||||
it('should return true if specified module is not loaded but default module is loaded', function() {
|
||||
external.init({
|
||||
contextStorage:{
|
||||
default:{
|
||||
module: {name:"test",init: function(){return true}},
|
||||
config: {}
|
||||
}
|
||||
}
|
||||
});
|
||||
external.canUse("$file").should.be.true();
|
||||
external.canUse("$file.foo").should.be.true();
|
||||
});
|
||||
it('should return false if argument does not contain module name', function() {
|
||||
external.init({
|
||||
contextStorage:{
|
||||
default:{
|
||||
module: {name:"test",init: function(){return true}},
|
||||
config: {}
|
||||
}
|
||||
}
|
||||
});
|
||||
external.canUse("file").should.be.false();
|
||||
external.canUse("file.foo").should.be.false();
|
||||
});
|
||||
});
|
||||
|
||||
describe('#parseKey()', function() {
|
||||
function returnModuleAndKey(input, expectedModule, expectedKey) {
|
||||
var result = external.parseKey(input);
|
||||
result[0].should.eql(expectedModule);
|
||||
result[1].should.eql(expectedKey);
|
||||
};
|
||||
|
||||
function returnModule(input, expectedModule) {
|
||||
var result = external.parseKey(input);
|
||||
result[0].should.eql(expectedModule);
|
||||
should(result[1]).be.null();
|
||||
};
|
||||
|
||||
it('should retrun module and key', function() {
|
||||
returnModuleAndKey("$test.aaa","test","aaa");
|
||||
returnModuleAndKey("$test.aaa.bbb","test","aaa.bbb");
|
||||
returnModuleAndKey("$1.234","1","234");
|
||||
returnModuleAndKey("$$test.foo","$test","foo");
|
||||
returnModuleAndKey("$test.$foo","test","$foo");
|
||||
returnModuleAndKey("$test.$foo.$bar","test","$foo.$bar");
|
||||
returnModuleAndKey("$test..foo","test",".foo");
|
||||
returnModuleAndKey("$test..","test",".");
|
||||
});
|
||||
|
||||
it('should retrun only module', function() {
|
||||
returnModule("$test","test",null);
|
||||
returnModule("$1","1",null);
|
||||
returnModule("$$test","$test",null);
|
||||
returnModule("$test.","test.",null);
|
||||
});
|
||||
|
||||
it('should retrun module as default', function() {
|
||||
returnModuleAndKey("$default.foo","default","foo");
|
||||
returnModuleAndKey("$.foo","default","foo");
|
||||
returnModule("$default","default");
|
||||
returnModule("$","default");
|
||||
});
|
||||
|
||||
it('should retrun null', function() {
|
||||
should(external.parseKey("test.aaa")).be.null();
|
||||
should(external.parseKey("test")).be.null();
|
||||
should(external.parseKey(null)).be.null();
|
||||
});
|
||||
});
|
||||
});
|
@@ -18,6 +18,7 @@ var should = require("should");
|
||||
var sinon = require('sinon');
|
||||
var path = require('path');
|
||||
var fs = require('fs-extra');
|
||||
var rewire = require("rewire");
|
||||
var Context = require("../../../../../red/runtime/nodes/context/index");
|
||||
|
||||
describe('context', function() {
|
||||
@@ -147,6 +148,7 @@ describe('context', function() {
|
||||
var stubGet = sinon.stub();
|
||||
var stubSet = sinon.stub();
|
||||
var stubKeys = sinon.stub();
|
||||
var stubDelete = sinon.stub();
|
||||
var contextStorage={
|
||||
test:{
|
||||
module: {
|
||||
@@ -156,6 +158,7 @@ describe('context', function() {
|
||||
get: stubGet,
|
||||
set: stubSet,
|
||||
keys: stubKeys,
|
||||
delete: stubDelete
|
||||
},
|
||||
config:{}
|
||||
}
|
||||
@@ -235,4 +238,52 @@ describe('context', function() {
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('#parseKey()', function() {
|
||||
var parseKey = rewire("../../../../../red/runtime/nodes/context/index").__get__("parseKey");
|
||||
|
||||
function returnModuleAndKey(input, expectedModule, expectedKey) {
|
||||
var result = parseKey(input);
|
||||
result[0].should.eql(expectedModule);
|
||||
result[1].should.eql(expectedKey);
|
||||
};
|
||||
|
||||
function returnModule(input, expectedModule) {
|
||||
var result = parseKey(input);
|
||||
result[0].should.eql(expectedModule);
|
||||
should(result[1]).be.null();
|
||||
};
|
||||
|
||||
it('should retrun module and key', function() {
|
||||
returnModuleAndKey("$test.aaa","test","aaa");
|
||||
returnModuleAndKey("$test.aaa.bbb","test","aaa.bbb");
|
||||
returnModuleAndKey("$1.234","1","234");
|
||||
returnModuleAndKey("$$test.foo","$test","foo");
|
||||
returnModuleAndKey("$test.$foo","test","$foo");
|
||||
returnModuleAndKey("$test.$foo.$bar","test","$foo.$bar");
|
||||
returnModuleAndKey("$test..foo","test",".foo");
|
||||
returnModuleAndKey("$test..","test",".");
|
||||
});
|
||||
|
||||
// it('should retrun only module', function() {
|
||||
// returnModule("$test","test",null);
|
||||
// returnModule("$1","1",null);
|
||||
// returnModule("$$test","$test",null);
|
||||
// returnModule("$test.","test.",null);
|
||||
// });
|
||||
|
||||
it('should retrun module as default', function() {
|
||||
returnModuleAndKey("$default.foo","default","foo");
|
||||
returnModuleAndKey("$.foo","default","foo");
|
||||
// returnModule("$default","default");
|
||||
// returnModule("$","default");
|
||||
});
|
||||
|
||||
it('should retrun null', function() {
|
||||
should(parseKey("test.aaa")).be.null();
|
||||
should(parseKey("test")).be.null();
|
||||
should(parseKey(null)).be.null();
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
|
@@ -0,0 +1,126 @@
|
||||
/**
|
||||
* Copyright JS Foundation and other contributors, http://js.foundation
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
**/
|
||||
|
||||
var should = require('should');
|
||||
var fs = require('fs-extra');
|
||||
var path = require("path");
|
||||
var context = require('../../../../../red/runtime/nodes/context/localfilesystem');
|
||||
|
||||
var resourcesDir = path.resolve(path.join(__dirname,"..","resources","context"));
|
||||
|
||||
describe('localfilesystem',function() {
|
||||
|
||||
beforeEach(function() {
|
||||
context.init({dir: resourcesDir});
|
||||
});
|
||||
|
||||
afterEach(function() {
|
||||
context.delete("nodeX");
|
||||
context.delete("nodeY");
|
||||
});
|
||||
|
||||
after(function() {
|
||||
fs.removeSync(resourcesDir);
|
||||
});
|
||||
|
||||
describe('#get/set',function() {
|
||||
it('should store property',function() {
|
||||
should.not.exist(context.get("foo","nodeX"));
|
||||
context.set("foo","test","nodeX");
|
||||
context.get("foo","nodeX").should.eql("test");
|
||||
});
|
||||
|
||||
it('should store property - creates parent properties',function() {
|
||||
context.set("foo.bar","test","nodeX");
|
||||
context.get("foo","nodeX").should.eql({bar:"test"});
|
||||
});
|
||||
|
||||
it('should delete property',function() {
|
||||
context.set("foo.abc.bar1","test1","nodeX");
|
||||
context.set("foo.abc.bar2","test2","nodeX");
|
||||
context.get("foo.abc","nodeX").should.eql({bar1:"test1",bar2:"test2"});
|
||||
context.set("foo.abc.bar1",undefined,"nodeX");
|
||||
context.get("foo.abc","nodeX").should.eql({bar2:"test2"});
|
||||
context.set("foo.abc",undefined,"nodeX");
|
||||
should.not.exist(context.get("foo.abc","nodeX"));
|
||||
context.set("foo",undefined,"nodeX");
|
||||
should.not.exist(context.get("foo","nodeX"));
|
||||
});
|
||||
|
||||
it('should not shared context with other scope', function() {
|
||||
should.not.exist(context.get("foo","nodeX"));
|
||||
should.not.exist(context.get("foo","nodeY"));
|
||||
context.set("foo","testX","nodeX");
|
||||
context.set("foo","testY","nodeY");
|
||||
|
||||
context.get("foo","nodeX").should.eql("testX");
|
||||
context.get("foo","nodeY").should.eql("testY");
|
||||
});
|
||||
});
|
||||
|
||||
describe('#keys',function() {
|
||||
it('should enumerate context keys', function() {
|
||||
var keys = context.keys("nodeX");
|
||||
keys.should.be.an.Array();
|
||||
keys.should.be.empty();
|
||||
|
||||
context.set("foo","bar","nodeX");
|
||||
keys = context.keys("nodeX");
|
||||
keys.should.have.length(1);
|
||||
keys[0].should.eql("foo");
|
||||
|
||||
context.set("abc.def","bar","nodeX");
|
||||
keys = context.keys("nodeX");
|
||||
keys.should.have.length(2);
|
||||
keys[1].should.eql("abc");
|
||||
});
|
||||
|
||||
it('should enumerate context keys in each scopes', function() {
|
||||
var keysX = context.keys("nodeX");
|
||||
keysX.should.be.an.Array();
|
||||
keysX.should.be.empty();
|
||||
|
||||
var keysY = context.keys("nodeY");
|
||||
keysY.should.be.an.Array();
|
||||
keysY.should.be.empty();
|
||||
|
||||
context.set("foo","bar","nodeX");
|
||||
context.set("hoge","piyo","nodeY");
|
||||
keysX = context.keys("nodeX");
|
||||
keysX.should.have.length(1);
|
||||
keysX[0].should.eql("foo");
|
||||
|
||||
keysY = context.keys("nodeY");
|
||||
keysY.should.have.length(1);
|
||||
keysY[0].should.eql("hoge");
|
||||
});
|
||||
});
|
||||
|
||||
describe('#delete',function() {
|
||||
it('should delete context',function() {
|
||||
should.not.exist(context.get("foo","nodeX"));
|
||||
should.not.exist(context.get("foo","nodeY"));
|
||||
context.set("foo","abc","nodeX");
|
||||
context.set("foo","abc","nodeY");
|
||||
context.get("foo","nodeX").should.eql("abc");
|
||||
context.get("foo","nodeY").should.eql("abc");
|
||||
|
||||
context.delete("nodeX");
|
||||
should.not.exist(context.get("foo","nodeX"));
|
||||
should.exist(context.get("foo","nodeY"));
|
||||
});
|
||||
});
|
||||
});
|
141
test/red/runtime/nodes/context/memory_spec.js
Normal file
141
test/red/runtime/nodes/context/memory_spec.js
Normal file
@@ -0,0 +1,141 @@
|
||||
/**
|
||||
* Copyright JS Foundation and other contributors, http://js.foundation
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
**/
|
||||
|
||||
var should = require('should');
|
||||
var context = require('../../../../../red/runtime/nodes/context/memory');
|
||||
|
||||
describe('memory',function() {
|
||||
|
||||
beforeEach(function() {
|
||||
context.init({});
|
||||
});
|
||||
|
||||
describe('#get/set',function() {
|
||||
it('should store property',function() {
|
||||
should.not.exist(context.get("foo","nodeX"));
|
||||
context.set("foo","test","nodeX");
|
||||
context.get("foo","nodeX").should.eql("test");
|
||||
});
|
||||
|
||||
it('should store property - creates parent properties',function() {
|
||||
context.set("foo.bar","test","nodeX");
|
||||
context.get("foo","nodeX").should.eql({bar:"test"});
|
||||
});
|
||||
|
||||
it('should delete property',function() {
|
||||
context.set("foo.abc.bar1","test1","nodeX");
|
||||
context.set("foo.abc.bar2","test2","nodeX");
|
||||
context.get("foo.abc","nodeX").should.eql({bar1:"test1",bar2:"test2"});
|
||||
context.set("foo.abc.bar1",undefined,"nodeX");
|
||||
context.get("foo.abc","nodeX").should.eql({bar2:"test2"});
|
||||
context.set("foo.abc",undefined,"nodeX");
|
||||
should.not.exist(context.get("foo.abc","nodeX"));
|
||||
context.set("foo",undefined,"nodeX");
|
||||
should.not.exist(context.get("foo","nodeX"));
|
||||
});
|
||||
|
||||
it('should not shared context with other scope', function() {
|
||||
should.not.exist(context.get("foo","nodeX"));
|
||||
should.not.exist(context.get("foo","nodeY"));
|
||||
context.set("foo","testX","nodeX");
|
||||
context.set("foo","testY","nodeY");
|
||||
|
||||
context.get("foo","nodeX").should.eql("testX");
|
||||
context.get("foo","nodeY").should.eql("testY");
|
||||
});
|
||||
});
|
||||
|
||||
describe('#keys',function() {
|
||||
it('should enumerate context keys', function() {
|
||||
var keys = context.keys("nodeX");
|
||||
keys.should.be.an.Array();
|
||||
keys.should.be.empty();
|
||||
|
||||
context.set("foo","bar","nodeX");
|
||||
keys = context.keys("nodeX");
|
||||
keys.should.have.length(1);
|
||||
keys[0].should.eql("foo");
|
||||
|
||||
context.set("abc.def","bar","nodeX");
|
||||
keys = context.keys("nodeX");
|
||||
keys.should.have.length(2);
|
||||
keys[1].should.eql("abc");
|
||||
});
|
||||
|
||||
it('should enumerate context keys in each scopes', function() {
|
||||
var keysX = context.keys("nodeX");
|
||||
keysX.should.be.an.Array();
|
||||
keysX.should.be.empty();
|
||||
|
||||
var keysY = context.keys("nodeY");
|
||||
keysY.should.be.an.Array();
|
||||
keysY.should.be.empty();
|
||||
|
||||
context.set("foo","bar","nodeX");
|
||||
context.set("hoge","piyo","nodeY");
|
||||
keysX = context.keys("nodeX");
|
||||
keysX.should.have.length(1);
|
||||
keysX[0].should.eql("foo");
|
||||
|
||||
keysY = context.keys("nodeY");
|
||||
keysY.should.have.length(1);
|
||||
keysY[0].should.eql("hoge");
|
||||
});
|
||||
|
||||
it('should enumerate only context keys when GlobalContext was given', function() {
|
||||
var keys = context.keys("global");
|
||||
keys.should.be.an.Array();
|
||||
keys.should.be.empty();
|
||||
|
||||
var data = {
|
||||
foo: "bar"
|
||||
}
|
||||
context.setGlobalContext(data);
|
||||
keys = context.keys("global");
|
||||
keys.should.have.length(1);
|
||||
keys[0].should.eql("foo");
|
||||
});
|
||||
});
|
||||
|
||||
describe('#delete',function() {
|
||||
it('should delete context',function() {
|
||||
should.not.exist(context.get("foo","nodeX"));
|
||||
should.not.exist(context.get("foo","nodeY"));
|
||||
context.set("foo","abc","nodeX");
|
||||
context.set("foo","abc","nodeY");
|
||||
context.get("foo","nodeX").should.eql("abc");
|
||||
context.get("foo","nodeY").should.eql("abc");
|
||||
|
||||
context.delete("nodeX");
|
||||
should.not.exist(context.get("foo","nodeX"));
|
||||
should.exist(context.get("foo","nodeY"));
|
||||
});
|
||||
});
|
||||
|
||||
describe('#setGlobalContext',function() {
|
||||
it('should initialize global context with argument', function() {
|
||||
var keys = context.keys("global");
|
||||
keys.should.be.an.Array();
|
||||
keys.should.be.empty();
|
||||
|
||||
var data = {
|
||||
foo: "bar"
|
||||
}
|
||||
context.setGlobalContext(data);
|
||||
context.get("foo","global").should.eql("bar");
|
||||
});
|
||||
});
|
||||
});
|
Reference in New Issue
Block a user