1
0
mirror of https://github.com/node-red/node-red.git synced 2023-10-10 13:36:53 +02:00

boost api index, nodes index and nodes Node test coverage

This commit is contained in:
dceejay 2015-03-06 22:58:30 +00:00
parent 910d983b82
commit 69f85bd688
5 changed files with 52 additions and 32 deletions

View File

@ -29,7 +29,7 @@ function Node(n) {
this.type = n.type; this.type = n.type;
this.z = n.z; this.z = n.z;
this._closeCallbacks = []; this._closeCallbacks = [];
if (n.name) { if (n.name) {
this.name = n.name; this.name = n.name;
} }
@ -41,7 +41,7 @@ util.inherits(Node, EventEmitter);
Node.prototype.updateWires = function(wires) { Node.prototype.updateWires = function(wires) {
this.wires = wires || []; this.wires = wires || [];
delete this._wire; delete this._wire;
var wc = 0; var wc = 0;
this.wires.forEach(function(w) { this.wires.forEach(function(w) {
wc+=w.length; wc+=w.length;
@ -103,10 +103,10 @@ function constructUniqueIdentifier() {
Node.prototype.send = function(msg) { Node.prototype.send = function(msg) {
var msgSent = false; var msgSent = false;
var node; var node;
if (msg === null || typeof msg === "undefined") { if (msg === null || typeof msg === "undefined") {
return; return;
} else if (!util.isArray(msg)) { } else if (!util.isArray(msg)) {
if (this._wire) { if (this._wire) {
// A single message and a single wire on output 0 // A single message and a single wire on output 0
// TODO: pre-load flows.get calls - cannot do in constructor // TODO: pre-load flows.get calls - cannot do in constructor
@ -116,6 +116,7 @@ Node.prototype.send = function(msg) {
} }
this.metric("send",msg); this.metric("send",msg);
node = flows.get(this._wire); node = flows.get(this._wire);
/* istanbul ignore else */
if (node) { if (node) {
node.receive(msg); node.receive(msg);
} }
@ -124,18 +125,19 @@ Node.prototype.send = function(msg) {
msg = [msg]; msg = [msg];
} }
} }
var numOutputs = this.wires.length; var numOutputs = this.wires.length;
// Build a list of send events so that all cloning is done before // Build a list of send events so that all cloning is done before
// any calls to node.receive // any calls to node.receive
var sendEvents = []; var sendEvents = [];
var sentMessageId = null; var sentMessageId = null;
// for each output of node eg. [msgs to output 0, msgs to output 1, ...] // for each output of node eg. [msgs to output 0, msgs to output 1, ...]
for (var i = 0; i < numOutputs; i++) { for (var i = 0; i < numOutputs; i++) {
var wires = this.wires[i]; // wires leaving output i var wires = this.wires[i]; // wires leaving output i
/* istanbul ignore else */
if (i < msg.length) { if (i < msg.length) {
var msgs = msg[i]; // msgs going to output i var msgs = msg[i]; // msgs going to output i
if (msgs !== null && typeof msgs !== "undefined") { if (msgs !== null && typeof msgs !== "undefined") {
@ -150,6 +152,7 @@ Node.prototype.send = function(msg) {
// for each msg to send eg. [[m1, m2, ...], ...] // for each msg to send eg. [[m1, m2, ...], ...]
for (k = 0; k < msgs.length; k++) { for (k = 0; k < msgs.length; k++) {
var m = msgs[k]; var m = msgs[k];
/* istanbul ignore else */
if (!sentMessageId) { if (!sentMessageId) {
sentMessageId = m._msgid; sentMessageId = m._msgid;
} }
@ -166,13 +169,15 @@ Node.prototype.send = function(msg) {
} }
} }
} }
/* istanbul ignore else */
if (!sentMessageId) { if (!sentMessageId) {
sentMessageId = constructUniqueIdentifier(); sentMessageId = constructUniqueIdentifier();
} }
this.metric("send",{_msgid:sentMessageId}); this.metric("send",{_msgid:sentMessageId});
for (i=0;i<sendEvents.length;i++) { for (i=0;i<sendEvents.length;i++) {
var ev = sendEvents[i]; var ev = sendEvents[i];
/* istanbul ignore else */
if (!ev.m._msgid) { if (!ev.m._msgid) {
ev.m._msgid = sentMessageId; ev.m._msgid = sentMessageId;
} }
@ -180,14 +185,14 @@ Node.prototype.send = function(msg) {
} }
}; };
Node.prototype.receive = function(msg) { Node.prototype.receive = function(msg) {
if (!msg) { if (!msg) {
msg = {}; msg = {};
} }
if (!msg._msgid) { if (!msg._msgid) {
msg._msgid = constructUniqueIdentifier(); msg._msgid = constructUniqueIdentifier();
} }
this.metric("receive",msg); this.metric("receive",msg);
this.emit("input", msg); this.emit("input", msg);
}; };
@ -215,6 +220,7 @@ Node.prototype.warn = function(msg) {
Node.prototype.error = function(logMessage,msg) { Node.prototype.error = function(logMessage,msg) {
logMessage = logMessage || ""; logMessage = logMessage || "";
log_helper(this, Log.ERROR, logMessage); log_helper(this, Log.ERROR, logMessage);
/* istanbul ignore else */
if (msg) { if (msg) {
flows.handleError(this,logMessage,msg); flows.handleError(this,logMessage,msg);
} }
@ -230,7 +236,7 @@ Node.prototype.metric = function(eventname, msg, metricValue) {
var metrics = {}; var metrics = {};
metrics.level = Log.METRIC; metrics.level = Log.METRIC;
metrics.nodeid = this.id; metrics.nodeid = this.id;
metrics.event = "node."+this.type+"."+eventname; metrics.event = "node."+this.type+"."+eventname;
metrics.msgid = msg._msgid; metrics.msgid = msg._msgid;
metrics.value = metricValue; metrics.value = metricValue;
Log.log(metrics); Log.log(metrics);

View File

@ -19,24 +19,22 @@ var request = require("supertest");
var express = require("express"); var express = require("express");
var fs = require("fs"); var fs = require("fs");
var path = require("path"); var path = require("path");
var settings = require("../../../red/settings"); var settings = require("../../../red/settings");
var api = require("../../../red/api"); var api = require("../../../red/api");
describe("api index", function() { describe("api index", function() {
var app; var app;
describe("disables editor", function() { describe("disables editor", function() {
before(function() { before(function() {
settings.init({disableEditor:true}); settings.init({disableEditor:true,adminAuth:{type: "credentials",users:[],default:{permissions:"read"}}});
app = express(); app = express();
api.init(app); api.init(app);
}); });
after(function() { after(function() {
settings.reset(); settings.reset();
}); });
it('does not serve the editor', function(done) { it('does not serve the editor', function(done) {
request(app) request(app)
.get("/") .get("/")
@ -53,7 +51,7 @@ describe("api index", function() {
.expect(200,done) .expect(200,done)
}); });
}); });
describe("enables editor", function() { describe("enables editor", function() {
before(function() { before(function() {
settings.init({disableEditor:false}); settings.init({disableEditor:false});
@ -63,7 +61,7 @@ describe("api index", function() {
after(function() { after(function() {
settings.reset(); settings.reset();
}); });
it('serves the editor', function(done) { it('serves the editor', function(done) {
request(app) request(app)
.get("/") .get("/")
@ -88,5 +86,10 @@ describe("api index", function() {
.get("/settings") .get("/settings")
.expect(200,done) .expect(200,done)
}); });
it('handles page not there', function(done) {
request(app)
.get("/foo")
.expect(404,done)
});
}); });
}); });

View File

@ -23,7 +23,7 @@ describe("red/log", function() {
}); });
var log = require("../../red/log"); var log = require("../../red/log");
var sett = {logging: { console: { level: 'metric', metrics: true } } } var sett = {logging: { console: { level: 'metric', metrics: true } } };
log.init(sett); log.init(sett);
beforeEach(function () { beforeEach(function () {

View File

@ -417,14 +417,13 @@ describe('Node', function() {
}); });
var message = {a:1}; var message = {a:1};
n.error("an error message",message); n.error(null,message);
should.deepEqual({level:Log.ERROR, id:n.id, should.deepEqual({level:Log.ERROR, id:n.id, type:n.type, msg:""}, loginfo);
type:n.type, msg:"an error message"}, loginfo);
flows.handleError.called.should.be.true; flows.handleError.called.should.be.true;
flows.handleError.args[0][0].should.eql(n); flows.handleError.args[0][0].should.eql(n);
flows.handleError.args[0][1].should.eql("an error message"); flows.handleError.args[0][1].should.eql("");
flows.handleError.args[0][2].should.eql(message); flows.handleError.args[0][2].should.eql(message);
Log.log.restore(); Log.log.restore();
@ -448,8 +447,22 @@ describe('Node', function() {
done(); done();
}); });
}); });
describe('#metric', function() {
it('returns metric value if eventname undefined', function(done) {
var n = new RedNode({id:'123',type:'abc'});
var loginfo = {};
sinon.stub(Log, 'log', function(msg) {
loginfo = msg;
});
var msg = {payload:"foo", _msgid:"987654321"};
var m = n.metric(undefined,msg,"15mb");
m.should.equal(true);
Log.log.restore();
done();
});
});
describe('#status', function() { describe('#status', function() {
after(function() { after(function() {
comms.publish.restore(); comms.publish.restore();

View File

@ -58,7 +58,6 @@ describe("red/nodes/index", function() {
} }
it('nodes are initialised with credentials',function(done) { it('nodes are initialised with credentials',function(done) {
index.init(settings, storage); index.init(settings, storage);
index.registerType('test', TestNode); index.registerType('test', TestNode);
index.loadFlows().then(function() { index.loadFlows().then(function() {
@ -69,7 +68,6 @@ describe("red/nodes/index", function() {
}).otherwise(function(err) { }).otherwise(function(err) {
done(err); done(err);
}); });
}); });
it('flows should be initialised',function(done) { it('flows should be initialised',function(done) {
@ -128,7 +126,7 @@ describe("red/nodes/index", function() {
foo: {type:"test"} foo: {type:"test"}
} }
}); });
var testnode = new TestNode({id:'tab1',type:'test',name:'barney'}); var testnode = new TestNode({id:'tab1',type:'test',name:'barney', '_alias':'tab1'});
credentials.getDefinition("test").should.have.property('foo'); credentials.getDefinition("test").should.have.property('foo');
done(); done();
}); });