removing mocha, switch to nodeunit

This commit is contained in:
Spencer Alger
2013-09-17 09:01:35 -07:00
parent a305581d9c
commit 0ac81e1989
19 changed files with 373 additions and 209 deletions

32
test/.jshintrc Normal file
View File

@ -0,0 +1,32 @@
{
"node": true,
"bitwise": false,
"curly": true,
"eqnull": true,
"eqeqeq": true,
"forin": true,
"immed": true,
"expr": false,
"indent": 2,
"latedef": "nofunc",
"newcap": true,
"noarg": true,
"noempty": true,
"undef": true,
"quotmark": "single",
"plusplus": false,
"boss": true,
"trailing": true,
"laxbreak": true,
"laxcomma": true,
"validthis": true,
"sub": true,
"maxlen": 140,
"globals": {
"describe": true,
"it": true,
"beforeEach": true
}
}

52
test/Log.test.js Normal file
View File

@ -0,0 +1,52 @@
describe('Log (the log bridge)', function () {
var Log = require('../src/lib/Log');
describe('level', function () {
it('should return 2 for "warning"', function () {
Log.level('warning', 1)
.should.equal(2);
});
it('should return 2 for 2', function () {
Log.level('2', 1)
.should.equal(2);
});
it('should return the default for an invalid level', function () {
Log.level('invalid level', 2)
.should.equal(2);
});
});
describe('join', function () {
it('should join strings', function () {
Log.join(['one', 'two']).should.equal('one two');
});
it('should flatten nested arrays', function () {
Log.join(['one', ['three', 'four']])
.should.equal('one three,four');
});
it('should flatten arguments', function () {
(function() {
Log.join(arguments).should.equal('one two');
}('one', 'two'));
});
});
describe('When an instance has no outputs', function () {
var log = new Log([]); // no log outputs
it('error should not emit an event and return false', function () {
});
});
});

72
test/Loggers.test.js Normal file
View File

@ -0,0 +1,72 @@
describe('StdIo Logger', function () {
var Log = require('../src/lib/Log')
, log = new Log([])
, StdIo = require('../src/lib/loggers/StdIo')
, warningLogger;
beforeEach(function () {
if (warningLogger) {
warningLogger.cleanUpListeners();
}
// new logger in warning mode
warningLogger = new StdIo({
level: 2,
type: 'StdIo'
}, log);
});
it('should log error messages', function (done) {
warningLogger.write = function (to, label, colorize, what) {
label.should.equal('ERROR');
what.should.be.an.instanceof(Array);
what[0].should.equal('Test Error Message');
done();
};
log.error('Test Error Message');
});
it('should log warnings', function (done) {
warningLogger.write = function (to, label, colorize, what) {
label.should.equal('WARNING');
what.should.equal('Test Warning Message');
done();
};
log.warn('Test Warning', 'Message');
});
it('should NOT log info messages', function (done) {
if (log.info('Info')) {
done(new Error('There shouldn\'t be listeners for info logs'));
} else {
done();
}
});
it('should NOT log debug messages', function (done) {
if (log.debug('Debug')) {
done(new Error('There shouldn\'t be listeners for debug logs'));
} else {
done();
}
});
it('should NOT log trace messages', function (done) {
if (log.trace('curl "http://localhost:9200" -d "{ \"query\": ... }"')) {
done(new Error('There shouldn\'t be listeners for trace logs'));
} else {
done();
}
});
});

107
test/Utils.test.js Normal file
View File

@ -0,0 +1,107 @@
var _ = require('../src/lib/Utils');
describe('Utils', function () {
describe('isArrayOfObjects', function () {
var is;
beforeEach(function () {
is = [{}, {}];
});
it('should identify an array of objects', function () {
_.isArrayOfObjects(is).should.equal(true);
});
it('should identify a non object in the array', function () {
is.push(' not ');
_.isArrayOfObjects(is).should.equal(false);
});
});
describe('isArrayOfStrings', function () {
var is;
beforeEach(function () {
is = ['spencer', 'poop'];
});
it('should identify an array of strings', function () {
_.isArrayOfStrings(is).should.equal(true);
});
it('should identify a non string in the array', function () {
is.push({});
_.isArrayOfStrings(is).should.equal(false);
});
});
describe('isArrayOfArrays', function () {
var is;
beforeEach(function () {
is = [['im'], ['usefull']];
});
it('should identify an array of arrays', function () {
_.isArrayOfArrays(is).should.equal(true);
});
it('should identify a non array in the array', function () {
is.push({});
_.isArrayOfArrays(is).should.equal(false);
});
});
describe('isArrayOfFinites', function () {
var is;
beforeEach(function () {
is = [11123, 666];
});
it('should identify an array of objects', function () {
_.isArrayOfFinites(is).should.equal(true);
});
it('should identify a non object in the array', function () {
is.push(Infinity);
_.isArrayOfFinites(is).should.equal(false);
});
});
describe('isArrayOfFunctions', function () {
var is;
beforeEach(function () {
is = [console.error, console.log];
});
it('should identify an array of functions', function () {
_.isArrayOfFunctions(is).should.equal(true);
});
it('should identify a non function in the array', function () {
is.push('not');
_.isArrayOfFunctions(is).should.equal(false);
});
});
describe('isArrayOfRegExps', function () {
var is;
beforeEach(function () {
is = [/.*/, new RegExp('a')];
});
it('should identify an array of regular expressions', function () {
_.isArrayOfRegExps(is).should.equal(true);
});
it('should identify a non regular expression in the array', function () {
is.push('not');
_.isArrayOfRegExps(is).should.equal(false);
});
});
});

