More tests for red, log, info and util.

This commit is contained in:
dceejay 2015-03-06 14:14:47 +00:00
parent 128415bc9e
commit 910d983b82
6 changed files with 52 additions and 30 deletions

View File

@ -17,7 +17,6 @@
var util = require("util");
var EventEmitter = require("events").EventEmitter;
var levels = {
off: 1,
fatal: 10,
@ -27,7 +26,8 @@ var levels = {
debug: 50,
trace: 60,
metric: 99
}
};
var levelNames = {
10: "fatal",
20: "error",
@ -36,7 +36,7 @@ var levelNames = {
50: "debug",
60: "trace",
99: "metric"
}
};
var logHandlers = [];
@ -47,6 +47,7 @@ var ConsoleLogHandler = function(settings) {
this.metricsOn = settings.metrics||false;
metricsEnabled = this.metricsOn;
this.on("log",function(msg) {
/* istanbul ignore else */
if (this.shouldReportMessage(msg.level)) {
if (msg.level == log.METRIC) {
util.log("[metric] "+JSON.stringify(msg));
@ -70,7 +71,7 @@ var log = module.exports = {
DEBUG: 50,
TRACE: 60,
METRIC: 99,
init: function(settings) {
logHandlers = [];
var consoleSettings = {};
@ -79,11 +80,9 @@ var log = module.exports = {
}
log.addHandler(new ConsoleLogHandler(consoleSettings));
},
addHandler: function(func) {
logHandlers.push(func);
},
log: function(msg) {
msg.timestamp = Date.now();
logHandlers.forEach(function(handler) {
@ -105,10 +104,7 @@ var log = module.exports = {
debug: function(msg) {
log.log({level:log.DEBUG,msg:msg});
},
metric: function() {
return metricsEnabled;
}
}

View File

@ -89,6 +89,7 @@ function compareObjects(obj1,obj2) {
return false;
}
for (var k in obj1) {
/* istanbul ignore else */
if (obj1.hasOwnProperty(k)) {
if (!compareObjects(obj1[k],obj2[k])) {
return false;
@ -104,4 +105,3 @@ module.exports = {
cloneMessage: cloneMessage,
compareObjects: compareObjects
};

View File

@ -52,6 +52,7 @@ describe("info api", function() {
}
res.body.should.have.property("httpNodeRoot","testHttpNodeRoot");
res.body.should.have.property("version","testVersion");
res.body.should.have.property("paletteCategories",["red","blue","green"]);
res.body.should.not.have.property("foo",123);
done();
});

View File

@ -14,6 +14,8 @@
* limitations under the License.
**/
var should = require("should");
var sinon = require("sinon");
var util = require("util");
describe("red/log", function() {
it('can be required without errors', function() {
@ -21,19 +23,33 @@ describe("red/log", function() {
});
var log = require("../../red/log");
var sett = {logging: { console: { level: 'metric', metrics: true } } }
log.init(sett);
beforeEach(function () {
var spy = sinon.spy(util, 'log');
});
afterEach(function() {
util.log.restore();
});
it('it can raise an error', function() {
var m = {level:20, msg:"This is an error", type:"test", id:"12345", name:"ERROR" };
var ret = log.error(m);
var ret = log.error("This is an error");
sinon.assert.calledWithMatch(util.log,"");
});
it('it can raise a trace', function() {
var m = {level:60, msg:"This is a trace", type:"test", id:"12345", name:"TRACE" };
var ret = log.trace(m);
var ret = log.trace("This is a trace");
sinon.assert.calledWithMatch(util.log,"");
});
it('it can raise a debug', function() {
var m = {level:50, msg:"This is a debug", type:"test", id:"12345", name:"DEBUG" };
var ret = log.debug(m);
var ret = log.debug("This is a debug");
sinon.assert.calledWithMatch(util.log,"");
});
it('it checks level of metrics', function() {
log.metric().should.equal(true);
});
});

View File

@ -24,22 +24,22 @@ describe("red/red", function() {
it('returns an app object', function() {
var srv = RED.app.use("/test", function() { return "app"; });
//srv.should.be.an.instanceOf(Object);
srv.should.be.an.instanceOf(Object);
});
it('returns an httpAdmin object', function() {
var srv = RED.httpAdmin.use("/test", function() { return "Admin"; });
//srv.should.be.an.instanceOf(Object);
srv.should.be.an.instanceOf(Object);
});
it('returns an httpNode object', function() {
var srv = RED.httpNode.use("/test", function() { return "Node"; });
//srv.should.be.an.instanceOf(Object);
srv.should.be.an.instanceOf(Object);
});
it('it returns a server object', function() {
var srv = RED.server;
//srv.should.be.an.instanceOf(Object).and.have.property('domain', null);
srv.should.be.an.instanceOf(Object).and.have.property('domain', null);
//srv.should.be.an.instanceOf(Object).and.have.property('timeout', 120000);
});

View File

@ -17,6 +17,16 @@ var should = require("should");
var util = require("../../red/util");
describe("red/util", function() {
// only test for things not tested by overall grunt
describe('compareObjects', function() {
it('unequal arrays are unequal', function() {
util.compareObjects(["a"],"a").should.equal(false);
});
it('unequal key lengths are unequal', function() {
util.compareObjects({"a":1},{"a":1,"b":1}).should.equal(false);
});
});
describe('ensureString', function() {
it('strings are preserved', function() {
util.ensureString('string').should.equal('string');
@ -66,35 +76,34 @@ describe("red/util", function() {
}
});
});
describe('cloneMessage', function() {
it('clones a simple message', function() {
var msg = {string:"hi",array:[1,2,3],object:{a:1,subobject:{b:2}}};
var cloned = util.cloneMessage(msg);
cloned.should.eql(msg);
cloned.should.not.equal(msg);
cloned.array.should.not.equal(msg.string);
cloned.object.should.not.equal(msg.object);
cloned.object.subobject.should.not.equal(msg.object.subobject);
cloned.should.not.have.property("req");
cloned.should.not.have.property("res");
});
it('does not clone http req/res properties', function() {
var msg = {req:{a:1},res:{b:2}};
var cloned = util.cloneMessage(msg);
cloned.should.eql(msg);
cloned.should.not.equal(msg);
cloned.req.should.equal(msg.req);
cloned.res.should.equal(msg.res);
});
});
});