diff --git a/controller/place.js b/controller/place.js index a4996a11..924292b6 100644 --- a/controller/place.js +++ b/controller/place.js @@ -1,7 +1,7 @@ var service = { mget: require('../service/mget') }; var logger = require('pelias-logger').get('api:controller:place'); -function setup( backend ){ +function setup( config, backend ){ // allow overriding of dependencies backend = backend || require('../src/backend'); @@ -16,7 +16,7 @@ function setup( backend ){ var query = req.clean.ids.map( function(id) { return { - _index: 'pelias', + _index: config.indexName, _type: id.layers, _id: id.id }; diff --git a/controller/search.js b/controller/search.js index 35a5f0b5..271d2899 100644 --- a/controller/search.js +++ b/controller/search.js @@ -4,7 +4,7 @@ var service = { search: require('../service/search') }; var logger = require('pelias-logger').get('api:controller:search'); var logging = require( '../helper/logging' ); -function setup( backend, query ){ +function setup( config, backend, query ){ // allow overriding of dependencies backend = backend || require('../src/backend'); @@ -26,7 +26,7 @@ function setup( backend, query ){ // backend command var cmd = { - index: 'pelias', + index: config.indexName, searchType: 'dfs_query_then_fetch', body: query( req.clean ) }; diff --git a/routes/v1.js b/routes/v1.js index c8c88de1..c15f28d7 100644 --- a/routes/v1.js +++ b/routes/v1.js @@ -61,7 +61,7 @@ function addRoutes(app, peliasConfig) { search: createRouter([ sanitisers.search.middleware, middleware.calcSize(), - controllers.search(), + controllers.search(peliasConfig), postProc.distances('focus.point.'), postProc.confidenceScores(peliasConfig), postProc.dedupe(), @@ -74,7 +74,7 @@ function addRoutes(app, peliasConfig) { ]), autocomplete: createRouter([ sanitisers.autocomplete.middleware, - controllers.search(null, require('../query/autocomplete')), + controllers.search(peliasConfig, null, require('../query/autocomplete')), postProc.distances('focus.point.'), postProc.confidenceScores(peliasConfig), postProc.dedupe(), @@ -88,7 +88,7 @@ function addRoutes(app, peliasConfig) { reverse: createRouter([ sanitisers.reverse.middleware, middleware.calcSize(), - controllers.search(undefined, reverseQuery), + controllers.search(peliasConfig, undefined, reverseQuery), postProc.distances('point.'), // reverse confidence scoring depends on distance from origin // so it must be calculated first diff --git a/test/unit/controller/place.js b/test/unit/controller/place.js index 977be59e..0376eec6 100644 --- a/test/unit/controller/place.js +++ b/test/unit/controller/place.js @@ -11,6 +11,11 @@ module.exports.tests.interface = function(test, common) { }); }; +// reminder: this is only the api subsection of the full config +var fakeDefaultConfig = { + indexName: 'pelias' +}; + // functionally test controller (backend success) module.exports.tests.functional_success = function(test, common) { @@ -43,7 +48,37 @@ 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'); }); - var controller = setup( backend ); + var controller = setup( fakeDefaultConfig, backend ); + 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: { ids: [ {'id' : 123, layers: [ 'a' ] } ] }, errors: [], warnings: [] }; + var next = function next() { + t.equal(req.errors.length, 0, 'next was called without error'); + 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'); + }); + var controller = setup( fakeCustomizedConfig, backend ); var res = { status: function( code ){ t.equal(code, 200, 'status set'); @@ -72,7 +107,7 @@ module.exports.tests.functional_failure = function(test, common) { var backend = mockBackend( 'client/mget/fail/1', function( cmd ){ t.deepEqual(cmd, { body: { docs: [ { _id: 123, _index: 'pelias', _type: ['b'] } ] } }, 'correct backend command'); }); - var controller = setup( backend ); + var controller = setup( fakeDefaultConfig, backend ); var req = { clean: { ids: [ {'id' : 123, layers: [ 'b' ] } ] }, errors: [], warnings: [] }; var next = function( message ){ t.equal(req.errors[0],'a backend error occurred','error passed to errorHandler'); diff --git a/test/unit/controller/search.js b/test/unit/controller/search.js index 967ba1b9..3e7494ac 100644 --- a/test/unit/controller/search.js +++ b/test/unit/controller/search.js @@ -12,6 +12,11 @@ module.exports.tests.interface = function(test, common) { }); }; +// reminder: this is only the api subsection of the full config +var fakeDefaultConfig = { + indexName: 'pelias' +}; + // functionally test controller (backend success) module.exports.tests.functional_success = function(test, common) { @@ -81,7 +86,7 @@ module.exports.tests.functional_success = function(test, common) { searchType: 'dfs_query_then_fetch' }, 'correct backend command'); }); - var controller = setup(backend, mockQuery()); + var controller = setup(fakeDefaultConfig, backend, mockQuery()); var res = { status: function (code) { t.equal(code, 200, 'status set'); @@ -104,6 +109,33 @@ module.exports.tests.functional_success = function(test, common) { }; 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) @@ -112,7 +144,7 @@ module.exports.tests.functional_failure = function(test, common) { 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( backend, mockQuery() ); + var controller = setup( fakeDefaultConfig, backend, mockQuery() ); var req = { clean: { a: 'b' }, errors: [], warnings: [] }; var next = function(){ t.equal(req.errors[0],'a backend error occurred'); @@ -127,7 +159,7 @@ module.exports.tests.timeout = function(test, common) { var backend = mockBackend( 'client/search/timeout/1', function( cmd ){ t.deepEqual(cmd, { body: { a: 'b' }, index: 'pelias', searchType: 'dfs_query_then_fetch' }, 'correct backend command'); }); - var controller = setup( backend, mockQuery() ); + var controller = setup( fakeDefaultConfig, backend, mockQuery() ); var req = { clean: { a: 'b' }, errors: [], warnings: [] }; var next = function(){ t.equal(req.errors[0],'Request Timeout after 5000ms');