[scan util] Add tests

This commit is contained in:
Jonathan Budzenski 2015-12-09 12:57:26 -06:00
parent 43f883979b
commit f5d9127d4a
2 changed files with 64 additions and 1 deletions

View file

@ -0,0 +1,63 @@
var Scanner = require('ui/utils/scanner');
var expect = require('expect.js');
var elasticsearch = require('elasticsearch-browser');
var sinon = require('sinon');
var es = new elasticsearch.Client({
host: 'http://localhost:9210',
});
describe('Scanner', function () {
describe('initialization', function () {
it('should throw errors if missing arguments on initialization', function () {
expect(() => new Scanner()).to.throwError();
expect(() => new Scanner(es)).to.throwError();
expect(() => new Scanner(es, {
index: 'foo',
type: 'bar'
})).not.to.throwError();
});
});
describe('scan', function () {
let search;
let scroll;
let scanner;
let mockSearch = {'_scroll_id':'abc','took':1,'timed_out':false,'_shards':{'total':1,'successful':1,'failed':0},'hits':{'total':2,'max_score':0.0,'hits':[]}}; // eslint-disable-line max-len
let mockScroll = {'took':1,'timed_out':false,'_shards':{'total':1,'successful':1,'failed':0},'hits':{'total':2,'max_score':0.0,'hits':['one', 'two']}}; // eslint-disable-line max-len
beforeEach(function () {
scanner = new Scanner(es, {
index: 'foo',
type: 'bar'
});
search = sinon.stub(scanner.client, 'search', (req, cb) => cb(null, mockSearch));
scroll = sinon.stub(scanner.client, 'scroll', (req, cb) => cb(null, mockScroll));
});
it('should search and then scroll for results', function () {
return scanner.scanAndMap('')
.then(function (error, response) {
expect(search.called).to.be(true);
expect(scroll.called).to.be(true);
});
});
it('should map results if a function is provided', function () {
return scanner.scanAndMap(null, null, function (hit) {
return hit.toUpperCase();
})
.then(function (response) {
expect(response.hits[0]).to.be('ONE');
expect(response.hits[1]).to.be('TWO');
});
});
afterEach(function () {
search.restore();
scroll.restore();
});
});
});

View file

@ -1,6 +1,6 @@
const _ = require('lodash');
let Scanner = function (client, {index, type}) {
let Scanner = function (client, {index, type} = {}) {
if (!index) throw new Error('Expected index');
if (!type) throw new Error('Expected type');
if (!client) throw new Error('Expected client');