mirror of
https://github.com/node-red/node-red.git
synced 2025-03-01 10:36:34 +00:00
Let CSV node only send headers once
(and then reset that on msg.reset) and also accept msg.columns csv string to set column headers if not specified in node. And Add tests
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
/* eslint-disable no-undef */
|
||||
/**
|
||||
* Copyright JS Foundation and other contributors, http://js.foundation
|
||||
*
|
||||
@@ -70,12 +71,13 @@ describe('CSV node', function() {
|
||||
|
||||
it('should convert a simple csv string to a javascript object', function(done) {
|
||||
var flow = [ { id:"n1", type:"csv", temp:"a,b,c,d", wires:[["n2"]] },
|
||||
{id:"n2", type:"helper"} ];
|
||||
{id:"n2", type:"helper"} ];
|
||||
helper.load(csvNode, flow, function() {
|
||||
var n1 = helper.getNode("n1");
|
||||
var n2 = helper.getNode("n2");
|
||||
n2.on("input", function(msg) {
|
||||
msg.should.have.property('payload', { a: 1, b: 2, c: 3, d: 4 });
|
||||
msg.should.have.property('columns', "a,b,c,d");
|
||||
check_parts(msg, 0, 1);
|
||||
done();
|
||||
});
|
||||
@@ -86,7 +88,7 @@ describe('CSV node', function() {
|
||||
|
||||
it('should remove quotes and whitespace from template', function(done) {
|
||||
var flow = [ { id:"n1", type:"csv", temp:'"a", "b" , " c "," d " ', wires:[["n2"]] },
|
||||
{id:"n2", type:"helper"} ];
|
||||
{id:"n2", type:"helper"} ];
|
||||
helper.load(csvNode, flow, function() {
|
||||
var n1 = helper.getNode("n1");
|
||||
var n2 = helper.getNode("n2");
|
||||
@@ -102,12 +104,13 @@ describe('CSV node', function() {
|
||||
|
||||
it('should create column names if no template provided', function(done) {
|
||||
var flow = [ { id:"n1", type:"csv", temp:'', wires:[["n2"]] },
|
||||
{id:"n2", type:"helper"} ];
|
||||
{id:"n2", type:"helper"} ];
|
||||
helper.load(csvNode, flow, function() {
|
||||
var n1 = helper.getNode("n1");
|
||||
var n2 = helper.getNode("n2");
|
||||
n2.on("input", function(msg) {
|
||||
msg.should.have.property('payload', { col1: 1, col2: 2, col3: 3, col4: 4 });
|
||||
msg.should.have.property('columns', "col1,col2,col3,col4");
|
||||
check_parts(msg, 0, 1);
|
||||
done();
|
||||
});
|
||||
@@ -118,12 +121,13 @@ describe('CSV node', function() {
|
||||
|
||||
it('should allow dropping of fields from the template', function(done) {
|
||||
var flow = [ { id:"n1", type:"csv", temp:"a,,,d", wires:[["n2"]] },
|
||||
{id:"n2", type:"helper"} ];
|
||||
{id:"n2", type:"helper"} ];
|
||||
helper.load(csvNode, flow, function() {
|
||||
var n1 = helper.getNode("n1");
|
||||
var n2 = helper.getNode("n2");
|
||||
n2.on("input", function(msg) {
|
||||
msg.should.have.property('payload', { a: 1, d: 4 });
|
||||
msg.should.have.property('columns', 'a,d');
|
||||
check_parts(msg, 0, 1);
|
||||
done();
|
||||
});
|
||||
@@ -134,7 +138,7 @@ describe('CSV node', function() {
|
||||
|
||||
it('should leave numbers starting with 0, e and + as strings (except 0.)', function(done) {
|
||||
var flow = [ { id:"n1", type:"csv", temp:"a,b,c,d,e,f,g", wires:[["n2"]] },
|
||||
{id:"n2", type:"helper"} ];
|
||||
{id:"n2", type:"helper"} ];
|
||||
helper.load(csvNode, flow, function() {
|
||||
var n1 = helper.getNode("n1");
|
||||
var n2 = helper.getNode("n2");
|
||||
@@ -150,7 +154,7 @@ describe('CSV node', function() {
|
||||
|
||||
it('should not parse numbers when told not to do so', function(done) {
|
||||
var flow = [ { id:"n1", type:"csv", temp:"a,b,c,d,e,f,g", strings:false, wires:[["n2"]] },
|
||||
{id:"n2", type:"helper"} ];
|
||||
{id:"n2", type:"helper"} ];
|
||||
helper.load(csvNode, flow, function() {
|
||||
var n1 = helper.getNode("n1");
|
||||
var n2 = helper.getNode("n2");
|
||||
@@ -166,7 +170,7 @@ describe('CSV node', function() {
|
||||
|
||||
it('should leave handle strings with scientific notation as numbers', function(done) {
|
||||
var flow = [ { id:"n1", type:"csv", temp:"a,b,c,d,e,f,g", wires:[["n2"]] },
|
||||
{id:"n2", type:"helper"} ];
|
||||
{id:"n2", type:"helper"} ];
|
||||
helper.load(csvNode, flow, function() {
|
||||
var n1 = helper.getNode("n1");
|
||||
var n2 = helper.getNode("n2");
|
||||
@@ -183,7 +187,7 @@ describe('CSV node', function() {
|
||||
|
||||
it('should allow quotes in the input (but drop blank strings)', function(done) {
|
||||
var flow = [ { id:"n1", type:"csv", temp:"a,b,c,d,e,f,g,h", wires:[["n2"]] },
|
||||
{id:"n2", type:"helper"} ];
|
||||
{id:"n2", type:"helper"} ];
|
||||
helper.load(csvNode, flow, function() {
|
||||
var n1 = helper.getNode("n1");
|
||||
var n2 = helper.getNode("n2");
|
||||
@@ -234,7 +238,7 @@ describe('CSV node', function() {
|
||||
|
||||
it('should recover from an odd number of quotes in the input', function(done) {
|
||||
var flow = [ { id:"n1", type:"csv", temp:"a,b,c,d,e,f,g", wires:[["n2"]] },
|
||||
{id:"n2", type:"helper"} ];
|
||||
{id:"n2", type:"helper"} ];
|
||||
helper.load(csvNode, flow, function() {
|
||||
var n1 = helper.getNode("n1");
|
||||
var n2 = helper.getNode("n2");
|
||||
@@ -277,12 +281,13 @@ describe('CSV node', function() {
|
||||
|
||||
it('should be able to output multiple lines as one array', function(done) {
|
||||
var flow = [ { id:"n1", type:"csv", temp:"a,b,c,d", multi:"yes", wires:[["n2"]] },
|
||||
{id:"n2", type:"helper"} ];
|
||||
{id:"n2", type:"helper"} ];
|
||||
helper.load(csvNode, flow, function() {
|
||||
var n1 = helper.getNode("n1");
|
||||
var n2 = helper.getNode("n2");
|
||||
n2.on("input", function(msg) {
|
||||
msg.should.have.property('payload', [ { a: 1, b: 2, c: 3, d: 4 },{ a: 5, b: -6, c: '07', d: '+8' },{ a: 9, b: 0, c: 'a', d: 'b' },{ a: 'c', b: 'd', c: 'e', d: 'f' } ]);
|
||||
msg.should.have.property('columns','a,b,c,d');
|
||||
msg.should.not.have.property('parts');
|
||||
done();
|
||||
});
|
||||
@@ -293,7 +298,7 @@ describe('CSV node', function() {
|
||||
|
||||
it('should handle numbers in strings but not IP addresses', function(done) {
|
||||
var flow = [ { id:"n1", type:"csv", temp:"a,b,c,d,e", wires:[["n2"]] },
|
||||
{id:"n2", type:"helper"} ];
|
||||
{id:"n2", type:"helper"} ];
|
||||
helper.load(csvNode, flow, function() {
|
||||
var n1 = helper.getNode("n1");
|
||||
var n2 = helper.getNode("n2");
|
||||
@@ -309,7 +314,7 @@ describe('CSV node', function() {
|
||||
|
||||
it('should preserve parts property', function(done) {
|
||||
var flow = [ { id:"n1", type:"csv", temp:"a,b,c,d", wires:[["n2"]] },
|
||||
{id:"n2", type:"helper"} ];
|
||||
{id:"n2", type:"helper"} ];
|
||||
helper.load(csvNode, flow, function() {
|
||||
var n1 = helper.getNode("n1");
|
||||
var n2 = helper.getNode("n2");
|
||||
@@ -353,7 +358,7 @@ describe('CSV node', function() {
|
||||
|
||||
it('should skip several lines from start if requested', function(done) {
|
||||
var flow = [ { id:"n1", type:"csv", temp:"a,b,c,d", skip: 2, wires:[["n2"]] },
|
||||
{id:"n2", type:"helper"} ];
|
||||
{id:"n2", type:"helper"} ];
|
||||
helper.load(csvNode, flow, function() {
|
||||
var n1 = helper.getNode("n1");
|
||||
var n2 = helper.getNode("n2");
|
||||
@@ -367,9 +372,9 @@ describe('CSV node', function() {
|
||||
});
|
||||
});
|
||||
|
||||
it('should skip several lines from start then use next line as a tempate', function(done) {
|
||||
it('should skip several lines from start then use next line as a template', function(done) {
|
||||
var flow = [ { id:"n1", type:"csv", temp:"a,b,c,d", hdrin:true, skip: 2, wires:[["n2"]] },
|
||||
{id:"n2", type:"helper"} ];
|
||||
{id:"n2", type:"helper"} ];
|
||||
helper.load(csvNode, flow, function() {
|
||||
var n1 = helper.getNode("n1");
|
||||
var n2 = helper.getNode("n2");
|
||||
@@ -385,7 +390,7 @@ describe('CSV node', function() {
|
||||
|
||||
it('should skip several lines from start and correct parts', function(done) {
|
||||
var flow = [ { id:"n1", type:"csv", temp:"a,b,c,d", skip: 2, wires:[["n2"]] },
|
||||
{id:"n2", type:"helper"} ];
|
||||
{id:"n2", type:"helper"} ];
|
||||
helper.load(csvNode, flow, function() {
|
||||
var n1 = helper.getNode("n1");
|
||||
var n2 = helper.getNode("n2");
|
||||
@@ -417,11 +422,13 @@ describe('CSV node', function() {
|
||||
n2.on("input", function(msg) {
|
||||
if (c === 0) {
|
||||
msg.should.have.property('payload', { w: 1, x: 2, y: 3, z: 4 });
|
||||
msg.should.have.property('columns', 'w,x,y,z');
|
||||
check_parts(msg, 0, 2);
|
||||
c += 1;
|
||||
}
|
||||
else {
|
||||
msg.should.have.property('payload', { w: 5, x: 6, y: 7, z: 8 });
|
||||
msg.should.have.property('columns', 'w,x,y,z');
|
||||
check_parts(msg, 1, 2);
|
||||
done();
|
||||
}
|
||||
@@ -445,7 +452,7 @@ describe('CSV node', function() {
|
||||
|
||||
it('should convert a simple object back to a csv', function(done) {
|
||||
var flow = [ { id:"n1", type:"csv", temp:"a,b,c,,e", wires:[["n2"]] },
|
||||
{id:"n2", type:"helper"} ];
|
||||
{id:"n2", type:"helper"} ];
|
||||
helper.load(csvNode, flow, function() {
|
||||
var n1 = helper.getNode("n1");
|
||||
var n2 = helper.getNode("n2");
|
||||
@@ -463,7 +470,7 @@ describe('CSV node', function() {
|
||||
|
||||
it('should convert a simple object back to a csv with no template', function(done) {
|
||||
var flow = [ { id:"n1", type:"csv", temp:" ", wires:[["n2"]] },
|
||||
{id:"n2", type:"helper"} ];
|
||||
{id:"n2", type:"helper"} ];
|
||||
helper.load(csvNode, flow, function() {
|
||||
var n1 = helper.getNode("n1");
|
||||
var n2 = helper.getNode("n2");
|
||||
@@ -481,7 +488,7 @@ describe('CSV node', function() {
|
||||
|
||||
it('should handle a template with spaces in the property names', function(done) {
|
||||
var flow = [ { id:"n1", type:"csv", temp:"a,b o,c p,,e", wires:[["n2"]] },
|
||||
{id:"n2", type:"helper"} ];
|
||||
{id:"n2", type:"helper"} ];
|
||||
helper.load(csvNode, flow, function() {
|
||||
var n1 = helper.getNode("n1");
|
||||
var n2 = helper.getNode("n2");
|
||||
@@ -499,7 +506,7 @@ describe('CSV node', function() {
|
||||
|
||||
it('should convert an array of objects to a multi-line csv', function(done) {
|
||||
var flow = [ { id:"n1", type:"csv", temp:"a,b,c,d", wires:[["n2"]] },
|
||||
{id:"n2", type:"helper"} ];
|
||||
{id:"n2", type:"helper"} ];
|
||||
helper.load(csvNode, flow, function() {
|
||||
var n1 = helper.getNode("n1");
|
||||
var n2 = helper.getNode("n2");
|
||||
@@ -517,7 +524,7 @@ describe('CSV node', function() {
|
||||
|
||||
it('should convert a simple array back to a csv', function(done) {
|
||||
var flow = [ { id:"n1", type:"csv", temp:"a,b,c,d", wires:[["n2"]] },
|
||||
{id:"n2", type:"helper"} ];
|
||||
{id:"n2", type:"helper"} ];
|
||||
helper.load(csvNode, flow, function() {
|
||||
var n1 = helper.getNode("n1");
|
||||
var n2 = helper.getNode("n2");
|
||||
@@ -535,7 +542,7 @@ describe('CSV node', function() {
|
||||
|
||||
it('should convert an array of arrays back to a multi-line csv', function(done) {
|
||||
var flow = [ { id:"n1", type:"csv", temp:"a,b,c,d", wires:[["n2"]] },
|
||||
{id:"n2", type:"helper"} ];
|
||||
{id:"n2", type:"helper"} ];
|
||||
helper.load(csvNode, flow, function() {
|
||||
var n1 = helper.getNode("n1");
|
||||
var n2 = helper.getNode("n2");
|
||||
@@ -553,7 +560,7 @@ describe('CSV node', function() {
|
||||
|
||||
it('should be able to include column names as first row', function(done) {
|
||||
var flow = [ { id:"n1", type:"csv", temp:"a,b,c,d", hdrout:true, ret:"\r\n", wires:[["n2"]] },
|
||||
{id:"n2", type:"helper"} ];
|
||||
{id:"n2", type:"helper"} ];
|
||||
helper.load(csvNode, flow, function() {
|
||||
var n1 = helper.getNode("n1");
|
||||
var n2 = helper.getNode("n2");
|
||||
@@ -569,9 +576,36 @@ describe('CSV node', function() {
|
||||
});
|
||||
});
|
||||
|
||||
it('should be able to pass in column names', function(done) {
|
||||
var flow = [ { id:"n1", type:"csv", temp:"", hdrout:"once", ret:"\r\n", wires:[["n2"]] },
|
||||
{id:"n2", type:"helper"} ];
|
||||
helper.load(csvNode, flow, function() {
|
||||
var n1 = helper.getNode("n1");
|
||||
var n2 = helper.getNode("n2");
|
||||
var count = 0;
|
||||
n2.on("input", function(msg) {
|
||||
count += 1;
|
||||
try {
|
||||
if (count === 1) {
|
||||
msg.should.have.property('payload', 'a,,b,a\r\n4,,3,4\r\n');
|
||||
}
|
||||
if (count === 3) {
|
||||
msg.should.have.property('payload', '4,,3,4\r\n');
|
||||
done()
|
||||
}
|
||||
}
|
||||
catch(e) { done(e); }
|
||||
});
|
||||
var testJson = [{ d: 1, b: 3, c: 2, a: 4 }];
|
||||
n1.emit("input", {payload:testJson, columns:"a,,b,a"});
|
||||
n1.emit("input", {payload:testJson});
|
||||
n1.emit("input", {payload:testJson});
|
||||
});
|
||||
});
|
||||
|
||||
it('should handle quotes and sub-properties', function(done) {
|
||||
var flow = [ { id:"n1", type:"csv", temp:"a,b,c,d", wires:[["n2"]] },
|
||||
{id:"n2", type:"helper"} ];
|
||||
{id:"n2", type:"helper"} ];
|
||||
helper.load(csvNode, flow, function() {
|
||||
var n1 = helper.getNode("n1");
|
||||
var n2 = helper.getNode("n2");
|
||||
@@ -591,7 +625,7 @@ describe('CSV node', function() {
|
||||
|
||||
it('should just pass through if no payload provided', function(done) {
|
||||
var flow = [ { id:"n1", type:"csv", temp:"a,b,c,d", wires:[["n2"]] },
|
||||
{id:"n2", type:"helper"} ];
|
||||
{id:"n2", type:"helper"} ];
|
||||
helper.load(csvNode, flow, function() {
|
||||
var n1 = helper.getNode("n1");
|
||||
var n2 = helper.getNode("n2");
|
||||
@@ -611,7 +645,7 @@ describe('CSV node', function() {
|
||||
|
||||
it('should warn if provided a number or boolean', function(done) {
|
||||
var flow = [ { id:"n1", type:"csv", temp:"a,b,c,d", wires:[["n2"]] },
|
||||
{id:"n2", type:"helper"} ];
|
||||
{id:"n2", type:"helper"} ];
|
||||
helper.load(csvNode, flow, function() {
|
||||
var n1 = helper.getNode("n1");
|
||||
var n2 = helper.getNode("n2");
|
||||
|
Reference in New Issue
Block a user