mirror of https://github.com/pelias/api.git
Stephen Hess
8 years ago
2 changed files with 390 additions and 107 deletions
@ -1,129 +1,372 @@
|
||||
var setup = require('../../../controller/place'), |
||||
mockBackend = require('../mock/backend'); |
||||
'use strict'; |
||||
|
||||
const setup = require('../../../controller/search'); |
||||
const proxyquire = require('proxyquire').noCallThru(); |
||||
|
||||
module.exports.tests = {}; |
||||
|
||||
module.exports.tests.interface = function(test, common) { |
||||
test('valid interface', function(t) { |
||||
module.exports.tests.interface = (test, common) => { |
||||
test('valid interface', (t) => { |
||||
t.equal(typeof setup, 'function', 'setup is a function'); |
||||
t.equal(typeof setup(), 'function', 'setup returns a controller'); |
||||
t.end(); |
||||
}); |
||||
}; |
||||
|
||||
// reminder: this is only the api subsection of the full config
|
||||
var fakeDefaultConfig = { |
||||
indexName: 'pelias' |
||||
module.exports.tests.success = (test, common) => { |
||||
test('successful request to search service should set data and meta', (t) => { |
||||
const config = { |
||||
indexName: 'indexName value' |
||||
}; |
||||
const esclient = 'this is the esclient'; |
||||
|
||||
// functionally test controller (backend success)
|
||||
module.exports.tests.functional_success = function(test, common) { |
||||
// request timeout messages willl be written here
|
||||
const infoMesssages = []; |
||||
|
||||
// expected geojson features for 'client/place/ok/1' fixture
|
||||
var expected = [{ |
||||
type: 'Feature', |
||||
geometry: { |
||||
type: 'Point', |
||||
coordinates: [ -50.5, 100.1 ] |
||||
}, |
||||
properties: { |
||||
id: 'myid1', |
||||
layer: 'mytype1', |
||||
text: 'test name1, city1, state1' |
||||
} |
||||
}, { |
||||
type: 'Feature', |
||||
geometry: { |
||||
type: 'Point', |
||||
coordinates: [ -51.5, 100.2 ] |
||||
// a controller that validates the esclient and cmd that was passed to the search service
|
||||
const controller = proxyquire('../../../controller/place', { |
||||
'../service/mget': (esclient, query, callback) => { |
||||
t.equal(esclient, 'this is the esclient'); |
||||
t.deepEqual(query, [ |
||||
{ |
||||
_index: 'indexName value', |
||||
_type: 'layer1', |
||||
_id: 'id1' |
||||
}, |
||||
properties: { |
||||
id: 'myid2', |
||||
layer: 'mytype2', |
||||
text: 'test name2, city2, state2' |
||||
{ |
||||
_index: 'indexName value', |
||||
_type: 'layer2', |
||||
_id: 'id2' |
||||
} |
||||
}]; |
||||
]); |
||||
|
||||
test('functional success', function(t) { |
||||
var backend = mockBackend( 'client/mget/ok/1', function( cmd ){ |
||||
t.deepEqual(cmd, { body: { docs: [ { _id: 123, _index: 'pelias', _type: [ 'a' ] } ] } }, 'correct backend command'); |
||||
}); |
||||
var controller = setup( fakeDefaultConfig, backend ); |
||||
var res = { |
||||
status: function( code ){ |
||||
t.equal(code, 200, 'status set'); |
||||
return res; |
||||
const docs = [{}, {}]; |
||||
|
||||
callback(undefined, docs); |
||||
} |
||||
})(config, esclient); |
||||
|
||||
const req = { |
||||
clean: { |
||||
ids: [ |
||||
{ |
||||
id: 'id1', |
||||
layers: 'layer1' |
||||
}, |
||||
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'); |
||||
{ |
||||
id: 'id2', |
||||
layers: 'layer2' |
||||
} |
||||
] |
||||
}, |
||||
errors: [], |
||||
warnings: [] |
||||
}; |
||||
var req = { clean: { ids: [ {'id' : 123, layers: [ 'a' ] } ] }, errors: [], warnings: [] }; |
||||
var next = function next() { |
||||
t.equal(req.errors.length, 0, 'next was called without error'); |
||||
const res = {}; |
||||
|
||||
const next = () => { |
||||
t.deepEqual(req.errors, []); |
||||
t.deepEqual(req.warnings, []); |
||||
t.deepEquals(res.data, [{}, {}]); |
||||
t.end(); |
||||
}; |
||||
|
||||
controller(req, res, next); |
||||
|
||||
}); |
||||
|
||||
test('functional success with custom index name', function(t) { |
||||
var fakeCustomizedConfig = { |
||||
indexName: 'alternateindexname' |
||||
}; |
||||
|
||||
var backend = mockBackend( 'client/mget/ok/1', function( cmd ){ |
||||
t.deepEqual(cmd, { body: { docs: [ { _id: 123, _index: 'alternateindexname', _type: [ 'a' ] } ] } }, 'correct backend command'); |
||||
module.exports.tests.error_conditions = (test, common) => { |
||||
test('non-empty req.errors should ', (t) => { |
||||
const esclient = () => { |
||||
throw new Error('esclient should not have been called'); |
||||
}; |
||||
const controller = setup( {}, esclient ); |
||||
|
||||
// the existence of `errors` means that a sanitizer detected an error,
|
||||
// so don't call the esclient
|
||||
const req = { |
||||
errors: ['error'] |
||||
}; |
||||
const res = { }; |
||||
|
||||
t.doesNotThrow(() => { |
||||
controller(req, res, () => {}); |
||||
}); |
||||
t.end(); |
||||
|
||||
}); |
||||
|
||||
test('mgetService returning error should add to req.errors and ignore docs', (t) => { |
||||
const config = { |
||||
indexName: 'indexName value' |
||||
}; |
||||
const esclient = 'this is the esclient'; |
||||
|
||||
// request timeout messages willl be written here
|
||||
const infoMesssages = []; |
||||
|
||||
const nonTimeoutError = { |
||||
status: 500, |
||||
displayName: 'InternalServerError', |
||||
message: 'an internal server error occurred' |
||||
}; |
||||
|
||||
// a controller that validates the esclient and cmd that was passed to the search service
|
||||
const controller = proxyquire('../../../controller/place', { |
||||
'../service/mget': (esclient, query, callback) => { |
||||
const docs = [{}, {}]; |
||||
|
||||
callback(nonTimeoutError, docs); |
||||
} |
||||
})(config, esclient); |
||||
|
||||
const req = { |
||||
clean: { |
||||
ids: [ |
||||
{ |
||||
id: 'id1', |
||||
layers: 'layer1' |
||||
} |
||||
] |
||||
}, |
||||
errors: [], |
||||
warnings: [] |
||||
}; |
||||
const res = {}; |
||||
|
||||
const next = () => { |
||||
t.deepEqual(req.errors, [nonTimeoutError]); |
||||
t.deepEqual(req.warnings, []); |
||||
t.deepEquals(res.data, undefined); |
||||
t.end(); |
||||
}; |
||||
|
||||
controller(req, res, next); |
||||
|
||||
}); |
||||
var controller = setup( fakeCustomizedConfig, backend ); |
||||
var res = { |
||||
status: function( code ){ |
||||
t.equal(code, 200, 'status set'); |
||||
return res; |
||||
|
||||
}; |
||||
|
||||
module.exports.tests.timeout = function(test, common) { |
||||
test('default # of request timeout retries should be 3', (t) => { |
||||
const config = { |
||||
indexName: 'indexName value' |
||||
}; |
||||
const esclient = 'this is the esclient'; |
||||
|
||||
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/place', { |
||||
'../service/mget': (esclient, cmd, callback) => { |
||||
// not that the searchService got called
|
||||
searchServiceCallCount++; |
||||
|
||||
callback(timeoutError); |
||||
}, |
||||
'pelias-logger': { |
||||
get: (service) => { |
||||
t.equal(service, 'api'); |
||||
return { |
||||
info: (msg) => { |
||||
infoMesssages.push(msg); |
||||
}, |
||||
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'); |
||||
debug: () => {} |
||||
}; |
||||
} |
||||
} |
||||
})(config, esclient); |
||||
|
||||
const req = { |
||||
clean: { |
||||
ids: [ |
||||
{ |
||||
id: 'id1', |
||||
layers: 'layer1' |
||||
} |
||||
] |
||||
}, |
||||
errors: [], |
||||
warnings: [] |
||||
}; |
||||
const res = {}; |
||||
|
||||
const next = () => { |
||||
t.equal(searchServiceCallCount, 3+1); |
||||
|
||||
t.ok(infoMesssages.indexOf('request timed out on attempt 1, retrying') !== -1); |
||||
t.ok(infoMesssages.indexOf('request timed out on attempt 2, retrying') !== -1); |
||||
t.ok(infoMesssages.indexOf('request timed out on attempt 3, retrying') !== -1); |
||||
|
||||
t.deepEqual(req.errors, [timeoutError]); |
||||
t.deepEqual(res, {}); |
||||
t.end(); |
||||
}; |
||||
|
||||
controller(req, res, next); |
||||
|
||||
}); |
||||
|
||||
test('explicit apiConfig.requestRetries should retry that many times', (t) => { |
||||
const config = { |
||||
indexName: 'indexName value', |
||||
requestRetries: 17 |
||||
}; |
||||
const esclient = 'this is the esclient'; |
||||
|
||||
let searchServiceCallCount = 0; |
||||
|
||||
const timeoutError = { |
||||
status: 408, |
||||
displayName: 'RequestTimeout', |
||||
message: 'Request Timeout after 17ms' |
||||
}; |
||||
|
||||
// a controller that validates the esclient and cmd that was passed to the search service
|
||||
const controller = proxyquire('../../../controller/place', { |
||||
'../service/mget': (esclient, cmd, callback) => { |
||||
// not that the searchService got called
|
||||
searchServiceCallCount++; |
||||
|
||||
callback(timeoutError); |
||||
} |
||||
})(config, esclient); |
||||
|
||||
const req = { |
||||
clean: { |
||||
ids: [ |
||||
{ |
||||
id: 'id1', |
||||
layers: 'layer1' |
||||
} |
||||
] |
||||
}, |
||||
errors: [], |
||||
warnings: [] |
||||
}; |
||||
var req = { clean: { ids: [ {'id' : 123, layers: [ 'a' ] } ] }, errors: [], warnings: [] }; |
||||
var next = function next() { |
||||
t.equal(req.errors.length, 0, 'next was called without error'); |
||||
const res = {}; |
||||
|
||||
const next = () => { |
||||
t.equal(searchServiceCallCount, 17+1); |
||||
t.end(); |
||||
}; |
||||
|
||||
controller(req, res, next); |
||||
|
||||
}); |
||||
|
||||
test('only status code 408 should be considered a retryable request', (t) => { |
||||
const config = { |
||||
indexName: 'indexName value' |
||||
}; |
||||
const esclient = 'this is the esclient'; |
||||
|
||||
let searchServiceCallCount = 0; |
||||
|
||||
const nonTimeoutError = { |
||||
status: 500, |
||||
displayName: 'InternalServerError', |
||||
message: 'an internal server error occurred' |
||||
}; |
||||
|
||||
// a controller that validates the esclient and cmd that was passed to the search service
|
||||
const controller = proxyquire('../../../controller/place', { |
||||
'../service/mget': (esclient, cmd, callback) => { |
||||
// not that the searchService got called
|
||||
searchServiceCallCount++; |
||||
|
||||
callback(nonTimeoutError); |
||||
} |
||||
})(config, esclient); |
||||
|
||||
const req = { |
||||
clean: { |
||||
ids: [ |
||||
{ |
||||
id: 'id1', |
||||
layers: 'layer1' |
||||
} |
||||
] |
||||
}, |
||||
errors: [], |
||||
warnings: [] |
||||
}; |
||||
const res = {}; |
||||
|
||||
const next = () => { |
||||
t.equal(searchServiceCallCount, 1); |
||||
t.deepEqual(req.errors, [nonTimeoutError]); |
||||
t.end(); |
||||
}; |
||||
|
||||
// functionally test controller (backend failure)
|
||||
module.exports.tests.functional_failure = function(test, common) { |
||||
test('functional failure', function(t) { |
||||
var backend = mockBackend( 'client/mget/fail/1', function( cmd ){ |
||||
t.deepEqual(cmd, { body: { docs: [ { _id: 123, _index: 'pelias', _type: ['b'] } ] } }, 'correct backend command'); |
||||
controller(req, res, next); |
||||
|
||||
}); |
||||
var controller = setup( fakeDefaultConfig, backend ); |
||||
var req = { clean: { ids: [ {'id' : 123, layers: [ 'b' ] } ] }, errors: [], warnings: [] }; |
||||
var next = function( message ){ |
||||
t.equal(req.errors[0],'an elasticsearch error occurred','error passed to errorHandler'); |
||||
|
||||
test('string error should not retry and be logged as-is', (t) => { |
||||
const config = { |
||||
indexName: 'indexName value' |
||||
}; |
||||
const esclient = 'this is the esclient'; |
||||
|
||||
let searchServiceCallCount = 0; |
||||
|
||||
const stringTypeError = 'this is an error string'; |
||||
|
||||
// a controller that validates the esclient and cmd that was passed to the search service
|
||||
const controller = proxyquire('../../../controller/place', { |
||||
'../service/mget': (esclient, cmd, callback) => { |
||||
// not that the searchService got called
|
||||
searchServiceCallCount++; |
||||
|
||||
callback(stringTypeError); |
||||
} |
||||
})(config, esclient); |
||||
|
||||
const req = { |
||||
clean: { |
||||
ids: [ |
||||
{ |
||||
id: 'id1', |
||||
layers: 'layer1' |
||||
} |
||||
] |
||||
}, |
||||
errors: [], |
||||
warnings: [] |
||||
}; |
||||
const res = {}; |
||||
|
||||
const next = () => { |
||||
t.equal(searchServiceCallCount, 1); |
||||
t.deepEqual(req.errors, [stringTypeError]); |
||||
t.end(); |
||||
}; |
||||
controller(req, undefined, next ); |
||||
|
||||
controller(req, res, next); |
||||
|
||||
}); |
||||
|
||||
}; |
||||
|
||||
module.exports.all = function (tape, common) { |
||||
module.exports.all = (tape, common) => { |
||||
|
||||
function test(name, testFunction) { |
||||
return tape('GET /place ' + name, testFunction); |
||||
} |
||||
|
||||
for( var testCase in module.exports.tests ){ |
||||
for( const testCase in module.exports.tests ){ |
||||
module.exports.tests[testCase](test, common); |
||||
} |
||||
}; |
||||
|
Loading…
Reference in new issue