Initial commit

This commit is contained in:
Rashid Khan
2013-06-21 15:30:43 -07:00
parent 8057543ca8
commit c3b2f0da73
21 changed files with 1246 additions and 0 deletions

58
test/test_logger.js Normal file
View File

@ -0,0 +1,58 @@
/* 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();
/*
======== A Handy Little Nodeunit Reference ========
https://github.com/caolan/nodeunit
Test methods:
test.expect(numAssertions)
test.done()
Test assertions:
test.ok(value, [message])
test.equal(actual, expected, [message])
test.notEqual(actual, expected, [message])
test.deepEqual(actual, expected, [message])
test.notDeepEqual(actual, expected, [message])
test.strictEqual(actual, expected, [message])
test.notStrictEqual(actual, expected, [message])
test.throws(block, [error], [message])
test.doesNotThrow(block, [error], [message])
test.ifError(value)
*/
exports.logger = {
setUp: function(done) {
// Suppress console messages (this, sadly, applies globally)
//console.error = console.warn = console.info = console.log = function() {}
done();
},
'log': function(test) {
test.expect(5);
// Test defaults
test.equal(_c.logger.error('error'),'error','Error should be logged');
test.equal(_c.logger.warn('warn'),'warn','Warn should be logged');
test.equal(_c.logger.info('info'),false,'Info should not be logged');
test.equal(_c.logger.debug('debug'),false,'Debug should not be logged');
// Turn on info logging in first client
_c.logger.options.info = true;
test.equal(_c.logger.info('info'),'info','Info should be logged');
test.done();
},
'trace': function(test) {
test.expect(2);
// Test defaults
test.equal(_c.tracer.info('info'),false,'Info should not be logged');
test.equal(_c.tracer.trace('trace'),false,'Trace should not be logged');
test.done();
}
};

59
test/test_selector.js Normal file
View File

@ -0,0 +1,59 @@
/* selector function tests */
'use strict';
var esj = require('../dist/elasticsearch-node.js');
var _c = new esj.Client();
/*
======== A Handy Little Nodeunit Reference ========
https://github.com/caolan/nodeunit
Test methods:
test.expect(numAssertions)
test.done()
Test assertions:
test.ok(value, [message])
test.equal(actual, expected, [message])
test.notEqual(actual, expected, [message])
test.deepEqual(actual, expected, [message])
test.notDeepEqual(actual, expected, [message])
test.strictEqual(actual, expected, [message])
test.notStrictEqual(actual, expected, [message])
test.throws(block, [error], [message])
test.doesNotThrow(block, [error], [message])
test.ifError(value)
*/
exports.selector = {
setUp: function(done) {
done();
},
'exists': function(test) {
test.expect(3);
test.ok(esj.Selector,'Should exist');
test.ok(esj.Selector.roundRobin,'Should exist');
test.ok(esj.Selector.random,'Should exist');
test.done();
},
'roundRobin' : function(test) {
test.expect(4);
var hosts = ['foo','bar','baz'];
test.equal(esj.Selector.roundRobin(hosts),'baz','Should be baz');
test.equal(esj.Selector.roundRobin(hosts),'bar','Should be bar');
test.equal(esj.Selector.roundRobin(hosts),'foo','Should be foo');
hosts = ['foo'];
test.equal(esj.Selector.roundRobin(hosts),'foo','Should be foo');
test.done();
},
'random' : function(test) {
test.expect(2);
var hosts = ['bar','baz','foo'];
test.ok(esj.Selector.roundRobin(hosts),'Should return something');
// This is how underscore.js tests its shuffle, will have to suffice
test.deepEqual(hosts.sort(),['bar','baz','foo'],'Should contain the same elements');
test.done();
}
};

39
test/test_serializer.js Normal file
View File

@ -0,0 +1,39 @@
/* Serializer tests */
'use strict';
var esj = require('../dist/elasticsearch-node.js');
/*
======== A Handy Little Nodeunit Reference ========
https://github.com/caolan/nodeunit
Test methods:
test.expect(numAssertions)
test.done()
Test assertions:
test.ok(value, [message])
test.equal(actual, expected, [message])
test.notEqual(actual, expected, [message])
test.deepEqual(actual, expected, [message])
test.notDeepEqual(actual, expected, [message])
test.strictEqual(actual, expected, [message])
test.notStrictEqual(actual, expected, [message])
test.throws(block, [error], [message])
test.doesNotThrow(block, [error], [message])
test.ifError(value)
*/
exports.serializer = {
setUp: function(done) {
// setup here
done();
},
'json': function(test) {
test.expect(2);
// Create serializer object
var _s = new esj.Serializer.json();
test.equal(_s.dump({foo:true}), '{"foo":true}', 'should be \'{"foo":true}\'');
test.deepEqual(_s.load('{"foo":true}'), {foo:true}, 'should be {foo:true}');
test.done();
},
};

68
test/test_shared.js Normal file
View File

@ -0,0 +1,68 @@
/* Shared tests */
/*
'use strict';
//var esj = require('../dist/elasticsearch-node.js');
/*
======== A Handy Little Nodeunit Reference ========
https://github.com/caolan/nodeunit
Test methods:
test.expect(numAssertions)
test.done()
Test assertions:
test.ok(value, [message])
test.equal(actual, expected, [message])
test.notEqual(actual, expected, [message])
test.deepEqual(actual, expected, [message])
test.notDeepEqual(actual, expected, [message])
test.strictEqual(actual, expected, [message])
test.notStrictEqual(actual, expected, [message])
test.throws(block, [error], [message])
test.doesNotThrow(block, [error], [message])
test.ifError(value)
exports.shared = {
setUp: function(done) {
// setup here
done();
},
'defaults': function(test) {
test.expect(1);
test.deepEqual(defaults({foo:1,bar:2},{bar:3,baz:4}),{foo:1,bar:2,baz:4},'should be foo:1,bar:2,baz:4')
test.done();
},
'extend': function(test) {
test.expect(1);
test.deepEqual(extend({foo:1},{bar:2}),{foo:1,bar:2},'should be foo:1,bar:2')
test.done();
},
'indexOf': function(test) {
test.expect(1);
test.equal(indexOf([1,2,3],3), 2, 'should be 2')
test.done();
},
'queryString': function(test) {
test.expect(1);
test.equal(queryString({foo:'bar'}),'foo=bar','should be foo=bar')
test.done();
},
'has': function(test) {
test.expect(1);
test.equal(has({foo:1},'foo'),true,'should be true')
test.done();
},
'each': function(test) {
test.expect(1);
var str = '';
each([1,2,3],function(v){str+=v;})
test.equal(str,'123','should be 123')
test.done();
},
'isUndefined': function (test) {
test.expect(1);
test.ok(isUndefined(undefined),'should be undefined')
test.done();
}
};
*/

107
test/transport/test_node.js Normal file
View File

@ -0,0 +1,107 @@
/* 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();
/*
======== A Handy Little Nodeunit Reference ========
https://github.com/caolan/nodeunit
Test methods:
test.expect(numAssertions)
test.done()
Test assertions:
test.ok(value, [message])
test.equal(actual, expected, [message])
test.notEqual(actual, expected, [message])
test.deepEqual(actual, expected, [message])
test.notDeepEqual(actual, expected, [message])
test.strictEqual(actual, expected, [message])
test.notStrictEqual(actual, expected, [message])
test.throws(block, [error], [message])
test.doesNotThrow(block, [error], [message])
test.ifError(value)
*/
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();
});
}
};