2018-03-23 09:18:56 +01:00
|
|
|
/**
|
|
|
|
* Copyright JS Foundation and other contributors, http://js.foundation
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
**/
|
|
|
|
|
|
|
|
var should = require('should');
|
2018-05-24 14:42:40 +02:00
|
|
|
var Memory = require('../../../../../red/runtime/nodes/context/memory');
|
2018-03-23 09:18:56 +01:00
|
|
|
|
|
|
|
describe('memory',function() {
|
2018-05-24 14:42:40 +02:00
|
|
|
var context;
|
2018-03-23 09:18:56 +01:00
|
|
|
|
|
|
|
beforeEach(function() {
|
2018-05-24 14:42:40 +02:00
|
|
|
context = Memory({});
|
2018-05-30 08:23:34 +02:00
|
|
|
return context.open();
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(function() {
|
|
|
|
return context.clean([]).then(function(){
|
|
|
|
return context.close();
|
|
|
|
});
|
2018-03-23 09:18:56 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('#get/set',function() {
|
2018-07-12 11:12:30 +02:00
|
|
|
describe('sync',function() {
|
|
|
|
it('should store property',function() {
|
|
|
|
should.not.exist(context.get("nodeX","foo"));
|
|
|
|
context.set("nodeX","foo","test");
|
|
|
|
context.get("nodeX","foo").should.equal("test");
|
|
|
|
});
|
2018-03-23 09:18:56 +01:00
|
|
|
|
2018-07-12 11:12:30 +02:00
|
|
|
it('should store property - creates parent properties',function() {
|
|
|
|
context.set("nodeX","foo.bar","test");
|
|
|
|
context.get("nodeX","foo").should.eql({bar:"test"});
|
|
|
|
});
|
2018-03-23 09:18:56 +01:00
|
|
|
|
2018-07-12 11:12:30 +02:00
|
|
|
it('should delete property',function() {
|
|
|
|
context.set("nodeX","foo.abc.bar1","test1");
|
|
|
|
context.set("nodeX","foo.abc.bar2","test2");
|
|
|
|
context.get("nodeX","foo.abc").should.eql({bar1:"test1",bar2:"test2"});
|
|
|
|
context.set("nodeX","foo.abc.bar1",undefined);
|
|
|
|
context.get("nodeX","foo.abc").should.eql({bar2:"test2"});
|
|
|
|
context.set("nodeX","foo.abc",undefined);
|
|
|
|
should.not.exist(context.get("nodeX","foo.abc"));
|
|
|
|
context.set("nodeX","foo",undefined);
|
|
|
|
should.not.exist(context.get("nodeX","foo"));
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should not shared context with other scope', function() {
|
|
|
|
should.not.exist(context.get("nodeX","foo"));
|
|
|
|
should.not.exist(context.get("nodeY","foo"));
|
|
|
|
context.set("nodeX","foo","testX");
|
|
|
|
context.set("nodeY","foo","testY");
|
|
|
|
|
|
|
|
context.get("nodeX","foo").should.equal("testX");
|
|
|
|
context.get("nodeY","foo").should.equal("testY");
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should thorw the error if the error occurs', function() {
|
|
|
|
try{
|
|
|
|
context.set("nodeX",".foo","test");
|
|
|
|
should.fail("Error was not thrown");
|
|
|
|
}catch(err){
|
|
|
|
should.exist(err);
|
|
|
|
try{
|
|
|
|
context.get("nodeX",".foo");
|
|
|
|
should.fail("Error was not thrown");
|
|
|
|
}catch(err){
|
|
|
|
should.exist(err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2018-03-23 09:18:56 +01:00
|
|
|
});
|
|
|
|
|
2018-07-12 11:12:30 +02:00
|
|
|
describe('async',function() {
|
|
|
|
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.equal("test");
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2018-03-23 09:18:56 +01:00
|
|
|
|
2018-07-12 11:12:30 +02:00
|
|
|
it('should pass the error to callback if the error occurs',function(done) {
|
|
|
|
context.set("nodeX",".foo","test",function(err, value){
|
|
|
|
should.exist(err);
|
|
|
|
context.get("nodeX",".foo",function(err){
|
|
|
|
should.exist(err);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2018-03-23 09:18:56 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('#keys',function() {
|
2018-07-12 11:12:30 +02:00
|
|
|
describe('sync',function() {
|
|
|
|
it('should enumerate context keys', function() {
|
|
|
|
var keys = context.keys("nodeX");
|
|
|
|
keys.should.be.an.Array();
|
|
|
|
keys.should.be.empty();
|
|
|
|
|
|
|
|
context.set("nodeX","foo","bar");
|
|
|
|
keys = context.keys("nodeX");
|
|
|
|
keys.should.have.length(1);
|
|
|
|
keys[0].should.equal("foo");
|
|
|
|
|
|
|
|
context.set("nodeX","abc.def","bar");
|
|
|
|
keys = context.keys("nodeX");
|
|
|
|
keys.should.have.length(2);
|
|
|
|
keys[1].should.equal("abc");
|
|
|
|
});
|
2018-03-23 09:18:56 +01:00
|
|
|
|
2018-07-12 11:12:30 +02:00
|
|
|
it('should enumerate context keys in each scopes', function() {
|
|
|
|
var keysX = context.keys("nodeX");
|
|
|
|
keysX.should.be.an.Array();
|
|
|
|
keysX.should.be.empty();
|
2018-03-23 09:18:56 +01:00
|
|
|
|
2018-07-12 11:12:30 +02:00
|
|
|
var keysY = context.keys("nodeY");
|
|
|
|
keysY.should.be.an.Array();
|
|
|
|
keysY.should.be.empty();
|
2018-03-23 09:18:56 +01:00
|
|
|
|
2018-07-12 11:12:30 +02:00
|
|
|
context.set("nodeX","foo","bar");
|
|
|
|
context.set("nodeY","hoge","piyo");
|
|
|
|
keysX = context.keys("nodeX");
|
|
|
|
keysX.should.have.length(1);
|
|
|
|
keysX[0].should.equal("foo");
|
2018-03-23 09:18:56 +01:00
|
|
|
|
2018-07-12 11:12:30 +02:00
|
|
|
keysY = context.keys("nodeY");
|
|
|
|
keysY.should.have.length(1);
|
|
|
|
keysY[0].should.equal("hoge");
|
|
|
|
});
|
2018-07-19 06:49:36 +02:00
|
|
|
|
|
|
|
it('should enumerate global context keys', function () {
|
|
|
|
var keys = context.keys("global");
|
|
|
|
keys.should.be.an.Array();
|
|
|
|
keys.should.be.empty();
|
|
|
|
|
|
|
|
context.set("global", "foo", "bar");
|
|
|
|
keys = context.keys("global");
|
|
|
|
keys.should.have.length(1);
|
|
|
|
keys[0].should.equal("foo");
|
|
|
|
|
|
|
|
context.set("global", "abc.def", "bar");
|
|
|
|
keys = context.keys("global");
|
|
|
|
keys.should.have.length(2);
|
|
|
|
keys[1].should.equal("abc");
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should not return specific keys as global context keys', function () {
|
|
|
|
var keys = context.keys("global");
|
|
|
|
|
|
|
|
context.set("global", "set", "bar");
|
|
|
|
context.set("global", "get", "bar");
|
|
|
|
context.set("global", "keys", "bar");
|
|
|
|
keys = context.keys("global");
|
|
|
|
keys.should.have.length(0);
|
|
|
|
});
|
2018-03-23 09:18:56 +01:00
|
|
|
});
|
|
|
|
|
2018-07-12 11:12:30 +02:00
|
|
|
describe('async',function() {
|
|
|
|
it('should enumerate context keys', function(done) {
|
|
|
|
context.keys("nodeX", function(err, keys) {
|
|
|
|
keys.should.be.an.Array();
|
|
|
|
keys.should.be.empty();
|
|
|
|
context.set("nodeX", "foo", "bar", function(err) {
|
|
|
|
context.keys("nodeX", function(err, keys) {
|
|
|
|
keys.should.have.length(1);
|
|
|
|
keys[0].should.equal("foo");
|
|
|
|
context.set("nodeX","abc.def","bar",function(err){
|
|
|
|
context.keys("nodeX",function(err, keys){
|
|
|
|
keys.should.have.length(2);
|
|
|
|
keys[1].should.equal("abc");
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2018-03-23 09:18:56 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('#delete',function() {
|
|
|
|
it('should delete context',function() {
|
2018-05-23 04:42:53 +02:00
|
|
|
should.not.exist(context.get("nodeX","foo"));
|
|
|
|
should.not.exist(context.get("nodeY","foo"));
|
|
|
|
context.set("nodeX","foo","abc");
|
|
|
|
context.set("nodeY","foo","abc");
|
2018-06-07 14:51:11 +02:00
|
|
|
context.get("nodeX","foo").should.equal("abc");
|
|
|
|
context.get("nodeY","foo").should.equal("abc");
|
2018-03-23 09:18:56 +01:00
|
|
|
|
2018-05-30 08:23:34 +02:00
|
|
|
return context.delete("nodeX").then(function(){
|
|
|
|
should.not.exist(context.get("nodeX","foo"));
|
|
|
|
should.exist(context.get("nodeY","foo"));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('#clean',function() {
|
|
|
|
it('should clean unnecessary context',function() {
|
|
|
|
should.not.exist(context.get("nodeX","foo"));
|
|
|
|
should.not.exist(context.get("nodeY","foo"));
|
|
|
|
context.set("nodeX","foo","abc");
|
|
|
|
context.set("nodeY","foo","abc");
|
2018-06-07 14:51:11 +02:00
|
|
|
context.get("nodeX","foo").should.equal("abc");
|
|
|
|
context.get("nodeY","foo").should.equal("abc");
|
2018-05-30 08:23:34 +02:00
|
|
|
|
|
|
|
return context.clean([]).then(function(){
|
|
|
|
should.not.exist(context.get("nodeX","foo"));
|
|
|
|
should.not.exist(context.get("nodeY","foo"));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
it('should not clean active context',function() {
|
2018-05-23 04:42:53 +02:00
|
|
|
should.not.exist(context.get("nodeX","foo"));
|
2018-05-30 08:23:34 +02:00
|
|
|
should.not.exist(context.get("nodeY","foo"));
|
|
|
|
context.set("nodeX","foo","abc");
|
|
|
|
context.set("nodeY","foo","abc");
|
2018-06-07 14:51:11 +02:00
|
|
|
context.get("nodeX","foo").should.equal("abc");
|
|
|
|
context.get("nodeY","foo").should.equal("abc");
|
2018-05-30 08:23:34 +02:00
|
|
|
|
|
|
|
return context.clean(["nodeX"]).then(function(){
|
|
|
|
should.exist(context.get("nodeX","foo"));
|
|
|
|
should.not.exist(context.get("nodeY","foo"));
|
|
|
|
});
|
2018-03-23 09:18:56 +01:00
|
|
|
});
|
2018-07-19 06:49:36 +02:00
|
|
|
it('should not clean global context', function () {
|
|
|
|
context.set("global", "foo", "abc");
|
|
|
|
context.get("global", "foo").should.equal("abc");
|
|
|
|
|
|
|
|
return context.clean(["global"]).then(function () {
|
|
|
|
should.exist(context.get("global", "foo"));
|
|
|
|
});
|
|
|
|
});
|
2018-03-23 09:18:56 +01:00
|
|
|
});
|
|
|
|
|
2018-07-02 16:21:13 +02:00
|
|
|
});
|