mirror of
https://github.com/node-red/node-red.git
synced 2025-03-01 10:36:34 +00:00
Refactor parseKey and implement parseStorage
This commit is contained in:
committed by
HirokiUchikawa
parent
3a476ac493
commit
e046fc1ac5
@@ -163,21 +163,6 @@ describe('context', function() {
|
||||
config:{}
|
||||
}
|
||||
};
|
||||
var contextDefaultStorage={
|
||||
default: "test",
|
||||
test:{
|
||||
module: {
|
||||
init: function() {
|
||||
return true;
|
||||
},
|
||||
get: stubGet,
|
||||
set: stubSet,
|
||||
keys: stubKeys,
|
||||
delete: stubDelete
|
||||
},
|
||||
config:{}
|
||||
}
|
||||
};
|
||||
|
||||
afterEach(function(done) {
|
||||
stubGet.reset();
|
||||
@@ -193,7 +178,68 @@ describe('context', function() {
|
||||
context = Context.get("1","flow");
|
||||
}
|
||||
|
||||
describe('key name',function() {
|
||||
var memoryStorage = {
|
||||
memory: {
|
||||
module: "memory"
|
||||
}
|
||||
};
|
||||
beforeEach(function() {
|
||||
Context.init({contextStorage:memoryStorage});
|
||||
Context.load();
|
||||
context = Context.get("1","flow");
|
||||
});
|
||||
afterEach(function() {
|
||||
Context.clean({allNodes:{}});
|
||||
});
|
||||
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");
|
||||
});
|
||||
it('should treat the key name without dot as a normal context',function() {
|
||||
context.set("$memory","normal");
|
||||
context.get("$memory").should.eql("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();
|
||||
});
|
||||
});
|
||||
|
||||
describe('if external context storage exists',function() {
|
||||
var contextDefaultStorage={
|
||||
default: "test",
|
||||
test:{
|
||||
module: {
|
||||
init: function() {
|
||||
return true;
|
||||
},
|
||||
get: stubGet,
|
||||
set: stubSet,
|
||||
keys: stubKeys,
|
||||
delete: stubDelete
|
||||
},
|
||||
config:{}
|
||||
}
|
||||
};
|
||||
|
||||
it('should store local property to external context storage',function() {
|
||||
initializeContext();
|
||||
should.not.exist(context.get("$test.foo"));
|
||||
@@ -224,7 +270,7 @@ describe('context', function() {
|
||||
stubSet.called.should.be.true();
|
||||
stubKeys.called.should.be.true();
|
||||
});
|
||||
it('should store data on default context', function() {
|
||||
it('should store data when non-existent context storage was specified', function() {
|
||||
Context.init({contextStorage:contextDefaultStorage});
|
||||
Context.load();
|
||||
context = Context.get("1","flow");
|
||||
@@ -236,12 +282,39 @@ describe('context', function() {
|
||||
stubSet.called.should.be.true();
|
||||
stubKeys.called.should.be.true();
|
||||
});
|
||||
it('should use the default context', function() {
|
||||
Context.init({contextStorage:contextDefaultStorage});
|
||||
Context.load();
|
||||
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.true();
|
||||
stubSet.called.should.be.true();
|
||||
stubKeys.called.should.be.true();
|
||||
});
|
||||
it('should use the alias of default context', function() {
|
||||
Context.init({contextStorage:contextDefaultStorage});
|
||||
Context.load();
|
||||
context = Context.get("1","flow");
|
||||
should.not.exist(context.get("$.foo"));
|
||||
context.set("$.foo","alias");
|
||||
context.get("$.foo");
|
||||
context.keys("$");
|
||||
stubGet.called.should.be.true();
|
||||
stubSet.called.should.be.true();
|
||||
stubKeys.called.should.be.true();
|
||||
});
|
||||
it('should load memory module', function(done) {
|
||||
Context.init({ contextStorage: { _: {}}});
|
||||
try {
|
||||
Context.load();
|
||||
context.set("_.foo","test");
|
||||
context.get("$_.foo");
|
||||
context.set("$_.foo","mem1");
|
||||
context.get("$_.foo").should.eql("mem1");
|
||||
var keys = context.keys("$_");
|
||||
keys.should.have.length(1);
|
||||
keys[0].should.eql("foo");
|
||||
done();
|
||||
} catch (err) {
|
||||
done(err);
|
||||
@@ -256,6 +329,27 @@ describe('context', function() {
|
||||
done(err);
|
||||
}
|
||||
});
|
||||
it('should accept special storage name', function(done) {
|
||||
Context.init({
|
||||
contextStorage:{
|
||||
"#%&":{module:"memory"},
|
||||
\u3042:{module:"memory"},
|
||||
1:{module:"localfilesystem"},
|
||||
}
|
||||
});
|
||||
try {
|
||||
Context.load();
|
||||
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");
|
||||
done();
|
||||
} catch (err) {
|
||||
done(err);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe('if external context storage does not exist',function() {
|
||||
@@ -285,18 +379,11 @@ describe('context', function() {
|
||||
}
|
||||
}
|
||||
});
|
||||
it('should fail when using invalid default context', function(done) {
|
||||
it('should fail when using invalid default context', function() {
|
||||
Context.init({contextStorage:{default:"noexist"}});
|
||||
try {
|
||||
(function() {
|
||||
Context.load();
|
||||
try {
|
||||
should.fail(null, null, "An error was not thrown using undefined storage for flow context");
|
||||
} catch (err) {
|
||||
done(err);
|
||||
}
|
||||
} catch (err) {
|
||||
done();
|
||||
}
|
||||
}).should.throw();
|
||||
});
|
||||
it('should store data on memory when contextStorage is not defined', function() {
|
||||
Context.init({});
|
||||
@@ -309,31 +396,17 @@ describe('context', function() {
|
||||
context.global.set("$nonexist.key1", "val3");
|
||||
context.global.get("$nonexist.key1").should.eql("val3");
|
||||
});
|
||||
it('should fail for the storage with no module', function(done) {
|
||||
it('should fail for the storage with no module', function() {
|
||||
Context.init({ contextStorage: { test: {}}});
|
||||
try {
|
||||
(function() {
|
||||
Context.load();
|
||||
try {
|
||||
should.fail(null, null, "Should fail when no module was specified");
|
||||
} catch (err) {
|
||||
done(err);
|
||||
}
|
||||
} catch (err) {
|
||||
done();
|
||||
}
|
||||
}).should.throw();
|
||||
});
|
||||
it('should fail to load non-existent module', function(done) {
|
||||
it('should fail to load non-existent module', function() {
|
||||
Context.init({contextStorage:{ file:{module:"nonexistent"} }});
|
||||
try {
|
||||
(function() {
|
||||
Context.load();
|
||||
try {
|
||||
should.fail(null, null, "Should fail to load non-existent module");
|
||||
} catch (err) {
|
||||
done(err);
|
||||
}
|
||||
} catch (err) {
|
||||
done();
|
||||
}
|
||||
}).should.throw();
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -347,12 +420,6 @@ describe('context', function() {
|
||||
result.key.should.eql(expectedKey);
|
||||
};
|
||||
|
||||
function returnModule(input, expectedModule) {
|
||||
var result = parseKey(input);
|
||||
result.storage.should.eql(expectedModule);
|
||||
should(result.key).be.null();
|
||||
};
|
||||
|
||||
it('should return module and key', function() {
|
||||
returnModuleAndKey("$test.aaa","test","aaa");
|
||||
returnModuleAndKey("$test.aaa.bbb","test","aaa.bbb");
|
||||
@@ -362,32 +429,48 @@ describe('context', function() {
|
||||
returnModuleAndKey("$test.$foo.$bar","test","$foo.$bar");
|
||||
returnModuleAndKey("$test..foo","test",".foo");
|
||||
returnModuleAndKey("$test..","test",".");
|
||||
returnModuleAndKey("$te-_st.aaa","te-_st","aaa");
|
||||
returnModuleAndKey("$te{st.a2","te{st","a2");
|
||||
returnModuleAndKey("$te[st.a3","te[st","a3");
|
||||
returnModuleAndKey("$te'st.a4","te'st","a4");
|
||||
returnModuleAndKey("$te\"st.a5","te\"st","a5");
|
||||
});
|
||||
|
||||
// it('should return only module', function() {
|
||||
// returnModule("$test","test",null);
|
||||
// returnModule("$1","1",null);
|
||||
// returnModule("$$test","$test",null);
|
||||
// returnModule("$test.","test.",null);
|
||||
// });
|
||||
|
||||
it('should return module as default', function() {
|
||||
returnModuleAndKey("$default.foo","default","foo");
|
||||
returnModuleAndKey("$.foo","default","foo");
|
||||
// returnModule("$default","default");
|
||||
// returnModule("$","default");
|
||||
});
|
||||
|
||||
it('should return null', function() {
|
||||
var keyPath = parseKey("test.aaa");
|
||||
keyPath.storage.should.eql("");
|
||||
keyPath.key.should.eql("test.aaa");
|
||||
it('should return only keys', function() {
|
||||
returnModuleAndKey("test.aaa", "", "test.aaa");
|
||||
returnModuleAndKey("test", "", "test");
|
||||
returnModuleAndKey("$test", "", "$test");
|
||||
});
|
||||
|
||||
keyPath = parseKey("test");
|
||||
keyPath.storage.should.eql("");
|
||||
keyPath.key.should.eql("test");
|
||||
it('should fail with null key', function() {
|
||||
(function() {
|
||||
parseKey("");
|
||||
}).should.throw();
|
||||
|
||||
should(parseKey(null)).be.null();
|
||||
(function() {
|
||||
parseKey(null);
|
||||
}).should.throw();
|
||||
});
|
||||
|
||||
it('should fail with space character', function() {
|
||||
(function() {
|
||||
parseKey(" $test");
|
||||
}).should.throw();
|
||||
|
||||
(function() {
|
||||
parseKey("$test .a");
|
||||
}).should.throw();
|
||||
});
|
||||
|
||||
it('should fail with empty key', function() {
|
||||
(function() {
|
||||
parseKey("$test.");
|
||||
}).should.throw();
|
||||
});
|
||||
});
|
||||
|
||||
|
Reference in New Issue
Block a user