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

Add async API to context

and add test cases for async
This commit is contained in:
HirokiUchikawa 2018-06-07 21:51:11 +09:00
parent ed1d34e678
commit 41a04a2849
4 changed files with 420 additions and 222 deletions

View File

@ -154,30 +154,45 @@ function createContext(id,seed) {
obj.get = function(key) {
var keyPath = parseKey(key);
var context = getContextStorage(keyPath.storage);
if(key === keyPath.key){
if(!keyPath.storage){
return context.get(scope, keyPath.key);
}else{
return context.getAsync(scope, keyPath.key);
throw new Error(keyPath.storage + " does not support get(). Use getAsync()");
}
};
obj.set = function(key, value) {
var keyPath = parseKey(key);
var context = getContextStorage(keyPath.storage);
if(key === keyPath.key){
if(!keyPath.storage){
return context.set(scope, keyPath.key, value);
}else{
return context.setAsync(scope, keyPath.key, value);
throw new Error(keyPath.storage + " does not support set(). Use setAsync()");
}
};
obj.keys = function(storage) {
var storageName = parseStorage(storage);
var context = getContextStorage(storageName);
if(!storage){
if(!storageName){
return context.keys(scope);
}else{
return context.keysAsync(scope);
throw new Error(storageName + " does not support keys(). Use keysAsync()");
}
};
obj.getAsync = function(key) {
var keyPath = parseKey(key);
var context = getContextStorage(keyPath.storage);
return context.getAsync(scope, keyPath.key);
};
obj.setAsync = function(key, value) {
var keyPath = parseKey(key);
var context = getContextStorage(keyPath.storage);
return context.setAsync(scope, keyPath.key, value);
};
obj.keysAsync = function(storage) {
var storageName = parseStorage(storage);
var context = getContextStorage(storageName);
return context.keysAsync(scope);
};
return obj;
}

View File

@ -16,6 +16,7 @@
var should = require("should");
var sinon = require('sinon');
var when = require("when")
var rewire = require("rewire");
var Context = require("../../../../../red/runtime/nodes/context/index");
@ -33,7 +34,7 @@ describe('context', function() {
var context1 = Context.get("1","flowA");
should.not.exist(context1.get("foo"));
context1.set("foo","test");
context1.get("foo").should.eql("test");
context1.get("foo").should.equal("test");
});
it('stores local property - creates parent properties',function() {
var context1 = Context.get("1","flowA");
@ -56,13 +57,13 @@ describe('context', function() {
var context1 = Context.get("1","flowA");
should.not.exist(context1.flow.get("foo"));
context1.flow.set("foo","test");
context1.flow.get("foo").should.eql("test");
context1.flow.get("foo").should.equal("test");
});
it('stores global property',function() {
var context1 = Context.get("1","flowA");
should.not.exist(context1.global.get("foo"));
context1.global.set("foo","test");
context1.global.get("foo").should.eql("test");
context1.global.get("foo").should.equal("test");
});
it('keeps local context local', function() {
@ -73,7 +74,7 @@ describe('context', function() {
should.not.exist(context2.get("foo"));
context1.set("foo","test");
context1.get("foo").should.eql("test");
context1.get("foo").should.equal("test");
should.not.exist(context2.get("foo"));
});
it('flow context accessible to all flow nodes', function() {
@ -84,8 +85,8 @@ describe('context', function() {
should.not.exist(context2.flow.get("foo"));
context1.flow.set("foo","test");
context1.flow.get("foo").should.eql("test");
context2.flow.get("foo").should.eql("test");
context1.flow.get("foo").should.equal("test");
context2.flow.get("foo").should.equal("test");
});
it('flow context not shared to nodes on other flows', function() {
@ -96,7 +97,7 @@ describe('context', function() {
should.not.exist(context2.flow.get("foo"));
context1.flow.set("foo","test");
context1.flow.get("foo").should.eql("test");
context1.flow.get("foo").should.equal("test");
should.not.exist(context2.flow.get("foo"));
});
@ -108,15 +109,15 @@ describe('context', function() {
should.not.exist(context2.global.get("foo"));
context1.global.set("foo","test");
context1.global.get("foo").should.eql("test");
context2.global.get("foo").should.eql("test");
context1.global.get("foo").should.equal("test");
context2.global.get("foo").should.equal("test");
});
it('deletes context',function() {
var context = Context.get("1","flowA");
should.not.exist(context.get("foo"));
context.set("foo","abc");
context.get("foo").should.eql("abc");
context.get("foo").should.equal("abc");
return Context.delete("1","flowA").then(function(){
context = Context.get("1","flowA");
@ -134,12 +135,12 @@ describe('context', function() {
context.set("foo","bar");
keys = context.keys();
keys.should.have.length(1);
keys[0].should.eql("foo");
keys[0].should.equal("foo");
context.set("abc.def","bar");
keys = context.keys();
keys.should.have.length(2);
keys[1].should.eql("abc");
keys[1].should.equal("abc");
});
it('should enumerate only context keys when GlobalContext was given', function() {
@ -148,42 +149,45 @@ describe('context', function() {
var context = Context.get("1","flowA");
var keys = context.global.keys("global");
keys.should.have.length(1);
keys[0].should.eql("foo");
keys[0].should.equal("foo");
});
});
it('should store data on memory when contextStorage is not defined', function() {
it('should throw error when persistable key is passed', function() {
var context = Context.get("1","flow");
context.set("#nonexist.key1", "val1");
context.get("#nonexist.key1").should.eql("val1");
context.flow.set("#nonexist.key2", "val2");
context.flow.get("#nonexist.key2").should.eql("val2");
context.global.set("#nonexist.key1", "val3");
context.global.get("#nonexist.key1").should.eql("val3");
(function() {
context.set("#nonexist.key1", "val1");
}).should.throw();
(function() {
context.get("#nonexist.key1");
}).should.throw();
(function() {
context.keys("#nonexist");
}).should.throw();
});
});
describe('external context storage',function() {
var sandbox = sinon.sandbox.create();
var stubGet = sandbox.stub();
var stubSet = sandbox.stub();
var stubKeys = sandbox.stub();
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());
var stubGet2 = sandbox.stub();
var stubSet2 = sandbox.stub();
var stubKeys2 = sandbox.stub();
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());
var stubGetAsync = sandbox.stub().returns(when.resolve());
var stubSetAsync = sandbox.stub().returns(when.resolve());
var stubKeysAsync = sandbox.stub().returns(when.resolve());
var stubDelete = sandbox.stub().returns(when.resolve());
var stubClean = sandbox.stub().returns(when.resolve());
var stubOpen = sandbox.stub().returns(when.resolve());
var stubClose = sandbox.stub().returns(when.resolve());
var stubGetAsync2 = sandbox.stub().returns(when.resolve());
var stubSetAsync2 = sandbox.stub().returns(when.resolve());
var stubKeysAsync2 = sandbox.stub().returns(when.resolve());
var stubDelete2 = sandbox.stub().returns(when.resolve());
var stubClean2 = sandbox.stub().returns(when.resolve());
var stubOpen2 = sandbox.stub().returns(when.resolve());
var stubClose2 = sandbox.stub().returns(when.resolve());
var testPlugin = function(config){
function Test(){}
Test.prototype.get = stubGet;
Test.prototype.set = stubSet;
Test.prototype.keys = stubKeys;
Test.prototype.getAsync = stubGetAsync;
Test.prototype.setAsync = stubSetAsync;
Test.prototype.keysAsync = stubKeysAsync;
Test.prototype.delete = stubDelete;
Test.prototype.clean = stubClean;
Test.prototype.open = stubOpen;
@ -192,9 +196,9 @@ describe('context', function() {
};
var testPlugin2 = function(config){
function Test2(){}
Test2.prototype.get = stubGet2;
Test2.prototype.set = stubSet2;
Test2.prototype.keys = stubKeys2;
Test2.prototype.getAsync = stubGetAsync2;
Test2.prototype.setAsync = stubSetAsync2;
Test2.prototype.keysAsync = stubKeysAsync2;
Test2.prototype.delete = stubDelete2;
Test2.prototype.clean = stubClean2;
Test2.prototype.open = stubOpen2;
@ -251,24 +255,32 @@ describe('context', function() {
});
return Context.load().then(function(){
var context = Context.get("1","flow");
context.set("##%&.sign","sign1");
context.get("##%&.sign").should.eql("sign1");
context.set("#\u3042.file2","file2");
context.get("#\u3042.file2").should.eql("file2");
context.set("#1.num","num3");
context.get("#1.num").should.eql("num3");
return when.all([
context.setAsync("##%&.sign","sign1").then(function(){
return context.getAsync("##%&.sign").should.finally.equal("sign1");
}),
context.setAsync("#\u3042.file2","file2").then(function(){
return context.getAsync("#\u3042.file2").should.finally.equal("file2");
}),
context.setAsync("#1.num","num3").then(function(){
return context.getAsync("#1.num").should.finally.equal("num3");
})
]);
});
});
it('should ignore reserved storage name `_`', function() {
Context.init({contextStorage:{_:{module:testPlugin}}});
return Context.load().then(function(){
var context = Context.get("1","flow");
context.set("#_.foo","bar");
context.get("#_.foo");
context.keys("#_");
stubSet.called.should.be.false();
stubGet.called.should.be.false();
stubKeys.called.should.be.false();
return when.all([
context.setAsync("#_.foo","bar"),
context.getAsync("#_.foo"),
context.keysAsync("#_")
]).then(function(){
stubSetAsync.called.should.be.false();
stubGetAsync.called.should.be.false();
stubKeysAsync.called.should.be.false();
});
});
});
it('should fail when using invalid default context', function(done) {
@ -314,94 +326,106 @@ describe('context', function() {
Context.init({contextStorage:contextStorage});
return Context.load().then(function(){
var context = Context.get("1","flow");
should.not.exist(context.get("#test.foo"));
context.set("#test.foo","test");
context.get("#test.foo");
context.keys("#test");
stubSet.calledWithExactly("1:flow","foo","test").should.be.true();
stubGet.calledWithExactly("1:flow","foo").should.be.true();
stubKeys.calledWithExactly("1:flow").should.be.true();
return when.all([
context.setAsync("#test.foo","test"),
context.getAsync("#test.foo"),
context.keysAsync("#test")
]).then(function(){
stubSetAsync.calledWithExactly("1:flow","foo","test").should.be.true();
stubGetAsync.calledWithExactly("1:flow","foo").should.be.true();
stubKeysAsync.calledWithExactly("1:flow").should.be.true();
});
});
});
it('should store flow property to external context storage',function() {
Context.init({contextStorage:contextStorage});
return Context.load().then(function(){
var context = Context.get("1","flow");
should.not.exist(context.flow.get("#test.foo"));
context.flow.set("#test.foo","test");
context.flow.get("#test.foo");
context.flow.keys("#test");
stubSet.calledWithExactly("flow","foo","test").should.be.true();
stubGet.calledWithExactly("flow","foo").should.be.true();
stubKeys.calledWithExactly("flow").should.be.true();
return when.all([
context.flow.setAsync("#test.foo","test"),
context.flow.getAsync("#test.foo"),
context.flow.keysAsync("#test")
]).then(function(){
stubSetAsync.calledWithExactly("flow","foo","test").should.be.true();
stubGetAsync.calledWithExactly("flow","foo").should.be.true();
stubKeysAsync.calledWithExactly("flow").should.be.true();
});
});
});
it('should store global property to external context storage',function() {
Context.init({contextStorage:contextStorage});
return Context.load().then(function(){
var context = Context.get("1","flow");
should.not.exist(context.global.get("#test.foo"));
context.global.set("#test.foo","test");
context.global.get("#test.foo");
context.global.keys("#test");
stubSet.calledWithExactly("global","foo","test").should.be.true();
stubGet.calledWithExactly("global","foo").should.be.true();
stubKeys.calledWithExactly("global").should.be.true();
return when.all([
context.global.setAsync("#test.foo","test"),
context.global.getAsync("#test.foo"),
context.global.keysAsync("#test")
]).then(function(){
stubSetAsync.calledWithExactly("global","foo","test").should.be.true();
stubGetAsync.calledWithExactly("global","foo").should.be.true();
stubKeysAsync.calledWithExactly("global").should.be.true();
});
});
});
it('should store data to the default context when non-existent context storage was specified', function() {
Context.init({contextStorage:contextDefaultStorage});
return Context.load().then(function(){
var context = Context.get("1","flow");
should.not.exist(context.get("#nonexist.foo"));
context.set("#nonexist.foo","test");
context.get("#nonexist.foo");
context.keys("#nonexist");
stubGet.called.should.be.false();
stubSet.called.should.be.false();
stubKeys.called.should.be.false();
stubSet2.calledWithExactly("1:flow","foo","test").should.be.true();
stubGet2.calledWithExactly("1:flow","foo").should.be.true();
stubKeys2.calledWithExactly("1:flow").should.be.true();
return when.all([
context.setAsync("#nonexist.foo","test"),
context.getAsync("#nonexist.foo"),
context.keysAsync("#nonexist")
]).then(function(){
stubGetAsync.called.should.be.false();
stubSetAsync.called.should.be.false();
stubKeysAsync.called.should.be.false();
stubSetAsync2.calledWithExactly("1:flow","foo","test").should.be.true();
stubGetAsync2.calledWithExactly("1:flow","foo").should.be.true();
stubKeysAsync2.calledWithExactly("1:flow").should.be.true();
});
});
});
it('should use the default context', function() {
Context.init({contextStorage:contextDefaultStorage});
return Context.load().then(function(){
var context = Context.get("1","flow");
should.not.exist(context.get("#default.foo"));
context.set("#default.foo","default");
context.get("#default.foo");
context.keys("#default");
stubGet.called.should.be.false();
stubSet.called.should.be.false();
stubKeys.called.should.be.false();
stubSet2.calledWithExactly("1:flow","foo","default").should.be.true();
stubGet2.calledWithExactly("1:flow","foo").should.be.true();
stubKeys2.calledWithExactly("1:flow").should.be.true();
return when.all([
context.setAsync("#default.foo","default"),
context.getAsync("#default.foo"),
context.keysAsync("#default")
]).then(function(){
stubGetAsync.called.should.be.false();
stubSetAsync.called.should.be.false();
stubKeysAsync.called.should.be.false();
stubSetAsync2.calledWithExactly("1:flow","foo","default").should.be.true();
stubGetAsync2.calledWithExactly("1:flow","foo").should.be.true();
stubKeysAsync2.calledWithExactly("1:flow").should.be.true();
});
});
});
it('should use the alias of default context', function() {
Context.init({contextStorage:contextDefaultStorage});
return Context.load().then(function(){
var context = Context.get("1","flow");
should.not.exist(context.get("#.foo"));
context.set("#.foo","alias");
context.get("#.foo");
context.keys("#");
stubGet.called.should.be.false();
stubSet.called.should.be.false();
stubKeys.called.should.be.false();
stubSet2.calledWithExactly("1:flow","foo","alias").should.be.true();
stubGet2.calledWithExactly("1:flow","foo").should.be.true();
stubKeys2.calledWithExactly("1:flow").should.be.true();
return when.all([
context.setAsync("#.foo","alias"),
context.getAsync("#.foo"),
context.keysAsync("#")
]).then(function(){
stubGetAsync.called.should.be.false();
stubSetAsync.called.should.be.false();
stubKeysAsync.called.should.be.false();
stubSetAsync2.calledWithExactly("1:flow","foo","alias").should.be.true();
stubGetAsync2.calledWithExactly("1:flow","foo").should.be.true();
stubKeysAsync2.calledWithExactly("1:flow").should.be.true();
});
});
});
it('should throw an error using undefined storage for local context', function(done) {
Context.init({contextStorage:contextStorage});
Context.load().then(function(){
var context = Context.get("1","flow");
context.get("#nonexist.local");
context.getAsync("#nonexist.local");
should.fail(null, null, "An error was not thrown using undefined storage for local context");
}).catch(function(err) {
if (err.name === "ContextError") {
@ -415,7 +439,7 @@ describe('context', function() {
Context.init({contextStorage:contextStorage});
Context.load().then(function(){
var context = Context.get("1","flow");
context.flow.set("#nonexist.flow");
context.flow.setAsync("#nonexist.flow");
should.fail(null, null, "An error was not thrown using undefined storage for flow context");
}).catch(function(err) {
if (err.name === "ContextError") {
@ -464,32 +488,36 @@ describe('context', function() {
return Context.close();
});
it('should work correctly with the valid key name',function() {
context.set("#memory.azAZ09#_","valid");
context.get("#memory.azAZ09#_").should.eql("valid");
context.set("#memory.a.b","ab");
context.get("#memory.a.b").should.eql("ab");
return when.all([
context.setAsync("#memory.azAZ09#_","valid"),
context.setAsync("#memory.a.b","ab")
]).then(function(){
context.getAsync("#memory.azAZ09#_").should.finally.equal("valid");
context.getAsync("#memory.a.b").should.finally.equal("ab");
});
});
it('should treat the key name without dot as a normal context',function() {
context.set("#memory","normal");
context.get("#memory").should.eql("normal");
return context.setAsync("#memory","normal").then(function(){
return context.getAsync("#memory").should.finally.equal("normal");
});
});
it('should fail when specifying invalid characters',function() {
(function() {
context.set("#memory.a.-","invalid1");
context.setAsync("#memory.a.-","invalid1");
}).should.throw();
(function() {
context.set("#memory.'abc","invalid2");
context.setAsync("#memory.'abc","invalid2");
}).should.throw();
});
it('should fail when specifying unnecesary space characters for key name',function() {
(function() {
context.set("# memory.space","space1");
context.setAsync("# memory.space","space1");
}).should.throw();
(function() {
context.set("#memory .space","space2");
context.setAsync("#memory .space","space2");
}).should.throw();
(function() {
context.set("#memory. space","space3");
context.setAsync("#memory. space","space3");
}).should.throw();
});
});
@ -500,8 +528,8 @@ describe('context', function() {
function returnModuleAndKey(input, expectedModule, expectedKey) {
var result = parseKey(input);
result.storage.should.eql(expectedModule);
result.key.should.eql(expectedKey);
result.storage.should.equal(expectedModule);
result.key.should.equal(expectedKey);
}
it('should return module and key', function() {

View File

@ -38,120 +38,275 @@ describe('localfilesystem',function() {
});
});
describe('#get/set',function() {
describe('#getAsync/setAsync',function() {
it('should store property',function() {
should.not.exist(context.get("nodeX","foo"));
context.set("nodeX","foo","test");
context.get("nodeX","foo").should.eql("test");
return context.getAsync("nodeX","foo").should.be.finally.undefined()
.then(function(){
return context.setAsync("nodeX","foo","test");
}).then(function(){
return context.getAsync("nodeX","foo").should.be.finally.equal("test");
});
});
it('should store property - creates parent properties',function() {
context.set("nodeX","foo.bar","test");
context.get("nodeX","foo").should.eql({bar:"test"});
return context.setAsync("nodeX","foo.bar","test").then(function(){
return context.getAsync("nodeX","foo").should.be.finally.eql({bar:"test"});
});
});
it('should delete property',function() {
context.set("nodeX","foo.abc.bar1","test1");
context.set("nodeX","foo.abc.bar2","test2");
context.get("nodeX","foo.abc").should.eql({bar1:"test1",bar2:"test2"});
context.set("nodeX","foo.abc.bar1",undefined);
context.get("nodeX","foo.abc").should.eql({bar2:"test2"});
context.set("nodeX","foo.abc",undefined);
should.not.exist(context.get("nodeX","foo.abc"));
context.set("nodeX","foo",undefined);
should.not.exist(context.get("nodeX","foo"));
return context.setAsync("nodeX","foo.abc.bar1","test1")
.then(function(){
return context.setAsync("nodeX","foo.abc.bar2","test2")
}).then(function(){
return context.getAsync("nodeX","foo.abc").should.be.finally.eql({bar1:"test1",bar2:"test2"});
}).then(function(){
return context.setAsync("nodeX","foo.abc.bar1",undefined).then(function(){
return context.getAsync("nodeX","foo.abc").should.be.finally.eql({bar2:"test2"});
});
}).then(function(){
return context.setAsync("nodeX","foo.abc",undefined).then(function(){
return context.getAsync("nodeX","foo.abc").should.be.finally.undefined();
});
}).then(function(){
return context.setAsync("nodeX","foo",undefined).then(function(){
return context.getAsync("nodeX","foo").should.be.finally.undefined();
});
});
});
it('should not shared context with other scope', function() {
should.not.exist(context.get("nodeX","foo"));
should.not.exist(context.get("nodeY","foo"));
context.set("nodeX","foo","testX");
context.set("nodeY","foo","testY");
return when.all([context.getAsync("nodeX","foo").should.be.finally.undefined(),
context.getAsync("nodeY","foo").should.be.finally.undefined()
]).then(function(){
return when.all([context.setAsync("nodeX","foo","testX"),
context.setAsync("nodeY","foo","testY")])
}).then(function(){
return when.all([context.getAsync("nodeX","foo").should.be.finally.equal("testX"),
context.getAsync("nodeY","foo").should.be.finally.equal("testY")]);
});
});
context.get("nodeX","foo").should.eql("testX");
context.get("nodeY","foo").should.eql("testY");
it('should store string',function() {
return context.getAsync("nodeX","foo").should.be.finally.undefined()
.then(function(){
return context.setAsync("nodeX","foo","bar");
}).then(function(){
return context.getAsync("nodeX","foo")
}).then(function(result){
result.should.be.String();
result.should.be.equal("bar");
}).then(function(){
return context.setAsync("nodeX","foo","1");
}).then(function(){
return context.getAsync("nodeX","foo")
}).then(function(result){
result.should.be.String();
result.should.be.equal("1");
});
});
it('should store number',function() {
return context.getAsync("nodeX","foo").should.be.finally.undefined()
.then(function(){
return context.setAsync("nodeX","foo",1);
}).then(function(){
return context.getAsync("nodeX","foo")
}).then(function(result){
result.should.be.Number();
result.should.be.equal(1);
});
});
it('should store null',function() {
return context.getAsync("nodeX","foo").should.be.finally.undefined()
.then(function(){
return context.setAsync("nodeX","foo",null);
}).then(function(){
return context.getAsync("nodeX","foo").should.be.finally.null();
});
});
it('should store boolean',function() {
return context.getAsync("nodeX","foo").should.be.finally.undefined()
.then(function(){
return context.setAsync("nodeX","foo",true);
}).then(function(){
return context.getAsync("nodeX","foo").should.be.finally.Boolean().and.true();
}).then(function(){
return context.setAsync("nodeX","foo",false);
}).then(function(){
return context.getAsync("nodeX","foo").should.be.finally.Boolean().and.false();
});
});
it('should store object',function() {
return context.getAsync("nodeX","foo").should.be.finally.undefined()
.then(function(){
return context.setAsync("nodeX","foo",{obj:"bar"});
}).then(function(){
return context.getAsync("nodeX","foo")
}).then(function(result){
result.should.be.Object();
result.should.eql({obj:"bar"});
});
});
it('should store array',function() {
return context.getAsync("nodeX","foo").should.be.finally.undefined()
.then(function(){
return context.setAsync("nodeX","foo",["a","b","c"]);
}).then(function(){
return context.getAsync("nodeX","foo")
}).then(function(result){
result.should.be.Array();
result.should.eql(["a","b","c"]);
}).then(function(){
return context.getAsync("nodeX","foo[1]")
}).then(function(result){
result.should.be.String();
result.should.equal("b");
});
});
it('should store array of arrays',function() {
return context.getAsync("nodeX","foo").should.be.finally.undefined()
.then(function(){
return context.setAsync("nodeX","foo",[["a","b","c"],[1,2,3,4],[true,false]]);
}).then(function(){
return context.getAsync("nodeX","foo")
}).then(function(result){
result.should.be.Array();
result.should.have.length(3);
result[0].should.have.length(3);
result[1].should.have.length(4);
result[2].should.have.length(2);
}).then(function(){
return context.getAsync("nodeX","foo[1]")
}).then(function(result){
result.should.be.Array();
result.should.have.length(4);
result.should.be.eql([1,2,3,4]);
});
});
it('should store array of objects',function() {
return context.getAsync("nodeX","foo").should.be.finally.undefined()
.then(function(){
return context.setAsync("nodeX","foo",[{obj:"bar1"},{obj:"bar2"},{obj:"bar3"}]);
}).then(function(){
return context.getAsync("nodeX","foo")
}).then(function(result){
result.should.be.Array();
result.should.have.length(3);
result[0].should.be.Object();
result[1].should.be.Object();
result[2].should.be.Object();
}).then(function(){
return context.getAsync("nodeX","foo[1]")
}).then(function(result){
result.should.be.Object();
result.should.be.eql({obj:"bar2"});
});
});
});
describe('#keys',function() {
describe('#keysAsync',function() {
it('should enumerate context keys', function() {
var keys = context.keys("nodeX");
keys.should.be.an.Array();
keys.should.be.empty();
context.set("nodeX","foo","bar");
keys = context.keys("nodeX");
keys.should.have.length(1);
keys[0].should.eql("foo");
context.set("nodeX","abc.def","bar");
keys = context.keys("nodeX");
keys.should.have.length(2);
keys[1].should.eql("abc");
return context.keysAsync("nodeX").then(function(result){
result.should.be.an.Array();
result.should.be.empty();
}).then(function(){
return context.setAsync("nodeX","foo","bar");
}).then(function(){
return context.keysAsync("nodeX").then(function(result){
result.should.have.length(1);
result[0].should.equal("foo");
});
}).then(function(){
return context.setAsync("nodeX","abc.def","bar");
}).then(function(){
return context.keysAsync("nodeX").then(function(result){
result.should.have.length(2);
result[1].should.equal("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("nodeX","foo","bar");
context.set("nodeY","hoge","piyo");
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");
return when.all([context.keysAsync("nodeX"),
context.keysAsync("nodeY")
]).then(function(results){
results[0].should.be.an.Array();
results[0].should.be.empty();
results[1].should.be.an.Array();
results[1].should.be.empty();
}).then(function(){
return when.all([context.setAsync("nodeX","foo","bar"),
context.setAsync("nodeY","hoge","piyo")]);
}).then(function(){
return when.all([context.keysAsync("nodeX"),
context.keysAsync("nodeY")]);
}).then(function(results){
results[0].should.have.length(1);
results[0][0].should.equal("foo");
results[1].should.have.length(1);
results[1][0].should.equal("hoge");
});
});
});
describe('#delete',function() {
it('should delete context',function() {
should.not.exist(context.get("nodeX","foo"));
should.not.exist(context.get("nodeY","foo"));
context.set("nodeX","foo","abc");
context.set("nodeY","foo","abc");
context.get("nodeX","foo").should.eql("abc");
context.get("nodeY","foo").should.eql("abc");
return context.delete("nodeX").then(function(){
should.not.exist(context.get("nodeX","foo"));
should.exist(context.get("nodeY","foo"));
})
return when.all([context.getAsync("nodeX","foo").should.be.finally.undefined(),
context.getAsync("nodeY","foo").should.be.finally.undefined()
]).then(function(){
return when.all([context.setAsync("nodeX","foo","abc"),
context.setAsync("nodeY","foo","abc")]);
}).then(function(){
return when.all([context.getAsync("nodeX","foo").should.be.finally.equal("abc"),
context.getAsync("nodeY","foo").should.be.finally.equal("abc")])
}).then(function(){
return context.delete("nodeX");
}).then(function(){
return when.all([context.getAsync("nodeX","foo").should.be.finally.undefined(),
context.getAsync("nodeY","foo").should.be.finally.equal("abc")]);
});
});
});
describe('#clean',function() {
it('should clean unnecessary context',function() {
should.not.exist(context.get("nodeX","foo"));
should.not.exist(context.get("nodeY","foo"));
context.set("nodeX","foo","abc");
context.set("nodeY","foo","abc");
context.get("nodeX","foo").should.eql("abc");
context.get("nodeY","foo").should.eql("abc");
return context.clean([]).then(function(){
should.not.exist(context.get("nodeX","foo"));
should.not.exist(context.get("nodeY","foo"));
return when.all([context.getAsync("nodeX","foo").should.be.finally.undefined(),
context.getAsync("nodeY","foo").should.be.finally.undefined()
]).then(function(values){
return when.all([context.setAsync("nodeX","foo","abc"),
context.setAsync("nodeY","foo","abc")]);
}).then(function(){
return when.all([context.getAsync("nodeX","foo").should.be.finally.equal("abc"),
context.getAsync("nodeY","foo").should.be.finally.equal("abc")])
}).then(function(){
return context.clean([]);
}).then(function(){
return when.all([context.getAsync("nodeX","foo").should.be.finally.undefined(),
context.getAsync("nodeY","foo").should.be.finally.undefined()]);
});
});
it('should not clean active context',function() {
should.not.exist(context.get("nodeX","foo"));
should.not.exist(context.get("nodeY","foo"));
context.set("nodeX","foo","abc");
context.set("nodeY","foo","abc");
context.get("nodeX","foo").should.eql("abc");
context.get("nodeY","foo").should.eql("abc");
return context.clean(["nodeX"]).then(function(){
should.exist(context.get("nodeX","foo"));
should.not.exist(context.get("nodeY","foo"));
it('should not clean active context',function() {
return when.all([context.getAsync("nodeX","foo").should.be.finally.undefined(),
context.getAsync("nodeY","foo").should.be.finally.undefined()
]).then(function(){
return when.all([context.setAsync("nodeX","foo","abc"),
context.setAsync("nodeY","foo","abc")]);
}).then(function(){
return when.all([context.getAsync("nodeX","foo").should.be.finally.equal("abc"),
context.getAsync("nodeY","foo").should.be.finally.equal("abc")])
}).then(function(){
return context.clean(["nodeX"]);
}).then(function(){
return when.all([context.getAsync("nodeX","foo").should.be.finally.equal("abc"),
context.getAsync("nodeY","foo").should.be.finally.undefined()]);
});
});
});

View File

@ -35,7 +35,7 @@ describe('memory',function() {
it('should store property',function() {
should.not.exist(context.get("nodeX","foo"));
context.set("nodeX","foo","test");
context.get("nodeX","foo").should.eql("test");
context.get("nodeX","foo").should.equal("test");
});
it('should store property - creates parent properties',function() {
@ -61,8 +61,8 @@ describe('memory',function() {
context.set("nodeX","foo","testX");
context.set("nodeY","foo","testY");
context.get("nodeX","foo").should.eql("testX");
context.get("nodeY","foo").should.eql("testY");
context.get("nodeX","foo").should.equal("testX");
context.get("nodeY","foo").should.equal("testY");
});
});
@ -75,12 +75,12 @@ describe('memory',function() {
context.set("nodeX","foo","bar");
keys = context.keys("nodeX");
keys.should.have.length(1);
keys[0].should.eql("foo");
keys[0].should.equal("foo");
context.set("nodeX","abc.def","bar");
keys = context.keys("nodeX");
keys.should.have.length(2);
keys[1].should.eql("abc");
keys[1].should.equal("abc");
});
it('should enumerate context keys in each scopes', function() {
@ -96,11 +96,11 @@ describe('memory',function() {
context.set("nodeY","hoge","piyo");
keysX = context.keys("nodeX");
keysX.should.have.length(1);
keysX[0].should.eql("foo");
keysX[0].should.equal("foo");
keysY = context.keys("nodeY");
keysY.should.have.length(1);
keysY[0].should.eql("hoge");
keysY[0].should.equal("hoge");
});
it('should enumerate only context keys when GlobalContext was given', function() {
@ -114,7 +114,7 @@ describe('memory',function() {
context.setGlobalContext(data);
keys = context.keys("global");
keys.should.have.length(1);
keys[0].should.eql("foo");
keys[0].should.equal("foo");
});
});
@ -124,8 +124,8 @@ describe('memory',function() {
should.not.exist(context.get("nodeY","foo"));
context.set("nodeX","foo","abc");
context.set("nodeY","foo","abc");
context.get("nodeX","foo").should.eql("abc");
context.get("nodeY","foo").should.eql("abc");
context.get("nodeX","foo").should.equal("abc");
context.get("nodeY","foo").should.equal("abc");
return context.delete("nodeX").then(function(){
should.not.exist(context.get("nodeX","foo"));
@ -140,8 +140,8 @@ describe('memory',function() {
should.not.exist(context.get("nodeY","foo"));
context.set("nodeX","foo","abc");
context.set("nodeY","foo","abc");
context.get("nodeX","foo").should.eql("abc");
context.get("nodeY","foo").should.eql("abc");
context.get("nodeX","foo").should.equal("abc");
context.get("nodeY","foo").should.equal("abc");
return context.clean([]).then(function(){
should.not.exist(context.get("nodeX","foo"));
@ -153,8 +153,8 @@ describe('memory',function() {
should.not.exist(context.get("nodeY","foo"));
context.set("nodeX","foo","abc");
context.set("nodeY","foo","abc");
context.get("nodeX","foo").should.eql("abc");
context.get("nodeY","foo").should.eql("abc");
context.get("nodeX","foo").should.equal("abc");
context.get("nodeY","foo").should.equal("abc");
return context.clean(["nodeX"]).then(function(){
should.exist(context.get("nodeX","foo"));
@ -173,7 +173,7 @@ describe('memory',function() {
foo: "bar"
}
context.setGlobalContext(data);
context.get("global","foo").should.eql("bar");
context.get("global","foo").should.equal("bar");
});
});
});