Add test cases for context runtime API

This commit is contained in:
Hiroki Uchikawa 2019-01-18 19:53:15 +09:00
parent f98f4085bf
commit f7c87e26db
1 changed files with 200 additions and 60 deletions

View File

@ -19,7 +19,7 @@ var should = require("should");
var sinon = require("sinon"); var sinon = require("sinon");
var NR_TEST_UTILS = require("nr-test-utils"); var NR_TEST_UTILS = require("nr-test-utils");
var context = NR_TEST_UTILS.require("@node-red/runtime/lib/api/context") var context = NR_TEST_UTILS.require("@node-red/runtime/lib/api/context");
var mockLog = () => ({ var mockLog = () => ({
log: sinon.stub(), log: sinon.stub(),
@ -29,8 +29,8 @@ var mockLog = () => ({
info: sinon.stub(), info: sinon.stub(),
metric: sinon.stub(), metric: sinon.stub(),
audit: sinon.stub(), audit: sinon.stub(),
_: function() { return "abc"} _: function() { return "abc";}
}) });
var mockContext = function(contents) { var mockContext = function(contents) {
return { return {
@ -41,51 +41,65 @@ var mockContext = function(contents) {
callback(null,undefined); callback(null,undefined);
} }
}, },
keys: function(store,callback) { set: function (key, value, store, callback) {
if (contents.hasOwnProperty(store)) { if (contents.hasOwnProperty(store)) {
callback(null,Object.keys(contents[store])); if (!value) {
delete contents[store][key];
callback(null);
}
} else {
callback("err store");
}
},
keys: function (store, callback) {
if (contents.hasOwnProperty(store)) {
callback(null, Object.keys(contents[store]));
} else { } else {
callback("err store"); callback("err store");
} }
} }
} };
} };
describe("runtime-api/context", function() { describe("runtime-api/context", function() {
describe("getValue", function() { var globalContext, flowContext, nodeContext, contexts;
var contexts = {
global: mockContext({ default: {abc:111}, file: {abc:222}}),
flow1: mockContext({ default: {abc:333}, file: {abc:444}})
}
var nodeContext = mockContext({ default: {abc:555}, file: {abc:666}})
beforeEach(function() { beforeEach(function() {
context.init({ globalContext = { default: { abc: 111 }, file: { abc: 222 } };
nodes: { flowContext = { default: { abc: 333 }, file: { abc: 444 } };
listContextStores: function() { nodeContext = { default: { abc: 555 }, file: { abc: 666 } };
return { default: 'default', stores: [ 'default', 'file' ] } contexts = {
}, global: mockContext(globalContext),
getContext: function(id) { flow1: mockContext(flowContext)
return contexts[id] };
}, context.init({
getNode: function(id) { nodes: {
if (id === 'known') { listContextStores: function() {
return { return { default: 'default', stores: [ 'default', 'file' ] };
context: function() { return nodeContext }
}
} else {
return null;
}
}
}, },
settings: { getContext: function(id) {
functionGlobalContext: { return contexts[id];
fgc:1234
}
}, },
log: mockLog() getNode: function(id) {
}) if (id === 'known') {
return {
context: function() { return mockContext(nodeContext); }
};
} else {
return null;
}
}
},
settings: {
functionGlobalContext: {
fgc:1234
}
},
log: mockLog()
}); });
});
describe("getValue", function() {
it('gets global value of default store', function() { it('gets global value of default store', function() {
return context.getValue({ return context.getValue({
scope: 'global', scope: 'global',
@ -95,8 +109,9 @@ describe("runtime-api/context", function() {
}).then(function(result) { }).then(function(result) {
result.should.have.property('msg','111'); result.should.have.property('msg','111');
result.should.have.property('format','number'); result.should.have.property('format','number');
}) });
}) });
it('gets global value of specified store', function() { it('gets global value of specified store', function() {
return context.getValue({ return context.getValue({
scope: 'global', scope: 'global',
@ -106,8 +121,9 @@ describe("runtime-api/context", function() {
}).then(function(result) { }).then(function(result) {
result.should.have.property('msg','222'); result.should.have.property('msg','222');
result.should.have.property('format','number'); result.should.have.property('format','number');
}) });
}) });
it('gets flow value of default store', function() { it('gets flow value of default store', function() {
return context.getValue({ return context.getValue({
scope: 'flow', scope: 'flow',
@ -117,8 +133,9 @@ describe("runtime-api/context", function() {
}).then(function(result) { }).then(function(result) {
result.should.have.property('msg','333'); result.should.have.property('msg','333');
result.should.have.property('format','number'); result.should.have.property('format','number');
}) });
}) });
it('gets flow value of specified store', function() { it('gets flow value of specified store', function() {
return context.getValue({ return context.getValue({
scope: 'flow', scope: 'flow',
@ -128,8 +145,9 @@ describe("runtime-api/context", function() {
}).then(function(result) { }).then(function(result) {
result.should.have.property('msg','444'); result.should.have.property('msg','444');
result.should.have.property('format','number'); result.should.have.property('format','number');
}) });
}) });
it('gets node value of default store', function() { it('gets node value of default store', function() {
return context.getValue({ return context.getValue({
scope: 'node', scope: 'node',
@ -139,8 +157,9 @@ describe("runtime-api/context", function() {
}).then(function(result) { }).then(function(result) {
result.should.have.property('msg','555'); result.should.have.property('msg','555');
result.should.have.property('format','number'); result.should.have.property('format','number');
}) });
}) });
it('gets node value of specified store', function() { it('gets node value of specified store', function() {
return context.getValue({ return context.getValue({
scope: 'node', scope: 'node',
@ -150,8 +169,8 @@ describe("runtime-api/context", function() {
}).then(function(result) { }).then(function(result) {
result.should.have.property('msg','666'); result.should.have.property('msg','666');
result.should.have.property('format','number'); result.should.have.property('format','number');
}) });
}) });
it('404s for unknown store', function(done) { it('404s for unknown store', function(done) {
context.getValue({ context.getValue({
@ -162,12 +181,11 @@ describe("runtime-api/context", function() {
}).then(function(result) { }).then(function(result) {
done("getValue for unknown store should not resolve"); done("getValue for unknown store should not resolve");
}).catch(function(err) { }).catch(function(err) {
err.should.have.property('code','not_found') err.should.have.property('code','not_found');
err.should.have.property('status',404); err.should.have.property('status',404);
done(); done();
}) });
}) });
it('gets all global value properties', function() { it('gets all global value properties', function() {
return context.getValue({ return context.getValue({
@ -180,8 +198,9 @@ describe("runtime-api/context", function() {
default: { abc: { msg: '111', format: 'number' } }, default: { abc: { msg: '111', format: 'number' } },
file: { abc: { msg: '222', format: 'number' } } file: { abc: { msg: '222', format: 'number' } }
}); });
}) });
}) });
it('gets all flow value properties', function() { it('gets all flow value properties', function() {
return context.getValue({ return context.getValue({
scope: 'flow', scope: 'flow',
@ -193,8 +212,9 @@ describe("runtime-api/context", function() {
default: { abc: { msg: '333', format: 'number' } }, default: { abc: { msg: '333', format: 'number' } },
file: { abc: { msg: '444', format: 'number' } } file: { abc: { msg: '444', format: 'number' } }
}); });
}) });
}) });
it('gets all node value properties', function() { it('gets all node value properties', function() {
return context.getValue({ return context.getValue({
scope: 'node', scope: 'node',
@ -206,8 +226,128 @@ describe("runtime-api/context", function() {
default: { abc: { msg: '555', format: 'number' } }, default: { abc: { msg: '555', format: 'number' } },
file: { abc: { msg: '666', format: 'number' } } file: { abc: { msg: '666', format: 'number' } }
}); });
}) });
}) });
}) it('gets empty object when specified context doesn\'t exist', function() {
return context.getValue({
scope: 'node',
id: 'non-existent',
store: 'file',
key: 'abc'
}).then(function(result) {
result.should.be.an.Object();
result.should.be.empty();
});
});
});
describe("delete", function () {
it('deletes global value of default store', function () {
return context.delete({
scope: 'global',
id: undefined,
store: undefined, // use default
key: 'abc'
}).then(function () {
globalContext.should.eql({
default: {}, file: { abc: 222 }
});
});
});
it('deletes global value of specified store', function () {
return context.delete({
scope: 'global',
id: undefined,
store: 'file',
key: 'abc'
}).then(function () {
globalContext.should.eql({
default: { abc: 111 }, file: {}
});
});
});
it('deletes flow value of default store', function () {
return context.delete({
scope: 'flow',
id: 'flow1',
store: undefined, // use default
key: 'abc'
}).then(function () {
flowContext.should.eql({
default: {}, file: { abc: 444 }
});
});
});
it('deletes flow value of specified store', function () {
return context.delete({
scope: 'flow',
id: 'flow1',
store: 'file',
key: 'abc'
}).then(function () {
flowContext.should.eql({
default: { abc: 333 }, file: {}
});
});
});
it('deletes node value of default store', function () {
return context.delete({
scope: 'node',
id: 'known',
store: undefined, // use default
key: 'abc'
}).then(function () {
nodeContext.should.eql({
default: {}, file: { abc: 666 }
});
});
});
it('deletes node value of specified store', function () {
return context.delete({
scope: 'node',
id: 'known',
store: 'file',
key: 'abc'
}).then(function () {
nodeContext.should.eql({
default: { abc: 555 }, file: {}
});
});
});
it('does nothing when specified context doesn\'t exist', function() {
return context.delete({
scope: 'node',
id: 'non-existent',
store: 'file',
key: 'abc'
}).then(function(result) {
should.not.exist(result);
nodeContext.should.eql({
default: { abc: 555 }, file: { abc: 666 }
});
});
});
it('404s for unknown store', function (done) {
context.delete({
scope: 'global',
id: undefined,
store: 'unknown',
key: 'abc'
}).then(function () {
done("delete for unknown store should not resolve");
}).catch(function (err) {
err.should.have.property('code', 'not_found');
err.should.have.property('status', 404);
done();
});
});
});
}); });