From f983e4da9f74f7da526426048c13cdcfee333605 Mon Sep 17 00:00:00 2001 From: Nick O'Leary Date: Thu, 29 Jan 2015 09:57:09 +0000 Subject: [PATCH] Renable unit tests following logging api changes --- test/nodes/core/core/58-debug_spec.js | 57 ++++++++-------- test/nodes/core/core/80-function_spec.js | 40 ++++++----- test/nodes/core/parsers/70-HTML_spec.js | 51 +++++++------- test/nodes/core/parsers/70-JSON_spec.js | 82 ++++++++++++----------- test/nodes/core/parsers/70-XML_spec.js | 84 ++++++++++++++---------- test/nodes/helper.js | 11 +++- 6 files changed, 180 insertions(+), 145 deletions(-) diff --git a/test/nodes/core/core/58-debug_spec.js b/test/nodes/core/core/58-debug_spec.js index c5b590fb8..d4d877d63 100644 --- a/test/nodes/core/core/58-debug_spec.js +++ b/test/nodes/core/core/58-debug_spec.js @@ -54,34 +54,35 @@ describe('debug node', function() { }); }); - // HELEN - commenting out for now -// it('should publish to console', function(done) { -// var flow = [{id:"n1", type:"debug", console: "true" }]; -// helper.load(debugNode, flow, function() { -// var n1 = helper.getNode("n1"); -// var count = 0; -// n1.on('log', function(msg) { -// var tstmp = msg._timestamp; -// msg.should.eql({level:'log',id:'n1',type:'debug',msg:'test', _timestamp:tstmp}); -// count++; -// if (count == 2) { -// done(); -// } -// }); -// websocket_test(function() { -// n1.emit("input", {payload:"test"}); -// }, function(msg) { -// JSON.parse(msg).should.eql({ -// topic:"debug",data:{id:"n1",msg:"test",property:"payload"} -// }); -// count++; -// }, function() { -// if (count == 2) { -// done(); -// } -// }); -// }); -// }); + it('should publish to console', function(done) { + var flow = [{id:"n1", type:"debug", console: "true" }]; + helper.load(debugNode, flow, function() { + var n1 = helper.getNode("n1"); + var count = 0; + websocket_test(function() { + n1.emit("input", {payload:"test"}); + }, function(msg) { + JSON.parse(msg).should.eql({ + topic:"debug",data:{id:"n1",msg:"test",property:"payload"} + }); + count++; + }, function() { + try { + helper.log().called.should.be.true; + var logEvents = helper.log().args.filter(function(evt) { + return evt[0].level == "log"; + }); + logEvents.should.have.length(1); + var tstmp = logEvents[0][0].timestamp; + logEvents[0][0].should.eql({level:'log',id:'n1',type:'debug',msg:'test', timestamp:tstmp}); + + done(); + } catch(err) { + done(err); + } + }); + }); + }); it('should publish complete message', function(done) { var flow = [{id:"n1", type:"debug", complete: "true" }]; diff --git a/test/nodes/core/core/80-function_spec.js b/test/nodes/core/core/80-function_spec.js index 6fbe1c69e..b39a39ae8 100644 --- a/test/nodes/core/core/80-function_spec.js +++ b/test/nodes/core/core/80-function_spec.js @@ -131,23 +131,27 @@ describe('function node', function() { }); }); - // HELEN - commenting out for now -// it('should handle and log script error', function(done) { -// var flow = [{id:"n1",type:"function",wires:[["n2"]],func:"retunr"}]; -// helper.load(functionNode, flow, function() { -// var n1 = helper.getNode("n1"); -// n1.on("log", function(msg) { -// if (msg.level === 'error') { -// msg.should.have.property('level', 'error'); -// msg.should.have.property('id', 'n1'); -// msg.should.have.property('type', 'function'); -// msg.should.have.property('msg', 'ReferenceError: retunr is not defined'); -// done(); -// } -// -// }); -// n1.receive({payload:"foo",topic: "bar"}); -// }); -// }); + it('should handle and log script error', function(done) { + var flow = [{id:"n1",type:"function",wires:[["n2"]],func:"retunr"}]; + helper.load(functionNode, flow, function() { + var n1 = helper.getNode("n1"); + n1.receive({payload:"foo",topic: "bar"}); + try { + helper.log().called.should.be.true; + var logEvents = helper.log().args.filter(function(evt) { + return evt[0].level == "error"; + }); + logEvents.should.have.length(1); + var msg = logEvents[0][0]; + msg.should.have.property('level', 'error'); + msg.should.have.property('id', 'n1'); + msg.should.have.property('type', 'function'); + msg.should.have.property('msg', 'ReferenceError: retunr is not defined'); + done(); + } catch(err) { + done(err); + } + }); + }); }); diff --git a/test/nodes/core/parsers/70-HTML_spec.js b/test/nodes/core/parsers/70-HTML_spec.js index 689e395d2..8e31368e5 100644 --- a/test/nodes/core/parsers/70-HTML_spec.js +++ b/test/nodes/core/parsers/70-HTML_spec.js @@ -122,29 +122,34 @@ describe('html node', function() { }); }); }); - // HELEN - commenting out for now -// it('should log on error', function(done) { -// fs.readFile(file,function(err, data) { -// var flow = [{id:"n1",type:"html",wires:[["n2"]],tag:"p"}, -// {id:"n2", type:"helper"}]; -// -// helper.load(htmlNode, flow, function() { -// var n1 = helper.getNode("n1"); -// var n2 = helper.getNode("n2"); -// n1.on("log", function(msg) { -// if (msg.level && (msg.level === 'metric')) { -// // do nothing as we've just hit a metric related msg -// } else { -// msg.should.have.property('msg'); -// msg.msg.indexOf("Error:").should.be.above(-1); -// msg.msg.should.startWith("Error:"); -// done(); -// } -// }); -// n1.receive({payload:null,topic: "bar"}); -// }); -// }); -// }); + + it('should log on error', function(done) { + fs.readFile(file,function(err, data) { + var flow = [{id:"n1",type:"html",wires:[["n2"]],tag:"p"}, + {id:"n2", type:"helper"}]; + + helper.load(htmlNode, flow, function() { + var n1 = helper.getNode("n1"); + var n2 = helper.getNode("n2"); + try { + helper.log().called.should.be.false; + n1.receive({payload:null,topic: "bar"}); + helper.log().called.should.be.true; + var logEvents = helper.log().args.filter(function(evt) { + return evt[0].level == "log"; + }); + logEvents.should.have.length(1); + // Each logEvent is the array of args passed to the function. + logEvents[0][0].should.have.a.property('msg'); + logEvents[0][0].msg.should.startWith("Error:"); + + done(); + } catch(err) { + done(err); + } + }); + }); + }); describe('multiple messages', function(){ var cnt = 0; diff --git a/test/nodes/core/parsers/70-JSON_spec.js b/test/nodes/core/parsers/70-JSON_spec.js index 03db58ab2..3b5504560 100644 --- a/test/nodes/core/parsers/70-JSON_spec.js +++ b/test/nodes/core/parsers/70-JSON_spec.js @@ -70,43 +70,49 @@ describe('JSON node', function() { jn1.receive({payload:obj,topic: "bar"}); }); }); - // HELEN - commenting out for now -// it('should log an error if asked to parse an invalid json string', function(done) { -// var flow = [{id:"jn1",type:"json",wires:[["jn2"]],func:"return msg;"}, -// {id:"jn2", type:"helper"}]; -// helper.load(jsonNode, flow, function() { -// var jn1 = helper.getNode("jn1"); -// var jn2 = helper.getNode("jn2"); -// jn1.on("log", function(msg) { -// if (msg.level && (msg.level === 'metric')) { -// // do nothing as we've just hit a metric related msg -// } else { -// msg.should.have.property('msg'); -// should.deepEqual("SyntaxError: Unexpected token o"+ "\nfoo", msg.msg); -// done(); -// } -// }); -// jn1.receive({payload:'foo',topic: "bar"}); -// }); -// }); - // HELEN - commenting out for now -// it('should log an error if asked to parse something thats not json or js', function(done) { -// var flow = [{id:"jn1",type:"json",wires:[["jn2"]],func:"return msg;"}, -// {id:"jn2", type:"helper"}]; -// helper.load(jsonNode, flow, function() { -// var jn1 = helper.getNode("jn1"); -// var jn2 = helper.getNode("jn2"); -// jn1.on("log", function(msg) { -// if (msg.level && (msg.level === 'metric')) { -// // do nothing as we've just hit a metric related msg -// } else { -// msg.should.have.property('msg'); -// should.deepEqual("dropped: 1", msg.msg); -// done(); -// } -// }); -// jn1.receive({payload:1,topic: "bar"}); -// }); -// }); + + it('should log an error if asked to parse an invalid json string', function(done) { + var flow = [{id:"jn1",type:"json",wires:[["jn2"]],func:"return msg;"}, + {id:"jn2", type:"helper"}]; + helper.load(jsonNode, flow, function() { + try { + helper.log().called.should.be.false; + var jn1 = helper.getNode("jn1"); + var jn2 = helper.getNode("jn2"); + jn1.receive({payload:'foo',topic: "bar"}); + helper.log().called.should.be.true; + var logEvents = helper.log().args.filter(function(evt) { + return evt[0].level == "log"; + }); + logEvents.should.have.length(1); + logEvents[0][0].should.have.a.property('msg',"SyntaxError: Unexpected token o"+ "\nfoo"); + done(); + } catch(err) { + done(err); + } + }); + }); + + it('should log an error if asked to parse something thats not json or js', function(done) { + var flow = [{id:"jn1",type:"json",wires:[["jn2"]],func:"return msg;"}, + {id:"jn2", type:"helper"}]; + helper.load(jsonNode, flow, function() { + try { + helper.log().called.should.be.false; + var jn1 = helper.getNode("jn1"); + var jn2 = helper.getNode("jn2"); + jn1.receive({payload:1,topic: "bar"}); + helper.log().called.should.be.true; + var logEvents = helper.log().args.filter(function(evt) { + return evt[0].level == "log"; + }); + logEvents.should.have.length(1); + logEvents[0][0].should.have.a.property('msg',"dropped: 1"); + done(); + } catch(err) { + done(err); + } + }); + }); }); diff --git a/test/nodes/core/parsers/70-XML_spec.js b/test/nodes/core/parsers/70-XML_spec.js index 2c169aa4a..d83360ac0 100644 --- a/test/nodes/core/parsers/70-XML_spec.js +++ b/test/nodes/core/parsers/70-XML_spec.js @@ -74,41 +74,53 @@ describe('XML node', function() { n1.receive({payload:obj,topic: "bar"}); }); }); - // HELEN - commenting out for now -// it('should log an error if asked to parse an invalid xml string', function(done) { -// var flow = [{id:"n1",type:"xml",wires:[["n2"]],func:"return msg;"}, -// {id:"n2", type:"helper"}]; -// helper.load(xmlNode, flow, function() { -// var n1 = helper.getNode("n1"); -// var n2 = helper.getNode("n2"); -// n1.on("log", function(msg) { -// if (msg.level && (msg.level === 'metric')) { -// // do nothing as we've just hit a metric related msg -// } else { -// should.deepEqual("error", msg.level); -// done(); -// } -// }); -// n1.receive({payload:'',topic: "bar"}); -// }); -// }); - // HELEN - commenting out for now -// it('should log an error if asked to parse something thats not xml or js', function(done) { -// var flow = [{id:"n1",type:"xml",wires:[["n2"]],func:"return msg;"}, -// {id:"n2", type:"helper"}]; -// helper.load(xmlNode, flow, function() { -// var n1 = helper.getNode("n1"); -// var n2 = helper.getNode("n2"); -// n1.on("log", function(msg) { -// if (msg.level && (msg.level === 'metric')) { -// // do nothing as we've just hit a metric related msg -// } else {msg.should.have.property('msg'); -// should.deepEqual("This node only handles xml strings or js objects.", msg.msg); -// done(); -// } -// }); -// n1.receive({payload:1,topic: "bar"}); -// }); -// }); + + it('should log an error if asked to parse an invalid xml string', function(done) { + var flow = [{id:"n1",type:"xml",wires:[["n2"]],func:"return msg;"}, + {id:"n2", type:"helper"}]; + helper.load(xmlNode, flow, function() { + var n1 = helper.getNode("n1"); + var n2 = helper.getNode("n2"); + n1.receive({payload:'',topic: "bar"}); + setTimeout(function() { + try { + helper.log().called.should.be.true; + var logEvents = helper.log().args.filter(function(evt) { + return evt[0].level == "error"; + }); + logEvents.should.have.length(1); + logEvents[0][0].should.have.a.property('msg'); + logEvents[0][0].msg.toString().should.startWith("Error: Attribute without value"); + + done(); + } catch(err) { + done(err); + } + },200); + }); + }); + + it('should log an error if asked to parse something thats not xml or js', function(done) { + var flow = [{id:"n1",type:"xml",wires:[["n2"]],func:"return msg;"}, + {id:"n2", type:"helper"}]; + helper.load(xmlNode, flow, function() { + var n1 = helper.getNode("n1"); + var n2 = helper.getNode("n2"); + n1.receive({payload:1,topic: "bar"}); + setTimeout(function() { + try { + helper.log().called.should.be.true; + var logEvents = helper.log().args.filter(function(evt) { + return evt[0].level == "log"; + }); + logEvents.should.have.length(1); + logEvents[0][0].should.have.a.property('msg',"This node only handles xml strings or js objects."); + done(); + } catch(err) { + done(err); + } + },200); + }); + }); }); diff --git a/test/nodes/helper.js b/test/nodes/helper.js index 943b4b227..8ba9b90fe 100644 --- a/test/nodes/helper.js +++ b/test/nodes/helper.js @@ -15,6 +15,7 @@ **/ var should = require("should"); +var sinon = require("sinon"); var when = require("when"); var request = require('supertest'); var nock; @@ -32,6 +33,7 @@ var redNodes = require("../../red/nodes"); var flows = require("../../red/nodes/flows"); var credentials = require("../../red/nodes/credentials"); var comms = require("../../red/comms.js"); +var log = require("../../red/log.js"); var http = require('http'); var express = require('express'); @@ -41,7 +43,7 @@ var address = '127.0.0.1'; var listenPort = 0; // use ephemeral port var port; var url; - +var logSpy; var server; function helperNode(n) { @@ -50,6 +52,8 @@ function helperNode(n) { module.exports = { load: function(testNode, testFlows, testCredentials, cb) { + logSpy = sinon.spy(log,"log"); + if (typeof testCredentials === 'function') { cb = testCredentials; testCredentials = {}; @@ -92,6 +96,7 @@ module.exports = { unload: function() { // TODO: any other state to remove between tests? redNodes.clearRegistry(); + logSpy.restore(); return flows.stopFlows(); }, @@ -130,5 +135,7 @@ module.exports = { url: function() { return url; }, nock: nock, - + + log: function() { return logSpy;} }; +