add extra tests to debug, delay & template

This commit is contained in:
dceejay 2015-03-22 09:38:42 +00:00
parent f07fd64ffb
commit 5352fc87ee
6 changed files with 128 additions and 24 deletions

View File

@ -25,14 +25,11 @@ module.exports = function(RED) {
function DebugNode(n) {
RED.nodes.createNode(this,n);
this.name = n.name;
this.complete = n.complete||"payload";
this.complete = (n.complete||"payload").toString();
if (this.complete === "false") {
this.complete = "payload";
}
if (this.complete === true) {
this.complete = "true";
}
this.console = n.console;
this.active = (n.active === null || typeof n.active === "undefined") || n.active;

View File

@ -31,32 +31,29 @@ module.exports = function(RED) {
if (n.timeoutUnits === "milliseconds") {
this.timeout = n.timeout;
} else if (n.timeoutUnits === "seconds") {
this.timeout = n.timeout * 1000;
} else if (n.timeoutUnits === "minutes") {
} else if (n.timeoutUnits === "minutes") {
this.timeout = n.timeout * (60 * 1000);
} else if (n.timeoutUnits === "hours") {
this.timeout = n.timeout * (60 * 60 * 1000);
} else if (n.timeoutUnits === "days") {
this.timeout = n.timeout * (24 * 60 * 60 * 1000);
} else { // Default to seconds
this.timeout = n.timeout * 1000;
}
if (n.rateUnits === "second") {
this.rate = 1000/n.rate;
} else if (n.rateUnits === "minute") {
if (n.rateUnits === "minute") {
this.rate = (60 * 1000)/n.rate;
} else if (n.rateUnits === "hour") {
this.rate = (60 * 60 * 1000)/n.rate;
} else if (n.rateUnits === "day") {
this.rate = (24 * 60 * 60 * 1000)/n.rate;
} else { // Default to seconds
this.rate = 1000/n.rate;
}
if (n.randomUnits === "milliseconds") {
this.randomFirst = n.randomFirst * 1;
this.randomLast = n.randomLast * 1;
} else if (n.randomUnits === "seconds") {
this.randomFirst = n.randomFirst * 1000;
this.randomLast = n.randomLast * 1000;
} else if (n.randomUnits === "minutes") {
this.randomFirst = n.randomFirst * (60 * 1000);
this.randomLast = n.randomLast * (60 * 1000);
@ -66,6 +63,9 @@ module.exports = function(RED) {
} else if (n.randomUnits === "days") {
this.randomFirst = n.randomFirst * (24 * 60 * 60 * 1000);
this.randomLast = n.randomLast * (24 * 60 * 60 * 1000);
} else { // Default to seconds
this.randomFirst = n.randomFirst * 1000;
this.randomLast = n.randomLast * 1000;
}
this.diff = this.randomLast - this.randomFirst;

View File

@ -31,10 +31,11 @@ describe('debug node', function() {
it('should be loaded', function(done) {
var flow = [{id:"n1", type:"debug", name: "Debug" }];
var flow = [{id:"n1", type:"debug", name: "Debug", complete:"false" }];
helper.load(debugNode, flow, function() {
var n1 = helper.getNode("n1");
n1.should.have.property('name', 'Debug');
n1.should.have.property('complete', "payload");
done();
});
});
@ -55,7 +56,7 @@ describe('debug node', function() {
});
it('should publish to console', function(done) {
var flow = [{id:"n1", type:"debug", console: "true" }];
var flow = [{id:"n1", type:"debug", console: "true"}];
helper.load(debugNode, flow, function() {
var n1 = helper.getNode("n1");
var count = 0;

View File

@ -23,7 +23,7 @@ describe('template node', function() {
before(function(done) {
helper.startServer(done);
});
afterEach(function() {
helper.unload();
});
@ -43,4 +43,51 @@ describe('template node', function() {
});
});
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() {
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.receive({payload:"foo",topic: "bar"});
});
});
it('should handle deeper objects', function(done) {
var flow = [{id:"n1", type:"template", field: "topic.foo.bar", template: "payload={{payload.doh.rei.me}}",wires:[["n2"]]},{id:"n2",type:"helper"}];
helper.load(templateNode, flow, function() {
var n1 = helper.getNode("n1");
var n2 = helper.getNode("n2");
n2.on("input", function(msg) {
msg.should.have.property('topic');
msg.topic.should.have.property('foo');
msg.topic.foo.should.have.a.property('bar', 'payload=foo');
done();
});
n1.receive({payload:{doh:{rei:{me:"foo"}}}, topic:"bar"});
});
});
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() {
var n1 = helper.getNode("n1");
var n2 = helper.getNode("n2");
setTimeout(function() {
var logEvents = helper.log().args.filter(function(evt) {
return evt[0].type == "template";
});
logEvents.should.have.length(1);
logEvents[0][0].should.have.a.property('msg');
logEvents[0][0].msg.toString().should.startWith("Unclosed tag at ");
done();
},25);
n1.receive({payload:"foo"});
});
});
});

View File

@ -29,26 +29,47 @@ var secondsToHours = 3600;
var secondsToDays = 86400;
describe('delayNode', function() {
describe('delay Node', function() {
beforeEach(function(done) {
helper.startServer(done);
});
afterEach(function(done) {
helper.unload();
helper.stopServer(done);
});
it('should be loaded', function(done) {
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":[[]]}];
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":[[]]}];
helper.load(delayNode, flow, function() {
var delayNode1 = helper.getNode("delayNode1");
delayNode1.should.have.property('name', 'delayNode');
delayNode1.should.have.property('rate', 86400000);
done();
});
});
it('should be able to set rate to hour', function(done) {
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":[[]]}];
helper.load(delayNode, flow, function() {
var delayNode1 = helper.getNode("delayNode1");
delayNode1.should.have.property('name', 'delayNode');
delayNode1.should.have.property('rate', 3600000);
done();
});
});
it('should be able to set rate to minute', function(done) {
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":[[]]}];
helper.load(delayNode, flow, function() {
var delayNode1 = helper.getNode("delayNode1");
delayNode1.should.have.property('name', 'delayNode');
delayNode1.should.have.property('rate', 60000);
done();
});
});
var TimeUnitEnum = {
MILLIS : "milliseconds",
SECONDS : "seconds",
@ -367,7 +388,7 @@ describe('delayNode', function() {
randomDelayTest(0.4, 0.8, "seconds", done);
});
it(' randomly delays the message in milliseconds', function(done) {
it('randomly delays the message in milliseconds', function(done) {
randomDelayTest("400", "800", "milliseconds", done);
});
@ -416,5 +437,43 @@ describe('delayNode', function() {
}
});
});
it('handles delay queue', function(done) {
this.timeout(6000);
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"]]},
{id:"helperNode1", type:"helper", wires:[]}];
helper.load(delayNode, flow, function() {
var delayNode1 = helper.getNode("delayNode1");
var helperNode1 = helper.getNode("helperNode1");
var messages = 2;
var c = 0;
helperNode1.on("input", function(msg) {
c += 1;
msg.should.have.a.property('payload');
msg.should.have.a.property('topic');
if (msg.topic === "A") {
msg.payload.should.equal(4);
}
else {
msg.topic.should.equal("_none_");
msg.payload.should.equal(2);
}
if (c == 2) {
done(); // it will timeout if we don't receive both messages
}
});
// send test messages
delayNode1.receive({payload:1,topic:"A"});
delayNode1.receive({payload:1});
delayNode1.receive({payload:2,topic:"A"});
delayNode1.receive({payload:3,topic:"A"});
delayNode1.receive({payload:2}); // only this should get out
delayNode1.receive({payload:4,topic:"A"}); // and this one also
});
});
});

View File

@ -18,7 +18,7 @@ var should = require("should");
var commentNode = require("../../../../nodes/core/core/90-comment.js");
var helper = require("../../helper.js");
describe('comment node', function() {
describe('comment Node', function() {
afterEach(function() {
helper.unload();