Merge pull request #1806 from node-red-hitachi/0.19-template-node-for-persistable-context

Add support of persistable context to template node
This commit is contained in:
Nick O'Leary
2018-07-14 22:40:50 +01:00
committed by GitHub
3 changed files with 252 additions and 41 deletions

View File

@@ -16,6 +16,7 @@
var should = require("should");
var templateNode = require("../../../../nodes/core/core/80-template.js");
var Context = require("../../../../red/runtime/nodes/context");
var helper = require("node-red-node-test-helper");
describe('template node', function() {
@@ -28,8 +29,32 @@ describe('template node', function() {
helper.stopServer(done);
});
beforeEach(function(done) {
done();
});
function initContext(done) {
Context.init({
contextStorage: {
memory1: {
module: "memory"
},
memory2: {
module: "memory"
}
}
});
Context.load().then(function () {
done();
});
}
afterEach(function() {
helper.unload();
helper.unload().then(function () {
return Context.clean({allNodes:{}});
}).then(function () {
return Context.close();
});
});
@@ -116,7 +141,6 @@ describe('template node', function() {
});
});
it('should modify payload from flow context', function(done) {
var flow = [{id:"n1",z:"t1", type:"template", field:"payload", template:"payload={{flow.value}}",wires:[["n2"]]},{id:"n2",z:"t1",type:"helper"}];
helper.load(templateNode, flow, function() {
@@ -132,6 +156,44 @@ describe('template node', function() {
});
});
it('should modify payload from persistable flow context', function(done) {
var flow = [{id:"n1",z:"t1", type:"template", field:"payload", template:"payload={{flow[memory1].value}}",wires:[["n2"]]},{id:"n2",z:"t1",type:"helper"}];
helper.load(templateNode, flow, function() {
initContext(function () {
var n1 = helper.getNode("n1");
var n2 = helper.getNode("n2");
n2.on("input", function(msg) {
msg.should.have.property('topic', 'bar');
msg.should.have.property('payload', 'payload=foo');
done();
});
n1.context().flow.set("value","foo","memory1",function (err) {
n1.receive({payload:"foo",topic: "bar"});
});
});
});
});
it('should modify payload from two persistable flow context', function(done) {
var flow = [{id:"n1",z:"t1", type:"template", field:"payload", template:"payload={{flow[memory1].value}}/{{flow[memory2].value}}",wires:[["n2"]]},{id:"n2",z:"t1",type:"helper"}];
helper.load(templateNode, flow, function() {
initContext(function() {
var n1 = helper.getNode("n1");
var n2 = helper.getNode("n2");
n2.on("input", function(msg) {
msg.should.have.property('topic', 'bar');
msg.should.have.property('payload', 'payload=foo/bar');
done();
});
n1.context().flow.set("value","foo","memory1",function (err) {
n1.context().flow.set("value","bar","memory2",function (err) {
n1.receive({payload:"foo",topic: "bar"});
});
});
});
});
});
it('should modify payload from global context', function(done) {
var flow = [{id:"n1",z:"t1", type:"template", field:"payload", template:"payload={{global.value}}",wires:[["n2"]]},{id:"n2",z:"t1",type:"helper"}];
helper.load(templateNode, flow, function() {
@@ -147,6 +209,64 @@ describe('template node', function() {
});
});
it('should modify payload from persistable global context', function(done) {
var flow = [{id:"n1",z:"t1", type:"template", field:"payload", template:"payload={{global[memory1].value}}",wires:[["n2"]]},{id:"n2",z:"t1",type:"helper"}];
helper.load(templateNode, flow, function() {
initContext(function () {
var n1 = helper.getNode("n1");
var n2 = helper.getNode("n2");
n2.on("input", function(msg) {
msg.should.have.property('topic', 'bar');
msg.should.have.property('payload', 'payload=foo');
done();
});
n1.context().global.set("value","foo","memory1", function (err) {
n1.receive({payload:"foo",topic: "bar"});
});
});
});
});
it('should modify payload from two persistable global context', function(done) {
var flow = [{id:"n1",z:"t1", type:"template", field:"payload", template:"payload={{global[memory1].value}}/{{global[memory2].value}}",wires:[["n2"]]},{id:"n2",z:"t1",type:"helper"}];
helper.load(templateNode, flow, function() {
initContext(function () {
var n1 = helper.getNode("n1");
var n2 = helper.getNode("n2");
n2.on("input", function(msg) {
msg.should.have.property('topic', 'bar');
msg.should.have.property('payload', 'payload=foo/bar');
done();
});
n1.context().global.set("value","foo","memory1", function (err) {
n1.context().global.set("value","bar","memory2", function (err) {
n1.receive({payload:"foo",topic: "bar"});
});
});
});
});
});
it('should modify payload from persistable flow & global context', function(done) {
var flow = [{id:"n1",z:"t1", type:"template", field:"payload", template:"payload={{flow[memory1].value}}/{{global[memory1].value}}",wires:[["n2"]]},{id:"n2",z:"t1",type:"helper"}];
helper.load(templateNode, flow, function() {
initContext(function () {
var n1 = helper.getNode("n1");
var n2 = helper.getNode("n2");
n2.on("input", function(msg) {
msg.should.have.property('topic', 'bar');
msg.should.have.property('payload', 'payload=foo/bar');
done();
});
n1.context().flow.set("value","foo","memory1", function (err) {
n1.context().global.set("value","bar","memory1", function (err) {
n1.receive({payload:"foo",topic: "bar"});
});
});
});
});
});
it('should handle missing node context', function(done) {
// this is artificial test because in flow there is missing z property (probably never happen in real usage)
var flow = [{id:"n1",type:"template", field:"payload", template:"payload={{flow.value}},{{global.value}}",wires:[["n2"]]},{id:"n2",type:"helper"}];
@@ -206,6 +326,27 @@ describe('template node', function() {
});
});
it('should modify persistable flow context', function(done) {
var flow = [{id:"n1",z:"t1", type:"template", field:"#:(memory1)::payload", fieldType:"flow", template:"payload={{payload}}",wires:[["n2"]]},{id:"n2",z:"t1",type:"helper"}];
helper.load(templateNode, flow, function() {
initContext(function () {
var n1 = helper.getNode("n1");
var n2 = helper.getNode("n2");
n2.on("input", function(msg) {
// mesage is intact
msg.should.have.property('topic', 'bar');
msg.should.have.property('payload', 'foo');
// result is in flow context
n2.context().flow.get("payload", "memory", function (err, val) {
val.should.equal("payload=foo");
done();
});
});
n1.receive({payload:"foo",topic: "bar"});
});
});
});
it('should modify global context', function(done) {
var flow = [{id:"n1",z:"t1", type:"template", field:"payload", fieldType:"global", template:"payload={{payload}}",wires:[["n2"]]},{id:"n2",z:"t1",type:"helper"}];
helper.load(templateNode, flow, function() {
@@ -223,6 +364,27 @@ describe('template node', function() {
});
});
it('should modify persistable global context', function(done) {
var flow = [{id:"n1",z:"t1", type:"template", field:"#:(memory1)::payload", fieldType:"global", template:"payload={{payload}}",wires:[["n2"]]},{id:"n2",z:"t1",type:"helper"}];
helper.load(templateNode, flow, function() {
initContext(function () {
var n1 = helper.getNode("n1");
var n2 = helper.getNode("n2");
n2.on("input", function(msg) {
// mesage is intact
msg.should.have.property('topic', 'bar');
msg.should.have.property('payload', 'foo');
// result is in global context
n2.context().global.get("payload", "memory", function (err, val) {
val.should.equal("payload=foo");
done();
});
});
n1.receive({payload:"foo",topic: "bar"});
});
});
});
it('should handle if the field isn\'t set', function(done) {
var flow = [{id:"n1", type:"template", template: "payload={{payload}}",wires:[["n2"]]},{id:"n2",type:"helper"}];
helper.load(templateNode, flow, function() {
@@ -264,6 +426,7 @@ describe('template node', function() {
n1.receive({payload:{A:"abc"}});
});
});
it('should raise error if passed bad template', function(done) {
var flow = [{id:"n1", type:"template", field: "payload", template: "payload={{payload",wires:[["n2"]]},{id:"n2",type:"helper"}];
helper.load(templateNode, flow, function() {