mirror of
				https://github.com/node-red/node-red.git
				synced 2025-03-01 10:36:34 +00:00 
			
		
		
		
	More tests for red, log, info and util.
This commit is contained in:
		
							
								
								
									
										14
									
								
								red/log.js
									
									
									
									
									
								
							
							
						
						
									
										14
									
								
								red/log.js
									
									
									
									
									
								
							| @@ -17,7 +17,6 @@ | |||||||
| var util = require("util"); | var util = require("util"); | ||||||
| var EventEmitter = require("events").EventEmitter; | var EventEmitter = require("events").EventEmitter; | ||||||
|  |  | ||||||
|  |  | ||||||
| var levels = { | var levels = { | ||||||
|     off:    1, |     off:    1, | ||||||
|     fatal:  10, |     fatal:  10, | ||||||
| @@ -27,7 +26,8 @@ var levels = { | |||||||
|     debug:  50, |     debug:  50, | ||||||
|     trace:  60, |     trace:  60, | ||||||
|     metric: 99 |     metric: 99 | ||||||
| } | }; | ||||||
|  |  | ||||||
| var levelNames = { | var levelNames = { | ||||||
|     10: "fatal", |     10: "fatal", | ||||||
|     20: "error", |     20: "error", | ||||||
| @@ -36,7 +36,7 @@ var levelNames = { | |||||||
|     50: "debug", |     50: "debug", | ||||||
|     60: "trace", |     60: "trace", | ||||||
|     99: "metric" |     99: "metric" | ||||||
| } | }; | ||||||
|  |  | ||||||
| var logHandlers = []; | var logHandlers = []; | ||||||
|  |  | ||||||
| @@ -47,6 +47,7 @@ var ConsoleLogHandler = function(settings) { | |||||||
|     this.metricsOn = settings.metrics||false; |     this.metricsOn = settings.metrics||false; | ||||||
|     metricsEnabled = this.metricsOn; |     metricsEnabled = this.metricsOn; | ||||||
|     this.on("log",function(msg) { |     this.on("log",function(msg) { | ||||||
|  |         /* istanbul ignore else */ | ||||||
|         if (this.shouldReportMessage(msg.level)) { |         if (this.shouldReportMessage(msg.level)) { | ||||||
|             if (msg.level == log.METRIC) { |             if (msg.level == log.METRIC) { | ||||||
|                 util.log("[metric] "+JSON.stringify(msg)); |                 util.log("[metric] "+JSON.stringify(msg)); | ||||||
| @@ -70,7 +71,7 @@ var log = module.exports = { | |||||||
|     DEBUG:  50, |     DEBUG:  50, | ||||||
|     TRACE:  60, |     TRACE:  60, | ||||||
|     METRIC: 99, |     METRIC: 99, | ||||||
|      |  | ||||||
|     init: function(settings) { |     init: function(settings) { | ||||||
|         logHandlers = []; |         logHandlers = []; | ||||||
|         var consoleSettings = {}; |         var consoleSettings = {}; | ||||||
| @@ -79,11 +80,9 @@ var log = module.exports = { | |||||||
|         } |         } | ||||||
|         log.addHandler(new ConsoleLogHandler(consoleSettings)); |         log.addHandler(new ConsoleLogHandler(consoleSettings)); | ||||||
|     }, |     }, | ||||||
|          |  | ||||||
|     addHandler: function(func) { |     addHandler: function(func) { | ||||||
|         logHandlers.push(func); |         logHandlers.push(func); | ||||||
|     }, |     }, | ||||||
|      |  | ||||||
|     log: function(msg) { |     log: function(msg) { | ||||||
|         msg.timestamp = Date.now(); |         msg.timestamp = Date.now(); | ||||||
|         logHandlers.forEach(function(handler) { |         logHandlers.forEach(function(handler) { | ||||||
| @@ -105,10 +104,7 @@ var log = module.exports = { | |||||||
|     debug: function(msg) { |     debug: function(msg) { | ||||||
|         log.log({level:log.DEBUG,msg:msg}); |         log.log({level:log.DEBUG,msg:msg}); | ||||||
|     }, |     }, | ||||||
|      |  | ||||||
|      |  | ||||||
|     metric: function() { |     metric: function() { | ||||||
|         return metricsEnabled; |         return metricsEnabled; | ||||||
|     } |     } | ||||||
| } | } | ||||||
|  |  | ||||||
|   | |||||||
| @@ -89,6 +89,7 @@ function compareObjects(obj1,obj2) { | |||||||
|         return false; |         return false; | ||||||
|     } |     } | ||||||
|     for (var k in obj1) { |     for (var k in obj1) { | ||||||
|  |         /* istanbul ignore else */ | ||||||
|         if (obj1.hasOwnProperty(k)) { |         if (obj1.hasOwnProperty(k)) { | ||||||
|             if (!compareObjects(obj1[k],obj2[k])) { |             if (!compareObjects(obj1[k],obj2[k])) { | ||||||
|                 return false; |                 return false; | ||||||
| @@ -104,4 +105,3 @@ module.exports = { | |||||||
|     cloneMessage: cloneMessage, |     cloneMessage: cloneMessage, | ||||||
|     compareObjects: compareObjects |     compareObjects: compareObjects | ||||||
| }; | }; | ||||||
|  |  | ||||||
|   | |||||||
| @@ -52,6 +52,7 @@ describe("info api", function() { | |||||||
|                     } |                     } | ||||||
|                     res.body.should.have.property("httpNodeRoot","testHttpNodeRoot"); |                     res.body.should.have.property("httpNodeRoot","testHttpNodeRoot"); | ||||||
|                     res.body.should.have.property("version","testVersion"); |                     res.body.should.have.property("version","testVersion"); | ||||||
|  |                     res.body.should.have.property("paletteCategories",["red","blue","green"]); | ||||||
|                     res.body.should.not.have.property("foo",123); |                     res.body.should.not.have.property("foo",123); | ||||||
|                     done(); |                     done(); | ||||||
|                 }); |                 }); | ||||||
|   | |||||||
| @@ -14,6 +14,8 @@ | |||||||
|  * limitations under the License. |  * limitations under the License. | ||||||
|  **/ |  **/ | ||||||
| var should = require("should"); | var should = require("should"); | ||||||
|  | var sinon = require("sinon"); | ||||||
|  | var util = require("util"); | ||||||
|  |  | ||||||
| describe("red/log", function() { | describe("red/log", function() { | ||||||
|     it('can be required without errors', function() { |     it('can be required without errors', function() { | ||||||
| @@ -21,19 +23,33 @@ describe("red/log", function() { | |||||||
|     }); |     }); | ||||||
|  |  | ||||||
|     var log = require("../../red/log"); |     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() { |     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("This is an error"); | ||||||
|         var ret = log.error(m); |         sinon.assert.calledWithMatch(util.log,""); | ||||||
|     }); |     }); | ||||||
|  |  | ||||||
|     it('it can raise a trace', function() { |     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("This is a trace"); | ||||||
|         var ret = log.trace(m); |         sinon.assert.calledWithMatch(util.log,""); | ||||||
|     }); |     }); | ||||||
|  |  | ||||||
|     it('it can raise a debug', function() { |     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("This is a debug"); | ||||||
|         var ret = log.debug(m); |         sinon.assert.calledWithMatch(util.log,""); | ||||||
|  |     }); | ||||||
|  |  | ||||||
|  |     it('it checks level of metrics', function() { | ||||||
|  |         log.metric().should.equal(true); | ||||||
|     }); |     }); | ||||||
| }); | }); | ||||||
|   | |||||||
| @@ -24,22 +24,22 @@ describe("red/red", function() { | |||||||
|  |  | ||||||
|     it('returns an app object', function() { |     it('returns an app object', function() { | ||||||
|         var srv = RED.app.use("/test", function() { return "app"; }); |         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() { |     it('returns an httpAdmin object', function() { | ||||||
|         var srv = RED.httpAdmin.use("/test", function() { return "Admin"; }); |         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() { |     it('returns an httpNode object', function() { | ||||||
|         var srv = RED.httpNode.use("/test", function() { return "Node"; }); |         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() { |     it('it returns a server object', function() { | ||||||
|         var srv = RED.server; |         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); |         //srv.should.be.an.instanceOf(Object).and.have.property('timeout', 120000); | ||||||
|     }); |     }); | ||||||
|  |  | ||||||
|   | |||||||
| @@ -17,6 +17,16 @@ var should = require("should"); | |||||||
| var util = require("../../red/util"); | var util = require("../../red/util"); | ||||||
|  |  | ||||||
| describe("red/util", function() { | 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() { |     describe('ensureString', function() { | ||||||
|         it('strings are preserved', function() { |         it('strings are preserved', function() { | ||||||
|             util.ensureString('string').should.equal('string'); |             util.ensureString('string').should.equal('string'); | ||||||
| @@ -66,35 +76,34 @@ describe("red/util", function() { | |||||||
|             } |             } | ||||||
|         }); |         }); | ||||||
|     }); |     }); | ||||||
|      |  | ||||||
|     describe('cloneMessage', function() { |     describe('cloneMessage', function() { | ||||||
|         it('clones a simple message', function() { |         it('clones a simple message', function() { | ||||||
|             var msg = {string:"hi",array:[1,2,3],object:{a:1,subobject:{b:2}}}; |             var msg = {string:"hi",array:[1,2,3],object:{a:1,subobject:{b:2}}}; | ||||||
|              |  | ||||||
|             var cloned = util.cloneMessage(msg); |             var cloned = util.cloneMessage(msg); | ||||||
|              |  | ||||||
|             cloned.should.eql(msg); |             cloned.should.eql(msg); | ||||||
|              |  | ||||||
|             cloned.should.not.equal(msg); |             cloned.should.not.equal(msg); | ||||||
|             cloned.array.should.not.equal(msg.string); |             cloned.array.should.not.equal(msg.string); | ||||||
|             cloned.object.should.not.equal(msg.object); |             cloned.object.should.not.equal(msg.object); | ||||||
|             cloned.object.subobject.should.not.equal(msg.object.subobject); |             cloned.object.subobject.should.not.equal(msg.object.subobject); | ||||||
|              |  | ||||||
|             cloned.should.not.have.property("req"); |             cloned.should.not.have.property("req"); | ||||||
|             cloned.should.not.have.property("res"); |             cloned.should.not.have.property("res"); | ||||||
|         }); |         }); | ||||||
|         it('does not clone http req/res properties', function() { |         it('does not clone http req/res properties', function() { | ||||||
|             var msg = {req:{a:1},res:{b:2}}; |             var msg = {req:{a:1},res:{b:2}}; | ||||||
|              |  | ||||||
|             var cloned = util.cloneMessage(msg); |             var cloned = util.cloneMessage(msg); | ||||||
|              |  | ||||||
|             cloned.should.eql(msg); |             cloned.should.eql(msg); | ||||||
|             cloned.should.not.equal(msg); |             cloned.should.not.equal(msg); | ||||||
|              |  | ||||||
|             cloned.req.should.equal(msg.req); |             cloned.req.should.equal(msg.req); | ||||||
|             cloned.res.should.equal(msg.res); |             cloned.res.should.equal(msg.res); | ||||||
|         }); |         }); | ||||||
|              |  | ||||||
|     }); |     }); | ||||||
| }); | }); | ||||||
|  |  | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user