Authored by ZhouQiqi

add BaseTransport unit test and some mock

@@ -34,7 +34,7 @@ gulp.task('doc', function() { @@ -34,7 +34,7 @@ gulp.task('doc', function() {
34 34
35 //jshint 35 //jshint
36 gulp.task('hint', function() { 36 gulp.task('hint', function() {
37 - return gulp.src(['lib/**/*.js']) 37 + return gulp.src(['libs/**/*.js'])
38 .pipe(jshint()) 38 .pipe(jshint())
39 .pipe(jshint.reporter(stylish)); 39 .pipe(jshint.reporter(stylish));
40 }); 40 });
1 "use strict"; 1 "use strict";
2 2
3 //winston logger 3 //winston logger
4 -const Transport = require('winston').Transport, 4 +let Transport = require('winston').Transport,
5 os = require('os'); 5 os = require('os');
6 6
7 /** 7 /**
@@ -47,7 +47,7 @@ class BaseTranport extends Transport { @@ -47,7 +47,7 @@ class BaseTranport extends Transport {
47 * @method addLine 47 * @method addLine
48 * @description abstract method,for sub class implement 48 * @description abstract method,for sub class implement
49 */ 49 */
50 - addline() { 50 + addLine() {
51 51
52 } 52 }
53 53
@@ -72,7 +72,7 @@ class BaseTranport extends Transport { @@ -72,7 +72,7 @@ class BaseTranport extends Transport {
72 'pid=' + process.pid, 72 'pid=' + process.pid,
73 'level=' + level, 73 'level=' + level,
74 74
75 - ] 75 + ];
76 76
77 //make fields 77 //make fields
78 let fields = [ 78 let fields = [
1 "use strict"; 1 "use strict";
2 //Base Class for influxdb trabsport 2 //Base Class for influxdb trabsport
3 -const BaseTransport = require('./BaseTransport'), 3 +let BaseTransport = require('./BaseTransport'),
4 http = require('http'); 4 http = require('http');
5 5
6 //for make influxdb query string 6 //for make influxdb query string
1 "use strict"; 1 "use strict";
2 //Base Class for influxdb trabsport 2 //Base Class for influxdb trabsport
3 -const BaseTransport = require('./BaseTransport'), 3 +let BaseTransport = require('./BaseTransport'),
4 dgram = require("dgram"); 4 dgram = require("dgram");
5 5
6 /** 6 /**
  1 +"use strict";
  2 +
  3 +let expect = require('expect.js'),
  4 +rewire = require('rewire'),
  5 +Transport = require('../mock/winston').Transport,
  6 +os = require('../mock/os');
  7 +
  8 +describe('/libs/BaseTransport',function(){
  9 + let BaseTransport = rewire("../../libs/BaseTransport");
  10 + BaseTransport.__set__('Transport',Transport);
  11 + BaseTransport.__set__('os',os);
  12 + describe('constructor',function(){
  13 + let trans = new BaseTransport({
  14 + level:'error',
  15 + host:'xxx',
  16 + port:'1234',
  17 + measurement:'xxx'
  18 + });
  19 +
  20 + it('level do not set expect default info',function(){
  21 + let trans = new BaseTransport({
  22 + host:'xxx',
  23 + port:'1234',
  24 + measurement:'xxx'
  25 + });
  26 + expect(trans.level).to.be('info');
  27 + });
  28 +
  29 + it('init options expect right level,options,logs',function(){
  30 + trans.log('info','xxx',{},function(){});
  31 + expect(trans.options.host).to.be('xxx');
  32 + expect(trans.level).to.be('error');
  33 + expect(trans.options.port).to.be('1234');
  34 + expect(trans.options.measurement).to.be('xxx');
  35 + });
  36 +
  37 + it('expect log method measurement null set in log',function(){
  38 + trans.options.measurement = null;
  39 + trans.log('info','xxx',{},function(){});
  40 + expect(trans.options.measurement).to.be('log');
  41 + });
  42 +
  43 + it('expect makeLine meta is not object',function(){
  44 + trans.log('info','xxx',1231,function(){});
  45 + expect(trans.options.line).to.be('log,host=test,pid='+process.pid+',level=info message="xxx",meta="1231"');
  46 + })
  47 + });
  48 +
  49 +});
  1 +"use strict";
  2 +
  3 +/**
  4 + * dgram mock
  5 + * @type {Object}
  6 + */
  7 +let dgram = {
  8 + createSocket:function(name) {
  9 + return {
  10 + send:function(p1,p2,p3,p4,p5,function() {
  11 +
  12 + }),
  13 + close:function() {
  14 +
  15 + }
  16 + }
  17 + }
  18 +}
  19 +
  20 +module.exports = dgram;
  1 +"use strict";
  2 +
  3 +/**
  4 + * mock http
  5 + * @type {Object}
  6 + */
  7 +let http = {
  8 + request:function(options,cb) {
  9 + cb({
  10 + on:function(event,cb) {
  11 + if(event == 'data') {
  12 + cd(http.buff);
  13 + } else {
  14 + cb();
  15 + }
  16 + }
  17 + })
  18 +
  19 + return {
  20 + write:function(line) {
  21 + http.writeRet = line;
  22 + },
  23 + end:function() {
  24 +
  25 + }
  26 + }
  27 + },
  28 + buff: null
  29 + writeRet: null
  30 +};
  31 +
  32 +module.exports = http
  1 +"use strict";
  2 +
  3 +/**
  4 + * os mock
  5 + * @type {Object}
  6 + */
  7 +let os = {
  8 + hostname : function() {
  9 + return 'test'
  10 + }
  11 +}
  12 +
  13 +module.exports = os;
  1 +"use strict";
  2 +
  3 +/**
  4 + * winston mock
  5 + * @type {Object}
  6 + */
  7 +let winston = {
  8 + Transport : function(){},
  9 + transports : {},
  10 + Logger : function(){}
  11 +};
  12 +
  13 +module.exports = winston;