5
test/mocha.opts Normal file
View File

@ -0,0 +1,5 @@
--recursive
--require should
--reporter spec
--ui bdd
--g *.test.js

View File

@ -0,0 +1,40 @@
/* JSON Serializer tests */
var JsonSerializer = require('../../src/lib/serializers/Json');
describe('json serializer', function () {
var json;
beforeEach(function () {
json = new JsonSerializer();
});
it('creates simple json strings', function () {
json.serialize({foo: true}).should.eql('{"foo":true}');
});
it('creates pretty json strings', function () {
json.serialize({foo: true, bake: 'cake', 'with': ['bacon']}, null, ' ')
.should.eql(['{',
' "foo": true,',
' "bake": "cake",',
' "with": [',
' "bacon"',
' ]',
'}'].join('\n'));
});
it('reads simple json strings', function () {
json.unserialize('{"foo":true}').should.eql({ foo: true });
});
it('does not create date objects', function () {
json
.unserialize('{"date":"2012-04-23T18:25:43.511Z"}')
.should.eql({
date: '2012-04-23T18:25:43.511Z'
});
});
});

View File

@ -0,0 +1,87 @@
/* node transport function tests */
// TODO: add check to see if any data in ES, fail if so.
'use strict';
var esj = require('../../dist/elasticsearch-node.js');
var _c = new esj.Client();
exports.transportNode = {
setUp: function(done) {
done();
},
'hosts': function(test) {
test.expect(4);
_c.options.hosts = ['foo:9200','bar:9200'];
test.equal(_c.options.hosts.length, 2, 'should be 2');
test.equal(_c.options.hosts[1], 'bar:9200', 'should be bar:9200');
_c.options.hosts = ['localhost:9200'];
test.equal(_c.options.hosts.length, 1, 'should be 1');
test.equal(_c.options.hosts[0], 'localhost:9200', 'should be localhost:9200');
test.done();
},
'options': function(test) {
test.expect(6);
var _n = new esj.Client();
test.equal(_c.options.sniff_on_start, false, 'should be false');
test.equal(_c.options.sniff_after_requests, 0, 'should be 0');
test.equal(_c.options.sniff_on_connection_fail, false, 'should be false');
test.equal(_c.options.max_retries, 3, 'should be 3');
_c.options.max_retries = 5;
test.equal(_c.options.max_retries, 5, '_c max_retries should be 5');
test.equal(_n.options.max_retries, 3, '_n max_retries should be 3');
test.done();
},
// Create an index with put
'put': function(test) {
test.expect(1);
_c.transport.put('/foo',{},'{"foo":1}',function(res) {
test.equal(res.data.ok,true,'index should be created');
test.done();
});
},
'post': function(test) {
test.expect(1);
_c.transport.post('/foo/bar/baz',{},'{"foo":1}',function(res) {
test.equal(res.data.ok,true,'document should be created');
test.done();
});
},
'get success': function(test) {
test.expect(1);
_c.transport.get('/foo/bar/baz',{},'',function(res) {
test.deepEqual(res.data._source,{foo:1},'should contain document source');
test.done();
});
},
'get error': function(test) {
test.expect(1);
_c.transport.get('/foo/bar',{},'',function(data){},function(res) {
test.equal(res.data,'No handler found for uri [/foo/bar?] and method [GET]','End point should not exist');
test.done();
});
},
'del': function(test) {
test.expect(1);
_c.transport.del('/foo',{},'',function(res) {
test.equal(res.data.ok,true,'index should be deleted');
test.done();
});
},
'error callback': function(test) {
test.expect(1);
_c.options.hosts = ['localhost:1'];
_c.transport.get('/foo/bar',{},'',function(res){
test.equal(res.data,'Test failed','Success function should not be called');
test.done();
},function(res) {
test.equal(res.code,'ECONNREFUSED','Connection should be refused');
test.done();
});
}
};