2015-12-29 23:16:51 +01:00
|
|
|
/**
|
2017-01-11 16:24:33 +01:00
|
|
|
* Copyright JS Foundation and other contributors, http://js.foundation
|
2015-12-29 23:16:51 +01:00
|
|
|
*
|
|
|
|
* 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');
|
2018-06-22 10:11:54 +02:00
|
|
|
var path = require("path");
|
2018-08-09 16:36:43 +02:00
|
|
|
var fs = require('fs-extra');
|
2018-08-20 17:17:24 +02:00
|
|
|
var NR_TEST_UTILS = require("nr-test-utils");
|
|
|
|
var Context = NR_TEST_UTILS.require("@node-red/runtime/lib/nodes/context/index");
|
2015-12-29 23:16:51 +01:00
|
|
|
|
|
|
|
describe('context', function() {
|
2018-05-30 03:24:27 +02:00
|
|
|
describe('local memory',function() {
|
2018-03-15 07:15:26 +01:00
|
|
|
beforeEach(function() {
|
|
|
|
Context.init({});
|
2018-07-02 16:47:47 +02:00
|
|
|
Context.load();
|
2018-03-15 07:15:26 +01:00
|
|
|
});
|
|
|
|
afterEach(function() {
|
|
|
|
Context.clean({allNodes:{}});
|
2018-05-30 03:24:27 +02:00
|
|
|
return Context.close();
|
2018-03-15 07:15:26 +01:00
|
|
|
});
|
|
|
|
it('stores local property',function() {
|
2020-03-28 00:47:12 +01:00
|
|
|
var flowContext = Context.getFlowContext("flowA")
|
2018-03-15 07:15:26 +01:00
|
|
|
var context1 = Context.get("1","flowA");
|
|
|
|
should.not.exist(context1.get("foo"));
|
|
|
|
context1.set("foo","test");
|
2018-06-07 14:51:11 +02:00
|
|
|
context1.get("foo").should.equal("test");
|
2018-03-15 07:15:26 +01:00
|
|
|
});
|
|
|
|
it('stores local property - creates parent properties',function() {
|
2020-03-28 00:47:12 +01:00
|
|
|
var flowContext = Context.getFlowContext("flowA")
|
2018-03-15 07:15:26 +01:00
|
|
|
var context1 = Context.get("1","flowA");
|
|
|
|
context1.set("foo.bar","test");
|
|
|
|
context1.get("foo").should.eql({bar:"test"});
|
|
|
|
});
|
|
|
|
it('deletes local property',function() {
|
2020-03-28 00:47:12 +01:00
|
|
|
var flowContext = Context.getFlowContext("flowA")
|
2018-03-15 07:15:26 +01:00
|
|
|
var context1 = Context.get("1","flowA");
|
|
|
|
context1.set("foo.abc.bar1","test1");
|
|
|
|
context1.set("foo.abc.bar2","test2");
|
|
|
|
context1.get("foo.abc").should.eql({bar1:"test1",bar2:"test2"});
|
|
|
|
context1.set("foo.abc.bar1",undefined);
|
|
|
|
context1.get("foo.abc").should.eql({bar2:"test2"});
|
|
|
|
context1.set("foo.abc",undefined);
|
|
|
|
should.not.exist(context1.get("foo.abc"));
|
|
|
|
context1.set("foo",undefined);
|
|
|
|
should.not.exist(context1.get("foo"));
|
|
|
|
});
|
|
|
|
it('stores flow property',function() {
|
2020-03-28 00:47:12 +01:00
|
|
|
var flowContext = Context.getFlowContext("flowA")
|
2018-03-15 07:15:26 +01:00
|
|
|
var context1 = Context.get("1","flowA");
|
|
|
|
should.not.exist(context1.flow.get("foo"));
|
|
|
|
context1.flow.set("foo","test");
|
2018-06-07 14:51:11 +02:00
|
|
|
context1.flow.get("foo").should.equal("test");
|
2018-03-15 07:15:26 +01:00
|
|
|
});
|
|
|
|
it('stores global property',function() {
|
2020-03-28 00:47:12 +01:00
|
|
|
var flowContext = Context.getFlowContext("flowA")
|
2018-03-15 07:15:26 +01:00
|
|
|
var context1 = Context.get("1","flowA");
|
|
|
|
should.not.exist(context1.global.get("foo"));
|
|
|
|
context1.global.set("foo","test");
|
2018-06-07 14:51:11 +02:00
|
|
|
context1.global.get("foo").should.equal("test");
|
2018-03-15 07:15:26 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
it('keeps local context local', function() {
|
2020-03-28 00:47:12 +01:00
|
|
|
var flowContext = Context.getFlowContext("flowA")
|
2018-03-15 07:15:26 +01:00
|
|
|
var context1 = Context.get("1","flowA");
|
|
|
|
var context2 = Context.get("2","flowA");
|
|
|
|
|
|
|
|
should.not.exist(context1.get("foo"));
|
|
|
|
should.not.exist(context2.get("foo"));
|
|
|
|
context1.set("foo","test");
|
|
|
|
|
2018-06-07 14:51:11 +02:00
|
|
|
context1.get("foo").should.equal("test");
|
2018-03-15 07:15:26 +01:00
|
|
|
should.not.exist(context2.get("foo"));
|
|
|
|
});
|
|
|
|
it('flow context accessible to all flow nodes', function() {
|
2020-03-28 00:47:12 +01:00
|
|
|
var flowContext = Context.getFlowContext("flowA")
|
2018-03-15 07:15:26 +01:00
|
|
|
var context1 = Context.get("1","flowA");
|
|
|
|
var context2 = Context.get("2","flowA");
|
|
|
|
|
|
|
|
should.not.exist(context1.flow.get("foo"));
|
|
|
|
should.not.exist(context2.flow.get("foo"));
|
|
|
|
|
|
|
|
context1.flow.set("foo","test");
|
2018-06-07 14:51:11 +02:00
|
|
|
context1.flow.get("foo").should.equal("test");
|
|
|
|
context2.flow.get("foo").should.equal("test");
|
2018-03-15 07:15:26 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
it('flow context not shared to nodes on other flows', function() {
|
2020-03-28 00:47:12 +01:00
|
|
|
var flowContextA = Context.getFlowContext("flowA")
|
|
|
|
var flowContextB = Context.getFlowContext("flowB")
|
2018-03-15 07:15:26 +01:00
|
|
|
var context1 = Context.get("1","flowA");
|
|
|
|
var context2 = Context.get("2","flowB");
|
|
|
|
|
|
|
|
should.not.exist(context1.flow.get("foo"));
|
|
|
|
should.not.exist(context2.flow.get("foo"));
|
|
|
|
|
|
|
|
context1.flow.set("foo","test");
|
2018-06-07 14:51:11 +02:00
|
|
|
context1.flow.get("foo").should.equal("test");
|
2018-03-15 07:15:26 +01:00
|
|
|
should.not.exist(context2.flow.get("foo"));
|
|
|
|
});
|
|
|
|
|
|
|
|
it('global context shared to all nodes', function() {
|
2020-03-28 00:47:12 +01:00
|
|
|
var flowContextA = Context.getFlowContext("flowA")
|
|
|
|
var flowContextB = Context.getFlowContext("flowB")
|
|
|
|
|
2018-03-15 07:15:26 +01:00
|
|
|
var context1 = Context.get("1","flowA");
|
|
|
|
var context2 = Context.get("2","flowB");
|
|
|
|
|
|
|
|
should.not.exist(context1.global.get("foo"));
|
|
|
|
should.not.exist(context2.global.get("foo"));
|
|
|
|
|
|
|
|
context1.global.set("foo","test");
|
2018-06-07 14:51:11 +02:00
|
|
|
context1.global.get("foo").should.equal("test");
|
|
|
|
context2.global.get("foo").should.equal("test");
|
2018-03-15 07:15:26 +01:00
|
|
|
});
|
|
|
|
|
2018-09-10 23:30:51 +02:00
|
|
|
it('context.flow/global are not enumerable', function() {
|
2020-03-28 00:47:12 +01:00
|
|
|
var flowContextA = Context.getFlowContext("flowA")
|
2018-09-10 23:30:51 +02:00
|
|
|
var context1 = Context.get("1","flowA");
|
|
|
|
Object.keys(context1).length.should.equal(0);
|
|
|
|
Object.keys(context1.flow).length.should.equal(0);
|
|
|
|
Object.keys(context1.global).length.should.equal(0);
|
|
|
|
})
|
|
|
|
|
|
|
|
it('context.flow/global cannot be deleted', function() {
|
2020-03-28 00:47:12 +01:00
|
|
|
var flowContextA = Context.getFlowContext("flowA")
|
2018-09-10 23:30:51 +02:00
|
|
|
var context1 = Context.get("1","flowA");
|
|
|
|
delete context1.flow;
|
|
|
|
should.exist(context1.flow);
|
|
|
|
delete context1.global;
|
|
|
|
should.exist(context1.global);
|
|
|
|
})
|
|
|
|
|
2018-03-15 07:15:26 +01:00
|
|
|
it('deletes context',function() {
|
2020-03-28 00:47:12 +01:00
|
|
|
var flowContextA = Context.getFlowContext("flowA")
|
2018-03-15 07:15:26 +01:00
|
|
|
var context = Context.get("1","flowA");
|
|
|
|
should.not.exist(context.get("foo"));
|
|
|
|
context.set("foo","abc");
|
2018-06-07 14:51:11 +02:00
|
|
|
context.get("foo").should.equal("abc");
|
2018-03-15 07:15:26 +01:00
|
|
|
|
2018-05-30 08:23:34 +02:00
|
|
|
return Context.delete("1","flowA").then(function(){
|
|
|
|
context = Context.get("1","flowA");
|
2018-06-11 09:04:27 +02:00
|
|
|
should.not.exist(context.get("foo"));
|
2018-05-30 08:23:34 +02:00
|
|
|
});
|
2018-03-15 07:15:26 +01:00
|
|
|
});
|
|
|
|
|
2018-07-02 16:21:13 +02:00
|
|
|
it('enumerates context keys - sync', function() {
|
2020-03-28 00:47:12 +01:00
|
|
|
var flowContextA = Context.getFlowContext("flowA")
|
2018-03-15 07:15:26 +01:00
|
|
|
var context = Context.get("1","flowA");
|
|
|
|
|
|
|
|
var keys = context.keys();
|
|
|
|
keys.should.be.an.Array();
|
|
|
|
keys.should.be.empty();
|
|
|
|
|
|
|
|
context.set("foo","bar");
|
|
|
|
keys = context.keys();
|
|
|
|
keys.should.have.length(1);
|
2018-06-07 14:51:11 +02:00
|
|
|
keys[0].should.equal("foo");
|
2018-03-15 07:15:26 +01:00
|
|
|
|
|
|
|
context.set("abc.def","bar");
|
|
|
|
keys = context.keys();
|
|
|
|
keys.should.have.length(2);
|
2018-06-07 14:51:11 +02:00
|
|
|
keys[1].should.equal("abc");
|
2018-03-15 07:15:26 +01:00
|
|
|
});
|
2015-12-29 23:16:51 +01:00
|
|
|
|
2018-07-02 16:21:13 +02:00
|
|
|
it('enumerates context keys - async', function(done) {
|
2020-03-28 00:47:12 +01:00
|
|
|
var flowContextA = Context.getFlowContext("flowA")
|
2018-07-02 16:21:13 +02:00
|
|
|
var context = Context.get("1","flowA");
|
|
|
|
|
|
|
|
var keys = context.keys(function(err,keys) {
|
|
|
|
keys.should.be.an.Array();
|
|
|
|
keys.should.be.empty();
|
|
|
|
context.set("foo","bar");
|
|
|
|
keys = context.keys(function(err,keys) {
|
|
|
|
keys.should.have.length(1);
|
|
|
|
keys[0].should.equal("foo");
|
|
|
|
|
|
|
|
context.set("abc.def","bar");
|
|
|
|
keys = context.keys(function(err,keys) {
|
|
|
|
keys.should.have.length(2);
|
|
|
|
keys[1].should.equal("abc");
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should enumerate only context keys when GlobalContext was given - sync', function() {
|
2018-05-31 02:47:21 +02:00
|
|
|
Context.init({functionGlobalContext: {foo:"bar"}});
|
2018-07-02 16:47:47 +02:00
|
|
|
Context.load().then(function(){
|
2020-03-28 00:47:12 +01:00
|
|
|
var flowContextA = Context.getFlowContext("flowA")
|
2018-05-31 02:47:21 +02:00
|
|
|
var context = Context.get("1","flowA");
|
2018-07-02 16:21:13 +02:00
|
|
|
context.global.set("foo2","bar2");
|
2018-06-22 10:11:54 +02:00
|
|
|
var keys = context.global.keys();
|
2018-07-02 16:21:13 +02:00
|
|
|
keys.should.have.length(2);
|
2018-06-07 14:51:11 +02:00
|
|
|
keys[0].should.equal("foo");
|
2018-07-02 16:21:13 +02:00
|
|
|
keys[1].should.equal("foo2");
|
2018-05-31 02:47:21 +02:00
|
|
|
});
|
|
|
|
});
|
2018-07-02 16:21:13 +02:00
|
|
|
|
|
|
|
it('should enumerate only context keys when GlobalContext was given - async', function(done) {
|
|
|
|
Context.init({functionGlobalContext: {foo:"bar"}});
|
2018-07-02 16:47:47 +02:00
|
|
|
Context.load().then(function(){
|
2020-03-28 00:47:12 +01:00
|
|
|
var flowContextA = Context.getFlowContext("flowA")
|
2018-07-02 16:21:13 +02:00
|
|
|
var context = Context.get("1","flowA");
|
|
|
|
context.global.set("foo2","bar2");
|
|
|
|
context.global.keys(function(err,keys) {
|
|
|
|
keys.should.have.length(2);
|
|
|
|
keys[0].should.equal("foo");
|
|
|
|
keys[1].should.equal("foo2");
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
}).catch(done);
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
it('returns functionGlobalContext value if store value undefined', function() {
|
|
|
|
Context.init({functionGlobalContext: {foo:"bar"}});
|
2018-09-09 12:07:44 +02:00
|
|
|
return Context.load().then(function(){
|
2020-03-28 00:47:12 +01:00
|
|
|
var flowContextA = Context.getFlowContext("flowA")
|
2018-07-02 16:21:13 +02:00
|
|
|
var context = Context.get("1","flowA");
|
|
|
|
var v = context.global.get('foo');
|
|
|
|
v.should.equal('bar');
|
|
|
|
});
|
|
|
|
})
|
2018-09-09 12:07:44 +02:00
|
|
|
|
|
|
|
it('returns functionGlobalContext sub-value if store value undefined', function() {
|
|
|
|
Context.init({functionGlobalContext: {foo:{bar:123}}});
|
|
|
|
return Context.load().then(function(){
|
2020-03-28 00:47:12 +01:00
|
|
|
var flowContextA = Context.getFlowContext("flowA")
|
2018-09-09 12:07:44 +02:00
|
|
|
var context = Context.get("1","flowA");
|
|
|
|
var v = context.global.get('foo.bar');
|
|
|
|
should.equal(v,123);
|
|
|
|
});
|
|
|
|
})
|
|
|
|
|
2019-01-16 15:10:03 +01:00
|
|
|
describe("$parent", function() {
|
2019-02-05 06:47:30 +01:00
|
|
|
it('should get undefined for $parent without key', function() {
|
2020-03-28 00:47:12 +01:00
|
|
|
var flowContextA = Context.getFlowContext("flowA")
|
|
|
|
var flowContextB = Context.getFlowContext("flowB","flowA")
|
2019-01-16 15:10:03 +01:00
|
|
|
var context0 = Context.get("0","flowA");
|
2020-03-28 00:47:12 +01:00
|
|
|
var context1 = Context.get("1","flowB");
|
2019-01-16 15:10:03 +01:00
|
|
|
var parent = context1.get("$parent");
|
2019-02-05 06:47:30 +01:00
|
|
|
should.equal(parent, undefined);
|
2019-01-16 15:10:03 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should get undefined for $parent of root', function() {
|
2020-03-28 00:47:12 +01:00
|
|
|
var flowContextA = Context.getFlowContext("flowA")
|
|
|
|
var flowContextB = Context.getFlowContext("flowB","flowA")
|
2019-01-16 15:10:03 +01:00
|
|
|
var context0 = Context.get("0","flowA");
|
2020-03-28 00:47:12 +01:00
|
|
|
var context1 = Context.get("1","flowB");
|
|
|
|
var parent = context1.flow.get("$parent.$parent.K");
|
2019-01-16 15:10:03 +01:00
|
|
|
should.equal(parent, undefined);
|
|
|
|
});
|
|
|
|
|
2020-03-28 00:47:12 +01:00
|
|
|
it('should get undefined for $parent of root - callback', function(done) {
|
|
|
|
var flowContextA = Context.getFlowContext("flowA")
|
|
|
|
var flowContextB = Context.getFlowContext("flowB","flowA")
|
|
|
|
var context0 = Context.get("0","flowA");
|
|
|
|
var context1 = Context.get("1","flowB");
|
|
|
|
context1.flow.get("$parent.$parent.K", function(err, result) {
|
|
|
|
try {
|
|
|
|
should.equal(err, undefined);
|
|
|
|
should.equal(result, undefined);
|
|
|
|
done();
|
|
|
|
} catch(err) {
|
|
|
|
done(err);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2019-02-05 06:47:30 +01:00
|
|
|
it('should get value in $parent', function() {
|
2020-03-28 00:47:12 +01:00
|
|
|
var flowContextA = Context.getFlowContext("flowA")
|
|
|
|
var flowContextB = Context.getFlowContext("flowB","flowA")
|
2019-01-16 15:10:03 +01:00
|
|
|
var context0 = Context.get("0","flowA");
|
2020-03-28 00:47:12 +01:00
|
|
|
var context1 = Context.get("1","flowB");
|
|
|
|
flowContextA.set("K", "v");
|
|
|
|
var v = context1.flow.get("$parent.K");
|
2019-01-16 15:10:03 +01:00
|
|
|
should.equal(v, "v");
|
|
|
|
});
|
|
|
|
|
2019-02-05 06:47:30 +01:00
|
|
|
it('should set value in $parent', function() {
|
2020-03-28 00:47:12 +01:00
|
|
|
var flowContextA = Context.getFlowContext("flowA")
|
|
|
|
var flowContextB = Context.getFlowContext("flowB","flowA")
|
2019-01-16 15:10:03 +01:00
|
|
|
var context0 = Context.get("0","flowA");
|
2020-03-28 00:47:12 +01:00
|
|
|
var context1 = Context.get("1","flowB");
|
|
|
|
context1.flow.set("$parent.K", "v");
|
|
|
|
var v = flowContextA.get("K");
|
2019-01-16 15:10:03 +01:00
|
|
|
should.equal(v, "v");
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should not contain $parent in keys', function() {
|
2020-03-28 00:47:12 +01:00
|
|
|
var flowContextA = Context.getFlowContext("flowA")
|
|
|
|
var flowContextB = Context.getFlowContext("flowB","flowA")
|
2019-01-16 15:10:03 +01:00
|
|
|
var context0 = Context.get("0","flowA");
|
2020-03-28 00:47:12 +01:00
|
|
|
var context1 = Context.get("1","flowB");
|
2019-01-16 15:10:03 +01:00
|
|
|
var parent = context1.get("$parent");
|
2020-03-28 00:47:12 +01:00
|
|
|
flowContextA.set("K0", "v0");
|
2019-01-16 15:10:03 +01:00
|
|
|
context1.set("K1", "v1");
|
|
|
|
var keys = context1.keys();
|
|
|
|
keys.should.have.length(1);
|
|
|
|
keys[0].should.equal("K1");
|
|
|
|
});
|
|
|
|
});
|
2018-09-09 12:07:44 +02:00
|
|
|
|
2018-05-30 03:24:27 +02:00
|
|
|
});
|
2018-03-15 07:15:26 +01:00
|
|
|
|
2018-06-22 10:11:54 +02:00
|
|
|
describe('external context storage',function() {
|
|
|
|
var resourcesDir = path.resolve(path.join(__dirname,"..","resources","context"));
|
2021-04-09 12:22:57 +02:00
|
|
|
var sandbox = sinon.createSandbox();
|
2018-06-22 10:11:54 +02:00
|
|
|
var stubGet = sandbox.stub();
|
|
|
|
var stubSet = sandbox.stub();
|
|
|
|
var stubKeys = sandbox.stub();
|
2018-06-20 12:50:55 +02:00
|
|
|
var stubDelete = sandbox.stub().returns(Promise.resolve());
|
|
|
|
var stubClean = sandbox.stub().returns(Promise.resolve());
|
|
|
|
var stubOpen = sandbox.stub().returns(Promise.resolve());
|
|
|
|
var stubClose = sandbox.stub().returns(Promise.resolve());
|
2018-06-22 10:11:54 +02:00
|
|
|
var stubGet2 = sandbox.stub();
|
|
|
|
var stubSet2 = sandbox.stub();
|
|
|
|
var stubKeys2 = sandbox.stub();
|
2018-06-20 12:50:55 +02:00
|
|
|
var stubDelete2 = sandbox.stub().returns(Promise.resolve());
|
|
|
|
var stubClean2 = sandbox.stub().returns(Promise.resolve());
|
|
|
|
var stubOpen2 = sandbox.stub().returns(Promise.resolve());
|
|
|
|
var stubClose2 = sandbox.stub().returns(Promise.resolve());
|
2018-05-30 08:23:34 +02:00
|
|
|
var testPlugin = function(config){
|
|
|
|
function Test(){}
|
2018-06-20 13:00:39 +02:00
|
|
|
Test.prototype.get = stubGet;
|
|
|
|
Test.prototype.set = stubSet;
|
|
|
|
Test.prototype.keys = stubKeys;
|
2018-05-30 08:23:34 +02:00
|
|
|
Test.prototype.delete = stubDelete;
|
|
|
|
Test.prototype.clean = stubClean;
|
|
|
|
Test.prototype.open = stubOpen;
|
|
|
|
Test.prototype.close = stubClose;
|
|
|
|
return new Test(config);
|
|
|
|
};
|
|
|
|
var testPlugin2 = function(config){
|
|
|
|
function Test2(){}
|
2018-06-20 13:00:39 +02:00
|
|
|
Test2.prototype.get = stubGet2;
|
|
|
|
Test2.prototype.set = stubSet2;
|
|
|
|
Test2.prototype.keys = stubKeys2;
|
2018-05-30 08:23:34 +02:00
|
|
|
Test2.prototype.delete = stubDelete2;
|
|
|
|
Test2.prototype.clean = stubClean2;
|
|
|
|
Test2.prototype.open = stubOpen2;
|
|
|
|
Test2.prototype.close = stubClose2;
|
|
|
|
return new Test2(config);
|
|
|
|
};
|
|
|
|
var contextStorage={
|
|
|
|
test:{
|
|
|
|
module: testPlugin,
|
|
|
|
config:{}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
var contextDefaultStorage={
|
|
|
|
default: {
|
|
|
|
module: testPlugin2,
|
|
|
|
config:{}
|
|
|
|
},
|
|
|
|
test:{
|
|
|
|
module: testPlugin,
|
|
|
|
config:{}
|
|
|
|
}
|
|
|
|
};
|
2018-06-11 09:04:27 +02:00
|
|
|
var contextAlias={
|
|
|
|
default: "test",
|
|
|
|
test:{
|
|
|
|
module: testPlugin,
|
|
|
|
config:{}
|
|
|
|
}
|
|
|
|
};
|
2018-07-02 16:21:13 +02:00
|
|
|
var memoryStorage ={
|
|
|
|
memory:{
|
|
|
|
module: "memory"
|
|
|
|
}
|
|
|
|
};
|
2018-05-30 08:23:34 +02:00
|
|
|
|
|
|
|
afterEach(function() {
|
|
|
|
sandbox.reset();
|
|
|
|
return Context.clean({allNodes:{}}).then(function(){
|
2018-05-30 03:24:27 +02:00
|
|
|
return Context.close();
|
2018-08-09 16:36:43 +02:00
|
|
|
}).then(function(){
|
|
|
|
return fs.remove(resourcesDir);
|
2018-04-20 04:41:29 +02:00
|
|
|
});
|
2018-05-30 08:23:34 +02:00
|
|
|
});
|
2018-06-11 09:04:27 +02:00
|
|
|
|
2018-05-30 08:23:34 +02:00
|
|
|
describe('load modules',function(){
|
|
|
|
it('should call open()', function() {
|
|
|
|
Context.init({contextStorage:contextDefaultStorage});
|
2018-07-02 16:47:47 +02:00
|
|
|
Context.load().then(function(){
|
2018-05-30 08:23:34 +02:00
|
|
|
stubOpen.called.should.be.true();
|
|
|
|
stubOpen2.called.should.be.true();
|
|
|
|
});
|
|
|
|
});
|
2018-05-30 03:24:27 +02:00
|
|
|
it('should load memory module', function() {
|
|
|
|
Context.init({contextStorage:{memory:{module:"memory"}}});
|
2018-08-09 16:36:43 +02:00
|
|
|
return Context.load();
|
2018-04-20 04:41:29 +02:00
|
|
|
});
|
2018-05-30 03:24:27 +02:00
|
|
|
it('should load localfilesystem module', function() {
|
2018-06-22 10:11:54 +02:00
|
|
|
Context.init({contextStorage:{file:{module:"localfilesystem",config:{dir:resourcesDir}}}});
|
2018-08-09 16:36:43 +02:00
|
|
|
return Context.load();
|
2018-04-20 04:41:29 +02:00
|
|
|
});
|
2018-06-22 10:11:54 +02:00
|
|
|
it('should ignore reserved storage name `_`', function(done) {
|
2018-05-30 08:23:34 +02:00
|
|
|
Context.init({contextStorage:{_:{module:testPlugin}}});
|
2018-06-22 10:11:54 +02:00
|
|
|
Context.load().then(function(){
|
2020-03-28 00:47:12 +01:00
|
|
|
var flowContext = Context.getFlowContext("flow")
|
2018-05-30 08:23:34 +02:00
|
|
|
var context = Context.get("1","flow");
|
2018-06-26 04:43:37 +02:00
|
|
|
var cb = function(){}
|
2018-06-22 10:11:54 +02:00
|
|
|
context.set("foo","bar","_",cb);
|
|
|
|
context.get("foo","_",cb);
|
|
|
|
context.keys("_",cb);
|
|
|
|
stubSet.called.should.be.false();
|
|
|
|
stubGet.called.should.be.false();
|
|
|
|
stubKeys.called.should.be.false();
|
|
|
|
done();
|
2018-07-02 16:21:13 +02:00
|
|
|
}).catch(done);
|
2018-05-30 08:23:34 +02:00
|
|
|
});
|
2018-07-16 17:44:33 +02:00
|
|
|
|
|
|
|
it('should fail when using invalid store name', function(done) {
|
2018-07-19 05:58:42 +02:00
|
|
|
Context.init({contextStorage:{'Invalid name':{module:testPlugin}}});
|
2018-07-16 17:44:33 +02:00
|
|
|
Context.load().then(function(){
|
|
|
|
done("An error was not thrown");
|
|
|
|
}).catch(function(){
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2018-07-19 05:58:42 +02:00
|
|
|
it('should fail when using invalid sign character', function (done) {
|
|
|
|
Context.init({ contextStorage:{'abc-123':{module:testPlugin}}});
|
|
|
|
Context.load().then(function () {
|
|
|
|
done("An error was not thrown");
|
|
|
|
}).catch(function () {
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2018-05-30 03:24:27 +02:00
|
|
|
it('should fail when using invalid default context', function(done) {
|
|
|
|
Context.init({contextStorage:{default:"noexist"}});
|
|
|
|
Context.load().then(function(){
|
|
|
|
done("An error was not thrown");
|
|
|
|
}).catch(function(){
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
it('should fail for the storage with no module', function(done) {
|
|
|
|
Context.init({ contextStorage: { test: {}}});
|
|
|
|
Context.load().then(function(){
|
|
|
|
done("An error was not thrown");
|
|
|
|
}).catch(function(){
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
it('should fail to load non-existent module', function(done) {
|
|
|
|
Context.init({contextStorage:{ file:{module:"nonexistent"} }});
|
|
|
|
Context.load().then(function(){
|
|
|
|
done("An error was not thrown");
|
|
|
|
}).catch(function(){
|
|
|
|
done();
|
|
|
|
});
|
2018-04-20 04:41:29 +02:00
|
|
|
});
|
2018-07-19 05:58:42 +02:00
|
|
|
it('should fail to load invalid module', function (done) {
|
|
|
|
Context.init({contextStorage: {
|
|
|
|
test: {
|
|
|
|
module: function (config) {
|
|
|
|
throw new Error("invalid plugin was loaded.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}});
|
|
|
|
Context.load().then(function () {
|
|
|
|
done("An error was not thrown");
|
|
|
|
}).catch(function () {
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2018-04-20 04:41:29 +02:00
|
|
|
});
|
|
|
|
|
2018-05-30 08:23:34 +02:00
|
|
|
describe('close modules',function(){
|
2018-07-02 16:21:13 +02:00
|
|
|
it('should call close()', function(done) {
|
2018-05-30 08:23:34 +02:00
|
|
|
Context.init({contextStorage:contextDefaultStorage});
|
2018-07-02 16:47:47 +02:00
|
|
|
Context.load().then(function(){
|
2018-05-30 08:23:34 +02:00
|
|
|
return Context.close().then(function(){
|
|
|
|
stubClose.called.should.be.true();
|
|
|
|
stubClose2.called.should.be.true();
|
2018-07-02 16:21:13 +02:00
|
|
|
done();
|
2018-05-30 08:23:34 +02:00
|
|
|
});
|
2018-07-02 16:21:13 +02:00
|
|
|
}).catch(done);
|
2018-05-30 03:24:27 +02:00
|
|
|
});
|
2018-05-30 08:23:34 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('store context',function() {
|
2018-06-22 10:11:54 +02:00
|
|
|
it('should store local property to external context storage',function(done) {
|
2018-05-30 03:24:27 +02:00
|
|
|
Context.init({contextStorage:contextStorage});
|
2018-06-22 10:11:54 +02:00
|
|
|
var cb = function(){done("An error occurred")}
|
|
|
|
Context.load().then(function(){
|
2020-03-28 00:47:12 +01:00
|
|
|
var flowContext = Context.getFlowContext("flow");
|
2018-05-30 03:24:27 +02:00
|
|
|
var context = Context.get("1","flow");
|
2018-06-22 10:11:54 +02:00
|
|
|
context.set("foo","bar","test",cb);
|
|
|
|
context.get("foo","test",cb);
|
|
|
|
context.keys("test",cb);
|
|
|
|
stubSet.calledWithExactly("1:flow","foo","bar",cb).should.be.true();
|
2018-07-23 14:27:43 +02:00
|
|
|
stubGet.calledWith("1:flow","foo").should.be.true();
|
2018-06-22 10:11:54 +02:00
|
|
|
stubKeys.calledWithExactly("1:flow",cb).should.be.true();
|
|
|
|
done();
|
2018-07-02 16:21:13 +02:00
|
|
|
}).catch(done);
|
2018-03-15 07:15:26 +01:00
|
|
|
});
|
2018-06-22 10:11:54 +02:00
|
|
|
it('should store flow property to external context storage',function(done) {
|
2018-05-30 03:24:27 +02:00
|
|
|
Context.init({contextStorage:contextStorage});
|
2018-06-22 10:11:54 +02:00
|
|
|
Context.load().then(function(){
|
2020-03-28 00:47:12 +01:00
|
|
|
var flowContext = Context.getFlowContext("flow");
|
2018-05-30 03:24:27 +02:00
|
|
|
var context = Context.get("1","flow");
|
2018-06-22 10:11:54 +02:00
|
|
|
var cb = function(){done("An error occurred")}
|
|
|
|
context.flow.set("foo","bar","test",cb);
|
|
|
|
context.flow.get("foo","test",cb);
|
|
|
|
context.flow.keys("test",cb);
|
|
|
|
stubSet.calledWithExactly("flow","foo","bar",cb).should.be.true();
|
2018-07-23 14:27:43 +02:00
|
|
|
stubGet.calledWith("flow","foo").should.be.true();
|
2018-06-22 10:11:54 +02:00
|
|
|
stubKeys.calledWithExactly("flow",cb).should.be.true();
|
|
|
|
done();
|
2018-07-02 16:21:13 +02:00
|
|
|
}).catch(done);
|
2018-03-15 07:15:26 +01:00
|
|
|
});
|
2018-06-22 10:11:54 +02:00
|
|
|
it('should store global property to external context storage',function(done) {
|
2018-05-30 03:24:27 +02:00
|
|
|
Context.init({contextStorage:contextStorage});
|
2018-06-22 10:11:54 +02:00
|
|
|
Context.load().then(function(){
|
2020-03-28 00:47:12 +01:00
|
|
|
var flowContext = Context.getFlowContext("flow");
|
2018-05-30 03:24:27 +02:00
|
|
|
var context = Context.get("1","flow");
|
2018-06-22 10:11:54 +02:00
|
|
|
var cb = function(){done("An error occurred")}
|
|
|
|
context.global.set("foo","bar","test",cb);
|
|
|
|
context.global.get("foo","test",cb);
|
|
|
|
context.global.keys("test",cb);
|
|
|
|
stubSet.calledWithExactly("global","foo","bar",cb).should.be.true();
|
2018-07-02 16:21:13 +02:00
|
|
|
stubGet.calledWith("global","foo").should.be.true();
|
|
|
|
stubKeys.calledWith("global").should.be.true();
|
2018-06-22 10:11:54 +02:00
|
|
|
done();
|
2018-07-02 16:21:13 +02:00
|
|
|
}).catch(done);
|
2018-03-15 07:15:26 +01:00
|
|
|
});
|
2018-06-22 10:11:54 +02:00
|
|
|
it('should store data to the default context when non-existent context storage was specified', function(done) {
|
2018-03-28 08:54:16 +02:00
|
|
|
Context.init({contextStorage:contextDefaultStorage});
|
2018-06-22 10:11:54 +02:00
|
|
|
Context.load().then(function(){
|
2020-03-28 00:47:12 +01:00
|
|
|
var flowContext = Context.getFlowContext("flow");
|
2018-05-30 03:24:27 +02:00
|
|
|
var context = Context.get("1","flow");
|
2018-06-22 10:11:54 +02:00
|
|
|
var cb = function(){done("An error occurred")}
|
|
|
|
context.set("foo","bar","nonexist",cb);
|
|
|
|
context.get("foo","nonexist",cb);
|
|
|
|
context.keys("nonexist",cb);
|
|
|
|
stubGet.called.should.be.false();
|
|
|
|
stubSet.called.should.be.false();
|
|
|
|
stubKeys.called.should.be.false();
|
|
|
|
stubSet2.calledWithExactly("1:flow","foo","bar",cb).should.be.true();
|
2018-07-23 14:27:43 +02:00
|
|
|
stubGet2.calledWith("1:flow","foo").should.be.true();
|
2018-06-22 10:11:54 +02:00
|
|
|
stubKeys2.calledWithExactly("1:flow",cb).should.be.true();
|
|
|
|
done();
|
2018-07-02 16:21:13 +02:00
|
|
|
}).catch(done);
|
2018-03-28 08:54:16 +02:00
|
|
|
});
|
2018-06-22 10:11:54 +02:00
|
|
|
it('should use the default context', function(done) {
|
2018-04-20 04:41:29 +02:00
|
|
|
Context.init({contextStorage:contextDefaultStorage});
|
2018-06-22 10:11:54 +02:00
|
|
|
Context.load().then(function(){
|
2020-03-28 00:47:12 +01:00
|
|
|
var flowContext = Context.getFlowContext("flow");
|
2018-05-30 03:24:27 +02:00
|
|
|
var context = Context.get("1","flow");
|
2018-06-22 10:11:54 +02:00
|
|
|
var cb = function(){done("An error occurred")}
|
2018-07-19 05:58:42 +02:00
|
|
|
context.set("foo","bar","default",cb);
|
2018-06-22 10:11:54 +02:00
|
|
|
context.get("foo","default",cb);
|
|
|
|
context.keys("default",cb);
|
|
|
|
stubGet.called.should.be.false();
|
|
|
|
stubSet.called.should.be.false();
|
|
|
|
stubKeys.called.should.be.false();
|
|
|
|
stubSet2.calledWithExactly("1:flow","foo","bar",cb).should.be.true();
|
2018-07-23 14:27:43 +02:00
|
|
|
stubGet2.calledWith("1:flow","foo").should.be.true();
|
2018-06-22 10:11:54 +02:00
|
|
|
stubKeys2.calledWithExactly("1:flow",cb).should.be.true();
|
|
|
|
done();
|
2018-07-02 16:21:13 +02:00
|
|
|
}).catch(done);
|
2018-04-20 04:41:29 +02:00
|
|
|
});
|
2018-06-22 10:11:54 +02:00
|
|
|
it('should use the alias of default context', function(done) {
|
2018-04-20 04:41:29 +02:00
|
|
|
Context.init({contextStorage:contextDefaultStorage});
|
2018-06-22 10:11:54 +02:00
|
|
|
Context.load().then(function(){
|
2020-03-28 00:47:12 +01:00
|
|
|
var flowContext = Context.getFlowContext("flow");
|
2018-05-30 03:24:27 +02:00
|
|
|
var context = Context.get("1","flow");
|
2018-06-22 10:11:54 +02:00
|
|
|
var cb = function(){done("An error occurred")}
|
|
|
|
context.set("foo","alias",cb);
|
|
|
|
context.get("foo",cb);
|
|
|
|
context.keys(cb);
|
|
|
|
stubGet.called.should.be.false();
|
|
|
|
stubSet.called.should.be.false();
|
|
|
|
stubKeys.called.should.be.false();
|
|
|
|
stubSet2.calledWithExactly("1:flow","foo","alias",cb).should.be.true();
|
2018-07-23 14:27:43 +02:00
|
|
|
stubGet2.calledWith("1:flow","foo").should.be.true();
|
2018-06-22 10:11:54 +02:00
|
|
|
stubKeys2.calledWithExactly("1:flow",cb).should.be.true();
|
|
|
|
done();
|
2018-07-02 16:21:13 +02:00
|
|
|
}).catch(done);
|
2018-04-20 04:41:29 +02:00
|
|
|
});
|
2020-03-28 00:47:12 +01:00
|
|
|
|
2018-09-15 00:21:05 +02:00
|
|
|
it('should allow the store name to be provide in the key', function(done) {
|
|
|
|
Context.init({contextStorage:contextDefaultStorage});
|
|
|
|
Context.load().then(function(){
|
2020-03-28 00:47:12 +01:00
|
|
|
var flowContext = Context.getFlowContext("flow");
|
2018-09-15 00:21:05 +02:00
|
|
|
var context = Context.get("1","flow");
|
|
|
|
var cb = function(){done("An error occurred")}
|
|
|
|
context.set("#:(test)::foo","bar");
|
|
|
|
context.get("#:(test)::foo");
|
|
|
|
stubGet2.called.should.be.false();
|
|
|
|
stubSet2.called.should.be.false();
|
|
|
|
stubSet.calledWithExactly("1:flow","foo","bar",undefined).should.be.true();
|
|
|
|
stubGet.calledWith("1:flow","foo").should.be.true();
|
|
|
|
done();
|
|
|
|
}).catch(done);
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2018-06-22 10:11:54 +02:00
|
|
|
it('should use default as the alias of other context', function(done) {
|
2018-06-11 09:04:27 +02:00
|
|
|
Context.init({contextStorage:contextAlias});
|
2018-06-22 10:11:54 +02:00
|
|
|
Context.load().then(function(){
|
2020-03-28 00:47:12 +01:00
|
|
|
var flowContext = Context.getFlowContext("flow");
|
2018-06-11 09:04:27 +02:00
|
|
|
var context = Context.get("1","flow");
|
2018-06-22 10:11:54 +02:00
|
|
|
var cb = function(){done("An error occurred")}
|
|
|
|
context.set("foo","alias",cb);
|
|
|
|
context.get("foo",cb);
|
|
|
|
context.keys(cb);
|
|
|
|
stubSet.calledWithExactly("1:flow","foo","alias",cb).should.be.true();
|
2018-07-23 14:27:43 +02:00
|
|
|
stubGet.calledWith("1:flow","foo").should.be.true();
|
2018-06-22 10:11:54 +02:00
|
|
|
stubKeys.calledWithExactly("1:flow",cb).should.be.true();
|
|
|
|
done();
|
2018-07-02 16:21:13 +02:00
|
|
|
}).catch(done);
|
2018-06-11 09:04:27 +02:00
|
|
|
});
|
2018-07-02 16:21:13 +02:00
|
|
|
it('should not throw an error using undefined storage for local context', function(done) {
|
2018-05-30 03:24:27 +02:00
|
|
|
Context.init({contextStorage:contextStorage});
|
|
|
|
Context.load().then(function(){
|
2020-03-28 00:47:12 +01:00
|
|
|
var flowContext = Context.getFlowContext("flow");
|
2018-05-30 03:24:27 +02:00
|
|
|
var context = Context.get("1","flow");
|
2018-06-22 10:11:54 +02:00
|
|
|
var cb = function(){done("An error occurred")}
|
|
|
|
context.get("local","nonexist",cb);
|
2018-07-02 16:21:13 +02:00
|
|
|
done()
|
|
|
|
}).catch(done);
|
2018-03-15 07:15:26 +01:00
|
|
|
});
|
2018-03-28 08:54:16 +02:00
|
|
|
it('should throw an error using undefined storage for flow context', function(done) {
|
2018-05-30 03:24:27 +02:00
|
|
|
Context.init({contextStorage:contextStorage});
|
|
|
|
Context.load().then(function(){
|
2020-03-28 00:47:12 +01:00
|
|
|
var flowContext = Context.getFlowContext("flow");
|
2018-05-30 03:24:27 +02:00
|
|
|
var context = Context.get("1","flow");
|
2018-06-22 10:11:54 +02:00
|
|
|
var cb = function(){done("An error occurred")}
|
|
|
|
context.flow.get("flow","nonexist",cb);
|
2018-07-02 16:21:13 +02:00
|
|
|
done();
|
|
|
|
}).catch(done);
|
2018-03-15 07:15:26 +01:00
|
|
|
});
|
2018-07-02 16:21:13 +02:00
|
|
|
|
|
|
|
it('should return functionGlobalContext value as a default - synchronous', function(done) {
|
|
|
|
var fGC = { "foo": 456 };
|
|
|
|
Context.init({contextStorage:memoryStorage, functionGlobalContext:fGC });
|
|
|
|
Context.load().then(function() {
|
2020-03-28 00:47:12 +01:00
|
|
|
var flowContext = Context.getFlowContext("flow");
|
2018-07-02 16:21:13 +02:00
|
|
|
var context = Context.get("1","flow");
|
|
|
|
// Get foo - should be value from fGC
|
|
|
|
var v = context.global.get("foo");
|
|
|
|
v.should.equal(456);
|
|
|
|
|
|
|
|
// Update foo - should not touch fGC object
|
|
|
|
context.global.set("foo","new value");
|
|
|
|
fGC.foo.should.equal(456);
|
|
|
|
|
|
|
|
// Get foo - should be the updated value
|
|
|
|
v = context.global.get("foo");
|
|
|
|
v.should.equal("new value");
|
|
|
|
done();
|
|
|
|
}).catch(done);
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should return functionGlobalContext value as a default - async', function(done) {
|
|
|
|
var fGC = { "foo": 456 };
|
|
|
|
Context.init({contextStorage:memoryStorage, functionGlobalContext:fGC });
|
|
|
|
Context.load().then(function() {
|
2020-03-28 00:47:12 +01:00
|
|
|
var flowContext = Context.getFlowContext("flow");
|
2018-07-02 16:21:13 +02:00
|
|
|
var context = Context.get("1","flow");
|
|
|
|
// Get foo - should be value from fGC
|
|
|
|
context.global.get("foo", function(err, v) {
|
|
|
|
if (err) {
|
|
|
|
done(err)
|
|
|
|
} else {
|
|
|
|
v.should.equal(456);
|
|
|
|
// Update foo - should not touch fGC object
|
|
|
|
context.global.set("foo","new value", function(err) {
|
|
|
|
if (err) {
|
|
|
|
done(err)
|
|
|
|
} else {
|
|
|
|
fGC.foo.should.equal(456);
|
|
|
|
// Get foo - should be the updated value
|
|
|
|
context.global.get("foo", function(err, v) {
|
|
|
|
if (err) {
|
|
|
|
done(err)
|
|
|
|
} else {
|
|
|
|
v.should.equal("new value");
|
|
|
|
done();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}).catch(done);
|
|
|
|
})
|
|
|
|
|
2018-07-12 04:13:29 +02:00
|
|
|
it('should return multiple values if key is an array', function(done) {
|
|
|
|
Context.init({contextStorage:memoryStorage});
|
|
|
|
Context.load().then(function(){
|
2020-03-28 00:47:12 +01:00
|
|
|
var flowContext = Context.getFlowContext("flow");
|
2018-07-12 04:13:29 +02:00
|
|
|
var context = Context.get("1","flow");
|
|
|
|
context.set("foo1","bar1","memory");
|
|
|
|
context.set("foo2","bar2","memory");
|
|
|
|
context.get(["foo1","foo2","foo3"], "memory", function(err,foo1,foo2,foo3){
|
|
|
|
if (err) {
|
|
|
|
done(err);
|
|
|
|
} else {
|
|
|
|
foo1.should.be.equal("bar1");
|
|
|
|
foo2.should.be.equal("bar2");
|
|
|
|
should.not.exist(foo3);
|
|
|
|
done();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}).catch(function(err){ done(err); });
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should return multiple functionGlobalContext values if key is an array', function(done) {
|
2018-09-09 12:07:44 +02:00
|
|
|
var fGC = { "foo1": 456, "foo2": {"bar":789} };
|
2018-07-12 04:13:29 +02:00
|
|
|
Context.init({contextStorage:memoryStorage, functionGlobalContext:fGC });
|
|
|
|
Context.load().then(function(){
|
2020-03-28 00:47:12 +01:00
|
|
|
var flowContext = Context.getFlowContext("flow");
|
2018-07-12 04:13:29 +02:00
|
|
|
var context = Context.get("1","flow");
|
2018-09-09 12:07:44 +02:00
|
|
|
context.global.get(["foo1","foo2.bar","foo3"], "memory", function(err,foo1,foo2,foo3){
|
2018-07-12 04:13:29 +02:00
|
|
|
if (err) {
|
|
|
|
done(err);
|
|
|
|
} else {
|
2018-09-09 12:07:44 +02:00
|
|
|
should.equal(foo1, 456);
|
|
|
|
should.equal(foo2, 789);
|
2018-07-12 04:13:29 +02:00
|
|
|
should.not.exist(foo3);
|
|
|
|
done();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}).catch(function(err){ done(err); });
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should return an error if an error occurs in getting multiple store values', function(done) {
|
|
|
|
Context.init({contextStorage:contextStorage});
|
2018-07-23 14:27:43 +02:00
|
|
|
stubGet.onFirstCall().callsArgWith(2, "error2", "bar1");
|
2018-07-12 04:13:29 +02:00
|
|
|
Context.load().then(function(){
|
2020-03-28 00:47:12 +01:00
|
|
|
var flowContext = Context.getFlowContext("flow")
|
2018-07-12 04:13:29 +02:00
|
|
|
var context = Context.get("1","flow");
|
|
|
|
context.global.get(["foo1","foo2","foo3"], "memory", function(err,foo1,foo2,foo3){
|
|
|
|
if (err === "error2") {
|
|
|
|
done();
|
|
|
|
} else {
|
|
|
|
done("An error occurred");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}).catch(function(err){ done(err); });
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should return a first error if some errors occur in getting multiple store values', function(done) {
|
|
|
|
Context.init({contextStorage:contextStorage});
|
|
|
|
stubGet.onFirstCall().callsArgWith(2, "error1");
|
|
|
|
stubGet.onSecondCall().callsArgWith(2, null, "bar2");
|
|
|
|
stubGet.onThirdCall().callsArgWith(2, "error3");
|
|
|
|
Context.load().then(function(){
|
2020-03-28 00:47:12 +01:00
|
|
|
var flowContext = Context.getFlowContext("flow");
|
2018-07-12 04:13:29 +02:00
|
|
|
var context = Context.get("1","flow");
|
|
|
|
context.get(["foo1","foo2","foo3"], "memory", function(err,foo1,foo2,foo3){
|
|
|
|
if (err === "error1") {
|
|
|
|
done();
|
|
|
|
} else {
|
|
|
|
done("An error occurred");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}).catch(function(err){ done(err); });
|
|
|
|
});
|
2018-07-12 12:19:55 +02:00
|
|
|
|
|
|
|
it('should store multiple properties if key and value are arrays', function(done) {
|
|
|
|
Context.init({contextStorage:memoryStorage});
|
|
|
|
Context.load().then(function(){
|
2020-03-28 00:47:12 +01:00
|
|
|
var flowContext = Context.getFlowContext("flow");
|
2018-07-12 12:19:55 +02:00
|
|
|
var context = Context.get("1","flow");
|
|
|
|
context.set(["foo1","foo2","foo3"], ["bar1","bar2","bar3"], "memory", function(err){
|
|
|
|
if (err) {
|
|
|
|
done(err);
|
|
|
|
} else {
|
|
|
|
context.get(["foo1","foo2","foo3"], "memory", function(err,foo1,foo2,foo3){
|
|
|
|
if (err) {
|
|
|
|
done(err);
|
|
|
|
} else {
|
|
|
|
foo1.should.be.equal("bar1");
|
|
|
|
foo2.should.be.equal("bar2");
|
|
|
|
foo3.should.be.equal("bar3");
|
|
|
|
done();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-07-13 13:59:45 +02:00
|
|
|
it('should deletes multiple properties', function(done) {
|
|
|
|
Context.init({contextStorage:memoryStorage});
|
|
|
|
Context.load().then(function(){
|
2020-03-28 00:47:12 +01:00
|
|
|
var flowContext = Context.getFlowContext("flow");
|
2018-07-13 13:59:45 +02:00
|
|
|
var context = Context.get("1","flow");
|
|
|
|
context.set(["foo1","foo2","foo3"], ["bar1","bar2","bar3"], "memory", function(err){
|
|
|
|
if (err) {
|
|
|
|
done(err);
|
|
|
|
} else {
|
|
|
|
context.get(["foo1","foo2","foo3"], "memory", function(err,foo1,foo2,foo3){
|
|
|
|
if (err) {
|
|
|
|
done(err);
|
|
|
|
} else {
|
|
|
|
foo1.should.be.equal("bar1");
|
|
|
|
foo2.should.be.equal("bar2");
|
|
|
|
foo3.should.be.equal("bar3");
|
|
|
|
context.set(["foo1","foo2","foo3"], new Array(3), "memory", function(err){
|
|
|
|
if (err) {
|
|
|
|
done(err);
|
|
|
|
} else {
|
|
|
|
context.get(["foo1","foo2","foo3"], "memory", function(err,foo1,foo2,foo3){
|
|
|
|
if (err) {
|
|
|
|
done(err);
|
|
|
|
} else {
|
|
|
|
should.not.exist(foo1);
|
|
|
|
should.not.exist(foo2);
|
|
|
|
should.not.exist(foo3);
|
|
|
|
done();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should use null for missing values if the value array is shorter than the key array', function(done) {
|
|
|
|
Context.init({contextStorage:memoryStorage});
|
|
|
|
Context.load().then(function(){
|
2020-03-28 00:47:12 +01:00
|
|
|
var flowContext = Context.getFlowContext("flow");
|
2018-07-13 13:59:45 +02:00
|
|
|
var context = Context.get("1","flow");
|
|
|
|
context.set(["foo1","foo2","foo3"], ["bar1","bar2"], "memory", function(err){
|
|
|
|
if (err) {
|
|
|
|
done(err);
|
|
|
|
} else {
|
|
|
|
context.keys(function(err, keys){
|
|
|
|
keys.should.have.length(3);
|
|
|
|
keys.should.eql(["foo1","foo2","foo3"]);
|
|
|
|
context.get(["foo1","foo2","foo3"], "memory", function(err,foo1,foo2,foo3){
|
|
|
|
if (err) {
|
|
|
|
done(err);
|
|
|
|
} else {
|
|
|
|
foo1.should.be.equal("bar1");
|
|
|
|
foo2.should.be.equal("bar2");
|
|
|
|
should(foo3).be.null();
|
|
|
|
done();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should use null for missing values if the value is not array', function(done) {
|
|
|
|
Context.init({contextStorage:memoryStorage});
|
|
|
|
Context.load().then(function(){
|
2020-03-28 00:47:12 +01:00
|
|
|
var flowContext = Context.getFlowContext("flow");
|
2018-07-13 13:59:45 +02:00
|
|
|
var context = Context.get("1","flow");
|
|
|
|
context.set(["foo1","foo2","foo3"], "bar1", "memory", function(err){
|
|
|
|
if (err) {
|
|
|
|
done(err);
|
|
|
|
} else {
|
|
|
|
context.keys(function(err, keys){
|
|
|
|
keys.should.have.length(3);
|
|
|
|
keys.should.eql(["foo1","foo2","foo3"]);
|
|
|
|
context.get(["foo1","foo2","foo3"], "memory", function(err,foo1,foo2,foo3){
|
|
|
|
if (err) {
|
|
|
|
done(err);
|
|
|
|
} else {
|
|
|
|
foo1.should.be.equal("bar1");
|
|
|
|
should(foo2).be.null();
|
|
|
|
should(foo3).be.null();
|
|
|
|
done();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should ignore the extra values if the value array is longer than the key array', function(done) {
|
|
|
|
Context.init({contextStorage:memoryStorage});
|
|
|
|
Context.load().then(function(){
|
2020-03-28 00:47:12 +01:00
|
|
|
var flowContext = Context.getFlowContext("flow");
|
2018-07-13 13:59:45 +02:00
|
|
|
var context = Context.get("1","flow");
|
|
|
|
context.set(["foo1","foo2","foo3"], ["bar1","bar2","bar3","ignored"], "memory", function(err){
|
|
|
|
if (err) {
|
|
|
|
done(err);
|
|
|
|
} else {
|
|
|
|
context.keys(function(err, keys){
|
|
|
|
keys.should.have.length(3);
|
|
|
|
keys.should.eql(["foo1","foo2","foo3"]);
|
|
|
|
context.get(["foo1","foo2","foo3"], "memory", function(err,foo1,foo2,foo3){
|
|
|
|
if (err) {
|
|
|
|
done(err);
|
|
|
|
} else {
|
|
|
|
foo1.should.be.equal("bar1");
|
|
|
|
foo2.should.be.equal("bar2");
|
|
|
|
foo3.should.be.equal("bar3");
|
|
|
|
done();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-07-12 12:19:55 +02:00
|
|
|
it('should return an error if an error occurs in storing multiple values', function(done) {
|
|
|
|
Context.init({contextStorage:contextStorage});
|
2018-07-23 14:27:43 +02:00
|
|
|
stubSet.onFirstCall().callsArgWith(3, "error2");
|
2018-07-12 12:19:55 +02:00
|
|
|
Context.load().then(function(){
|
2020-03-28 00:47:12 +01:00
|
|
|
var flowContext = Context.getFlowContext("flow");
|
2018-07-12 12:19:55 +02:00
|
|
|
var context = Context.get("1","flow");
|
|
|
|
context.set(["foo1","foo2","foo3"], ["bar1","bar2","bar3"], "memory", function(err){
|
|
|
|
if (err === "error2") {
|
|
|
|
done();
|
|
|
|
} else {
|
|
|
|
done("An error occurred");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}).catch(function(err){ done(err); });
|
|
|
|
});
|
2018-07-19 05:58:42 +02:00
|
|
|
|
|
|
|
it('should throw an error if callback of context.get is not a function', function (done) {
|
|
|
|
Context.init({ contextStorage: memoryStorage });
|
|
|
|
Context.load().then(function () {
|
2020-03-28 00:47:12 +01:00
|
|
|
var flowContext = Context.getFlowContext("flow");
|
2018-07-19 05:58:42 +02:00
|
|
|
var context = Context.get("1", "flow");
|
|
|
|
context.get("foo", "memory", "callback");
|
|
|
|
done("should throw an error.");
|
|
|
|
}).catch(function () {
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-07-27 14:33:38 +02:00
|
|
|
it('should not throw an error if callback of context.get is not specified', function (done) {
|
2018-07-19 05:58:42 +02:00
|
|
|
Context.init({ contextStorage: memoryStorage });
|
|
|
|
Context.load().then(function () {
|
2020-03-28 00:47:12 +01:00
|
|
|
var flowContext = Context.getFlowContext("flow");
|
2018-07-19 05:58:42 +02:00
|
|
|
var context = Context.get("1", "flow");
|
|
|
|
context.get("foo", "memory");
|
|
|
|
done();
|
2018-07-27 14:33:38 +02:00
|
|
|
}).catch(done);
|
2018-07-19 05:58:42 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should throw an error if callback of context.set is not a function', function (done) {
|
|
|
|
Context.init({ contextStorage: memoryStorage });
|
|
|
|
Context.load().then(function () {
|
2020-03-28 00:47:12 +01:00
|
|
|
var flowContext = Context.getFlowContext("flow");
|
2018-07-19 05:58:42 +02:00
|
|
|
var context = Context.get("1", "flow");
|
|
|
|
context.set("foo", "bar", "memory", "callback");
|
|
|
|
done("should throw an error.");
|
|
|
|
}).catch(function () {
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should not throw an error if callback of context.set is not specified', function (done) {
|
|
|
|
Context.init({ contextStorage: memoryStorage });
|
|
|
|
Context.load().then(function () {
|
2020-03-28 00:47:12 +01:00
|
|
|
var flowContext = Context.getFlowContext("flow");
|
2018-07-19 05:58:42 +02:00
|
|
|
var context = Context.get("1", "flow");
|
|
|
|
context.set("foo", "bar", "memory");
|
|
|
|
done();
|
|
|
|
}).catch(done);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should throw an error if callback of context.keys is not a function', function (done) {
|
|
|
|
Context.init({ contextStorage: memoryStorage });
|
|
|
|
Context.load().then(function () {
|
2020-03-28 00:47:12 +01:00
|
|
|
var flowContext = Context.getFlowContext("flow");
|
2018-07-19 05:58:42 +02:00
|
|
|
var context = Context.get("1", "flow");
|
|
|
|
context.keys("memory", "callback");
|
|
|
|
done("should throw an error.");
|
|
|
|
}).catch(function () {
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-07-27 14:33:38 +02:00
|
|
|
it('should not throw an error if callback of context.keys is not specified', function (done) {
|
2018-07-19 05:58:42 +02:00
|
|
|
Context.init({ contextStorage: memoryStorage });
|
|
|
|
Context.load().then(function () {
|
2020-03-28 00:47:12 +01:00
|
|
|
var flowContext = Context.getFlowContext("flow");
|
2018-07-19 05:58:42 +02:00
|
|
|
var context = Context.get("1", "flow");
|
|
|
|
context.keys("memory");
|
|
|
|
done();
|
2018-07-27 14:33:38 +02:00
|
|
|
}).catch(done);
|
2018-07-19 05:58:42 +02:00
|
|
|
});
|
2021-05-26 14:04:09 +02:00
|
|
|
it('should throw an error in context.get if key is empty string', function (done) {
|
|
|
|
Context.init({ contextStorage: memoryStorage });
|
|
|
|
Context.load().then(function () {
|
|
|
|
var context = Context.get("1", "flow");
|
|
|
|
context.get("");
|
|
|
|
done("should throw an error.");
|
|
|
|
}).catch(function () {
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
it('should throw an error in context.get if key is an object', function (done) {
|
|
|
|
Context.init({ contextStorage: memoryStorage });
|
|
|
|
Context.load().then(function () {
|
|
|
|
var context = Context.get("1", "flow");
|
|
|
|
context.get({});
|
|
|
|
done("should throw an error.");
|
|
|
|
}).catch(function () {
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
it('should throw an error in context.get if key is a number', function (done) {
|
|
|
|
Context.init({ contextStorage: memoryStorage });
|
|
|
|
Context.load().then(function () {
|
|
|
|
var context = Context.get("1", "flow");
|
|
|
|
context.get(1);
|
|
|
|
done("should throw an error.");
|
|
|
|
}).catch(function () {
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
it('should throw an error in context.get if key array contains an empty string', function (done) {
|
|
|
|
Context.init({ contextStorage: memoryStorage });
|
|
|
|
Context.load().then(function () {
|
|
|
|
var context = Context.get("1", "flow");
|
|
|
|
context.get(["ok1", "", "ok2"]);
|
|
|
|
done("should throw an error.");
|
|
|
|
}).catch(function () {
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
it('should throw an error in context.get if key array contains an object', function (done) {
|
|
|
|
Context.init({ contextStorage: memoryStorage });
|
|
|
|
Context.load().then(function () {
|
|
|
|
var context = Context.get("1", "flow");
|
|
|
|
context.get(["ok1", {}, "ok2"]);
|
|
|
|
done("should throw an error.");
|
|
|
|
}).catch(function () {
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
it('should throw an error in context.get if key array contains a number', function (done) {
|
|
|
|
Context.init({ contextStorage: memoryStorage });
|
|
|
|
Context.load().then(function () {
|
|
|
|
var context = Context.get("1", "flow");
|
|
|
|
context.get(["ok1", 1, "ok2"]);
|
|
|
|
done("should throw an error.");
|
|
|
|
}).catch(function () {
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should throw an error in context.set if key is empty string', function (done) {
|
|
|
|
Context.init({ contextStorage: memoryStorage });
|
|
|
|
Context.load().then(function () {
|
|
|
|
var context = Context.get("1", "flow");
|
|
|
|
context.set("", 1);
|
|
|
|
done("should throw an error.");
|
|
|
|
}).catch(function () {
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
it('should throw an error in context.set if key is an object', function (done) {
|
|
|
|
Context.init({ contextStorage: memoryStorage });
|
|
|
|
Context.load().then(function () {
|
|
|
|
var context = Context.get("1", "flow");
|
|
|
|
context.set({}, 1);
|
|
|
|
done("should throw an error.");
|
|
|
|
}).catch(function () {
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
it('should throw an error in context.set if key is a number', function (done) {
|
|
|
|
Context.init({ contextStorage: memoryStorage });
|
|
|
|
Context.load().then(function () {
|
|
|
|
var context = Context.get("1", "flow");
|
|
|
|
context.set(1, 1);
|
|
|
|
done("should throw an error.");
|
|
|
|
}).catch(function () {
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
it('should throw an error in context.set if key array contains an empty string', function (done) {
|
|
|
|
Context.init({ contextStorage: memoryStorage });
|
|
|
|
Context.load().then(function () {
|
|
|
|
var context = Context.get("1", "flow");
|
|
|
|
context.set(["ok1", "", "ok2"], 1);
|
|
|
|
done("should throw an error.");
|
|
|
|
}).catch(function () {
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
it('should throw an error in context.set if key array contains an object', function (done) {
|
|
|
|
Context.init({ contextStorage: memoryStorage });
|
|
|
|
Context.load().then(function () {
|
|
|
|
var context = Context.get("1", "flow");
|
|
|
|
context.set(["ok1", {}, "ok2"], 1);
|
|
|
|
done("should throw an error.");
|
|
|
|
}).catch(function () {
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
it('should throw an error in context.set if key array contains a number', function (done) {
|
|
|
|
Context.init({ contextStorage: memoryStorage });
|
|
|
|
Context.load().then(function () {
|
|
|
|
var context = Context.get("1", "flow");
|
|
|
|
context.set(["ok1", 1, "ok2"], 1);
|
|
|
|
done("should throw an error.");
|
|
|
|
}).catch(function () {
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2018-07-19 05:58:42 +02:00
|
|
|
|
2021-05-26 14:04:09 +02:00
|
|
|
it('should have an err set in callback for invalid key in context.get', function (done) {
|
|
|
|
Context.init({ contextStorage: memoryStorage });
|
|
|
|
Context.load().then(function () {
|
|
|
|
var context = Context.get("1", "flow");
|
|
|
|
context.get("", function(err) {
|
|
|
|
if(err) {
|
|
|
|
done();
|
|
|
|
} else {
|
|
|
|
done("should throw an error.");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}).catch(done);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should have an err set in callback for invalid key in context.set', function (done) {
|
|
|
|
Context.init({ contextStorage: memoryStorage });
|
|
|
|
Context.load().then(function () {
|
|
|
|
var context = Context.get("1", "flow");
|
|
|
|
context.set("", "value", function(err) {
|
|
|
|
if(err) {
|
|
|
|
done();
|
|
|
|
} else {
|
|
|
|
done("should throw an error.");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}).catch(done);
|
|
|
|
});
|
2018-07-19 05:58:42 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('listStores', function () {
|
|
|
|
it('should list context storages', function (done) {
|
|
|
|
Context.init({ contextStorage: contextDefaultStorage });
|
|
|
|
Context.load().then(function () {
|
|
|
|
var list = Context.listStores();
|
|
|
|
list.default.should.equal("default");
|
|
|
|
list.stores.should.eql(["default", "test"]);
|
|
|
|
done();
|
|
|
|
}).catch(done);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should list context storages without default storage', function (done) {
|
|
|
|
Context.init({ contextStorage: contextStorage });
|
|
|
|
Context.load().then(function () {
|
|
|
|
var list = Context.listStores();
|
|
|
|
list.default.should.equal("test");
|
|
|
|
list.stores.should.eql(["test"]);
|
|
|
|
done();
|
|
|
|
}).catch(done);
|
|
|
|
});
|
2018-05-30 03:24:27 +02:00
|
|
|
});
|
2018-05-30 08:23:34 +02:00
|
|
|
describe('delete context',function(){
|
2018-07-02 16:21:13 +02:00
|
|
|
it('should not call delete() when external context storage is used', function(done) {
|
2018-05-30 08:23:34 +02:00
|
|
|
Context.init({contextStorage:contextDefaultStorage});
|
2018-07-02 16:47:47 +02:00
|
|
|
Context.load().then(function(){
|
2018-05-30 08:23:34 +02:00
|
|
|
Context.get("flowA");
|
|
|
|
return Context.delete("flowA").then(function(){
|
|
|
|
stubDelete.called.should.be.false();
|
|
|
|
stubDelete2.called.should.be.false();
|
2018-07-02 16:21:13 +02:00
|
|
|
done();
|
2018-05-30 08:23:34 +02:00
|
|
|
});
|
2018-07-02 16:21:13 +02:00
|
|
|
}).catch(done);
|
2018-05-30 08:23:34 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('clean context',function(){
|
2018-07-02 16:21:13 +02:00
|
|
|
it('should call clean()', function(done) {
|
2018-05-30 08:23:34 +02:00
|
|
|
Context.init({contextStorage:contextDefaultStorage});
|
2018-07-02 16:47:47 +02:00
|
|
|
Context.load().then(function(){
|
2018-05-30 08:23:34 +02:00
|
|
|
return Context.clean({allNodes:{}}).then(function(){
|
|
|
|
stubClean.calledWithExactly([]).should.be.true();
|
|
|
|
stubClean2.calledWithExactly([]).should.be.true();
|
2018-07-02 16:21:13 +02:00
|
|
|
done();
|
2018-05-30 08:23:34 +02:00
|
|
|
});
|
2018-07-02 16:21:13 +02:00
|
|
|
}).catch(done);
|
2018-05-30 08:23:34 +02:00
|
|
|
});
|
|
|
|
});
|
2015-12-29 23:16:51 +01:00
|
|
|
});
|
2018-07-19 00:40:52 +02:00
|
|
|
|
2015-12-29 23:16:51 +01:00
|
|
|
});
|