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. 39
      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
if( err ){
logger.error( 'backend error', err );
logger.error( `backend error ${err}`);
return cb( err );
}

2
service/search.js

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

16
test/unit/middleware/parseBBox.js

@ -1,5 +1,7 @@
var parseBBox = require('../../../middleware/parseBBox')();
const proxyquire = require('proxyquire').noCallThru();
module.exports.tests = {};
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 () {
t.deepEquals(res, expected, 'correct bounding_box');
t.end();

60
test/unit/run.js

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

39
test/unit/service/mget.js

@ -1,17 +1,28 @@
var setup = require('../../../service/mget'),
var service = require('../../../service/mget'),
mockBackend = require('../mock/backend');
const proxyquire = require('proxyquire').noCallThru();
module.exports.tests = {};
module.exports.tests.interface = function(test, common) {
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();
});
};
// functionally test service
// functionally test service
module.exports.tests.functional_success = function(test, common) {
var expected = [
@ -21,7 +32,7 @@ module.exports.tests.functional_success = function(test, common) {
center_point: { lat: 100.1, lon: -50.5 },
name: { default: 'test name1' },
parent: { country: ['country1'], region: ['state1'], county: ['city1'] }
},
},
{
_id: 'myid2', _type: 'mytype2',
value: 2,
@ -35,7 +46,7 @@ module.exports.tests.functional_success = function(test, common) {
var backend = mockBackend( 'client/mget/ok/1', function( cmd ){
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');
data.forEach(function(d) {
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');
});
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(data, undefined, 'data is undefined');
});
@ -81,4 +106,4 @@ module.exports.all = function (tape, common) {
for( var testCase in module.exports.tests ){
module.exports.tests[testCase](test, common);
}
};
};

33
test/unit/service/search.js

@ -1,14 +1,25 @@
var setup = require('../../../service/search'),
var service = require('../../../service/search'),
mockBackend = require('../mock/backend');
const proxyquire = require('proxyquire').noCallThru();
var example_valid_es_query = { body: { a: 'b' }, index: 'pelias' };
module.exports.tests = {};
module.exports.tests.interface = function(test, common) {
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();
});
};
@ -45,7 +56,7 @@ module.exports.tests.functional_success = function(test, common) {
var backend = mockBackend( 'client/search/ok/1', function( cmd ){
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');
data.forEach(function(d) {
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');
});
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(data, undefined, 'data is undefined');
});

Loading…
Cancel
Save