mirror of
https://github.com/node-red/node-red.git
synced 2025-03-01 10:36:34 +00:00
Use the callback instead of Promise in context API
and remove unnecessary functions
This commit is contained in:
parent
fd67d08402
commit
f2fa26fb07
@ -161,9 +161,6 @@
|
|||||||
"error-module-not-loaded": "'__module__' could not be loaded",
|
"error-module-not-loaded": "'__module__' could not be loaded",
|
||||||
"error-module-not-defined": "'module' is not defined in '__storage__' of settings.contextStorage",
|
"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-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"
|
"error-use-undefined-storage": "Undefined storage '__storage__' is specified"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -16,6 +16,7 @@
|
|||||||
|
|
||||||
var clone = require("clone");
|
var clone = require("clone");
|
||||||
var log = require("../../log");
|
var log = require("../../log");
|
||||||
|
var memory = require("./memory");
|
||||||
|
|
||||||
var settings;
|
var settings;
|
||||||
var contexts = {};
|
var contexts = {};
|
||||||
@ -28,7 +29,6 @@ function init(_settings) {
|
|||||||
externalContexts = {};
|
externalContexts = {};
|
||||||
|
|
||||||
// init memory plugin
|
// init memory plugin
|
||||||
var memory = require("./memory");
|
|
||||||
var seed = settings.functionGlobalContext || {};
|
var seed = settings.functionGlobalContext || {};
|
||||||
externalContexts["_"] = memory();
|
externalContexts["_"] = memory();
|
||||||
externalContexts["_"].setGlobalContext(seed);
|
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) {
|
function getContextStorage(storage) {
|
||||||
if (noContextStorage || !storage) {
|
if (noContextStorage || !storage) {
|
||||||
return externalContexts["_"];
|
return externalContexts["_"];
|
||||||
@ -150,47 +113,30 @@ function createContext(id,seed) {
|
|||||||
var scope = id;
|
var scope = id;
|
||||||
var obj = seed || {};
|
var obj = seed || {};
|
||||||
|
|
||||||
obj.get = function(key) {
|
obj.get = function(key, storage, callback) {
|
||||||
var keyPath = parseKey(key);
|
if (typeof storage === 'function') {
|
||||||
var context = getContextStorage(keyPath.storage);
|
callback = storage;
|
||||||
if(!keyPath.storage){
|
storage = "default";
|
||||||
return context.get(scope, keyPath.key);
|
} else if(typeof storage === "string" && typeof callback !== 'function'){
|
||||||
}else{
|
throw new Error("Callback must be a function");
|
||||||
throw new Error(keyPath.storage + " does not support get(). Use getAsync()");
|
|
||||||
}
|
}
|
||||||
|
return getContextStorage(storage).get(scope, key, callback);
|
||||||
};
|
};
|
||||||
obj.set = function(key, value) {
|
obj.set = function(key, value, storage, callback) {
|
||||||
var keyPath = parseKey(key);
|
if (typeof storage === 'function') {
|
||||||
var context = getContextStorage(keyPath.storage);
|
callback = storage;
|
||||||
if(!keyPath.storage){
|
storage = "default";
|
||||||
return context.set(scope, keyPath.key, value);
|
|
||||||
}else{
|
|
||||||
throw new Error(keyPath.storage + " does not support set(). Use setAsync()");
|
|
||||||
}
|
}
|
||||||
|
getContextStorage(storage).set(scope, key, value, callback);
|
||||||
};
|
};
|
||||||
obj.keys = function(storage) {
|
obj.keys = function(storage, callback) {
|
||||||
var storageName = parseStorage(storage);
|
if (typeof storage === 'function') {
|
||||||
var context = getContextStorage(storageName);
|
callback = storage;
|
||||||
if(!storageName){
|
storage = "default";
|
||||||
return context.keys(scope);
|
} else if(typeof storage === "string" && typeof callback !== 'function'){
|
||||||
}else{
|
throw new Error("Callback must be a function");
|
||||||
throw new Error(storageName + " does not support keys(). Use keysAsync()");
|
|
||||||
}
|
}
|
||||||
};
|
return getContextStorage(storage).keys(scope, callback);
|
||||||
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 obj;
|
return obj;
|
||||||
}
|
}
|
||||||
|
@ -86,43 +86,52 @@ LocalFileSystem.prototype.close = function(){
|
|||||||
return Promise.resolve();
|
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);
|
var storagePath = getStoragePath(this.storageBaseDir ,scope);
|
||||||
return loadFile(storagePath + ".json").then(function(data){
|
loadFile(storagePath + ".json").then(function(data){
|
||||||
if(data){
|
if(data){
|
||||||
return util.getMessageProperty(JSON.parse(data),key);
|
callback(null, util.getMessageProperty(JSON.parse(data),key));
|
||||||
}else{
|
}else{
|
||||||
return undefined
|
callback(null, undefined);
|
||||||
}
|
}
|
||||||
}).catch(function(err){
|
}).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);
|
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) : {}
|
var obj = data ? JSON.parse(data) : {}
|
||||||
util.setMessageProperty(obj,key,value);
|
util.setMessageProperty(obj,key,value);
|
||||||
return obj;
|
return fs.outputFile(storagePath + ".json", JSON.stringify(obj, undefined, 4), "utf8");
|
||||||
}).then(function(obj){
|
}).then(function(){
|
||||||
var str = JSON.stringify(obj, undefined, 4);
|
if(typeof callback === "function"){
|
||||||
return fs.outputFile(storagePath + ".json", str, {encoding:"utf8",flag:"w+"});
|
callback(null);
|
||||||
|
}
|
||||||
}).catch(function(err){
|
}).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);
|
var storagePath = getStoragePath(this.storageBaseDir ,scope);
|
||||||
return loadFile(storagePath + ".json").then(function(data){
|
loadFile(storagePath + ".json").then(function(data){
|
||||||
if(data){
|
if(data){
|
||||||
return Object.keys(JSON.parse(data));
|
callback(null, Object.keys(JSON.parse(data)));
|
||||||
}else{
|
}else{
|
||||||
return []
|
callback(null, []);
|
||||||
}
|
}
|
||||||
}).catch(function(err){
|
}).catch(function(err){
|
||||||
return Promise.reject(err);
|
callback(err);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -50,7 +50,7 @@ Memory.prototype.keys = function(scope){
|
|||||||
return Object.keys(this.data[scope]);
|
return Object.keys(this.data[scope]);
|
||||||
} else {
|
} else {
|
||||||
return Object.keys(this.data[scope]).filter(function (key) {
|
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";
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -16,6 +16,7 @@
|
|||||||
|
|
||||||
var should = require("should");
|
var should = require("should");
|
||||||
var sinon = require('sinon');
|
var sinon = require('sinon');
|
||||||
|
var path = require("path");
|
||||||
var Context = require("../../../../../red/runtime/nodes/context/index");
|
var Context = require("../../../../../red/runtime/nodes/context/index");
|
||||||
|
|
||||||
describe('context', function() {
|
describe('context', function() {
|
||||||
@ -145,38 +146,26 @@ describe('context', function() {
|
|||||||
Context.init({functionGlobalContext: {foo:"bar"}});
|
Context.init({functionGlobalContext: {foo:"bar"}});
|
||||||
return Context.load().then(function(){
|
return Context.load().then(function(){
|
||||||
var context = Context.get("1","flowA");
|
var context = Context.get("1","flowA");
|
||||||
var keys = context.global.keys("global");
|
var keys = context.global.keys();
|
||||||
keys.should.have.length(1);
|
keys.should.have.length(1);
|
||||||
keys[0].should.equal("foo");
|
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 sandbox = sinon.sandbox.create();
|
||||||
var stubGet = sandbox.stub().returns(Promise.resolve());
|
var stubGet = sandbox.stub();
|
||||||
var stubSet = sandbox.stub().returns(Promise.resolve());
|
var stubSet = sandbox.stub();
|
||||||
var stubKeys = sandbox.stub().returns(Promise.resolve());
|
var stubKeys = sandbox.stub();
|
||||||
var stubDelete = sandbox.stub().returns(Promise.resolve());
|
var stubDelete = sandbox.stub().returns(Promise.resolve());
|
||||||
var stubClean = sandbox.stub().returns(Promise.resolve());
|
var stubClean = sandbox.stub().returns(Promise.resolve());
|
||||||
var stubOpen = sandbox.stub().returns(Promise.resolve());
|
var stubOpen = sandbox.stub().returns(Promise.resolve());
|
||||||
var stubClose = sandbox.stub().returns(Promise.resolve());
|
var stubClose = sandbox.stub().returns(Promise.resolve());
|
||||||
var stubGet2 = sandbox.stub().returns(Promise.resolve());
|
var stubGet2 = sandbox.stub();
|
||||||
var stubSet2 = sandbox.stub().returns(Promise.resolve());
|
var stubSet2 = sandbox.stub();
|
||||||
var stubKeys2 = sandbox.stub().returns(Promise.resolve());
|
var stubKeys2 = sandbox.stub();
|
||||||
var stubDelete2 = sandbox.stub().returns(Promise.resolve());
|
var stubDelete2 = sandbox.stub().returns(Promise.resolve());
|
||||||
var stubClean2 = sandbox.stub().returns(Promise.resolve());
|
var stubClean2 = sandbox.stub().returns(Promise.resolve());
|
||||||
var stubOpen2 = sandbox.stub().returns(Promise.resolve());
|
var stubOpen2 = sandbox.stub().returns(Promise.resolve());
|
||||||
@ -247,45 +236,41 @@ describe('context', function() {
|
|||||||
return Context.load();
|
return Context.load();
|
||||||
});
|
});
|
||||||
it('should load localfilesystem module', function() {
|
it('should load localfilesystem module', function() {
|
||||||
Context.init({contextStorage:{file:{module:"localfilesystem"}}});
|
Context.init({contextStorage:{file:{module:"localfilesystem",config:{dir:resourcesDir}}}});
|
||||||
return Context.load();
|
return Context.load();
|
||||||
});
|
});
|
||||||
it('should accept special storage name', function() {
|
it('should accept special storage name', function(done) {
|
||||||
Context.init({
|
Context.init({
|
||||||
contextStorage:{
|
contextStorage:{
|
||||||
"#%&":{module:"memory"},
|
"#%&":{module:testPlugin},
|
||||||
\u3042:{module:"memory"},
|
\u3042:{module:testPlugin},
|
||||||
1:{module:"memory"},
|
1:{module:testPlugin},
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
return Context.load().then(function(){
|
Context.load().then(function(){
|
||||||
var context = Context.get("1","flow");
|
var context = Context.get("1","flow");
|
||||||
return Promise.all([
|
var cb = function(){done("An error occurred")}
|
||||||
context.set("##%&.sign","sign1").then(function(){
|
context.set("sign","sign1","#%&",cb);
|
||||||
return context.get("##%&.sign").should.finally.equal("sign1");
|
context.set("file","file2","\u3042",cb);
|
||||||
}),
|
context.set("num","num3","1",cb);
|
||||||
context.set("#\u3042.file2","file2").then(function(){
|
stubSet.calledWithExactly("1:flow","sign","sign1",cb).should.be.true();
|
||||||
return context.get("#\u3042.file2").should.finally.equal("file2");
|
stubSet.calledWithExactly("1:flow","file","file2",cb).should.be.true();
|
||||||
}),
|
stubSet.calledWithExactly("1:flow","num","num3",cb).should.be.true();
|
||||||
context.set("#1.num","num3").then(function(){
|
done();
|
||||||
return context.get("#1.num").should.finally.equal("num3");
|
|
||||||
})
|
|
||||||
]);
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
it('should ignore reserved storage name `_`', function() {
|
it('should ignore reserved storage name `_`', function(done) {
|
||||||
Context.init({contextStorage:{_:{module:testPlugin}}});
|
Context.init({contextStorage:{_:{module:testPlugin}}});
|
||||||
return Context.load().then(function(){
|
Context.load().then(function(){
|
||||||
var context = Context.get("1","flow");
|
var context = Context.get("1","flow");
|
||||||
return Promise.all([
|
var cb = function(){done("An error occurred")}
|
||||||
context.set("#_.foo","bar"),
|
context.set("foo","bar","_",cb);
|
||||||
context.get("#_.foo"),
|
context.get("foo","_",cb);
|
||||||
context.keys("#_")
|
context.keys("_",cb);
|
||||||
]).then(function(){
|
stubSet.called.should.be.false();
|
||||||
stubSet.called.should.be.false();
|
stubGet.called.should.be.false();
|
||||||
stubGet.called.should.be.false();
|
stubKeys.called.should.be.false();
|
||||||
stubKeys.called.should.be.false();
|
done();
|
||||||
});
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
it('should fail when using invalid default context', function(done) {
|
it('should fail when using invalid default context', function(done) {
|
||||||
@ -327,125 +312,119 @@ describe('context', function() {
|
|||||||
});
|
});
|
||||||
|
|
||||||
describe('store 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});
|
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");
|
var context = Context.get("1","flow");
|
||||||
return Promise.all([
|
context.set("foo","bar","test",cb);
|
||||||
context.set("#test.foo","test"),
|
context.get("foo","test",cb);
|
||||||
context.get("#test.foo"),
|
context.keys("test",cb);
|
||||||
context.keys("#test")
|
stubSet.calledWithExactly("1:flow","foo","bar",cb).should.be.true();
|
||||||
]).then(function(){
|
stubGet.calledWithExactly("1:flow","foo",cb).should.be.true();
|
||||||
stubSet.calledWithExactly("1:flow","foo","test").should.be.true();
|
stubKeys.calledWithExactly("1:flow",cb).should.be.true();
|
||||||
stubGet.calledWithExactly("1:flow","foo").should.be.true();
|
done();
|
||||||
stubKeys.calledWithExactly("1:flow").should.be.true();
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
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});
|
Context.init({contextStorage:contextStorage});
|
||||||
return Context.load().then(function(){
|
Context.load().then(function(){
|
||||||
var context = Context.get("1","flow");
|
var context = Context.get("1","flow");
|
||||||
return Promise.all([
|
var cb = function(){done("An error occurred")}
|
||||||
context.flow.set("#test.foo","test"),
|
context.flow.set("foo","bar","test",cb);
|
||||||
context.flow.get("#test.foo"),
|
context.flow.get("foo","test",cb);
|
||||||
context.flow.keys("#test")
|
context.flow.keys("test",cb);
|
||||||
]).then(function(){
|
stubSet.calledWithExactly("flow","foo","bar",cb).should.be.true();
|
||||||
stubSet.calledWithExactly("flow","foo","test").should.be.true();
|
stubGet.calledWithExactly("flow","foo",cb).should.be.true();
|
||||||
stubGet.calledWithExactly("flow","foo").should.be.true();
|
stubKeys.calledWithExactly("flow",cb).should.be.true();
|
||||||
stubKeys.calledWithExactly("flow").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});
|
Context.init({contextStorage:contextStorage});
|
||||||
return Context.load().then(function(){
|
Context.load().then(function(){
|
||||||
var context = Context.get("1","flow");
|
var context = Context.get("1","flow");
|
||||||
return Promise.all([
|
var cb = function(){done("An error occurred")}
|
||||||
context.global.set("#test.foo","test"),
|
context.global.set("foo","bar","test",cb);
|
||||||
context.global.get("#test.foo"),
|
context.global.get("foo","test",cb);
|
||||||
context.global.keys("#test")
|
context.global.keys("test",cb);
|
||||||
]).then(function(){
|
stubSet.calledWithExactly("global","foo","bar",cb).should.be.true();
|
||||||
stubSet.calledWithExactly("global","foo","test").should.be.true();
|
stubGet.calledWithExactly("global","foo",cb).should.be.true();
|
||||||
stubGet.calledWithExactly("global","foo").should.be.true();
|
stubKeys.calledWithExactly("global",cb).should.be.true();
|
||||||
stubKeys.calledWithExactly("global").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});
|
Context.init({contextStorage:contextDefaultStorage});
|
||||||
return Context.load().then(function(){
|
Context.load().then(function(){
|
||||||
var context = Context.get("1","flow");
|
var context = Context.get("1","flow");
|
||||||
return Promise.all([
|
var cb = function(){done("An error occurred")}
|
||||||
context.set("#nonexist.foo","test"),
|
context.set("foo","bar","nonexist",cb);
|
||||||
context.get("#nonexist.foo"),
|
context.get("foo","nonexist",cb);
|
||||||
context.keys("#nonexist")
|
context.keys("nonexist",cb);
|
||||||
]).then(function(){
|
stubGet.called.should.be.false();
|
||||||
stubGet.called.should.be.false();
|
stubSet.called.should.be.false();
|
||||||
stubSet.called.should.be.false();
|
stubKeys.called.should.be.false();
|
||||||
stubKeys.called.should.be.false();
|
stubSet2.calledWithExactly("1:flow","foo","bar",cb).should.be.true();
|
||||||
stubSet2.calledWithExactly("1:flow","foo","test").should.be.true();
|
stubGet2.calledWithExactly("1:flow","foo",cb).should.be.true();
|
||||||
stubGet2.calledWithExactly("1:flow","foo").should.be.true();
|
stubKeys2.calledWithExactly("1:flow",cb).should.be.true();
|
||||||
stubKeys2.calledWithExactly("1:flow").should.be.true();
|
done();
|
||||||
});
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
it('should use the default context', function() {
|
it('should use the default context', function(done) {
|
||||||
Context.init({contextStorage:contextDefaultStorage});
|
Context.init({contextStorage:contextDefaultStorage});
|
||||||
return Context.load().then(function(){
|
Context.load().then(function(){
|
||||||
var context = Context.get("1","flow");
|
var context = Context.get("1","flow");
|
||||||
return Promise.all([
|
var cb = function(){done("An error occurred")}
|
||||||
context.set("#default.foo","default"),
|
context.set("foo","bar","defaultt",cb);
|
||||||
context.get("#default.foo"),
|
context.get("foo","default",cb);
|
||||||
context.keys("#default")
|
context.keys("default",cb);
|
||||||
]).then(function(){
|
stubGet.called.should.be.false();
|
||||||
stubGet.called.should.be.false();
|
stubSet.called.should.be.false();
|
||||||
stubSet.called.should.be.false();
|
stubKeys.called.should.be.false();
|
||||||
stubKeys.called.should.be.false();
|
stubSet2.calledWithExactly("1:flow","foo","bar",cb).should.be.true();
|
||||||
stubSet2.calledWithExactly("1:flow","foo","default").should.be.true();
|
stubGet2.calledWithExactly("1:flow","foo",cb).should.be.true();
|
||||||
stubGet2.calledWithExactly("1:flow","foo").should.be.true();
|
stubKeys2.calledWithExactly("1:flow",cb).should.be.true();
|
||||||
stubKeys2.calledWithExactly("1:flow").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});
|
Context.init({contextStorage:contextDefaultStorage});
|
||||||
return Context.load().then(function(){
|
Context.load().then(function(){
|
||||||
var context = Context.get("1","flow");
|
var context = Context.get("1","flow");
|
||||||
return Promise.all([
|
var cb = function(){done("An error occurred")}
|
||||||
context.set("#.foo","alias"),
|
context.set("foo","alias",cb);
|
||||||
context.get("#.foo"),
|
context.get("foo",cb);
|
||||||
context.keys("#")
|
context.keys(cb);
|
||||||
]).then(function(){
|
stubGet.called.should.be.false();
|
||||||
stubGet.called.should.be.false();
|
stubSet.called.should.be.false();
|
||||||
stubSet.called.should.be.false();
|
stubKeys.called.should.be.false();
|
||||||
stubKeys.called.should.be.false();
|
stubSet2.calledWithExactly("1:flow","foo","alias",cb).should.be.true();
|
||||||
stubSet2.calledWithExactly("1:flow","foo","alias").should.be.true();
|
stubGet2.calledWithExactly("1:flow","foo",cb).should.be.true();
|
||||||
stubGet2.calledWithExactly("1:flow","foo").should.be.true();
|
stubKeys2.calledWithExactly("1:flow",cb).should.be.true();
|
||||||
stubKeys2.calledWithExactly("1:flow").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});
|
Context.init({contextStorage:contextAlias});
|
||||||
return Context.load().then(function(){
|
Context.load().then(function(){
|
||||||
var context = Context.get("1","flow");
|
var context = Context.get("1","flow");
|
||||||
return Promise.all([
|
var cb = function(){done("An error occurred")}
|
||||||
context.set("#.foo","alias"),
|
context.set("foo","alias",cb);
|
||||||
context.get("#.foo"),
|
context.get("foo",cb);
|
||||||
context.keys("#")
|
context.keys(cb);
|
||||||
]).then(function(){
|
stubSet.calledWithExactly("1:flow","foo","alias",cb).should.be.true();
|
||||||
stubSet.calledWithExactly("1:flow","foo","alias").should.be.true();
|
stubGet.calledWithExactly("1:flow","foo",cb).should.be.true();
|
||||||
stubGet.calledWithExactly("1:flow","foo").should.be.true();
|
stubKeys.calledWithExactly("1:flow",cb).should.be.true();
|
||||||
stubKeys.calledWithExactly("1:flow").should.be.true();
|
done();
|
||||||
});
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
it('should throw an error using undefined storage for local context', function(done) {
|
it('should throw an error using undefined storage for local context', function(done) {
|
||||||
Context.init({contextStorage:contextStorage});
|
Context.init({contextStorage:contextStorage});
|
||||||
Context.load().then(function(){
|
Context.load().then(function(){
|
||||||
var context = Context.get("1","flow");
|
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");
|
should.fail(null, null, "An error was not thrown using undefined storage for local context");
|
||||||
}).catch(function(err) {
|
}).catch(function(err) {
|
||||||
if (err.name === "ContextError") {
|
if (err.name === "ContextError") {
|
||||||
@ -459,7 +438,8 @@ describe('context', function() {
|
|||||||
Context.init({contextStorage:contextStorage});
|
Context.init({contextStorage:contextStorage});
|
||||||
Context.load().then(function(){
|
Context.load().then(function(){
|
||||||
var context = Context.get("1","flow");
|
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");
|
should.fail(null, null, "An error was not thrown using undefined storage for flow context");
|
||||||
}).catch(function(err) {
|
}).catch(function(err) {
|
||||||
if (err.name === "ContextError") {
|
if (err.name === "ContextError") {
|
||||||
@ -472,7 +452,7 @@ describe('context', function() {
|
|||||||
});
|
});
|
||||||
|
|
||||||
describe('delete 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});
|
Context.init({contextStorage:contextDefaultStorage});
|
||||||
return Context.load().then(function(){
|
return Context.load().then(function(){
|
||||||
Context.get("flowA");
|
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();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -24,6 +24,10 @@ var resourcesDir = path.resolve(path.join(__dirname,"..","resources","context"))
|
|||||||
describe('localfilesystem',function() {
|
describe('localfilesystem',function() {
|
||||||
var context;
|
var context;
|
||||||
|
|
||||||
|
before(function() {
|
||||||
|
return fs.remove(resourcesDir);
|
||||||
|
});
|
||||||
|
|
||||||
beforeEach(function() {
|
beforeEach(function() {
|
||||||
context = LocalFileSystem({dir: resourcesDir});
|
context = LocalFileSystem({dir: resourcesDir});
|
||||||
return context.open();
|
return context.open();
|
||||||
@ -38,274 +42,336 @@ describe('localfilesystem',function() {
|
|||||||
});
|
});
|
||||||
|
|
||||||
describe('#get/set',function() {
|
describe('#get/set',function() {
|
||||||
it('should store property',function() {
|
it('should store property',function(done) {
|
||||||
return context.get("nodeX","foo").should.be.finally.undefined()
|
context.get("nodeX","foo",function(err, value){
|
||||||
.then(function(){
|
should.not.exist(value);
|
||||||
return context.set("nodeX","foo","test");
|
context.set("nodeX","foo","test",function(err){
|
||||||
}).then(function(){
|
context.get("nodeX","foo",function(err, value){
|
||||||
return context.get("nodeX","foo").should.be.finally.equal("test");
|
value.should.be.equal("test");
|
||||||
});
|
done();
|
||||||
});
|
});
|
||||||
|
|
||||||
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 not shared context with other scope', function() {
|
it('should store property - creates parent properties',function(done) {
|
||||||
return Promise.all([context.get("nodeX","foo").should.be.finally.undefined(),
|
context.set("nodeX","foo.bar","test",function(err){
|
||||||
context.get("nodeY","foo").should.be.finally.undefined()
|
context.get("nodeX","foo",function(err, value){
|
||||||
]).then(function(){
|
value.should.be.eql({bar:"test"});
|
||||||
return Promise.all([context.set("nodeX","foo","testX"),
|
done();
|
||||||
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 string',function() {
|
it('should delete property',function(done) {
|
||||||
return context.get("nodeX","foo").should.be.finally.undefined()
|
context.set("nodeX","foo.abc.bar1","test1",function(err){
|
||||||
.then(function(){
|
context.set("nodeX","foo.abc.bar2","test2",function(err){
|
||||||
return context.set("nodeX","foo","bar");
|
context.get("nodeX","foo.abc",function(err, value){
|
||||||
}).then(function(){
|
value.should.be.eql({bar1:"test1",bar2:"test2"});
|
||||||
return context.get("nodeX","foo")
|
context.set("nodeX","foo.abc.bar1",undefined,function(err){
|
||||||
}).then(function(result){
|
context.get("nodeX","foo.abc",function(err, value){
|
||||||
result.should.be.String();
|
value.should.be.eql({bar2:"test2"});
|
||||||
result.should.be.equal("bar");
|
context.set("nodeX","foo.abc",undefined,function(err){
|
||||||
}).then(function(){
|
context.get("nodeX","foo.abc",function(err, value){
|
||||||
return context.set("nodeX","foo","1");
|
should.not.exist(value);
|
||||||
}).then(function(){
|
context.set("nodeX","foo",undefined,function(err){
|
||||||
return context.get("nodeX","foo")
|
context.get("nodeX","foo",function(err, value){
|
||||||
}).then(function(result){
|
should.not.exist(value);
|
||||||
result.should.be.String();
|
done();
|
||||||
result.should.be.equal("1");
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should store number',function() {
|
it('should not shared context with other scope', function(done) {
|
||||||
return context.get("nodeX","foo").should.be.finally.undefined()
|
context.get("nodeX","foo",function(err, value){
|
||||||
.then(function(){
|
should.not.exist(value);
|
||||||
return context.set("nodeX","foo",1);
|
context.get("nodeY","foo",function(err, value){
|
||||||
}).then(function(){
|
should.not.exist(value);
|
||||||
return context.get("nodeX","foo")
|
context.set("nodeX","foo","testX",function(err){
|
||||||
}).then(function(result){
|
context.set("nodeY","foo","testY",function(err){
|
||||||
result.should.be.Number();
|
context.get("nodeX","foo",function(err, value){
|
||||||
result.should.be.equal(1);
|
value.should.be.equal("testX"),
|
||||||
|
context.get("nodeY","foo",function(err, value){
|
||||||
|
value.should.be.equal("testY");
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should store null',function() {
|
it('should store string',function(done) {
|
||||||
return context.get("nodeX","foo").should.be.finally.undefined()
|
context.get("nodeX","foo",function(err, value){
|
||||||
.then(function(){
|
should.not.exist(value);
|
||||||
return context.set("nodeX","foo",null);
|
context.set("nodeX","foo","bar",function(err){
|
||||||
}).then(function(){
|
context.get("nodeX","foo",function(err, value){
|
||||||
return context.get("nodeX","foo").should.be.finally.null();
|
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() {
|
it('should store number',function(done) {
|
||||||
return context.get("nodeX","foo").should.be.finally.undefined()
|
context.get("nodeX","foo",function(err, value){
|
||||||
.then(function(){
|
should.not.exist(value);
|
||||||
return context.set("nodeX","foo",true);
|
context.set("nodeX","foo",1,function(err){
|
||||||
}).then(function(){
|
context.get("nodeX","foo",function(err, value){
|
||||||
return context.get("nodeX","foo").should.be.finally.Boolean().and.true();
|
value.should.be.Number();
|
||||||
}).then(function(){
|
value.should.be.equal(1);
|
||||||
return context.set("nodeX","foo",false);
|
done();
|
||||||
}).then(function(){
|
});
|
||||||
return context.get("nodeX","foo").should.be.finally.Boolean().and.false();
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should store object',function() {
|
it('should store null',function(done) {
|
||||||
return context.get("nodeX","foo").should.be.finally.undefined()
|
context.get("nodeX","foo",function(err, value){
|
||||||
.then(function(){
|
should.not.exist(value);
|
||||||
return context.set("nodeX","foo",{obj:"bar"});
|
context.set("nodeX","foo",null,function(err){
|
||||||
}).then(function(){
|
context.get("nodeX","foo",function(err, value){
|
||||||
return context.get("nodeX","foo")
|
should(value).be.null();
|
||||||
}).then(function(result){
|
done();
|
||||||
result.should.be.Object();
|
});
|
||||||
result.should.eql({obj:"bar"});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should store array',function() {
|
it('should store boolean',function(done) {
|
||||||
return context.get("nodeX","foo").should.be.finally.undefined()
|
context.get("nodeX","foo",function(err, value){
|
||||||
.then(function(){
|
should.not.exist(value);
|
||||||
return context.set("nodeX","foo",["a","b","c"]);
|
context.set("nodeX","foo",true,function(err){
|
||||||
}).then(function(){
|
context.get("nodeX","foo",function(err, value){
|
||||||
return context.get("nodeX","foo")
|
value.should.be.Boolean().and.true();
|
||||||
}).then(function(result){
|
context.set("nodeX","foo",false,function(err){
|
||||||
result.should.be.Array();
|
context.get("nodeX","foo",function(err, value){
|
||||||
result.should.eql(["a","b","c"]);
|
value.should.be.Boolean().and.false();
|
||||||
}).then(function(){
|
done();
|
||||||
return context.get("nodeX","foo[1]")
|
});
|
||||||
}).then(function(result){
|
});
|
||||||
result.should.be.String();
|
});
|
||||||
result.should.equal("b");
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should store array of arrays',function() {
|
it('should store object',function(done) {
|
||||||
return context.get("nodeX","foo").should.be.finally.undefined()
|
context.get("nodeX","foo",function(err, value){
|
||||||
.then(function(){
|
should.not.exist(value);
|
||||||
return context.set("nodeX","foo",[["a","b","c"],[1,2,3,4],[true,false]]);
|
context.set("nodeX","foo",{obj:"bar"},function(err){
|
||||||
}).then(function(){
|
context.get("nodeX","foo",function(err, value){
|
||||||
return context.get("nodeX","foo")
|
value.should.be.Object();
|
||||||
}).then(function(result){
|
value.should.eql({obj:"bar"});
|
||||||
result.should.be.Array();
|
done();
|
||||||
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 array of objects',function() {
|
it('should store array',function(done) {
|
||||||
return context.get("nodeX","foo").should.be.finally.undefined()
|
context.get("nodeX","foo",function(err, value){
|
||||||
.then(function(){
|
should.not.exist(value);
|
||||||
return context.set("nodeX","foo",[{obj:"bar1"},{obj:"bar2"},{obj:"bar3"}]);
|
context.set("nodeX","foo",["a","b","c"],function(err){
|
||||||
}).then(function(){
|
context.get("nodeX","foo",function(err, value){
|
||||||
return context.get("nodeX","foo")
|
value.should.be.Array();
|
||||||
}).then(function(result){
|
value.should.eql(["a","b","c"]);
|
||||||
result.should.be.Array();
|
context.get("nodeX","foo[1]",function(err, value){
|
||||||
result.should.have.length(3);
|
value.should.be.String();
|
||||||
result[0].should.be.Object();
|
value.should.equal("b");
|
||||||
result[1].should.be.Object();
|
done();
|
||||||
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 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() {
|
describe('#keys',function() {
|
||||||
it('should enumerate context keys', function() {
|
it('should enumerate context keys', function(done) {
|
||||||
return context.keys("nodeX").then(function(result){
|
context.keys("nodeX",function(err, value){
|
||||||
result.should.be.an.Array();
|
value.should.be.an.Array();
|
||||||
result.should.be.empty();
|
value.should.be.empty();
|
||||||
}).then(function(){
|
context.set("nodeX","foo","bar",function(err){
|
||||||
return context.set("nodeX","foo","bar");
|
context.keys("nodeX",function(err, value){
|
||||||
}).then(function(){
|
value.should.have.length(1);
|
||||||
return context.keys("nodeX").then(function(result){
|
value[0].should.equal("foo");
|
||||||
result.should.have.length(1);
|
context.set("nodeX","abc.def","bar",function(err){
|
||||||
result[0].should.equal("foo");
|
context.keys("nodeX",function(err, value){
|
||||||
});
|
value.should.have.length(2);
|
||||||
}).then(function(){
|
value[1].should.equal("abc");
|
||||||
return context.set("nodeX","abc.def","bar");
|
done();
|
||||||
}).then(function(){
|
});
|
||||||
return context.keys("nodeX").then(function(result){
|
});
|
||||||
result.should.have.length(2);
|
});
|
||||||
result[1].should.equal("abc");
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should enumerate context keys in each scopes', function() {
|
it('should enumerate context keys in each scopes', function(done) {
|
||||||
return Promise.all([context.keys("nodeX"),
|
context.keys("nodeX",function(err, value){
|
||||||
context.keys("nodeY")
|
value.should.be.an.Array();
|
||||||
]).then(function(results){
|
value.should.be.empty();
|
||||||
results[0].should.be.an.Array();
|
context.keys("nodeY",function(err, value){
|
||||||
results[0].should.be.empty();
|
value.should.be.an.Array();
|
||||||
results[1].should.be.an.Array();
|
value.should.be.empty();
|
||||||
results[1].should.be.empty();
|
context.set("nodeX","foo","bar",function(err){
|
||||||
}).then(function(){
|
context.set("nodeY","hoge","piyo",function(err){
|
||||||
return Promise.all([context.set("nodeX","foo","bar"),
|
context.keys("nodeX",function(err, value){
|
||||||
context.set("nodeY","hoge","piyo")]);
|
value.should.have.length(1);
|
||||||
}).then(function(){
|
value[0].should.equal("foo");
|
||||||
return Promise.all([context.keys("nodeX"),
|
context.keys("nodeY",function(err, value){
|
||||||
context.keys("nodeY")]);
|
value.should.have.length(1);
|
||||||
}).then(function(results){
|
value[0].should.equal("hoge");
|
||||||
results[0].should.have.length(1);
|
done();
|
||||||
results[0][0].should.equal("foo");
|
});
|
||||||
results[1].should.have.length(1);
|
});
|
||||||
results[1][0].should.equal("hoge");
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('#delete',function() {
|
describe('#delete',function() {
|
||||||
it('should delete context',function() {
|
it('should delete context',function(done) {
|
||||||
return Promise.all([context.get("nodeX","foo").should.be.finally.undefined(),
|
context.get("nodeX","foo",function(err, value){
|
||||||
context.get("nodeY","foo").should.be.finally.undefined()
|
should.not.exist(value);
|
||||||
]).then(function(){
|
context.get("nodeY","foo",function(err, value){
|
||||||
return Promise.all([context.set("nodeX","foo","abc"),
|
should.not.exist(value);
|
||||||
context.set("nodeY","foo","abc")]);
|
context.set("nodeX","foo","testX",function(err){
|
||||||
}).then(function(){
|
context.set("nodeY","foo","testY",function(err){
|
||||||
return Promise.all([context.get("nodeX","foo").should.be.finally.equal("abc"),
|
context.get("nodeX","foo",function(err, value){
|
||||||
context.get("nodeY","foo").should.be.finally.equal("abc")])
|
value.should.be.equal("testX");
|
||||||
}).then(function(){
|
context.get("nodeY","foo",function(err, value){
|
||||||
return context.delete("nodeX");
|
value.should.be.equal("testY");
|
||||||
}).then(function(){
|
context.delete("nodeX").then(function(){
|
||||||
return Promise.all([context.get("nodeX","foo").should.be.finally.undefined(),
|
context.get("nodeX","foo",function(err, value){
|
||||||
context.get("nodeY","foo").should.be.finally.equal("abc")]);
|
should.not.exist(value);
|
||||||
|
context.get("nodeY","foo",function(err, value){
|
||||||
|
value.should.be.equal("testY");
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('#clean',function() {
|
describe('#clean',function() {
|
||||||
it('should clean unnecessary context',function() {
|
it('should clean unnecessary context',function(done) {
|
||||||
return Promise.all([context.get("nodeX","foo").should.be.finally.undefined(),
|
context.get("nodeX","foo",function(err, value){
|
||||||
context.get("nodeY","foo").should.be.finally.undefined()
|
should.not.exist(value);
|
||||||
]).then(function(){
|
context.get("nodeY","foo",function(err, value){
|
||||||
return Promise.all([context.set("nodeX","foo","abc"),
|
should.not.exist(value);
|
||||||
context.set("nodeY","foo","abc")]);
|
context.set("nodeX","foo","testX",function(err){
|
||||||
}).then(function(){
|
context.set("nodeY","foo","testY",function(err){
|
||||||
return Promise.all([context.get("nodeX","foo").should.be.finally.equal("abc"),
|
context.get("nodeX","foo",function(err, value){
|
||||||
context.get("nodeY","foo").should.be.finally.equal("abc")])
|
value.should.be.equal("testX");
|
||||||
}).then(function(){
|
context.get("nodeY","foo",function(err, value){
|
||||||
return context.clean([]);
|
value.should.be.equal("testY");
|
||||||
}).then(function(){
|
context.clean([]).then(function(){
|
||||||
return Promise.all([context.get("nodeX","foo").should.be.finally.undefined(),
|
context.get("nodeX","foo",function(err, value){
|
||||||
context.get("nodeY","foo").should.be.finally.undefined()]);
|
should.not.exist(value);
|
||||||
|
context.get("nodeY","foo",function(err, value){
|
||||||
|
should.not.exist(value);
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should not clean active context',function() {
|
it('should not clean active context',function(done) {
|
||||||
return Promise.all([context.get("nodeX","foo").should.be.finally.undefined(),
|
context.get("nodeX","foo",function(err, value){
|
||||||
context.get("nodeY","foo").should.be.finally.undefined()
|
should.not.exist(value);
|
||||||
]).then(function(){
|
context.get("nodeY","foo",function(err, value){
|
||||||
return Promise.all([context.set("nodeX","foo","abc"),
|
should.not.exist(value);
|
||||||
context.set("nodeY","foo","abc")]);
|
context.set("nodeX","foo","testX",function(err){
|
||||||
}).then(function(){
|
context.set("nodeY","foo","testY",function(err){
|
||||||
return Promise.all([context.get("nodeX","foo").should.be.finally.equal("abc"),
|
context.get("nodeX","foo",function(err, value){
|
||||||
context.get("nodeY","foo").should.be.finally.equal("abc")])
|
value.should.be.equal("testX");
|
||||||
}).then(function(){
|
context.get("nodeY","foo",function(err, value){
|
||||||
return context.clean(["nodeX"]);
|
value.should.be.equal("testY");
|
||||||
}).then(function(){
|
context.clean(["nodeX"]).then(function(){
|
||||||
return Promise.all([context.get("nodeX","foo").should.be.finally.equal("abc"),
|
context.get("nodeX","foo",function(err, value){
|
||||||
context.get("nodeY","foo").should.be.finally.undefined()]);
|
value.should.be.equal("testX");
|
||||||
|
context.get("nodeY","foo",function(err, value){
|
||||||
|
should.not.exist(value);
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user