From 98b6829e316072c3907c84c00d7bea237b84ce90 Mon Sep 17 00:00:00 2001 From: Stephen Hess Date: Fri, 30 Jun 2017 14:20:03 -0400 Subject: [PATCH] exclude venue, address, and street from layers filter --- controller/placeholder.js | 20 ++-- test/unit/controller/placeholder.js | 136 ++++++++++++++++++++++++++++ 2 files changed, 144 insertions(+), 12 deletions(-) diff --git a/controller/placeholder.js b/controller/placeholder.js index 27843403..0d00b1b8 100644 --- a/controller/placeholder.js +++ b/controller/placeholder.js @@ -59,16 +59,16 @@ function hasName(result) { } // filter that passes only results that match on requested layers -// passes everything if req.clean.layers is not found function getLayersFilter(clean) { - if (_.isEmpty(_.get(clean, 'layers', []))) { - return _.constant(true); + // passes everything if: + // - req.clean.layers is empty + // - req.clean.parsed_text.street is available + if (_.isEmpty(_.get(clean, 'layers', [])) || _.has(clean, ['parsed_text', 'street'])) { + return () => true; } // otherwise return a function that checks for set inclusion of a result placetype - return (result) => { - return _.includes(clean.layers, result.placetype); - }; + return (result) => _.includes(clean.layers, result.placetype); } @@ -222,12 +222,8 @@ function setup(placeholderService, should_execute) { placeholderService(req, (err, results) => { if (err) { - // bubble up an error if one occurred - if (_.isObject(err) && err.message) { - req.errors.push( err.message ); - } else { - req.errors.push( err ); - } + // push err.message or err onto req.errors + req.errors.push( _.get(err, 'message', err)); } else { const boundaryCountry = _.get(req, ['clean', 'boundary.country']); diff --git a/test/unit/controller/placeholder.js b/test/unit/controller/placeholder.js index 74118c8f..ef925de9 100644 --- a/test/unit/controller/placeholder.js +++ b/test/unit/controller/placeholder.js @@ -1143,6 +1143,142 @@ module.exports.tests.result_filtering = (test, common) => { }); + test('if req.clean.parsed_text contains street, don\'t filter on anything', (t) => { + const logger = mock_logger(); + + const placeholder_service = (req, callback) => { + t.deepEqual(req, { + param1: 'param1 value', + clean: { + layers: ['neighbourhood'], + parsed_text: { + street: 'street value' + } + } + }); + + const 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 + } + } + ]; + + callback(null, response); + }; + + const controller = proxyquire('../../../controller/placeholder', { + 'pelias-logger': logger + })(placeholder_service, _.constant(true)); + + const req = { + param1: 'param1 value', + clean: { + layers: ['neighbourhood'], + parsed_text: { + street: 'street value' + } + } + }; + const res = { }; + + controller(req, 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: '2', + _type: 'borough', + layer: 'borough', + source: 'whosonfirst', + source_id: '2', + center_point: { + lat: 15.151515, + lon: 51.515151 + }, + name: { + 'default': 'name 2' + }, + phrase: { + 'default': 'name 2' + }, + 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: { } + } + ] + }; + + t.deepEquals(res, expected_res); + t.ok(logger.isInfoMessage('[controller:placeholder] [result_count:3]')); + t.end(); + }); + + }); + test('only synthesized docs matching explicit boundary.country should be returned', (t) => { const logger = require('pelias-mock-logger')();