From e97950c142c8e18b74daa627da7cec4d471f85d8 Mon Sep 17 00:00:00 2001 From: Stephen Hess Date: Tue, 2 May 2017 15:40:58 -0400 Subject: [PATCH] adapted coarse reverse controller to generic service --- controller/coarse_reverse.js | 8 +- test/unit/controller/coarse_reverse.js | 107 +++++++------------------ 2 files changed, 30 insertions(+), 85 deletions(-) diff --git a/controller/coarse_reverse.js b/controller/coarse_reverse.js index e8acbf32..07a370d7 100644 --- a/controller/coarse_reverse.js +++ b/controller/coarse_reverse.js @@ -1,7 +1,6 @@ const logger = require('pelias-logger').get('coarse_reverse'); const _ = require('lodash'); const Document = require('pelias-model').Document; -const isDNT = require('../helper/logging').isDNT; const granularities = [ 'neighbourhood', @@ -84,12 +83,7 @@ function setup(service, should_execute) { return next(); } - const centroid = { - lat: req.clean['point.lat'], - lon: req.clean['point.lon'] - }; - - service(centroid, isDNT(req), (err, results) => { + service(req, (err, results) => { // if there's an error, log it and bail if (err) { logger.info(`[controller:coarse_reverse][error]`); diff --git a/test/unit/controller/coarse_reverse.js b/test/unit/controller/coarse_reverse.js index 2389cc43..c37c76e0 100644 --- a/test/unit/controller/coarse_reverse.js +++ b/test/unit/controller/coarse_reverse.js @@ -46,8 +46,8 @@ module.exports.tests.early_exit_conditions = (test, common) => { module.exports.tests.error_conditions = (test, common) => { test('service error should log and call next', (t) => { - const service = (point, do_not_track, callback) => { - t.equals(do_not_track, 'do_not_track value'); + const service = (req, callback) => { + t.deepEquals(req, { clean: { layers: ['locality'] } } ); callback('this is an error'); }; @@ -55,21 +55,12 @@ module.exports.tests.error_conditions = (test, common) => { const should_execute = () => { return true; }; const controller = proxyquire('../../../controller/coarse_reverse', { - 'pelias-logger': logger, - '../helper/logging': { - isDNT: () => { - return 'do_not_track value'; - } - } + 'pelias-logger': logger })(service, should_execute); const req = { clean: { - layers: ['locality'], - point: { - lat: 12.121212, - lon: 21.212121 - } + layers: ['locality'] } }; @@ -92,8 +83,9 @@ module.exports.tests.error_conditions = (test, common) => { module.exports.tests.success_conditions = (test, common) => { test('service returning results should use first entry for each layer', (t) => { - const service = (point, do_not_track, callback) => { - t.equals(do_not_track, 'do_not_track value'); + const service = (req, callback) => { + t.deepEquals(req, { clean: { layers: ['neighbourhood'] } } ); + const results = { neighbourhood: [ { @@ -153,21 +145,12 @@ module.exports.tests.success_conditions = (test, common) => { const should_execute = () => { return true; }; const controller = proxyquire('../../../controller/coarse_reverse', { - 'pelias-logger': logger, - '../helper/logging': { - isDNT: () => { - return 'do_not_track value'; - } - } + 'pelias-logger': logger })(service, should_execute); const req = { clean: { - layers: ['neighbourhood'], - point: { - lat: 12.121212, - lon: 21.212121 - } + layers: ['neighbourhood'] } }; @@ -247,8 +230,9 @@ module.exports.tests.success_conditions = (test, common) => { }); test('layers missing from results should be ignored', (t) => { - const service = (point, do_not_track, callback) => { - t.equals(do_not_track, 'do_not_track value'); + const service = (req, callback) => { + t.deepEquals(req, { clean: { layers: ['neighbourhood'] } } ); + const results = { neighbourhood: [ { @@ -271,21 +255,12 @@ module.exports.tests.success_conditions = (test, common) => { const should_execute = () => { return true; }; const controller = proxyquire('../../../controller/coarse_reverse', { - 'pelias-logger': logger, - '../helper/logging': { - isDNT: () => { - return 'do_not_track value'; - } - } + 'pelias-logger': logger })(service, should_execute); const req = { clean: { - layers: ['neighbourhood'], - point: { - lat: 12.121212, - lon: 21.212121 - } + layers: ['neighbourhood'] } }; @@ -337,8 +312,9 @@ module.exports.tests.success_conditions = (test, common) => { }); test('most granular layer missing centroid should not set', (t) => { - const service = (point, do_not_track, callback) => { - t.equals(do_not_track, 'do_not_track value'); + const service = (req, callback) => { + t.deepEquals(req, { clean: { layers: ['neighbourhood'] } } ); + const results = { neighbourhood: [ { @@ -357,21 +333,12 @@ module.exports.tests.success_conditions = (test, common) => { const should_execute = () => { return true; }; const controller = proxyquire('../../../controller/coarse_reverse', { - 'pelias-logger': logger, - '../helper/logging': { - isDNT: () => { - return 'do_not_track value'; - } - } + 'pelias-logger': logger })(service, should_execute); const req = { clean: { - layers: ['neighbourhood'], - point: { - lat: 12.121212, - lon: 21.212121 - } + layers: ['neighbourhood'] } }; @@ -419,8 +386,9 @@ module.exports.tests.success_conditions = (test, common) => { }); test('most granular layer missing bounding_box should not set', (t) => { - const service = (point, do_not_track, callback) => { - t.equals(do_not_track, 'do_not_track value'); + const service = (req, callback) => { + t.deepEquals(req, { clean: { layers: ['neighbourhood'] } } ); + const results = { neighbourhood: [ { @@ -442,21 +410,12 @@ module.exports.tests.success_conditions = (test, common) => { const should_execute = () => { return true; }; const controller = proxyquire('../../../controller/coarse_reverse', { - 'pelias-logger': logger, - '../helper/logging': { - isDNT: () => { - return 'do_not_track value'; - } - } + 'pelias-logger': logger })(service, should_execute); const req = { clean: { - layers: ['neighbourhood'], - point: { - lat: 12.121212, - lon: 21.212121 - } + layers: ['neighbourhood'] } }; @@ -510,8 +469,9 @@ module.exports.tests.success_conditions = (test, common) => { module.exports.tests.failure_conditions = (test, common) => { test('service returning 0 results at the requested layer should return nothing', (t) => { - const service = (point, do_not_track, callback) => { - t.equals(do_not_track, 'do_not_track value'); + const service = (req, callback) => { + t.deepEquals(req, { clean: { layers: ['neighbourhood'] } } ); + // response without neighbourhood results const results = { borough: [ @@ -559,21 +519,12 @@ module.exports.tests.failure_conditions = (test, common) => { const should_execute = () => { return true; }; const controller = proxyquire('../../../controller/coarse_reverse', { - 'pelias-logger': logger, - '../helper/logging': { - isDNT: () => { - return 'do_not_track value'; - } - } + 'pelias-logger': logger })(service, should_execute); const req = { clean: { - layers: ['neighbourhood'], - point: { - lat: 12.121212, - lon: 21.212121 - } + layers: ['neighbourhood'] } };