Skip to content

Commit 5352fc8

Browse files
author
dceejay
committed
add extra tests to debug, delay & template
1 parent f07fd64 commit 5352fc8

6 files changed

Lines changed: 128 additions & 24 deletions

File tree

nodes/core/core/58-debug.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,11 @@ module.exports = function(RED) {
2525
function DebugNode(n) {
2626
RED.nodes.createNode(this,n);
2727
this.name = n.name;
28-
this.complete = n.complete||"payload";
28+
this.complete = (n.complete||"payload").toString();
2929

3030
if (this.complete === "false") {
3131
this.complete = "payload";
3232
}
33-
if (this.complete === true) {
34-
this.complete = "true";
35-
}
3633

3734
this.console = n.console;
3835
this.active = (n.active === null || typeof n.active === "undefined") || n.active;

nodes/core/core/89-delay.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,32 +31,29 @@ module.exports = function(RED) {
3131

3232
if (n.timeoutUnits === "milliseconds") {
3333
this.timeout = n.timeout;
34-
} else if (n.timeoutUnits === "seconds") {
35-
this.timeout = n.timeout * 1000;
36-
} else if (n.timeoutUnits === "minutes") {
34+
} else if (n.timeoutUnits === "minutes") {
3735
this.timeout = n.timeout * (60 * 1000);
3836
} else if (n.timeoutUnits === "hours") {
3937
this.timeout = n.timeout * (60 * 60 * 1000);
4038
} else if (n.timeoutUnits === "days") {
4139
this.timeout = n.timeout * (24 * 60 * 60 * 1000);
40+
} else { // Default to seconds
41+
this.timeout = n.timeout * 1000;
4242
}
4343

44-
if (n.rateUnits === "second") {
45-
this.rate = 1000/n.rate;
46-
} else if (n.rateUnits === "minute") {
44+
if (n.rateUnits === "minute") {
4745
this.rate = (60 * 1000)/n.rate;
4846
} else if (n.rateUnits === "hour") {
4947
this.rate = (60 * 60 * 1000)/n.rate;
5048
} else if (n.rateUnits === "day") {
5149
this.rate = (24 * 60 * 60 * 1000)/n.rate;
50+
} else { // Default to seconds
51+
this.rate = 1000/n.rate;
5252
}
5353

5454
if (n.randomUnits === "milliseconds") {
5555
this.randomFirst = n.randomFirst * 1;
5656
this.randomLast = n.randomLast * 1;
57-
} else if (n.randomUnits === "seconds") {
58-
this.randomFirst = n.randomFirst * 1000;
59-
this.randomLast = n.randomLast * 1000;
6057
} else if (n.randomUnits === "minutes") {
6158
this.randomFirst = n.randomFirst * (60 * 1000);
6259
this.randomLast = n.randomLast * (60 * 1000);
@@ -66,6 +63,9 @@ module.exports = function(RED) {
6663
} else if (n.randomUnits === "days") {
6764
this.randomFirst = n.randomFirst * (24 * 60 * 60 * 1000);
6865
this.randomLast = n.randomLast * (24 * 60 * 60 * 1000);
66+
} else { // Default to seconds
67+
this.randomFirst = n.randomFirst * 1000;
68+
this.randomLast = n.randomLast * 1000;
6969
}
7070

7171
this.diff = this.randomLast - this.randomFirst;

test/nodes/core/core/58-debug_spec.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,11 @@ describe('debug node', function() {
3131

3232

3333
it('should be loaded', function(done) {
34-
var flow = [{id:"n1", type:"debug", name: "Debug" }];
34+
var flow = [{id:"n1", type:"debug", name: "Debug", complete:"false" }];
3535
helper.load(debugNode, flow, function() {
3636
var n1 = helper.getNode("n1");
3737
n1.should.have.property('name', 'Debug');
38+
n1.should.have.property('complete', "payload");
3839
done();
3940
});
4041
});
@@ -55,7 +56,7 @@ describe('debug node', function() {
5556
});
5657

5758
it('should publish to console', function(done) {
58-
var flow = [{id:"n1", type:"debug", console: "true" }];
59+
var flow = [{id:"n1", type:"debug", console: "true"}];
5960
helper.load(debugNode, flow, function() {
6061
var n1 = helper.getNode("n1");
6162
var count = 0;

test/nodes/core/core/80-template_spec.js

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ describe('template node', function() {
2323
before(function(done) {
2424
helper.startServer(done);
2525
});
26-
26+
2727
afterEach(function() {
2828
helper.unload();
2929
});
@@ -43,4 +43,51 @@ describe('template node', function() {
4343
});
4444
});
4545

46+
it('should handle if the field isn\'t set', function(done) {
47+
var flow = [{id:"n1", type:"template", template: "payload={{payload}}",wires:[["n2"]]},{id:"n2",type:"helper"}];
48+
helper.load(templateNode, flow, function() {
49+
var n1 = helper.getNode("n1");
50+
var n2 = helper.getNode("n2");
51+
n2.on("input", function(msg) {
52+
msg.should.have.property('topic', 'bar');
53+
msg.should.have.property('payload', 'payload=foo');
54+
done();
55+
});
56+
n1.receive({payload:"foo",topic: "bar"});
57+
});
58+
});
59+
60+
it('should handle deeper objects', function(done) {
61+
var flow = [{id:"n1", type:"template", field: "topic.foo.bar", template: "payload={{payload.doh.rei.me}}",wires:[["n2"]]},{id:"n2",type:"helper"}];
62+
helper.load(templateNode, flow, function() {
63+
var n1 = helper.getNode("n1");
64+
var n2 = helper.getNode("n2");
65+
n2.on("input", function(msg) {
66+
msg.should.have.property('topic');
67+
msg.topic.should.have.property('foo');
68+
msg.topic.foo.should.have.a.property('bar', 'payload=foo');
69+
done();
70+
});
71+
n1.receive({payload:{doh:{rei:{me:"foo"}}}, topic:"bar"});
72+
});
73+
});
74+
75+
it('should raise error if passed bad template', function(done) {
76+
var flow = [{id:"n1", type:"template", field: "payload", template: "payload={{payload",wires:[["n2"]]},{id:"n2",type:"helper"}];
77+
helper.load(templateNode, flow, function() {
78+
var n1 = helper.getNode("n1");
79+
var n2 = helper.getNode("n2");
80+
setTimeout(function() {
81+
var logEvents = helper.log().args.filter(function(evt) {
82+
return evt[0].type == "template";
83+
});
84+
logEvents.should.have.length(1);
85+
logEvents[0][0].should.have.a.property('msg');
86+
logEvents[0][0].msg.toString().should.startWith("Unclosed tag at ");
87+
done();
88+
},25);
89+
n1.receive({payload:"foo"});
90+
});
91+
});
92+
4693
});

test/nodes/core/core/89-delay_spec.js

Lines changed: 66 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,26 +29,47 @@ var secondsToHours = 3600;
2929
var secondsToDays = 86400;
3030

3131

32-
describe('delayNode', function() {
33-
32+
describe('delay Node', function() {
33+
3434
beforeEach(function(done) {
3535
helper.startServer(done);
3636
});
37-
37+
3838
afterEach(function(done) {
3939
helper.unload();
4040
helper.stopServer(done);
4141
});
4242

4343
it('should be loaded', function(done) {
44-
var flow = [{"id":"delayNode1","type":"delay","name":"delayNode","pauseType":"delay","timeout":"5","timeoutUnits":"seconds","rate":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"wires":[[]]}];
44+
var flow = [{"id":"delayNode1","type":"delay","name":"delayNode","pauseType":"delay","timeout":"5","timeoutUnits":"seconds","rate":"1","rateUnits":"day","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"wires":[[]]}];
4545
helper.load(delayNode, flow, function() {
4646
var delayNode1 = helper.getNode("delayNode1");
4747
delayNode1.should.have.property('name', 'delayNode');
48+
delayNode1.should.have.property('rate', 86400000);
4849
done();
4950
});
5051
});
51-
52+
53+
it('should be able to set rate to hour', function(done) {
54+
var flow = [{"id":"delayNode1","type":"delay","name":"delayNode","pauseType":"delay","timeout":"5","timeoutUnits":"seconds","rate":"1","rateUnits":"hour","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"wires":[[]]}];
55+
helper.load(delayNode, flow, function() {
56+
var delayNode1 = helper.getNode("delayNode1");
57+
delayNode1.should.have.property('name', 'delayNode');
58+
delayNode1.should.have.property('rate', 3600000);
59+
done();
60+
});
61+
});
62+
63+
it('should be able to set rate to minute', function(done) {
64+
var flow = [{"id":"delayNode1","type":"delay","name":"delayNode","pauseType":"delay","timeout":"5","timeoutUnits":"seconds","rate":"1","rateUnits":"minute","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"wires":[[]]}];
65+
helper.load(delayNode, flow, function() {
66+
var delayNode1 = helper.getNode("delayNode1");
67+
delayNode1.should.have.property('name', 'delayNode');
68+
delayNode1.should.have.property('rate', 60000);
69+
done();
70+
});
71+
});
72+
5273
var TimeUnitEnum = {
5374
MILLIS : "milliseconds",
5475
SECONDS : "seconds",
@@ -367,7 +388,7 @@ describe('delayNode', function() {
367388
randomDelayTest(0.4, 0.8, "seconds", done);
368389
});
369390

370-
it(' randomly delays the message in milliseconds', function(done) {
391+
it('randomly delays the message in milliseconds', function(done) {
371392
randomDelayTest("400", "800", "milliseconds", done);
372393
});
373394

@@ -416,5 +437,43 @@ describe('delayNode', function() {
416437
}
417438
});
418439
});
419-
440+
441+
it('handles delay queue', function(done) {
442+
this.timeout(6000);
443+
444+
var flow = [{"id":"delayNode1","type":"delay","name":"delayNode","pauseType":"queue","timeout":5,"timeoutUnits":"seconds","rate":1000,"rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"wires":[["helperNode1"]]},
445+
{id:"helperNode1", type:"helper", wires:[]}];
446+
helper.load(delayNode, flow, function() {
447+
var delayNode1 = helper.getNode("delayNode1");
448+
var helperNode1 = helper.getNode("helperNode1");
449+
var messages = 2;
450+
var c = 0;
451+
452+
helperNode1.on("input", function(msg) {
453+
c += 1;
454+
msg.should.have.a.property('payload');
455+
msg.should.have.a.property('topic');
456+
if (msg.topic === "A") {
457+
msg.payload.should.equal(4);
458+
}
459+
else {
460+
msg.topic.should.equal("_none_");
461+
msg.payload.should.equal(2);
462+
}
463+
if (c == 2) {
464+
done(); // it will timeout if we don't receive both messages
465+
}
466+
});
467+
468+
// send test messages
469+
delayNode1.receive({payload:1,topic:"A"});
470+
delayNode1.receive({payload:1});
471+
delayNode1.receive({payload:2,topic:"A"});
472+
delayNode1.receive({payload:3,topic:"A"});
473+
delayNode1.receive({payload:2}); // only this should get out
474+
delayNode1.receive({payload:4,topic:"A"}); // and this one also
475+
476+
});
477+
});
478+
420479
});

test/nodes/core/core/90-comment_spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ var should = require("should");
1818
var commentNode = require("../../../../nodes/core/core/90-comment.js");
1919
var helper = require("../../helper.js");
2020

21-
describe('comment node', function() {
21+
describe('comment Node', function() {
2222

2323
afterEach(function() {
2424
helper.unload();

0 commit comments

Comments
 (0)