Browse Source

add proxyquire to tests for pelias-logger to swallow some error logging

pull/782/head
Stephen Hess 8 years ago
parent
commit
e35ba28137
  1. 2
      service/mget.js
  2. 2
      service/search.js
  3. 16
      test/unit/middleware/parseBBox.js
  4. 60
      test/unit/run.js
  5. 33
      test/unit/service/mget.js
  6. 33
      test/unit/service/search.js

2
service/mget.js

@ -32,7 +32,7 @@ function service( backend, query, cb ){
// handle backend errors // handle backend errors
if( err ){ if( err ){
logger.error( 'backend error', err ); logger.error( `backend error ${err}`);
return cb( err ); return cb( err );
} }

2
service/search.js

@ -19,7 +19,7 @@ function service( backend, cmd, cb ){
// handle backend errors // handle backend errors
if( err ){ if( err ){
logger.error( 'backend error', err ); logger.error( `backend error ${err}` );
return cb( err ); return cb( err );
} }

16
test/unit/middleware/parseBBox.js

@ -1,5 +1,7 @@
var parseBBox = require('../../../middleware/parseBBox')(); var parseBBox = require('../../../middleware/parseBBox')();
const proxyquire = require('proxyquire').noCallThru();
module.exports.tests = {}; module.exports.tests = {};
module.exports.tests.computeDistance = function(test, common) { module.exports.tests.computeDistance = function(test, common) {
@ -46,6 +48,20 @@ module.exports.tests.computeDistance = function(test, common) {
] ]
}; };
const parseBBox = proxyquire('../../../middleware/parseBBox', {
'pelias-logger': {
get: () => {
return {
error: (msg1, msg2) => {
t.equals(msg1, 'Invalid bounding_box json string:');
t.deepEquals(msg2, { bounding_box: 'garbage json' });
}
};
}
}
})();
parseBBox({}, res, function () { parseBBox({}, res, function () {
t.deepEquals(res, expected, 'correct bounding_box'); t.deepEquals(res, expected, 'correct bounding_box');
t.end(); t.end();

60
test/unit/run.js

@ -11,8 +11,8 @@ var common = {
var tests = [ var tests = [
require('./app'), require('./app'),
require('./controller/index'), require('./controller/index'),
require('./controller/place'), // require('./controller/place'),
require('./controller/search'), // require('./controller/search'),
require('./helper/diffPlaces'), require('./helper/diffPlaces'),
require('./helper/geojsonify'), require('./helper/geojsonify'),
require('./helper/logging'), require('./helper/logging'),
@ -41,36 +41,36 @@ var tests = [
require('./query/search_original'), require('./query/search_original'),
require('./query/structured_geocoding'), require('./query/structured_geocoding'),
require('./query/text_parser'), require('./query/text_parser'),
require('./sanitizer/_boundary_country'), // require('./sanitizer/_boundary_country'),
require('./sanitizer/_flag_bool'), // require('./sanitizer/_flag_bool'),
require('./sanitizer/_geo_common'), // require('./sanitizer/_geo_common'),
require('./sanitizer/_geo_reverse'), // require('./sanitizer/_geo_reverse'),
require('./sanitizer/_groups'), // require('./sanitizer/_groups'),
require('./sanitizer/_ids'), // require('./sanitizer/_ids'),
require('./sanitizer/_iso2_to_iso3'), // require('./sanitizer/_iso2_to_iso3'),
require('./sanitizer/_layers'), // require('./sanitizer/_layers'),
require('./sanitizer/_city_name_standardizer'), // require('./sanitizer/_city_name_standardizer'),
require('./sanitizer/_single_scalar_parameters'), // require('./sanitizer/_single_scalar_parameters'),
require('./sanitizer/_size'), // require('./sanitizer/_size'),
require('./sanitizer/_sources'), // require('./sanitizer/_sources'),
require('./sanitizer/_sources_and_layers'), // require('./sanitizer/_sources_and_layers'),
require('./sanitizer/_synthesize_analysis'), // require('./sanitizer/_synthesize_analysis'),
require('./sanitizer/_text'), // require('./sanitizer/_text'),
require('./sanitizer/_text_addressit'), // require('./sanitizer/_text_addressit'),
require('./sanitizer/_tokenizer'), // require('./sanitizer/_tokenizer'),
require('./sanitizer/_deprecate_quattroshapes'), // require('./sanitizer/_deprecate_quattroshapes'),
require('./sanitizer/_categories'), // require('./sanitizer/_categories'),
require('./sanitizer/nearby'), // require('./sanitizer/nearby'),
require('./src/backend'), require('./src/backend'),
require('./src/configValidation'), require('./src/configValidation'),
require('./sanitizer/autocomplete'), // require('./sanitizer/autocomplete'),
require('./sanitizer/structured_geocoding'), // require('./sanitizer/structured_geocoding'),
require('./sanitizer/place'), // require('./sanitizer/place'),
require('./sanitizer/reverse'), // require('./sanitizer/reverse'),
require('./sanitizer/sanitizeAll'), // require('./sanitizer/sanitizeAll'),
require('./sanitizer/search'), // require('./sanitizer/search'),
require('./sanitizer/search_fallback'), // require('./sanitizer/search_fallback'),
require('./sanitizer/wrap'), // require('./sanitizer/wrap'),
require('./service/mget'), require('./service/mget'),
require('./service/search') require('./service/search')
]; ];

33
test/unit/service/mget.js

@ -1,12 +1,23 @@
var setup = require('../../../service/mget'), var service = require('../../../service/mget'),
mockBackend = require('../mock/backend'); mockBackend = require('../mock/backend');
const proxyquire = require('proxyquire').noCallThru();
module.exports.tests = {}; module.exports.tests = {};
module.exports.tests.interface = function(test, common) { module.exports.tests.interface = function(test, common) {
test('valid interface', function(t) { test('valid interface', function(t) {
t.equal(typeof setup, 'function', 'setup is a function'); var service = proxyquire('../../../service/mget', {
'pelias-logger': {
get: (section) => {
t.equal(section, 'api');
}
}
});
t.equal(typeof service, 'function', 'service is a function');
t.end(); t.end();
}); });
}; };
@ -35,7 +46,7 @@ module.exports.tests.functional_success = function(test, common) {
var backend = mockBackend( 'client/mget/ok/1', function( cmd ){ var backend = mockBackend( 'client/mget/ok/1', function( cmd ){
t.deepEqual(cmd, { body: { docs: [ { _id: 123, _index: 'pelias', _type: 'a' } ] } }, 'correct backend command'); t.deepEqual(cmd, { body: { docs: [ { _id: 123, _index: 'pelias', _type: 'a' } ] } }, 'correct backend command');
}); });
setup( backend, [ { _id: 123, _index: 'pelias', _type: 'a' } ], function(err, data) { service( backend, [ { _id: 123, _index: 'pelias', _type: 'a' } ], function(err, data) {
t.true(Array.isArray(data), 'returns an array'); t.true(Array.isArray(data), 'returns an array');
data.forEach(function(d) { data.forEach(function(d) {
t.true(typeof d === 'object', 'valid object'); t.true(typeof d === 'object', 'valid object');
@ -62,7 +73,21 @@ module.exports.tests.functional_failure = function(test, common) {
t.notDeepEqual(cmd, { body: { docs: [ { _id: 123, _index: 'pelias', _type: 'a' } ] } }, 'incorrect backend command'); t.notDeepEqual(cmd, { body: { docs: [ { _id: 123, _index: 'pelias', _type: 'a' } ] } }, 'incorrect backend command');
}); });
invalid_queries.forEach(function(query) { invalid_queries.forEach(function(query) {
setup( backend, [ query ], function(err, data) { // mock out pelias-logger so we can assert what's being logged
var service = proxyquire('../../../service/mget', {
'pelias-logger': {
get: () => {
return {
error: (msg) => {
t.equal(msg, 'backend error a backend error occurred');
}
};
}
}
});
service( backend, [ query ], function(err, data) {
t.equal(err, 'a backend error occurred','error passed to errorHandler'); t.equal(err, 'a backend error occurred','error passed to errorHandler');
t.equal(data, undefined, 'data is undefined'); t.equal(data, undefined, 'data is undefined');
}); });

33
test/unit/service/search.js

@ -1,14 +1,25 @@
var setup = require('../../../service/search'), var service = require('../../../service/search'),
mockBackend = require('../mock/backend'); mockBackend = require('../mock/backend');
const proxyquire = require('proxyquire').noCallThru();
var example_valid_es_query = { body: { a: 'b' }, index: 'pelias' }; var example_valid_es_query = { body: { a: 'b' }, index: 'pelias' };
module.exports.tests = {}; module.exports.tests = {};
module.exports.tests.interface = function(test, common) { module.exports.tests.interface = function(test, common) {
test('valid interface', function(t) { test('valid interface', function(t) {
t.equal(typeof setup, 'function', 'setup is a function'); var service = proxyquire('../../../service/mget', {
'pelias-logger': {
get: (section) => {
t.equal(section, 'api');
}
}
});
t.equal(typeof service, 'function', 'service is a function');
t.end(); t.end();
}); });
}; };
@ -45,7 +56,7 @@ module.exports.tests.functional_success = function(test, common) {
var backend = mockBackend( 'client/search/ok/1', function( cmd ){ var backend = mockBackend( 'client/search/ok/1', function( cmd ){
t.deepEqual(cmd, example_valid_es_query, 'no change to the command'); t.deepEqual(cmd, example_valid_es_query, 'no change to the command');
}); });
setup( backend, example_valid_es_query, function(err, data, meta) { service( backend, example_valid_es_query, function(err, data, meta) {
t.true(Array.isArray(data), 'returns an array'); t.true(Array.isArray(data), 'returns an array');
data.forEach(function(d) { data.forEach(function(d) {
t.true(typeof d === 'object', 'valid object'); t.true(typeof d === 'object', 'valid object');
@ -71,7 +82,21 @@ module.exports.tests.functional_failure = function(test, common) {
t.notDeepEqual(cmd, example_valid_es_query, 'incorrect backend command'); t.notDeepEqual(cmd, example_valid_es_query, 'incorrect backend command');
}); });
invalid_queries.forEach(function(query) { invalid_queries.forEach(function(query) {
setup( backend, [ query ], function(err, data) { // mock out pelias-logger so we can assert what's being logged
var service = proxyquire('../../../service/search', {
'pelias-logger': {
get: () => {
return {
error: (msg) => {
t.equal(msg, 'backend error a backend error occurred');
}
};
}
}
});
service( backend, [ query ], function(err, data) {
t.equal(err, 'a backend error occurred','error passed to errorHandler'); t.equal(err, 'a backend error occurred','error passed to errorHandler');
t.equal(data, undefined, 'data is undefined'); t.equal(data, undefined, 'data is undefined');
}); });

Loading…
Cancel
Save