Use the callback instead of Promise in context API

and remove unnecessary functions
This commit is contained in:
HirokiUchikawa 2018-06-22 17:11:54 +09:00
parent fd67d08402
commit f2fa26fb07
6 changed files with 449 additions and 497 deletions

View File

@ -161,9 +161,6 @@
"error-module-not-loaded": "'__module__' could not be loaded",
"error-module-not-defined": "'module' is not defined in '__storage__' of settings.contextStorage",
"error-invalid-default-module": "Invalid storage '__storage__' is specified as a default storage",
"error-key-zero-length": "Invalid property expression: zero-length",
"error-unexpected-space-character": "Invalid property expression: unexpected ' ' at position __index__",
"error-empty-key": "Invalid property expression: key is empty",
"error-use-undefined-storage": "Undefined storage '__storage__' is specified"
}

View File

@ -16,6 +16,7 @@
var clone = require("clone");
var log = require("../../log");
var memory = require("./memory");
var settings;
var contexts = {};
@ -28,7 +29,6 @@ function init(_settings) {
externalContexts = {};
// init memory plugin
var memory = require("./memory");
var seed = settings.functionGlobalContext || {};
externalContexts["_"] = memory();
externalContexts["_"].setGlobalContext(seed);
@ -95,43 +95,6 @@ function copySettings(config, settings){
});
}
function parseStorage(key) {
if (!key || key.charAt(0) !== '#') {
return "";
} else {
var endOfStorageName = key.indexOf(".");
if (endOfStorageName == -1) {
endOfStorageName = key.length;
}
return key.substring(1,endOfStorageName)||"default";
}
}
function parseKey(key) {
if (!key) {
throw new Error(log._("context.error-key-zero-length"));
}
var indexSpace = key.indexOf(" ");
if (indexSpace != -1) {
throw new Error(log._("context.error-unexpected-space-character", {index:indexSpace}));
}
var keyPath = { storage: "", key: "" };
var indexDot = key.indexOf(".");
// The key of "#file" should be treated as a key without persistable context.
if (indexDot != -1) {
keyPath.storage = parseStorage(key);
}
if (keyPath.storage) {
keyPath.key = key.substring(indexDot + 1);
} else {
keyPath.key = key;
}
if(!keyPath.key) {
throw new Error(log._("context.error-empty-key"));
}
return keyPath;
}
function getContextStorage(storage) {
if (noContextStorage || !storage) {
return externalContexts["_"];
@ -150,47 +113,30 @@ function createContext(id,seed) {
var scope = id;
var obj = seed || {};
obj.get = function(key) {
var keyPath = parseKey(key);
var context = getContextStorage(keyPath.storage);
if(!keyPath.storage){
return context.get(scope, keyPath.key);
}else{
throw new Error(keyPath.storage + " does not support get(). Use getAsync()");
obj.get = function(key, storage, callback) {
if (typeof storage === 'function') {
callback = storage;
storage = "default";
} else if(typeof storage === "string" && typeof callback !== 'function'){
throw new Error("Callback must be a function");
}
return getContextStorage(storage).get(scope, key, callback);
};
obj.set = function(key, value) {
var keyPath = parseKey(key);
var context = getContextStorage(keyPath.storage);
if(!keyPath.storage){
return context.set(scope, keyPath.key, value);
}else{
throw new Error(keyPath.storage + " does not support set(). Use setAsync()");
obj.set = function(key, value, storage, callback) {
if (typeof storage === 'function') {
callback = storage;
storage = "default";
}
getContextStorage(storage).set(scope, key, value, callback);
};
obj.keys = function(storage) {
var storageName = parseStorage(storage);
var context = getContextStorage(storageName);
if(!storageName){
return context.keys(scope);
}else{
throw new Error(storageName + " does not support keys(). Use keysAsync()");
obj.keys = function(storage, callback) {
if (typeof storage === 'function') {
callback = storage;
storage = "default";
} else if(typeof storage === "string" && typeof callback !== 'function'){
throw new Error("Callback must be a function");
}
};
obj.getAsync = function(key) {
var keyPath = parseKey(key);
var context = getContextStorage(keyPath.storage);
return context.get(scope, keyPath.key);
};
obj.setAsync = function(key, value) {
var keyPath = parseKey(key);
var context = getContextStorage(keyPath.storage);
return context.set(scope, keyPath.key, value);
};
obj.keysAsync = function(storage) {
var storageName = parseStorage(storage);
var context = getContextStorage(storageName);
return context.keys(scope);
return getContextStorage(storage).keys(scope, callback);
};
return obj;
}

View File

@ -86,43 +86,52 @@ LocalFileSystem.prototype.close = function(){
return Promise.resolve();
}
LocalFileSystem.prototype.get = function(scope, key) {
LocalFileSystem.prototype.get = function(scope, key, callback) {
if(typeof callback !== "function"){
throw new Error("Callback must be a function");
}
var storagePath = getStoragePath(this.storageBaseDir ,scope);
return loadFile(storagePath + ".json").then(function(data){
loadFile(storagePath + ".json").then(function(data){
if(data){
return util.getMessageProperty(JSON.parse(data),key);
callback(null, util.getMessageProperty(JSON.parse(data),key));
}else{
return undefined
callback(null, undefined);
}
}).catch(function(err){
return Promise.reject(err);
callback(err);
});
};
LocalFileSystem.prototype.set =function(scope, key, value) {
LocalFileSystem.prototype.set =function(scope, key, value, callback) {
var storagePath = getStoragePath(this.storageBaseDir ,scope);
return loadFile(storagePath + ".json").then(function(data){
loadFile(storagePath + ".json").then(function(data){
var obj = data ? JSON.parse(data) : {}
util.setMessageProperty(obj,key,value);
return obj;
}).then(function(obj){
var str = JSON.stringify(obj, undefined, 4);
return fs.outputFile(storagePath + ".json", str, {encoding:"utf8",flag:"w+"});
return fs.outputFile(storagePath + ".json", JSON.stringify(obj, undefined, 4), "utf8");
}).then(function(){
if(typeof callback === "function"){
callback(null);
}
}).catch(function(err){
return Promise.reject(err);
if(typeof callback === "function"){
callback(err);
}
});
};
LocalFileSystem.prototype.keys = function(scope){
LocalFileSystem.prototype.keys = function(scope, callback){
if(typeof callback !== "function"){
throw new Error("Callback must be a function");
}
var storagePath = getStoragePath(this.storageBaseDir ,scope);
return loadFile(storagePath + ".json").then(function(data){
loadFile(storagePath + ".json").then(function(data){
if(data){
return Object.keys(JSON.parse(data));
callback(null, Object.keys(JSON.parse(data)));
}else{
return []
callback(null, []);
}
}).catch(function(err){
return Promise.reject(err);
callback(err);
});
};

View File

@ -50,7 +50,7 @@ Memory.prototype.keys = function(scope){
return Object.keys(this.data[scope]);
} else {
return Object.keys(this.data[scope]).filter(function (key) {
return key !== "set" && key !== "get" && key !== "keys" && key !== "setAsync" && key !== "getAsync" && key !== "keysAsync";
return key !== "set" && key !== "get" && key !== "keys";
});
}
};

View File

@ -16,6 +16,7 @@
var should = require("should");
var sinon = require('sinon');
var path = require("path");
var Context = require("../../../../../red/runtime/nodes/context/index");
describe('context', function() {
@ -145,38 +146,26 @@ describe('context', function() {
Context.init({functionGlobalContext: {foo:"bar"}});
return Context.load().then(function(){
var context = Context.get("1","flowA");
var keys = context.global.keys("global");
var keys = context.global.keys();
keys.should.have.length(1);
keys[0].should.equal("foo");
});
});
it('should throw error when persistable key is passed', function() {
var context = Context.get("1","flow");
(function() {
context.set("#nonexist.key1", "val1");
}).should.throw();
(function() {
context.get("#nonexist.key1");
}).should.throw();
(function() {
context.keys("#nonexist");
}).should.throw();
});
});
describe.skip('external context storage',function() {
describe('external context storage',function() {
var resourcesDir = path.resolve(path.join(__dirname,"..","resources","context"));
var sandbox = sinon.sandbox.create();
var stubGet = sandbox.stub().returns(Promise.resolve());
var stubSet = sandbox.stub().returns(Promise.resolve());
var stubKeys = sandbox.stub().returns(Promise.resolve());
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().returns(Promise.resolve());
var stubSet2 = sandbox.stub().returns(Promise.resolve());
var stubKeys2 = 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());
@ -247,45 +236,41 @@ describe('context', function() {
return Context.load();
});
it('should load localfilesystem module', function() {
Context.init({contextStorage:{file:{module:"localfilesystem"}}});
Context.init({contextStorage:{file:{module:"localfilesystem",config:{dir:resourcesDir}}}});
return Context.load();
});
it('should accept special storage name', function() {
it('should accept special storage name', function(done) {
Context.init({
contextStorage:{
"#%&":{module:"memory"},
\u3042:{module:"memory"},
1:{module:"memory"},
"#%&":{module:testPlugin},
\u3042:{module:testPlugin},
1:{module:testPlugin},
}
});
return Context.load().then(function(){
Context.load().then(function(){
var context = Context.get("1","flow");
return Promise.all([
context.set("##%&.sign","sign1").then(function(){
return context.get("##%&.sign").should.finally.equal("sign1");
}),
context.set("#\u3042.file2","file2").then(function(){
return context.get("#\u3042.file2").should.finally.equal("file2");
}),
context.set("#1.num","num3").then(function(){
return context.get("#1.num").should.finally.equal("num3");
})
]);
var cb = function(){done("An error occurred")}
context.set("sign","sign1","#%&",cb);
context.set("file","file2","\u3042",cb);
context.set("num","num3","1",cb);
stubSet.calledWithExactly("1:flow","sign","sign1",cb).should.be.true();
stubSet.calledWithExactly("1:flow","file","file2",cb).should.be.true();
stubSet.calledWithExactly("1:flow","num","num3",cb).should.be.true();
done();
});
});
it('should ignore reserved storage name `_`', function() {
it('should ignore reserved storage name `_`', function(done) {
Context.init({contextStorage:{_:{module:testPlugin}}});
return Context.load().then(function(){
Context.load().then(function(){
var context = Context.get("1","flow");
return Promise.all([
context.set("#_.foo","bar"),
context.get("#_.foo"),
context.keys("#_")
]).then(function(){
stubSet.called.should.be.false();
stubGet.called.should.be.false();
stubKeys.called.should.be.false();
});
var cb = function(){done("An error occurred")}
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();
});
});
it('should fail when using invalid default context', function(done) {
@ -327,125 +312,119 @@ describe('context', function() {
});
describe('store context',function() {
it('should store local property to external context storage',function() {
it('should store local property to external context storage',function(done) {
Context.init({contextStorage:contextStorage});
return Context.load().then(function(){
var cb = function(){done("An error occurred")}
Context.load().then(function(){
var context = Context.get("1","flow");
return Promise.all([
context.set("#test.foo","test"),
context.get("#test.foo"),
context.keys("#test")
]).then(function(){
stubSet.calledWithExactly("1:flow","foo","test").should.be.true();
stubGet.calledWithExactly("1:flow","foo").should.be.true();
stubKeys.calledWithExactly("1:flow").should.be.true();
});
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();
stubGet.calledWithExactly("1:flow","foo",cb).should.be.true();
stubKeys.calledWithExactly("1:flow",cb).should.be.true();
done();
});
});
it('should store flow property to external context storage',function() {
it('should store flow property to external context storage',function(done) {
Context.init({contextStorage:contextStorage});
return Context.load().then(function(){
Context.load().then(function(){
var context = Context.get("1","flow");
return Promise.all([
context.flow.set("#test.foo","test"),
context.flow.get("#test.foo"),
context.flow.keys("#test")
]).then(function(){
stubSet.calledWithExactly("flow","foo","test").should.be.true();
stubGet.calledWithExactly("flow","foo").should.be.true();
stubKeys.calledWithExactly("flow").should.be.true();
});
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();
stubGet.calledWithExactly("flow","foo",cb).should.be.true();
stubKeys.calledWithExactly("flow",cb).should.be.true();
done();
});
});
it('should store global property to external context storage',function() {
it('should store global property to external context storage',function(done) {
Context.init({contextStorage:contextStorage});
return Context.load().then(function(){
Context.load().then(function(){
var context = Context.get("1","flow");
return Promise.all([
context.global.set("#test.foo","test"),
context.global.get("#test.foo"),
context.global.keys("#test")
]).then(function(){
stubSet.calledWithExactly("global","foo","test").should.be.true();
stubGet.calledWithExactly("global","foo").should.be.true();
stubKeys.calledWithExactly("global").should.be.true();
});
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();
stubGet.calledWithExactly("global","foo",cb).should.be.true();
stubKeys.calledWithExactly("global",cb).should.be.true();
done();
});
});
it('should store data to the default context when non-existent context storage was specified', function() {
it('should store data to the default context when non-existent context storage was specified', function(done) {
Context.init({contextStorage:contextDefaultStorage});
return Context.load().then(function(){
Context.load().then(function(){
var context = Context.get("1","flow");
return Promise.all([
context.set("#nonexist.foo","test"),
context.get("#nonexist.foo"),
context.keys("#nonexist")
]).then(function(){
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();
});
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();
stubGet2.calledWithExactly("1:flow","foo",cb).should.be.true();
stubKeys2.calledWithExactly("1:flow",cb).should.be.true();
done();
});
});
it('should use the default context', function() {
it('should use the default context', function(done) {
Context.init({contextStorage:contextDefaultStorage});
return Context.load().then(function(){
Context.load().then(function(){
var context = Context.get("1","flow");
return Promise.all([
context.set("#default.foo","default"),
context.get("#default.foo"),
context.keys("#default")
]).then(function(){
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();
});
var cb = function(){done("An error occurred")}
context.set("foo","bar","defaultt",cb);
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();
stubGet2.calledWithExactly("1:flow","foo",cb).should.be.true();
stubKeys2.calledWithExactly("1:flow",cb).should.be.true();
done();
});
});
it('should use the alias of default context', function() {
it('should use the alias of default context', function(done) {
Context.init({contextStorage:contextDefaultStorage});
return Context.load().then(function(){
Context.load().then(function(){
var context = Context.get("1","flow");
return Promise.all([
context.set("#.foo","alias"),
context.get("#.foo"),
context.keys("#")
]).then(function(){
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();
});
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();
stubGet2.calledWithExactly("1:flow","foo",cb).should.be.true();
stubKeys2.calledWithExactly("1:flow",cb).should.be.true();
done();
});
});
it('should use default as the alias of other context', function() {
it('should use default as the alias of other context', function(done) {
Context.init({contextStorage:contextAlias});
return Context.load().then(function(){
Context.load().then(function(){
var context = Context.get("1","flow");
return Promise.all([
context.set("#.foo","alias"),
context.get("#.foo"),
context.keys("#")
]).then(function(){
stubSet.calledWithExactly("1:flow","foo","alias").should.be.true();
stubGet.calledWithExactly("1:flow","foo").should.be.true();
stubKeys.calledWithExactly("1:flow").should.be.true();
});
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();
stubGet.calledWithExactly("1:flow","foo",cb).should.be.true();
stubKeys.calledWithExactly("1:flow",cb).should.be.true();
done();
});
});
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");
var cb = function(){done("An error occurred")}
context.get("local","nonexist",cb);
should.fail(null, null, "An error was not thrown using undefined storage for local context");
}).catch(function(err) {
if (err.name === "ContextError") {
@ -459,7 +438,8 @@ describe('context', function() {
Context.init({contextStorage:contextStorage});
Context.load().then(function(){
var context = Context.get("1","flow");
context.flow.set("#nonexist.flow");
var cb = function(){done("An error occurred")}
context.flow.get("flow","nonexist",cb);
should.fail(null, null, "An error was not thrown using undefined storage for flow context");
}).catch(function(err) {
if (err.name === "ContextError") {
@ -472,7 +452,7 @@ describe('context', function() {
});
describe('delete context',function(){
it('should not call delete()', function() {
it('should not call delete() when external context storage is used', function() {
Context.init({contextStorage:contextDefaultStorage});
return Context.load().then(function(){
Context.get("flowA");
@ -495,51 +475,5 @@ describe('context', function() {
});
});
});
describe('key name',function() {
beforeEach(function() {
Context.init({contextStorage:{memory:{module:"memory"}}});
return Context.load().then(function(){
context = Context.get("1","flow");
});
});
afterEach(function() {
Context.clean({allNodes:{}});
return Context.close();
});
it('should work correctly with the valid key name',function() {
return Promise.all([
context.set("#memory.azAZ09#_","valid"),
context.set("#memory.a.b","ab")
]).then(function(){
context.get("#memory.azAZ09#_").should.finally.equal("valid");
context.get("#memory.a.b").should.finally.equal("ab");
});
});
it('should treat the key name without dot as a normal context',function() {
return context.set("#memory","normal").then(function(){
return context.get("#memory").should.finally.equal("normal");
});
});
it('should fail when specifying invalid characters',function() {
(function() {
context.set("#memory.a.-","invalid1");
}).should.throw();
(function() {
context.set("#memory.'abc","invalid2");
}).should.throw();
});
it('should fail when specifying unnecesary space characters for key name',function() {
(function() {
context.set("# memory.space","space1");
}).should.throw();
(function() {
context.set("#memory .space","space2");
}).should.throw();
(function() {
context.set("#memory. space","space3");
}).should.throw();
});
});
});
});

View File

@ -24,6 +24,10 @@ var resourcesDir = path.resolve(path.join(__dirname,"..","resources","context"))
describe('localfilesystem',function() {
var context;
before(function() {
return fs.remove(resourcesDir);
});
beforeEach(function() {
context = LocalFileSystem({dir: resourcesDir});
return context.open();
@ -38,274 +42,336 @@ describe('localfilesystem',function() {
});
describe('#get/set',function() {
it('should store property',function() {
return context.get("nodeX","foo").should.be.finally.undefined()
.then(function(){
return context.set("nodeX","foo","test");
}).then(function(){
return context.get("nodeX","foo").should.be.finally.equal("test");
});
});
it('should store property - creates parent properties',function() {
return context.set("nodeX","foo.bar","test").then(function(){
return context.get("nodeX","foo").should.be.finally.eql({bar:"test"});
});
});
it('should delete property',function() {
return context.set("nodeX","foo.abc.bar1","test1")
.then(function(){
return context.set("nodeX","foo.abc.bar2","test2")
}).then(function(){
return context.get("nodeX","foo.abc").should.be.finally.eql({bar1:"test1",bar2:"test2"});
}).then(function(){
return context.set("nodeX","foo.abc.bar1",undefined).then(function(){
return context.get("nodeX","foo.abc").should.be.finally.eql({bar2:"test2"});
});
}).then(function(){
return context.set("nodeX","foo.abc",undefined).then(function(){
return context.get("nodeX","foo.abc").should.be.finally.undefined();
});
}).then(function(){
return context.set("nodeX","foo",undefined).then(function(){
return context.get("nodeX","foo").should.be.finally.undefined();
it('should store property',function(done) {
context.get("nodeX","foo",function(err, value){
should.not.exist(value);
context.set("nodeX","foo","test",function(err){
context.get("nodeX","foo",function(err, value){
value.should.be.equal("test");
done();
});
});
});
});
it('should not shared context with other scope', function() {
return Promise.all([context.get("nodeX","foo").should.be.finally.undefined(),
context.get("nodeY","foo").should.be.finally.undefined()
]).then(function(){
return Promise.all([context.set("nodeX","foo","testX"),
context.set("nodeY","foo","testY")])
}).then(function(){
return Promise.all([context.get("nodeX","foo").should.be.finally.equal("testX"),
context.get("nodeY","foo").should.be.finally.equal("testY")]);
it('should store property - creates parent properties',function(done) {
context.set("nodeX","foo.bar","test",function(err){
context.get("nodeX","foo",function(err, value){
value.should.be.eql({bar:"test"});
done();
});
});
});
it('should store string',function() {
return context.get("nodeX","foo").should.be.finally.undefined()
.then(function(){
return context.set("nodeX","foo","bar");
}).then(function(){
return context.get("nodeX","foo")
}).then(function(result){
result.should.be.String();
result.should.be.equal("bar");
}).then(function(){
return context.set("nodeX","foo","1");
}).then(function(){
return context.get("nodeX","foo")
}).then(function(result){
result.should.be.String();
result.should.be.equal("1");
it('should delete property',function(done) {
context.set("nodeX","foo.abc.bar1","test1",function(err){
context.set("nodeX","foo.abc.bar2","test2",function(err){
context.get("nodeX","foo.abc",function(err, value){
value.should.be.eql({bar1:"test1",bar2:"test2"});
context.set("nodeX","foo.abc.bar1",undefined,function(err){
context.get("nodeX","foo.abc",function(err, value){
value.should.be.eql({bar2:"test2"});
context.set("nodeX","foo.abc",undefined,function(err){
context.get("nodeX","foo.abc",function(err, value){
should.not.exist(value);
context.set("nodeX","foo",undefined,function(err){
context.get("nodeX","foo",function(err, value){
should.not.exist(value);
done();
});
});
});
});
});
});
});
});
});
});
it('should store number',function() {
return context.get("nodeX","foo").should.be.finally.undefined()
.then(function(){
return context.set("nodeX","foo",1);
}).then(function(){
return context.get("nodeX","foo")
}).then(function(result){
result.should.be.Number();
result.should.be.equal(1);
it('should not shared context with other scope', function(done) {
context.get("nodeX","foo",function(err, value){
should.not.exist(value);
context.get("nodeY","foo",function(err, value){
should.not.exist(value);
context.set("nodeX","foo","testX",function(err){
context.set("nodeY","foo","testY",function(err){
context.get("nodeX","foo",function(err, value){
value.should.be.equal("testX"),
context.get("nodeY","foo",function(err, value){
value.should.be.equal("testY");
done();
});
});
});
});
});
});
});
it('should store null',function() {
return context.get("nodeX","foo").should.be.finally.undefined()
.then(function(){
return context.set("nodeX","foo",null);
}).then(function(){
return context.get("nodeX","foo").should.be.finally.null();
it('should store string',function(done) {
context.get("nodeX","foo",function(err, value){
should.not.exist(value);
context.set("nodeX","foo","bar",function(err){
context.get("nodeX","foo",function(err, value){
value.should.be.String();
value.should.be.equal("bar");
context.set("nodeX","foo","1",function(err){
context.get("nodeX","foo",function(err, value){
value.should.be.String();
value.should.be.equal("1");
done();
});
});
});
});
});
});
it('should store boolean',function() {
return context.get("nodeX","foo").should.be.finally.undefined()
.then(function(){
return context.set("nodeX","foo",true);
}).then(function(){
return context.get("nodeX","foo").should.be.finally.Boolean().and.true();
}).then(function(){
return context.set("nodeX","foo",false);
}).then(function(){
return context.get("nodeX","foo").should.be.finally.Boolean().and.false();
it('should store number',function(done) {
context.get("nodeX","foo",function(err, value){
should.not.exist(value);
context.set("nodeX","foo",1,function(err){
context.get("nodeX","foo",function(err, value){
value.should.be.Number();
value.should.be.equal(1);
done();
});
});
});
});
it('should store object',function() {
return context.get("nodeX","foo").should.be.finally.undefined()
.then(function(){
return context.set("nodeX","foo",{obj:"bar"});
}).then(function(){
return context.get("nodeX","foo")
}).then(function(result){
result.should.be.Object();
result.should.eql({obj:"bar"});
it('should store null',function(done) {
context.get("nodeX","foo",function(err, value){
should.not.exist(value);
context.set("nodeX","foo",null,function(err){
context.get("nodeX","foo",function(err, value){
should(value).be.null();
done();
});
});
});
});
it('should store array',function() {
return context.get("nodeX","foo").should.be.finally.undefined()
.then(function(){
return context.set("nodeX","foo",["a","b","c"]);
}).then(function(){
return context.get("nodeX","foo")
}).then(function(result){
result.should.be.Array();
result.should.eql(["a","b","c"]);
}).then(function(){
return context.get("nodeX","foo[1]")
}).then(function(result){
result.should.be.String();
result.should.equal("b");
it('should store boolean',function(done) {
context.get("nodeX","foo",function(err, value){
should.not.exist(value);
context.set("nodeX","foo",true,function(err){
context.get("nodeX","foo",function(err, value){
value.should.be.Boolean().and.true();
context.set("nodeX","foo",false,function(err){
context.get("nodeX","foo",function(err, value){
value.should.be.Boolean().and.false();
done();
});
});
});
});
});
});
it('should store array of arrays',function() {
return context.get("nodeX","foo").should.be.finally.undefined()
.then(function(){
return context.set("nodeX","foo",[["a","b","c"],[1,2,3,4],[true,false]]);
}).then(function(){
return context.get("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.get("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 object',function(done) {
context.get("nodeX","foo",function(err, value){
should.not.exist(value);
context.set("nodeX","foo",{obj:"bar"},function(err){
context.get("nodeX","foo",function(err, value){
value.should.be.Object();
value.should.eql({obj:"bar"});
done();
});
});
});
});
it('should store array of objects',function() {
return context.get("nodeX","foo").should.be.finally.undefined()
.then(function(){
return context.set("nodeX","foo",[{obj:"bar1"},{obj:"bar2"},{obj:"bar3"}]);
}).then(function(){
return context.get("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.get("nodeX","foo[1]")
}).then(function(result){
result.should.be.Object();
result.should.be.eql({obj:"bar2"});
it('should store array',function(done) {
context.get("nodeX","foo",function(err, value){
should.not.exist(value);
context.set("nodeX","foo",["a","b","c"],function(err){
context.get("nodeX","foo",function(err, value){
value.should.be.Array();
value.should.eql(["a","b","c"]);
context.get("nodeX","foo[1]",function(err, value){
value.should.be.String();
value.should.equal("b");
done();
});
});
});
});
});
it('should store array of arrays',function(done) {
context.get("nodeX","foo",function(err, value){
should.not.exist(value);
context.set("nodeX","foo",[["a","b","c"],[1,2,3,4],[true,false]],function(err){
context.get("nodeX","foo",function(err, value){
value.should.be.Array();
value.should.have.length(3);
value[0].should.have.length(3);
value[1].should.have.length(4);
value[2].should.have.length(2);
context.get("nodeX","foo[1]",function(err, value){
value.should.be.Array();
value.should.have.length(4);
value.should.be.eql([1,2,3,4]);
done();
});
});
});
});
});
it('should store array of objects',function(done) {
context.get("nodeX","foo",function(err, value){
should.not.exist(value);
context.set("nodeX","foo",[{obj:"bar1"},{obj:"bar2"},{obj:"bar3"}],function(err){
context.get("nodeX","foo",function(err, value){
value.should.be.Array();
value.should.have.length(3);
value[0].should.be.Object();
value[1].should.be.Object();
value[2].should.be.Object();
context.get("nodeX","foo[1]",function(err, value){
value.should.be.Object();
value.should.be.eql({obj:"bar2"});
done();
});
});
});
});
});
});
describe('#keys',function() {
it('should enumerate context keys', function() {
return context.keys("nodeX").then(function(result){
result.should.be.an.Array();
result.should.be.empty();
}).then(function(){
return context.set("nodeX","foo","bar");
}).then(function(){
return context.keys("nodeX").then(function(result){
result.should.have.length(1);
result[0].should.equal("foo");
});
}).then(function(){
return context.set("nodeX","abc.def","bar");
}).then(function(){
return context.keys("nodeX").then(function(result){
result.should.have.length(2);
result[1].should.equal("abc");
it('should enumerate context keys', function(done) {
context.keys("nodeX",function(err, value){
value.should.be.an.Array();
value.should.be.empty();
context.set("nodeX","foo","bar",function(err){
context.keys("nodeX",function(err, value){
value.should.have.length(1);
value[0].should.equal("foo");
context.set("nodeX","abc.def","bar",function(err){
context.keys("nodeX",function(err, value){
value.should.have.length(2);
value[1].should.equal("abc");
done();
});
});
});
});
});
});
it('should enumerate context keys in each scopes', function() {
return Promise.all([context.keys("nodeX"),
context.keys("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 Promise.all([context.set("nodeX","foo","bar"),
context.set("nodeY","hoge","piyo")]);
}).then(function(){
return Promise.all([context.keys("nodeX"),
context.keys("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");
it('should enumerate context keys in each scopes', function(done) {
context.keys("nodeX",function(err, value){
value.should.be.an.Array();
value.should.be.empty();
context.keys("nodeY",function(err, value){
value.should.be.an.Array();
value.should.be.empty();
context.set("nodeX","foo","bar",function(err){
context.set("nodeY","hoge","piyo",function(err){
context.keys("nodeX",function(err, value){
value.should.have.length(1);
value[0].should.equal("foo");
context.keys("nodeY",function(err, value){
value.should.have.length(1);
value[0].should.equal("hoge");
done();
});
});
});
});
});
});
});
});
describe('#delete',function() {
it('should delete context',function() {
return Promise.all([context.get("nodeX","foo").should.be.finally.undefined(),
context.get("nodeY","foo").should.be.finally.undefined()
]).then(function(){
return Promise.all([context.set("nodeX","foo","abc"),
context.set("nodeY","foo","abc")]);
}).then(function(){
return Promise.all([context.get("nodeX","foo").should.be.finally.equal("abc"),
context.get("nodeY","foo").should.be.finally.equal("abc")])
}).then(function(){
return context.delete("nodeX");
}).then(function(){
return Promise.all([context.get("nodeX","foo").should.be.finally.undefined(),
context.get("nodeY","foo").should.be.finally.equal("abc")]);
it('should delete context',function(done) {
context.get("nodeX","foo",function(err, value){
should.not.exist(value);
context.get("nodeY","foo",function(err, value){
should.not.exist(value);
context.set("nodeX","foo","testX",function(err){
context.set("nodeY","foo","testY",function(err){
context.get("nodeX","foo",function(err, value){
value.should.be.equal("testX");
context.get("nodeY","foo",function(err, value){
value.should.be.equal("testY");
context.delete("nodeX").then(function(){
context.get("nodeX","foo",function(err, value){
should.not.exist(value);
context.get("nodeY","foo",function(err, value){
value.should.be.equal("testY");
done();
});
});
});
});
});
});
});
});
});
});
});
describe('#clean',function() {
it('should clean unnecessary context',function() {
return Promise.all([context.get("nodeX","foo").should.be.finally.undefined(),
context.get("nodeY","foo").should.be.finally.undefined()
]).then(function(){
return Promise.all([context.set("nodeX","foo","abc"),
context.set("nodeY","foo","abc")]);
}).then(function(){
return Promise.all([context.get("nodeX","foo").should.be.finally.equal("abc"),
context.get("nodeY","foo").should.be.finally.equal("abc")])
}).then(function(){
return context.clean([]);
}).then(function(){
return Promise.all([context.get("nodeX","foo").should.be.finally.undefined(),
context.get("nodeY","foo").should.be.finally.undefined()]);
it('should clean unnecessary context',function(done) {
context.get("nodeX","foo",function(err, value){
should.not.exist(value);
context.get("nodeY","foo",function(err, value){
should.not.exist(value);
context.set("nodeX","foo","testX",function(err){
context.set("nodeY","foo","testY",function(err){
context.get("nodeX","foo",function(err, value){
value.should.be.equal("testX");
context.get("nodeY","foo",function(err, value){
value.should.be.equal("testY");
context.clean([]).then(function(){
context.get("nodeX","foo",function(err, value){
should.not.exist(value);
context.get("nodeY","foo",function(err, value){
should.not.exist(value);
done();
});
});
});
});
});
});
});
});
});
});
it('should not clean active context',function() {
return Promise.all([context.get("nodeX","foo").should.be.finally.undefined(),
context.get("nodeY","foo").should.be.finally.undefined()
]).then(function(){
return Promise.all([context.set("nodeX","foo","abc"),
context.set("nodeY","foo","abc")]);
}).then(function(){
return Promise.all([context.get("nodeX","foo").should.be.finally.equal("abc"),
context.get("nodeY","foo").should.be.finally.equal("abc")])
}).then(function(){
return context.clean(["nodeX"]);
}).then(function(){
return Promise.all([context.get("nodeX","foo").should.be.finally.equal("abc"),
context.get("nodeY","foo").should.be.finally.undefined()]);
it('should not clean active context',function(done) {
context.get("nodeX","foo",function(err, value){
should.not.exist(value);
context.get("nodeY","foo",function(err, value){
should.not.exist(value);
context.set("nodeX","foo","testX",function(err){
context.set("nodeY","foo","testY",function(err){
context.get("nodeX","foo",function(err, value){
value.should.be.equal("testX");
context.get("nodeY","foo",function(err, value){
value.should.be.equal("testY");
context.clean(["nodeX"]).then(function(){
context.get("nodeX","foo",function(err, value){
value.should.be.equal("testX");
context.get("nodeY","foo",function(err, value){
should.not.exist(value);
done();
});
});
});
});
});
});
});
});
});
});
});