add a few tests

geohash, smooth, base64, msgpack
This commit is contained in:
Dave Conway-Jones 2016-04-23 23:57:16 +01:00
parent d89a2bb7a1
commit da8dd1dc30
7 changed files with 542 additions and 9 deletions

View File

@ -1,6 +1,6 @@
{
"name" : "node-red-nodes",
"version" : "0.0.4",
"version" : "0.0.5",
"description" : "Node-RED-nodes package to hold the test framework ONLY - use npm to install individual nodes",
"homepage" : "http://nodered.org",
"license" : "Apache-2.0",
@ -34,7 +34,8 @@
"nodemailer" : "1.11.*",
"poplib" : "0.1.7",
"mailparser" : "0.5.3",
"imap" : "0.8.17"
"imap" : "0.8.17",
"msgpack-js": "0.3.0"
},
"engines": {
"node": ">=0.10"

View File

@ -61,7 +61,13 @@ module.exports = function(RED) {
else if (msg.payload.indexOf(",") !== -1) {
// has a comma so assume it's lat,lon(,precision)
var bits = msg.payload.split(",");
if (bits.length === 2) {
if ((bits.length === 2) || (bits.length === 3)) {
var li = 9;
if (bits.length === 3) {
li = parseInt(bits[2]);
if (li < 1) { li = 1; }
if (li > 9) { li = 9; }
}
var la = Number(bits[0]);
if (la < -90) { la = -90; }
if (la > 90) { la = 90; }
@ -69,7 +75,7 @@ module.exports = function(RED) {
if (lo < -180) { lo = ((lo-180)%360)+180; }
if (lo > 180) { lo = ((lo+180)%360)-180; }
if (!isNaN(la) && !isNaN(lo)) {
msg.payload = geohash.encode(la, lo);
msg.payload = geohash.encode(la, lo, li);
node.send(msg);
} else {
node.warn("Incorrect string format - should be lat,lon");

View File

@ -1,6 +1,6 @@
{
"name" : "node-red-node-geohash",
"version" : "0.0.5",
"version" : "0.0.6",
"description" : "A Node-RED node to encode and decode lat,lon pairs to a geohash.",
"dependencies" : {
"ngeohash" : "0.6.0"

View File

@ -0,0 +1,216 @@
/**
* Copyright 2015,2016 IBM Corp.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
var should = require("should");
var helper = require('../../../test/helper.js');
var testNode = require('../../../function/smooth/17-smooth.js');
describe('smooth node', function() {
"use strict";
beforeEach(function(done) {
helper.startServer(done);
});
afterEach(function(done) {
helper.unload().then(function() {
helper.stopServer(done);
});
});
it("should be loaded with correct defaults", function(done) {
var flow = [{"id":"n1", "type":"smooth", "name":"smooth1", "wires":[[]]}];
helper.load(testNode, flow, function() {
var n1 = helper.getNode("n1");
n1.should.have.property("name");
n1.should.have.property("action");
n1.should.have.property("count");
done();
});
});
it('should average over a number of inputs', function(done) {
var flow = [{"id":"n1", "type":"smooth", action:"mean", count:"5", round:"true", wires:[["n2"]] },
{id:"n2", type:"helper"} ];
helper.load(testNode, flow, function() {
var n1 = helper.getNode("n1");
var n2 = helper.getNode("n2");
var c = 0;
n2.on("input", function(msg) {
c += 1;
if (c === 4) { msg.should.have.a.property("payload", 1.8); }
if (c === 6) { msg.should.have.a.property("payload", 3); done(); }
});
n1.emit("input", {payload:1});
n1.emit("input", {payload:1});
n1.emit("input", {payload:2});
n1.emit("input", {payload:3});
n1.emit("input", {payload:4});
n1.emit("input", {payload:4.786});
});
});
it('should output max over a number of inputs', function(done) {
var flow = [{"id":"n1", "type":"smooth", action:"max", count:"5", wires:[["n2"]] },
{id:"n2", type:"helper"} ];
helper.load(testNode, flow, function() {
var n1 = helper.getNode("n1");
var n2 = helper.getNode("n2");
var c = 0;
n2.on("input", function(msg) {
c += 1;
if (c === 2) { msg.should.have.a.property("payload", 5); }
if (c === 3) { msg.should.have.a.property("payload", 5); }
if (c === 5) { msg.should.have.a.property("payload", 7); done(); }
});
n1.emit("input", {payload:5});
n1.emit("input", {payload:2});
n1.emit("input", {payload:3});
n1.emit("input", {payload:7});
n1.emit("input", {payload:4});
});
});
it('should output min over a number of inputs', function(done) {
var flow = [{"id":"n1", "type":"smooth", action:"min", count:"5", wires:[["n2"]] },
{id:"n2", type:"helper"} ];
helper.load(testNode, flow, function() {
var n1 = helper.getNode("n1");
var n2 = helper.getNode("n2");
var c = 0;
n2.on("input", function(msg) {
c += 1;
if (c === 2) { msg.should.have.a.property("payload", 2); }
if (c === 3) { msg.should.have.a.property("payload", 2); }
if (c === 4) { msg.should.have.a.property("payload", 2); }
if (c === 5) { msg.should.have.a.property("payload", 1); done(); }
});
n1.emit("input", {payload:5});
n1.emit("input", {payload:2});
n1.emit("input", {payload:3});
n1.emit("input", {payload:7});
n1.emit("input", {payload:1});
});
});
it('should output standard deviation over a number of inputs', function(done) {
var flow = [{"id":"n1", "type":"smooth", action:"sd", count:"5", round:"3", wires:[["n2"]] },
{id:"n2", type:"helper"} ];
helper.load(testNode, flow, function() {
var n1 = helper.getNode("n1");
var n2 = helper.getNode("n2");
var c = 0;
n2.on("input", function(msg) {
c += 1;
//console.log(c,msg);
if (c === 2) { msg.should.have.a.property("payload", 0.707); }
if (c === 3) { msg.should.have.a.property("payload", 1); }
if (c === 4) { msg.should.have.a.property("payload", 1.291); }
if (c === 5) { msg.should.have.a.property("payload", 1.581); done(); }
});
n1.emit("input", {payload:1});
n1.emit("input", {payload:2});
n1.emit("input", {payload:3});
n1.emit("input", {payload:4});
n1.emit("input", {payload:5});
});
});
it('should do a low pass filter over a number of inputs', function(done) {
var flow = [{"id":"n1", "type":"smooth", action:"low", count:"5", round:"3", wires:[["n2"]] },
{id:"n2", type:"helper"} ];
helper.load(testNode, flow, function() {
var n1 = helper.getNode("n1");
var n2 = helper.getNode("n2");
var c = 0;
n2.on("input", function(msg) {
c += 1;
if (c === 5) { msg.should.have.a.property("payload", 2.638); }
if (c === 9) { msg.should.have.a.property("payload", 5.471); }
if (c === 10) { msg.should.have.a.property("payload", 5.977); done(); }
});
n1.emit("input", {payload:1});
n1.emit("input", {payload:2});
n1.emit("input", {payload:3});
n1.emit("input", {payload:4});
n1.emit("input", {payload:5});
n1.emit("input", {payload:6});
n1.emit("input", {payload:7});
n1.emit("input", {payload:8});
n1.emit("input", {payload:8});
n1.emit("input", {payload:8});
});
});
it('should do a high pass filter over a number of inputs', function(done) {
var flow = [{"id":"n1", "type":"smooth", action:"high", count:"5", round:"3", wires:[["n2"]] },
{id:"n2", type:"helper"} ];
helper.load(testNode, flow, function() {
var n1 = helper.getNode("n1");
var n2 = helper.getNode("n2");
var c = 0;
n2.on("input", function(msg) {
c += 1;
if (c === 5) { msg.should.have.a.property("payload", 2.362); }
if (c === 9) { msg.should.have.a.property("payload", 2.529); }
if (c === 10) { msg.should.have.a.property("payload", 2.023); done(); }
});
n1.emit("input", {payload:1});
n1.emit("input", {payload:2});
n1.emit("input", {payload:3});
n1.emit("input", {payload:4});
n1.emit("input", {payload:5});
n1.emit("input", {payload:6});
n1.emit("input", {payload:7});
n1.emit("input", {payload:8});
n1.emit("input", {payload:8});
n1.emit("input", {payload:8});
});
});
it('ignore msg with non numeric payload', function(done) {
var flow = [{"id":"n1", "type":"smooth", action:"mean", count:"5", round:"3", wires:[["n2"]] },
{id:"n2", type:"helper"} ];
helper.load(testNode, flow, function() {
var n1 = helper.getNode("n1");
var n2 = helper.getNode("n2");
n2.on("input", function(msg) {
done("should not get here with no payload.");
});
setTimeout(function() {
done();
}, 50);
n1.emit("input", {payload:"banana"});
});
});
it('ignore msg with no payload', function(done) {
var flow = [{"id":"n1", "type":"smooth", action:"mean", count:"5", round:"3", wires:[["n2"]] },
{id:"n2", type:"helper"} ];
helper.load(testNode, flow, function() {
var n1 = helper.getNode("n1");
var n2 = helper.getNode("n2");
n2.on("input", function(msg) {
done("should not get here with no payload.");
});
setTimeout(function() {
done();
}, 50);
n1.emit("input", {topic:1});
});
});
});

View File

@ -0,0 +1,151 @@
/**
* Copyright 2016 IBM Corp.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
var should = require("should");
var helper = require('../../../test/helper.js');
var testNode = require('../../../parsers/base64/70-base64.js');
describe('base64 node', function() {
"use strict";
beforeEach(function(done) {
helper.startServer(done);
});
afterEach(function(done) {
helper.unload().then(function() {
helper.stopServer(done);
});
});
it("should be loaded with correct defaults", function(done) {
var flow = [{"id":"n1", "type":"base64", "name":"base641", "wires":[[]]}];
helper.load(testNode, flow, function() {
var n1 = helper.getNode("n1");
n1.should.have.property("name", "base641");
done();
});
});
it('should convert a Buffer to base64', function(done) {
var flow = [{"id":"n1", "type":"base64", wires:[["n2"]] },
{id:"n2", type:"helper"} ];
helper.load(testNode, flow, function() {
var n1 = helper.getNode("n1");
var n2 = helper.getNode("n2");
n2.on("input", function(msg) {
msg.should.have.a.property("payload","QUJDREVGR0hJSktMTU5PUFFSU1RVVldYWVo=");
done();
});
n1.emit("input", {payload: Buffer("ABCDEFGHIJKLMNOPQRSTUVWXYZ")});
});
});
it('should convert base64 to a Buffer', function(done) {
var flow = [{"id":"n1", "type":"base64", wires:[["n2"]] },
{id:"n2", type:"helper"} ];
helper.load(testNode, flow, function() {
var n1 = helper.getNode("n1");
var n2 = helper.getNode("n2");
n2.on("input", function(msg) {
msg.should.have.a.property("payload");
msg.payload.toString().should.equal("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
done();
});
n1.emit("input", {payload:"QUJDREVGR0hJSktMTU5PUFFSU1RVVldYWVo="});
});
});
it('should try to encode a non base64 string', function(done) {
var flow = [{"id":"n1", "type":"base64", wires:[["n2"]] },
{id:"n2", type:"helper"} ];
helper.load(testNode, flow, function() {
var n1 = helper.getNode("n1");
var n2 = helper.getNode("n2");
n2.on("input", function(msg) {
msg.should.have.a.property("payload");
msg.payload.should.equal("QUJDREVGR0hJSktMTU5PUFFSU1RVVldYWVo=");
done();
});
n1.emit("input", {payload:"ABCDEFGHIJKLMNOPQRSTUVWXYZ"});
});
});
it('ignore msg with a boolean payload', function(done) {
var flow = [{"id":"n1", "type":"base64", wires:[["n2"]] },
{id:"n2", type:"helper"} ];
helper.load(testNode, flow, function() {
var n1 = helper.getNode("n1");
var n2 = helper.getNode("n2");
n2.on("input", function(msg) {
done("should not get here with no payload.");
});
setTimeout(function() {
done();
}, 25);
n1.emit("input", {payload:true});
});
});
it('ignore msg with a numeric payload', function(done) {
var flow = [{"id":"n1", "type":"base64", wires:[["n2"]] },
{id:"n2", type:"helper"} ];
helper.load(testNode, flow, function() {
var n1 = helper.getNode("n1");
var n2 = helper.getNode("n2");
n2.on("input", function(msg) {
done("should not get here with no payload.");
});
setTimeout(function() {
done();
}, 25);
n1.emit("input", {payload:9999});
});
});
it('ignore msg with an object payload', function(done) {
var flow = [{"id":"n1", "type":"base64", wires:[["n2"]] },
{id:"n2", type:"helper"} ];
helper.load(testNode, flow, function() {
var n1 = helper.getNode("n1");
var n2 = helper.getNode("n2");
n2.on("input", function(msg) {
done("should not get here with no payload.");
});
setTimeout(function() {
done();
}, 25);
n1.emit("input", {payload:{A:1}});
});
});
it('ignore msg with no payload', function(done) {
var flow = [{"id":"n1", "type":"base64", wires:[["n2"]] },
{id:"n2", type:"helper"} ];
helper.load(testNode, flow, function() {
var n1 = helper.getNode("n1");
var n2 = helper.getNode("n2");
n2.on("input", function(msg) {
done("should not get here with no payload.");
});
setTimeout(function() {
done();
}, 25);
n1.emit("input", {topic:1});
});
});
});

View File

@ -50,7 +50,21 @@ describe('geohash node', function() {
msg.should.have.a.property("payload", "gcnfju78x");
done();
});
n1.emit("input", {payload:"51.0,-1.5"});
n1.emit("input", {payload:"51.0,-1.5,10"});
});
});
it('should convert payload lat,lon to geohash at low precision', function(done) {
var flow = [{"id":"n1", "type":"geohash", func:"geohash", gap:0, wires:[["n2"]] },
{id:"n2", type:"helper"} ];
helper.load(testNode, flow, function() {
var n1 = helper.getNode("n1");
var n2 = helper.getNode("n2");
n2.on("input", function(msg) {
msg.should.have.a.property("payload", "g");
done();
});
n1.emit("input", {payload:"51.0,-1.5,0"});
});
});
@ -93,7 +107,22 @@ describe('geohash node', function() {
msg.payload.should.have.a.property("geohash", "t9cbbukqn");
done();
});
n1.emit("input", {payload:{latitude:10,longitude:70}});
n1.emit("input", {payload:{latitude:10,longitude:70,precision:10}});
});
});
it('should convert payload object lat,lon to geohash (low precision)', function(done) {
var flow = [{"id":"n1", "type":"geohash", func:"geohash", gap:0, wires:[["n2"]] },
{id:"n2", type:"helper"} ];
helper.load(testNode, flow, function() {
var n1 = helper.getNode("n1");
var n2 = helper.getNode("n2");
n2.on("input", function(msg) {
msg.should.have.a.property("payload");
msg.payload.should.have.a.property("geohash", "t");
done();
});
n1.emit("input", {payload:{latitude:10,longitude:70,precision:-1}});
});
});
@ -125,7 +154,22 @@ describe('geohash node', function() {
msg.location.should.have.a.property("geohash", "kukqnpp5e");
done();
});
n1.emit("input", {location:{lat:-20,lon:40}});
n1.emit("input", {location:{lat:-20,lon:40,precision:10}});
});
});
it('should convert location lat, lon to geohash (low precision)', function(done) {
var flow = [{"id":"n1", "type":"geohash", func:"geohash", gap:0, wires:[["n2"]] },
{id:"n2", type:"helper"} ];
helper.load(testNode, flow, function() {
var n1 = helper.getNode("n1");
var n2 = helper.getNode("n2");
n2.on("input", function(msg) {
msg.should.have.a.property("location");
msg.location.should.have.a.property("geohash", "k");
done();
});
n1.emit("input", {location:{lat:-20,lon:40,precision:-1}});
});
});
@ -227,7 +271,7 @@ describe('geohash node', function() {
msg.should.have.property('msg', 'Unexpected string format - should be lat,lon');
done();
},50);
n1.emit("input", {payload:"bananas,apples,pears"});
n1.emit("input", {payload:"bananas,apples,pears,oranges"});
});
});

View File

@ -0,0 +1,115 @@
/**
* Copyright 2016 IBM Corp.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
var should = require("should");
var helper = require('../../../test/helper.js');
var testNode = require('../../../parsers/msgpack/70-msgpack.js');
describe('msgpack node', function() {
"use strict";
beforeEach(function(done) {
helper.startServer(done);
});
afterEach(function(done) {
helper.unload().then(function() {
helper.stopServer(done);
});
});
it("should be loaded with correct defaults", function(done) {
var flow = [{"id":"n1", "type":"msgpack", "name":"msgpack1", "wires":[[]]}];
helper.load(testNode, flow, function() {
var n1 = helper.getNode("n1");
n1.should.have.property("name", "msgpack1");
done();
});
});
var buf;
it('should pack an object', function(done) {
var flow = [{"id":"n1", "type":"msgpack", wires:[["n2"]] },
{id:"n2", type:"helper"} ];
helper.load(testNode, flow, function() {
var n1 = helper.getNode("n1");
var n2 = helper.getNode("n2");
n2.on("input", function(msg) {
msg.should.have.a.property("payload");
msg.payload.should.be.a.Object;
msg.payload.should.have.length(43);
buf = msg.payload;
done();
});
n1.emit("input", {payload:{A:1, B:"string", C:true, D:[1,true,"string"], E:{Y:9,Z:"string"}}});
});
});
it('should unpack a Buffer', function(done) {
var flow = [{"id":"n1", "type":"msgpack", wires:[["n2"]] },
{id:"n2", type:"helper"} ];
helper.load(testNode, flow, function() {
var n1 = helper.getNode("n1");
var n2 = helper.getNode("n2");
n2.on("input", function(msg) {
msg.should.have.a.property("payload");
msg.payload.should.have.a.property("A",1);
msg.payload.should.have.a.property("B",'string');
msg.payload.should.have.a.property("C",true);
msg.payload.should.have.a.property("D",[1,true,"string"]);
msg.payload.should.have.a.property("E");
msg.payload.E.should.have.a.property("Y",9);
msg.payload.E.should.have.a.property("Z","string");
done();
});
n1.emit("input", {payload:buf});
});
});
it('should error if the buffer fails to decode', function(done) {
var flow = [{"id":"n1", "type":"msgpack", wires:[["n2"]] },
{id:"n2", type:"helper"} ];
helper.load(testNode, flow, function() {
var n1 = helper.getNode("n1");
var n2 = helper.getNode("n2");
n2.on("input", function(msg) {
done("should not get here if there is an error.");
});
setTimeout(function() {
done();
}, 25);
n1.emit("input", {payload:Buffer("12345")});
});
});
it('ignore msg with no payload', function(done) {
var flow = [{"id":"n1", "type":"msgpack", wires:[["n2"]] },
{id:"n2", type:"helper"} ];
helper.load(testNode, flow, function() {
var n1 = helper.getNode("n1");
var n2 = helper.getNode("n2");
n2.on("input", function(msg) {
done("should not get here with no payload.");
});
setTimeout(function() {
done();
}, 25);
n1.emit("input", {topic:1});
});
});
});