mirror of
https://github.com/node-red/node-red.git
synced 2023-10-10 13:36:53 +02:00
Add test cases accessing context with JSONata to Sort Node
This commit is contained in:
parent
83a8979309
commit
13d887028a
@ -18,20 +18,32 @@ var should = require("should");
|
|||||||
var sortNode = require("../../../../nodes/core/logic/18-sort.js");
|
var sortNode = require("../../../../nodes/core/logic/18-sort.js");
|
||||||
var helper = require("node-red-node-test-helper");
|
var helper = require("node-red-node-test-helper");
|
||||||
var RED = require("../../../../red/red.js");
|
var RED = require("../../../../red/red.js");
|
||||||
|
var Context = require("../../../../red/runtime/nodes/context");
|
||||||
|
|
||||||
describe('SORT node', function() {
|
describe('SORT node', function() {
|
||||||
|
|
||||||
before(function(done) {
|
beforeEach(function(done) {
|
||||||
helper.startServer(done);
|
helper.startServer(done);
|
||||||
});
|
});
|
||||||
|
|
||||||
after(function(done) {
|
function initContext(done) {
|
||||||
helper.stopServer(done);
|
Context.init({
|
||||||
});
|
contextStorage: {
|
||||||
|
memory: {
|
||||||
|
module: "memory"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
Context.load().then(function () {
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
afterEach(function() {
|
afterEach(function(done) {
|
||||||
helper.unload();
|
helper.unload().then(function(){
|
||||||
RED.settings.nodeMessageBufferMaxLength = 0;
|
RED.settings.nodeMessageBufferMaxLength = 0;
|
||||||
|
helper.stopServer(done);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should be loaded', function(done) {
|
it('should be loaded', function(done) {
|
||||||
@ -48,8 +60,8 @@ describe('SORT node', function() {
|
|||||||
var sort = flow[0];
|
var sort = flow[0];
|
||||||
sort.target = target;
|
sort.target = target;
|
||||||
sort.targetType = "msg";
|
sort.targetType = "msg";
|
||||||
sort.msgKey = key;
|
sort.msgKey = key;
|
||||||
sort.msgKeyType = key_type;
|
sort.msgKeyType = key_type;
|
||||||
helper.load(sortNode, flow, function() {
|
helper.load(sortNode, flow, function() {
|
||||||
var n1 = helper.getNode("n1");
|
var n1 = helper.getNode("n1");
|
||||||
var n2 = helper.getNode("n2");
|
var n2 = helper.getNode("n2");
|
||||||
@ -213,7 +225,7 @@ describe('SORT node', function() {
|
|||||||
check_sort1C(flow, "$substring(payload,1)", data_in, data_out, done);
|
check_sort1C(flow, "$substring(payload,1)", data_in, data_out, done);
|
||||||
});
|
});
|
||||||
})();
|
})();
|
||||||
|
|
||||||
(function() {
|
(function() {
|
||||||
var flow = [{id:"n1", type:"sort", order:"descending", as_num:false, wires:[["n2"]]},
|
var flow = [{id:"n1", type:"sort", order:"descending", as_num:false, wires:[["n2"]]},
|
||||||
{id:"n2", type:"helper"}];
|
{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) {
|
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"]]},
|
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"}];
|
{id:"n2", type:"helper"}];
|
||||||
@ -248,7 +388,7 @@ describe('SORT node', function() {
|
|||||||
n1.receive(msg1);
|
n1.receive(msg1);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should handle too many pending messages', function(done) {
|
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"]]},
|
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"}];
|
{id:"n2", type:"helper"}];
|
||||||
|
Loading…
Reference in New Issue
Block a user