Browse Source

rewrote tests to proxyquire service/search to reduce complexity

added a few more tests for coverage, removed unused things from unit/mock/backend
pull/782/head
Stephen Hess 8 years ago
parent
commit
96f9d12ff5
  1. 12
      controller/search.js
  2. 438
      test/unit/controller/search.js
  3. 3
      test/unit/mock/backend.js

12
controller/search.js

@ -1,10 +1,10 @@
'use strict'; 'use strict';
var _ = require('lodash'); const _ = require('lodash');
var searchService = require('../service/search'); const searchService = require('../service/search');
var logger = require('pelias-logger').get('api'); const logger = require('pelias-logger').get('api');
var logging = require( '../helper/logging' ); const logging = require( '../helper/logging' );
const retry = require('retry'); const retry = require('retry');
function setup( apiConfig, esclient, query ){ function setup( apiConfig, esclient, query ){
@ -68,7 +68,7 @@ function setup( apiConfig, esclient, query ){
// (handles bookkeeping of maxRetries) // (handles bookkeeping of maxRetries)
// only consider for status 408 (request timeout) // only consider for status 408 (request timeout)
if (isRequestTimeout(err) && operation.retry(err)) { if (isRequestTimeout(err) && operation.retry(err)) {
logger.info('request timed out, retrying'); logger.info(`request timed out on attempt ${currentAttempt}, retrying`);
return; return;
} }
@ -94,7 +94,7 @@ function setup( apiConfig, esclient, query ){
res.meta.query_type = renderedQuery.type; res.meta.query_type = renderedQuery.type;
logger.info(`[controller:search] [queryType:${renderedQuery.type}] [es_result_count:` + logger.info(`[controller:search] [queryType:${renderedQuery.type}] [es_result_count:` +
(res.data && res.data.length ? res.data.length : 0) + ']'); _.get(res, 'data', []).length + ']');
} }
logger.debug('[ES response]', docs); logger.debug('[ES response]', docs);
next(); next();

438
test/unit/controller/search.js

@ -15,150 +15,286 @@ module.exports.tests.interface = function(test, common) {
}); });
}; };
// reminder: this is only the api subsection of the full config module.exports.tests.success = function(test, common) {
var fakeDefaultConfig = { test('successful request to search service should set data and meta', (t) => {
indexName: 'pelias' const config = {
}; indexName: 'indexName value'
};
const esclient = 'this is the esclient';
const query = () => {
return {
body: 'this is the query body',
type: 'this is the query type'
};
};
// request timeout messages willl be written here
const infoMesssages = [];
// a controller that validates the esclient and cmd that was passed to the search service
const controller = proxyquire('../../../controller/search', {
'../service/search': (esclient, cmd, callback) => {
t.equal(esclient, 'this is the esclient');
t.deepEqual(cmd, {
index: 'indexName value',
searchType: 'dfs_query_then_fetch',
body: 'this is the query body'
});
const docs = [{}, {}];
const meta = { key: 'value' };
callback(undefined, docs, meta);
},
'pelias-logger': {
get: (service) => {
t.equal(service, 'api');
return {
info: (msg) => {
infoMesssages.push(msg);
},
debug: () => {}
};
}
}
})(config, esclient, query);
const req = { clean: { }, errors: [], warnings: [] };
const res = {};
var next = function() {
t.deepEqual(req, {
clean: {},
errors: [],
warnings: []
});
t.deepEquals(res.data, [{}, {}]);
t.deepEquals(res.meta, { key: 'value', query_type: 'this is the query type' });
t.ok(infoMesssages.find((msg) => {
return msg === '[controller:search] [queryType:this is the query type] [es_result_count:2]';
}));
t.end();
};
controller(req, res, next);
});
test('undefined meta should set empty object into res', (t) => {
const config = {
indexName: 'indexName value'
};
const esclient = 'this is the esclient';
const query = () => {
return {
body: 'this is the query body',
type: 'this is the query type'
};
};
// request timeout messages willl be written here
const infoMesssages = [];
// a controller that validates the esclient and cmd that was passed to the search service
const controller = proxyquire('../../../controller/search', {
'../service/search': (esclient, cmd, callback) => {
t.equal(esclient, 'this is the esclient');
t.deepEqual(cmd, {
index: 'indexName value',
searchType: 'dfs_query_then_fetch',
body: 'this is the query body'
});
const docs = [{}, {}];
callback(undefined, docs, undefined);
},
'pelias-logger': {
get: (service) => {
t.equal(service, 'api');
return {
info: (msg) => {
infoMesssages.push(msg);
},
debug: () => {}
};
}
}
})(config, esclient, query);
const req = { clean: { }, errors: [], warnings: [] };
const res = {};
var next = function() {
t.deepEqual(req, {
clean: {},
errors: [],
warnings: []
});
t.deepEquals(res.data, [{}, {}]);
t.deepEquals(res.meta, { query_type: 'this is the query type' });
t.ok(infoMesssages.find((msg) => {
return msg === '[controller:search] [queryType:this is the query type] [es_result_count:2]';
}));
t.end();
};
controller(req, res, next);
});
test('undefined docs should log 0 results', (t) => {
const config = {
indexName: 'indexName value'
};
const esclient = 'this is the esclient';
const query = () => {
return {
body: 'this is the query body',
type: 'this is the query type'
};
};
// request timeout messages willl be written here
const infoMesssages = [];
// a controller that validates the esclient and cmd that was passed to the search service
const controller = proxyquire('../../../controller/search', {
'../service/search': (esclient, cmd, callback) => {
t.equal(esclient, 'this is the esclient');
t.deepEqual(cmd, {
index: 'indexName value',
searchType: 'dfs_query_then_fetch',
body: 'this is the query body'
});
const meta = { key: 'value' };
callback(undefined, undefined, meta);
},
'pelias-logger': {
get: (service) => {
t.equal(service, 'api');
return {
info: (msg) => {
infoMesssages.push(msg);
},
debug: () => {}
};
}
}
})(config, esclient, query);
const req = { clean: { }, errors: [], warnings: [] };
const res = {};
var next = function() {
t.deepEqual(req, {
clean: {},
errors: [],
warnings: []
});
t.equals(res.data, undefined);
t.deepEquals(res.meta, { key: 'value', query_type: 'this is the query type' });
t.ok(infoMesssages.find((msg) => {
return msg === '[controller:search] [queryType:this is the query type] [es_result_count:0]';
}));
t.end();
};
controller(req, res, next);
});
test('successful request on retry to search service should log info message', (t) => {
const config = {
indexName: 'indexName value'
};
const esclient = 'this is the esclient';
const query = () => {
return {
body: 'this is the query body',
type: 'this is the query type'
};
};
let searchServiceCallCount = 0;
const timeoutError = {
status: 408,
displayName: 'RequestTimeout',
message: 'Request Timeout after 17ms'
};
// request timeout messages willl be written here
const infoMesssages = [];
// a controller that validates the esclient and cmd that was passed to the search service
const controller = proxyquire('../../../controller/search', {
'../service/search': (esclient, cmd, callback) => {
t.equal(esclient, 'this is the esclient');
t.deepEqual(cmd, {
index: 'indexName value',
searchType: 'dfs_query_then_fetch',
body: 'this is the query body'
});
// functionally test controller (backend success) if (searchServiceCallCount < 2) {
// module.exports.tests.functional_success = function(test, common) { // note that the searchService got called
// searchServiceCallCount++;
// // expected geojson features for 'client/suggest/ok/1' fixture callback(timeoutError);
// var expected = [{ } else {
// type: 'Feature', const docs = [{}, {}];
// geometry: { const meta = { key: 'value' };
// type: 'Point',
// coordinates: [-50.5, 100.1] callback(undefined, docs, meta);
// }, }
// properties: {
// id: 'myid1', },
// layer: 'mytype1', 'pelias-logger': {
// text: 'test name1, city1, state1' get: (service) => {
// } t.equal(service, 'api');
// }, { return {
// type: 'Feature', info: (msg) => {
// geometry: { infoMesssages.push(msg);
// type: 'Point', },
// coordinates: [-51.5, 100.2] debug: () => {}
// }, };
// properties: { }
// id: 'myid2', }
// layer: 'mytype2', })(config, esclient, query);
// text: 'test name2, city2, state2'
// } const req = { clean: { }, errors: [], warnings: [] };
// }]; const res = {};
//
// var expectedMeta = { var next = function() {
// scores: [10, 20], t.deepEqual(req, {
// query_type: 'mock' clean: {},
// }; errors: [],
// warnings: []
// var expectedData = [ });
// { t.deepEquals(res.data, [{}, {}]);
// _id: 'myid1', t.deepEquals(res.meta, { key: 'value', query_type: 'this is the query type' });
// _score: 10,
// _type: 'mytype1', t.ok(infoMesssages.find((msg) => {
// _matched_queries: ['query 1', 'query 2'], return msg === '[controller:search] [queryType:this is the query type] [es_result_count:2]';
// parent: { }));
// country: ['country1'],
// region: ['state1'], t.ok(infoMesssages.find((msg) => {
// county: ['city1'] return msg === 'succeeded on retry 2';
// }, }));
// center_point: { lat: 100.1, lon: -50.5 },
// name: { default: 'test name1' }, t.end();
// value: 1 };
// },
// { controller(req, res, next);
// _id: 'myid2',
// _score: 20, });
// _type: 'mytype2',
// _matched_queries: ['query 3'], };
// parent: {
// country: ['country2'],
// region: ['state2'],
// county: ['city2']
// },
// center_point: { lat: 100.2, lon: -51.5 },
// name: { default: 'test name2' },
// value: 2
// }
// ];
//
// test('functional success', function (t) {
// var backend = mockBackend('client/search/ok/1', function (cmd) {
// t.deepEqual(cmd, {
// body: {a: 'b'},
// index: 'pelias',
// searchType: 'dfs_query_then_fetch'
// }, 'correct backend command');
// });
// var controller = setup(fakeDefaultConfig, backend, mockQuery());
// var res = {
// status: function (code) {
// t.equal(code, 200, 'status set');
// return res;
// },
// json: function (json) {
// t.equal(typeof json, 'object', 'returns json');
// t.equal(typeof json.date, 'number', 'date set');
// t.equal(json.type, 'FeatureCollection', 'valid geojson');
// t.true(Array.isArray(json.features), 'features is array');
// t.deepEqual(json.features, expected, 'values correctly mapped');
// }
// };
// var req = { clean: { a: 'b' }, errors: [], warnings: [] };
// var next = function next() {
// t.equal(req.errors.length, 0, 'next was called without error');
// t.deepEqual(res.meta, expectedMeta, 'meta data was set');
// t.deepEqual(res.data, expectedData, 'data was set');
// t.end();
// };
// controller(req, res, next);
// });
//
// test('functional success with alternate index name', function(t) {
// var fakeCustomizedConfig = {
// indexName: 'alternateindexname'
// };
//
// var backend = mockBackend('client/search/ok/1', function (cmd) {
// t.deepEqual(cmd, {
// body: {a: 'b'},
// index: 'alternateindexname',
// searchType: 'dfs_query_then_fetch'
// }, 'correct backend command');
// });
// var controller = setup(fakeCustomizedConfig, backend, mockQuery());
// var res = {
// status: function (code) {
// t.equal(code, 200, 'status set');
// return res;
// }
// };
// var req = { clean: { a: 'b' }, errors: [], warnings: [] };
// var next = function next() {
// t.equal(req.errors.length, 0, 'next was called without error');
// t.end();
// };
// controller(req, res, next);
// });
// };
//
// // functionally test controller (backend failure)
// module.exports.tests.functional_failure = function(test, common) {
// test('functional failure', function(t) {
// var backend = mockBackend( 'client/search/fail/1', function( cmd ){
// t.deepEqual(cmd, { body: { a: 'b' }, index: 'pelias', searchType: 'dfs_query_then_fetch' }, 'correct backend command');
// });
// var controller = setup( fakeDefaultConfig, backend, mockQuery() );
// var req = { clean: { a: 'b' }, errors: [], warnings: [] };
// var next = function(){
// t.equal(req.errors[0],'an elasticsearch error occurred');
// t.end();
// };
// controller(req, undefined, next );
// });
// };
module.exports.tests.timeout = function(test, common) { module.exports.tests.timeout = function(test, common) {
test('default # of request timeout retries should be 3', (t) => { test('default # of request timeout retries should be 3', (t) => {
@ -214,11 +350,11 @@ module.exports.tests.timeout = function(test, common) {
var next = function() { var next = function() {
t.equal(searchServiceCallCount, 3+1); t.equal(searchServiceCallCount, 3+1);
t.deepEqual(
infoMesssages.filter((msg)=> { return msg === 'request timed out, retrying'; } ).length, t.ok(infoMesssages.indexOf('request timed out on attempt 1, retrying') !== -1);
3, t.ok(infoMesssages.indexOf('request timed out on attempt 2, retrying') !== -1);
'there should be 3 request timed out info messages' t.ok(infoMesssages.indexOf('request timed out on attempt 3, retrying') !== -1);
);
t.deepEqual(req, { t.deepEqual(req, {
clean: {}, clean: {},
errors: [timeoutError.message], errors: [timeoutError.message],
@ -365,7 +501,7 @@ module.exports.tests.existing_errors = function(test, common) {
var esclient = function() { var esclient = function() {
throw new Error('esclient should not have been called'); throw new Error('esclient should not have been called');
}; };
var controller = setup( fakeDefaultConfig, esclient, mockQuery() ); var controller = setup( {}, esclient, mockQuery() );
// the existence of `errors` means that a sanitizer detected an error, // the existence of `errors` means that a sanitizer detected an error,
// so don't call the esclient // so don't call the esclient
@ -385,10 +521,10 @@ module.exports.tests.existing_errors = function(test, common) {
module.exports.tests.existing_results = function(test, common) { module.exports.tests.existing_results = function(test, common) {
test('res with existing data should not call backend', function(t) { test('res with existing data should not call backend', function(t) {
var backend = function() { var esclient = function() {
throw new Error('backend should not have been called'); throw new Error('backend should not have been called');
}; };
var controller = setup( fakeDefaultConfig, backend, mockQuery() ); var controller = setup( {}, esclient, mockQuery() );
var req = { }; var req = { };
// the existence of `data` means that there are already results so // the existence of `data` means that there are already results so

3
test/unit/mock/backend.js

@ -1,8 +1,5 @@
var responses = {}; var responses = {};
responses['client/suggest/ok/1'] = function( cmd, cb ){
return cb( undefined, suggestEnvelope([ { score: 1, text: 'mocktype:mockid1' } ], [ { score: 2, text: 'mocktype:mockid2' } ]) );
};
responses['client/suggest/fail/1'] = function( cmd, cb ){ responses['client/suggest/fail/1'] = function( cmd, cb ){
return cb( 'an elasticsearch error occurred' ); return cb( 'an elasticsearch error occurred' );
}; };

Loading…
Cancel
Save