Authored by ZhouQiqi

add BaseTransport unit test and some mock

... ... @@ -34,7 +34,7 @@ gulp.task('doc', function() {
//jshint
gulp.task('hint', function() {
return gulp.src(['lib/**/*.js'])
return gulp.src(['libs/**/*.js'])
.pipe(jshint())
.pipe(jshint.reporter(stylish));
});
\ No newline at end of file
... ...
"use strict";
//winston logger
const Transport = require('winston').Transport,
let Transport = require('winston').Transport,
os = require('os');
/**
... ... @@ -47,7 +47,7 @@ class BaseTranport extends Transport {
* @method addLine
* @description abstract method,for sub class implement
*/
addline() {
addLine() {
}
... ... @@ -72,7 +72,7 @@ class BaseTranport extends Transport {
'pid=' + process.pid,
'level=' + level,
]
];
//make fields
let fields = [
... ...
"use strict";
//Base Class for influxdb trabsport
const BaseTransport = require('./BaseTransport'),
let BaseTransport = require('./BaseTransport'),
http = require('http');
//for make influxdb query string
... ...
"use strict";
//Base Class for influxdb trabsport
const BaseTransport = require('./BaseTransport'),
let BaseTransport = require('./BaseTransport'),
dgram = require("dgram");
/**
... ...
"use strict";
let expect = require('expect.js'),
rewire = require('rewire'),
Transport = require('../mock/winston').Transport,
os = require('../mock/os');
describe('/libs/BaseTransport',function(){
let BaseTransport = rewire("../../libs/BaseTransport");
BaseTransport.__set__('Transport',Transport);
BaseTransport.__set__('os',os);
describe('constructor',function(){
let trans = new BaseTransport({
level:'error',
host:'xxx',
port:'1234',
measurement:'xxx'
});
it('level do not set expect default info',function(){
let trans = new BaseTransport({
host:'xxx',
port:'1234',
measurement:'xxx'
});
expect(trans.level).to.be('info');
});
it('init options expect right level,options,logs',function(){
trans.log('info','xxx',{},function(){});
expect(trans.options.host).to.be('xxx');
expect(trans.level).to.be('error');
expect(trans.options.port).to.be('1234');
expect(trans.options.measurement).to.be('xxx');
});
it('expect log method measurement null set in log',function(){
trans.options.measurement = null;
trans.log('info','xxx',{},function(){});
expect(trans.options.measurement).to.be('log');
});
it('expect makeLine meta is not object',function(){
trans.log('info','xxx',1231,function(){});
expect(trans.options.line).to.be('log,host=test,pid='+process.pid+',level=info message="xxx",meta="1231"');
})
});
});
\ No newline at end of file
... ...
"use strict";
/**
* dgram mock
* @type {Object}
*/
let dgram = {
createSocket:function(name) {
return {
send:function(p1,p2,p3,p4,p5,function() {
}),
close:function() {
}
}
}
}
module.exports = dgram;
\ No newline at end of file
... ...
"use strict";
/**
* mock http
* @type {Object}
*/
let http = {
request:function(options,cb) {
cb({
on:function(event,cb) {
if(event == 'data') {
cd(http.buff);
} else {
cb();
}
}
})
return {
write:function(line) {
http.writeRet = line;
},
end:function() {
}
}
},
buff: null
writeRet: null
};
module.exports = http
\ No newline at end of file
... ...
"use strict";
/**
* os mock
* @type {Object}
*/
let os = {
hostname : function() {
return 'test'
}
}
module.exports = os;
\ No newline at end of file
... ...
"use strict";
/**
* winston mock
* @type {Object}
*/
let winston = {
Transport : function(){},
transports : {},
Logger : function(){}
};
module.exports = winston;
\ No newline at end of file
... ...