From 13d887028a0f94e5141daa4db2c8fbacb33cad5a Mon Sep 17 00:00:00 2001 From: HirokiUchikawa Date: Tue, 17 Jul 2018 18:43:10 +0900 Subject: [PATCH] Add test cases accessing context with JSONata to Sort Node --- test/nodes/core/logic/18-sort_spec.js | 162 ++++++++++++++++++++++++-- 1 file changed, 151 insertions(+), 11 deletions(-) diff --git a/test/nodes/core/logic/18-sort_spec.js b/test/nodes/core/logic/18-sort_spec.js index e92974523..993cec8d2 100644 --- a/test/nodes/core/logic/18-sort_spec.js +++ b/test/nodes/core/logic/18-sort_spec.js @@ -18,20 +18,32 @@ var should = require("should"); var sortNode = require("../../../../nodes/core/logic/18-sort.js"); var helper = require("node-red-node-test-helper"); var RED = require("../../../../red/red.js"); +var Context = require("../../../../red/runtime/nodes/context"); describe('SORT node', function() { - before(function(done) { + beforeEach(function(done) { helper.startServer(done); }); - after(function(done) { - helper.stopServer(done); - }); + function initContext(done) { + Context.init({ + contextStorage: { + memory: { + module: "memory" + } + } + }); + Context.load().then(function () { + done(); + }); + } - afterEach(function() { - helper.unload(); - RED.settings.nodeMessageBufferMaxLength = 0; + afterEach(function(done) { + helper.unload().then(function(){ + RED.settings.nodeMessageBufferMaxLength = 0; + helper.stopServer(done); + }); }); it('should be loaded', function(done) { @@ -48,8 +60,8 @@ describe('SORT node', function() { var sort = flow[0]; sort.target = target; sort.targetType = "msg"; - sort.msgKey = key; - sort.msgKeyType = key_type; + sort.msgKey = key; + sort.msgKeyType = key_type; helper.load(sortNode, flow, function() { var n1 = helper.getNode("n1"); var n2 = helper.getNode("n2"); @@ -213,7 +225,7 @@ describe('SORT node', function() { check_sort1C(flow, "$substring(payload,1)", data_in, data_out, done); }); })(); - + (function() { var flow = [{id:"n1", type:"sort", order:"descending", as_num:false, wires:[["n2"]]}, {id:"n2", type:"helper"}]; @@ -227,6 +239,134 @@ describe('SORT node', function() { }); })(); + it('should sort payload by context (exp, not number, ascending)', function(done) { + var flow = [{id:"n1", type:"sort", target:"data", targetType:"msg", msgKey:"$flowContext($)", msgKeyType:"jsonata", order:"ascending", as_num:false, wires:[["n2"]],z:"flow"}, + {id:"n2", type:"helper",z:"flow"}, + {id:"flow", type:"tab"}]; + var data_in = [ "first", "second", "third", "fourth" ]; + var data_out = [ "second", "third", "first", "fourth" ]; + helper.load(sortNode, flow, function() { + var n1 = helper.getNode("n1"); + var n2 = helper.getNode("n2"); + n1.context()["flow"].set("first","3"); + n1.context()["flow"].set("second","1"); + n1.context()["flow"].set("third","2"); + n1.context()["flow"].set("fourth","4"); + n2.on("input", function(msg) { + msg.should.have.property("data"); + var data = msg["data"]; + data.length.should.equal(data_out.length); + for(var i = 0; i < data_out.length; i++) { + data[i].should.equal(data_out[i]); + } + done(); + }); + var msg = {}; + msg["data"] = data_in; + n1.receive(msg); + }); + }); + + it('should sort message group by context (exp, not number, ascending)', function(done) { + var flow = [{id:"n1", type:"sort", target:"data", targetType:"msg", msgKey:"$globalContext(payload)", msgKeyType:"jsonata", order:"ascending", as_num:false, wires:[["n2"]],z:"flow"}, + {id:"n2", type:"helper",z:"flow"}, + {id:"flow", type:"tab"}]; + var data_in = [ "first", "second", "third", "fourth" ]; + var data_out = [ "second", "fourth", "third", "first" ]; + helper.load(sortNode, flow, function() { + var n1 = helper.getNode("n1"); + var n2 = helper.getNode("n2"); + var count = 0; + n1.context()["global"].set("first","4"); + n1.context()["global"].set("second","1"); + n1.context()["global"].set("third","3"); + n1.context()["global"].set("fourth","2"); + n2.on("input", function(msg) { + msg.should.have.property("payload"); + msg.should.have.property("parts"); + msg.parts.should.have.property("count", data_out.length); + var data = msg["payload"]; + var index = data_out.indexOf(data); + msg.parts.should.have.property("index", index); + count++; + if (count === data_out.length) { + done(); + } + }); + var len = data_in.length; + for(var i = 0; i < len; i++) { + var parts = { id: "X", index: i, count: len }; + var msg = {parts: parts}; + msg["payload"] = data_in[i]; + n1.receive(msg); + } + }); + }); + + it('should sort payload by persistable context (exp, not number, descending)', function(done) { + var flow = [{id:"n1", type:"sort", target:"data", targetType:"msg", msgKey:"$globalContext($,\"memory\")", msgKeyType:"jsonata", order:"descending", as_num:false, wires:[["n2"]],z:"flow"}, + {id:"n2", type:"helper",z:"flow"}, + {id:"flow", type:"tab"}]; + var data_in = [ "first", "second", "third", "fourth" ]; + var data_out = [ "fourth", "first", "third", "second" ]; + helper.load(sortNode, flow, function() { + initContext(function(){ + var n1 = helper.getNode("n1"); + var n2 = helper.getNode("n2"); + n1.context()["global"].set(["first","second","third","fourth"],["3","1","2","4"],"memory",function(){ + n2.on("input", function(msg) { + msg.should.have.property("data"); + var data = msg["data"]; + data.length.should.equal(data_out.length); + for(var i = 0; i < data_out.length; i++) { + data[i].should.equal(data_out[i]); + } + done(); + }); + var msg = {}; + msg["data"] = data_in; + n1.receive(msg); + }); + }); + }); + }); + + it('should sort message group by persistable context (exp, not number, descending)', function(done) { + var flow = [{id:"n1", type:"sort", target:"data", targetType:"msg", msgKey:"$flowContext(payload,\"memory\")", msgKeyType:"jsonata", order:"descending", as_num:false, wires:[["n2"]],z:"flow"}, + {id:"n2", type:"helper",z:"flow"}, + {id:"flow", type:"tab"}]; + var data_in = [ "first", "second", "third", "fourth" ]; + var data_out = [ "first", "third", "fourth", "second" ]; + helper.load(sortNode, flow, function() { + initContext(function(){ + var n1 = helper.getNode("n1"); + var n2 = helper.getNode("n2"); + var count = 0; + n1.context()["flow"].set(["first","second","third","fourth"],["4","1","3","2"],"memory",function(){ + n2.on("input", function(msg) { + msg.should.have.property("payload"); + msg.should.have.property("parts"); + msg.parts.should.have.property("count", data_out.length); + var data = msg["payload"]; + var index = data_out.indexOf(data); + msg.parts.should.have.property("index", index); + count++; + if (count === data_out.length) { + done(); + } + }); + var len = data_in.length; + for(var i = 0; i < len; i++) { + var parts = { id: "X", index: i, count: len }; + var msg = {parts: parts}; + msg["payload"] = data_in[i]; + n1.receive(msg); + } + }); + }); + }); + }); + it('should handle JSONata script error', function(done) { var flow = [{id:"n1", type:"sort", order:"ascending", as_num:false, target:"payload", targetType:"seq", seqKey:"$unknown()", seqKeyType:"jsonata", wires:[["n2"]]}, {id:"n2", type:"helper"}]; @@ -248,7 +388,7 @@ describe('SORT node', function() { n1.receive(msg1); }); }); - + it('should handle too many pending messages', function(done) { var flow = [{id:"n1", type:"sort", order:"ascending", as_num:false, target:"payload", targetType:"seq", seqKey:"payload", seqKeyType:"msg", wires:[["n2"]]}, {id:"n2", type:"helper"}];