From 4a8711017e7ac88c5d6b15e8ef03cac7a40526e8 Mon Sep 17 00:00:00 2001 From: Stephen Hess Date: Wed, 10 May 2017 16:20:31 -0400 Subject: [PATCH] filter placeholder results by requested layers --- controller/placeholder.js | 10 +- test/unit/controller/placeholder.js | 158 ++++++++++++++++++++++++++++ 2 files changed, 167 insertions(+), 1 deletion(-) diff --git a/controller/placeholder.js b/controller/placeholder.js index c1b34a43..3ea0a89d 100644 --- a/controller/placeholder.js +++ b/controller/placeholder.js @@ -98,9 +98,17 @@ function setup(placeholderService, should_execute) { } } else { + const matchesLayers = (result) => { + return req.clean.layers.indexOf(result.placetype) >= 0; + }; + + // filter that passes only results that match on requested layers + // passes everything if req.clean.layers is not found + const layersFilter = _.get(req, 'clean.layers', []).length ? matchesLayers : _.constant(true); + // otherwise convert results to ES docs res.meta = {}; - res.data = _.flatten(results.map(synthesizeDocs)); + res.data = _.flatten(results.filter(layersFilter).map(synthesizeDocs)); } return next(); diff --git a/test/unit/controller/placeholder.js b/test/unit/controller/placeholder.js index 6e4545da..63ee29ee 100644 --- a/test/unit/controller/placeholder.js +++ b/test/unit/controller/placeholder.js @@ -519,6 +519,164 @@ module.exports.tests.success = (test, common) => { }); + test('only results matching explicit layers should be returned', (t) => { + let placeholderService_was_called = false; + + const placeholder_response = [ + { + id: 1, + name: 'name 1', + placetype: 'neighbourhood', + lineage: [ {} ], + geom: { + area: 1, + lat: 14.141414, + lon: 41.414141 + } + }, + { + id: 2, + name: 'name 2', + placetype: 'borough', + lineage: [ {} ], + geom: { + area: 2, + lat: 15.151515, + lon: 51.515151 + } + }, + { + id: 3, + name: 'name 3', + placetype: 'locality', + lineage: [ {} ], + geom: { + area: 3, + lat: 16.161616, + lon: 61.616161 + } + }, + { + id: 4, + name: 'name 4', + placetype: 'localadmin', + lineage: [ {} ], + geom: { + area: 4, + lat: 17.171717, + lon: 71.717171 + } + }, + { + id: 5, + name: 'name 5', + placetype: 'county', + lineage: [ {} ], + geom: { + area: 5, + lat: 18.181818, + lon: 81.818181 + } + } + ]; + + const placeholderService = (req, callback) => { + t.deepEqual(req, { + param1: 'param1 value', + clean: { + layers: ['neighbourhood', 'locality', 'county'] + } + }); + placeholderService_was_called = true; + callback(null, placeholder_response); + }; + + const should_execute = (req, res) => { + return true; + }; + + const controller = placeholder(placeholderService, should_execute); + + const req = { + param1: 'param1 value', + clean: { + layers: [ + 'neighbourhood', + 'locality', + 'county' + ] + } + }; + const res = { }; + + const expected_res = { + meta: {}, + data: [ + { + _id: '1', + _type: 'neighbourhood', + layer: 'neighbourhood', + source: 'whosonfirst', + source_id: '1', + center_point: { + lat: 14.141414, + lon: 41.414141 + }, + name: { + 'default': 'name 1' + }, + phrase: { + 'default': 'name 1' + }, + parent: { } + }, + { + _id: '3', + _type: 'locality', + layer: 'locality', + source: 'whosonfirst', + source_id: '3', + center_point: { + lat: 16.161616, + lon: 61.616161 + }, + name: { + 'default': 'name 3' + }, + phrase: { + 'default': 'name 3' + }, + parent: { } + }, + { + _id: '5', + _type: 'county', + layer: 'county', + source: 'whosonfirst', + source_id: '5', + center_point: { + lat: 18.181818, + lon: 81.818181 + }, + name: { + 'default': 'name 5' + }, + phrase: { + 'default': 'name 5' + }, + parent: { } + } + ] + }; + + controller(req, res, () => { + t.ok(placeholderService_was_called); + t.deepEquals(res, expected_res); + t.end(); + }); + + }); + }; module.exports.tests.error_conditions = (test, common) => {