From da074639995f9a1c53b0f25f1efcb4591495d6d0 Mon Sep 17 00:00:00 2001 From: Stephen Hess Date: Mon, 9 Oct 2017 15:42:04 -0400 Subject: [PATCH 01/23] set req.clean.parser to 'libpostal' when it's called --- controller/libpostal.js | 1 + test/unit/controller/libpostal.js | 4 ++++ 2 files changed, 5 insertions(+) diff --git a/controller/libpostal.js b/controller/libpostal.js index 8f275dd9..23c82f85 100644 --- a/controller/libpostal.js +++ b/controller/libpostal.js @@ -20,6 +20,7 @@ function setup(should_execute) { parsed_text.country = iso3166.to3(_.toUpper(parsed_text.country)); } + req.clean.parser = 'libpostal'; req.clean.parsed_text = parsed_text; debugLog.push(req, {parsed_text: req.clean.parsed_text}); } diff --git a/test/unit/controller/libpostal.js b/test/unit/controller/libpostal.js index e0d012a9..f117007d 100644 --- a/test/unit/controller/libpostal.js +++ b/test/unit/controller/libpostal.js @@ -149,6 +149,7 @@ module.exports.tests.parse_is_called = (test, common) => { controller(req, res, () => { t.deepEquals(req, { clean: { + parser: 'libpostal', parsed_text: 'replacement parsed_text' } }); @@ -184,6 +185,7 @@ module.exports.tests.iso2_conversion = (test, common) => { controller(req, res, () => { t.deepEquals(req, { clean: { + parser: 'libpostal', parsed_text: { locality: 'this is the locality' } @@ -223,6 +225,7 @@ module.exports.tests.iso2_conversion = (test, common) => { controller(req, res, () => { t.deepEquals(req, { clean: { + parser: 'libpostal', parsed_text: { country: 'unknown country code' } @@ -265,6 +268,7 @@ module.exports.tests.iso2_conversion = (test, common) => { controller(req, res, () => { t.deepEquals(req, { clean: { + parser: 'libpostal', parsed_text: { country: 'ISO3 COUNTRY CODE' } From 02cfa36d2485d4165f84e12e9618271413c08c0e Mon Sep 17 00:00:00 2001 From: Stephen Hess Date: Tue, 10 Oct 2017 16:20:01 -0400 Subject: [PATCH 02/23] switched structured query tests away from fixtures --- query/structured_geocoding.js | 14 +- .../boundary_country.json | 59 -- .../structured_geocoding/fallback.json | 859 ------------------ .../structured_geocoding/linguistic_bbox.json | 62 -- .../linguistic_focus.json | 65 -- .../linguistic_focus_bbox.json | 76 -- .../linguistic_focus_null_island.json | 65 -- .../structured_geocoding/linguistic_only.json | 51 -- .../linguistic_viewport.json | 51 -- .../linguistic_viewport_min_diagonal.json | 51 -- .../structured_geocoding/postalcode_only.js | 60 -- .../with_source_filtering.json | 52 -- test/unit/query/structured_geocoding.js | 663 ++++++++++---- 13 files changed, 515 insertions(+), 1613 deletions(-) delete mode 100644 test/unit/fixture/structured_geocoding/boundary_country.json delete mode 100644 test/unit/fixture/structured_geocoding/fallback.json delete mode 100644 test/unit/fixture/structured_geocoding/linguistic_bbox.json delete mode 100644 test/unit/fixture/structured_geocoding/linguistic_focus.json delete mode 100644 test/unit/fixture/structured_geocoding/linguistic_focus_bbox.json delete mode 100644 test/unit/fixture/structured_geocoding/linguistic_focus_null_island.json delete mode 100644 test/unit/fixture/structured_geocoding/linguistic_only.json delete mode 100644 test/unit/fixture/structured_geocoding/linguistic_viewport.json delete mode 100644 test/unit/fixture/structured_geocoding/linguistic_viewport_min_diagonal.json delete mode 100644 test/unit/fixture/structured_geocoding/postalcode_only.js delete mode 100644 test/unit/fixture/structured_geocoding/with_source_filtering.json diff --git a/query/structured_geocoding.js b/query/structured_geocoding.js index a69f42bb..317caba1 100644 --- a/query/structured_geocoding.js +++ b/query/structured_geocoding.js @@ -1,12 +1,12 @@ -var peliasQuery = require('pelias-query'), - defaults = require('./search_defaults'), - textParser = require('./text_parser'), - check = require('check-types'); +const peliasQuery = require('pelias-query'), + defaults = require('./search_defaults'), + textParser = require('./text_parser'), + check = require('check-types'); //------------------------------ // general-purpose search query //------------------------------ -var structuredQuery = new peliasQuery.layout.StructuredFallbackQuery(); +const structuredQuery = new peliasQuery.layout.StructuredFallbackQuery(); // scoring boost structuredQuery.score( peliasQuery.view.focus_only_function( peliasQuery.view.phrase ) ); @@ -29,7 +29,7 @@ structuredQuery.filter( peliasQuery.view.categories ); **/ function generateQuery( clean ){ - var vs = new peliasQuery.Vars( defaults ); + const vs = new peliasQuery.Vars( defaults ); // input text vs.var( 'input:name', clean.text ); @@ -95,7 +95,7 @@ function generateQuery( clean ){ textParser( clean.parsed_text, vs ); } - var q = getQuery(vs); + const q = getQuery(vs); // console.log(JSON.stringify(q.body, null, 2)); diff --git a/test/unit/fixture/structured_geocoding/boundary_country.json b/test/unit/fixture/structured_geocoding/boundary_country.json deleted file mode 100644 index edff5d63..00000000 --- a/test/unit/fixture/structured_geocoding/boundary_country.json +++ /dev/null @@ -1,59 +0,0 @@ -{ - "query": { - "function_score": { - "query": { - "bool": { - "minimum_should_match": 1, - "should": [], - "filter": { - "bool": { - "must": [ - { - "match": { - "parent.country_a": { - "analyzer": "standard", - "query": "ABC" - } - } - }, - { - "terms": { - "layer": [ - "test" - ] - } - } - ] - } - } - } - }, - "max_boost": 20, - "functions": [ - { - "field_value_factor": { - "modifier": "log1p", - "field": "popularity", - "missing": 1 - }, - "weight": 1 - }, - { - "field_value_factor": { - "modifier": "log1p", - "field": "population", - "missing": 1 - }, - "weight": 2 - } - ], - "score_mode": "avg", - "boost_mode": "multiply" - } - }, - "sort": [ - "_score" - ], - "size": 10, - "track_scores": true -} diff --git a/test/unit/fixture/structured_geocoding/fallback.json b/test/unit/fixture/structured_geocoding/fallback.json deleted file mode 100644 index 7b737ffd..00000000 --- a/test/unit/fixture/structured_geocoding/fallback.json +++ /dev/null @@ -1,859 +0,0 @@ -{ - "query": { - "function_score": { - "query": { - "bool": { - "minimum_should_match": 1, - "should": [ - { - "bool": { - "_name": "fallback.venue", - "must": [ - { - "multi_match": { - "query": "query value", - "type": "phrase", - "fields": [ - "phrase.default", - "category" - ] - } - }, - { - "multi_match": { - "query": "neighbourhood value", - "type": "phrase", - "fields": [ - "parent.neighbourhood", - "parent.neighbourhood_a" - ] - } - }, - { - "multi_match": { - "query": "borough value", - "type": "phrase", - "fields": [ - "parent.borough", - "parent.borough_a" - ] - } - }, - { - "multi_match": { - "query": "city value", - "type": "phrase", - "fields": [ - "parent.locality", - "parent.locality_a", - "parent.localadmin", - "parent.localadmin_a" - ] - } - }, - { - "multi_match": { - "query": "county value", - "type": "phrase", - "fields": [ - "parent.county", - "parent.county_a", - "parent.macrocounty", - "parent.macrocounty_a" - ] - } - }, - { - "multi_match": { - "query": "state value", - "type": "phrase", - "fields": [ - "parent.region", - "parent.region_a", - "parent.macroregion", - "parent.macroregion_a" - ] - } - }, - { - "multi_match": { - "query": "country value", - "type": "phrase", - "fields": [ - "parent.country", - "parent.country_a", - "parent.dependency", - "parent.dependency_a" - ] - } - } - ], - "filter": { - "term": { - "layer": "venue" - } - } - } - }, - { - "bool": { - "_name": "fallback.address", - "must": [ - { - "match_phrase": { - "address_parts.number": "number value" - } - }, - { - "match_phrase": { - "address_parts.street": "street value" - } - }, - { - "multi_match": { - "query": "neighbourhood value", - "type": "phrase", - "fields": [ - "parent.neighbourhood", - "parent.neighbourhood_a" - ] - } - }, - { - "multi_match": { - "query": "borough value", - "type": "phrase", - "fields": [ - "parent.borough", - "parent.borough_a" - ] - } - }, - { - "multi_match": { - "query": "city value", - "type": "phrase", - "fields": [ - "parent.locality", - "parent.locality_a", - "parent.localadmin", - "parent.localadmin_a" - ] - } - }, - { - "multi_match": { - "query": "county value", - "type": "phrase", - "fields": [ - "parent.county", - "parent.county_a", - "parent.macrocounty", - "parent.macrocounty_a" - ] - } - }, - { - "multi_match": { - "query": "state value", - "type": "phrase", - "fields": [ - "parent.region", - "parent.region_a", - "parent.macroregion", - "parent.macroregion_a" - ] - } - }, - { - "multi_match": { - "query": "country value", - "type": "phrase", - "fields": [ - "parent.country", - "parent.country_a", - "parent.dependency", - "parent.dependency_a" - ] - } - } - ], - "should": [ - { - "match_phrase": { - "address_parts.zip": "postalcode value" - } - } - ], - "filter": { - "term": { - "layer": "address" - } - }, - "boost": 10 - } - }, - { - "bool": { - "_name": "fallback.street", - "must": [ - { - "match_phrase": { - "address_parts.street": "street value" - } - }, - { - "multi_match": { - "query": "neighbourhood value", - "type": "phrase", - "fields": [ - "parent.neighbourhood", - "parent.neighbourhood_a" - ] - } - }, - { - "multi_match": { - "query": "borough value", - "type": "phrase", - "fields": [ - "parent.borough", - "parent.borough_a" - ] - } - }, - { - "multi_match": { - "query": "city value", - "type": "phrase", - "fields": [ - "parent.locality", - "parent.locality_a", - "parent.localadmin", - "parent.localadmin_a" - ] - } - }, - { - "multi_match": { - "query": "county value", - "type": "phrase", - "fields": [ - "parent.county", - "parent.county_a", - "parent.macrocounty", - "parent.macrocounty_a" - ] - } - }, - { - "multi_match": { - "query": "state value", - "type": "phrase", - "fields": [ - "parent.region", - "parent.region_a", - "parent.macroregion", - "parent.macroregion_a" - ] - } - }, - { - "multi_match": { - "query": "country value", - "type": "phrase", - "fields": [ - "parent.country", - "parent.country_a", - "parent.dependency", - "parent.dependency_a" - ] - } - } - ], - "should": [ - { - "match_phrase": { - "address_parts.zip": "postalcode value" - } - } - ], - "filter": { - "term": { - "layer": "street" - } - }, - "boost": 5 - } - }, - { - "bool": { - "_name": "fallback.postalcode", - "must": [ - { - "multi_match": { - "query": "postalcode value", - "type": "phrase", - "fields": [ - "parent.postalcode" - ] - } - }, - { - "multi_match": { - "query": "city value", - "type": "phrase", - "fields": [ - "parent.locality", - "parent.locality_a", - "parent.localadmin", - "parent.localadmin_a" - ] - } - }, - { - "multi_match": { - "query": "county value", - "type": "phrase", - "fields": [ - "parent.county", - "parent.county_a", - "parent.macrocounty", - "parent.macrocounty_a" - ] - } - }, - { - "multi_match": { - "query": "state value", - "type": "phrase", - "fields": [ - "parent.region", - "parent.region_a", - "parent.macroregion", - "parent.macroregion_a" - ] - } - }, - { - "multi_match": { - "query": "country value", - "type": "phrase", - "fields": [ - "parent.country", - "parent.country_a", - "parent.dependency", - "parent.dependency_a" - ] - } - } - ], - "filter": { - "term": { - "layer": "postalcode" - } - } - } - }, - { - "bool": { - "_name": "fallback.neighbourhood", - "must": [ - { - "multi_match": { - "query": "neighbourhood value", - "type": "phrase", - "fields": [ - "parent.neighbourhood", - "parent.neighbourhood_a" - ] - } - }, - { - "multi_match": { - "query": "borough value", - "type": "phrase", - "fields": [ - "parent.borough", - "parent.borough_a" - ] - } - }, - { - "multi_match": { - "query": "city value", - "type": "phrase", - "fields": [ - "parent.locality", - "parent.locality_a", - "parent.localadmin", - "parent.localadmin_a" - ] - } - }, - { - "multi_match": { - "query": "county value", - "type": "phrase", - "fields": [ - "parent.county", - "parent.county_a", - "parent.macrocounty", - "parent.macrocounty_a" - ] - } - }, - { - "multi_match": { - "query": "state value", - "type": "phrase", - "fields": [ - "parent.region", - "parent.region_a", - "parent.macroregion", - "parent.macroregion_a" - ] - } - }, - { - "multi_match": { - "query": "country value", - "type": "phrase", - "fields": [ - "parent.country", - "parent.country_a", - "parent.dependency", - "parent.dependency_a" - ] - } - } - ], - "filter": { - "term": { - "layer": "neighbourhood" - } - } - } - }, - { - "bool": { - "_name": "fallback.borough", - "must": [ - { - "multi_match": { - "query": "borough value", - "type": "phrase", - "fields": [ - "parent.borough", - "parent.borough_a" - ] - } - }, - { - "multi_match": { - "query": "city value", - "type": "phrase", - "fields": [ - "parent.locality", - "parent.locality_a", - "parent.localadmin", - "parent.localadmin_a" - ] - } - }, - { - "multi_match": { - "query": "county value", - "type": "phrase", - "fields": [ - "parent.county", - "parent.county_a", - "parent.macrocounty", - "parent.macrocounty_a" - ] - } - }, - { - "multi_match": { - "query": "state value", - "type": "phrase", - "fields": [ - "parent.region", - "parent.region_a", - "parent.macroregion", - "parent.macroregion_a" - ] - } - }, - { - "multi_match": { - "query": "country value", - "type": "phrase", - "fields": [ - "parent.country", - "parent.country_a", - "parent.dependency", - "parent.dependency_a" - ] - } - } - ], - "filter": { - "term": { - "layer": "borough" - } - } - } - }, - { - "bool": { - "_name": "fallback.locality", - "must": [ - { - "multi_match": { - "query": "city value", - "type": "phrase", - "fields": [ - "parent.locality", - "parent.locality_a" - ] - } - }, - { - "multi_match": { - "query": "county value", - "type": "phrase", - "fields": [ - "parent.county", - "parent.county_a", - "parent.macrocounty", - "parent.macrocounty_a" - ] - } - }, - { - "multi_match": { - "query": "state value", - "type": "phrase", - "fields": [ - "parent.region", - "parent.region_a", - "parent.macroregion", - "parent.macroregion_a" - ] - } - }, - { - "multi_match": { - "query": "country value", - "type": "phrase", - "fields": [ - "parent.country", - "parent.country_a", - "parent.dependency", - "parent.dependency_a" - ] - } - } - ], - "filter": { - "term": { - "layer": "locality" - } - } - } - }, - { - "bool": { - "_name": "fallback.localadmin", - "must": [ - { - "multi_match": { - "query": "city value", - "type": "phrase", - "fields": [ - "parent.localadmin", - "parent.localadmin_a" - ] - } - }, - { - "multi_match": { - "query": "county value", - "type": "phrase", - "fields": [ - "parent.county", - "parent.county_a", - "parent.macrocounty", - "parent.macrocounty_a" - ] - } - }, - { - "multi_match": { - "query": "state value", - "type": "phrase", - "fields": [ - "parent.region", - "parent.region_a", - "parent.macroregion", - "parent.macroregion_a" - ] - } - }, - { - "multi_match": { - "query": "country value", - "type": "phrase", - "fields": [ - "parent.country", - "parent.country_a", - "parent.dependency", - "parent.dependency_a" - ] - } - } - ], - "filter": { - "term": { - "layer": "localadmin" - } - } - } - }, - { - "bool": { - "_name": "fallback.county", - "must": [ - { - "multi_match": { - "query": "county value", - "type": "phrase", - "fields": [ - "parent.county", - "parent.county_a" - ] - } - }, - { - "multi_match": { - "query": "state value", - "type": "phrase", - "fields": [ - "parent.region", - "parent.region_a", - "parent.macroregion", - "parent.macroregion_a" - ] - } - }, - { - "multi_match": { - "query": "country value", - "type": "phrase", - "fields": [ - "parent.country", - "parent.country_a", - "parent.dependency", - "parent.dependency_a" - ] - } - } - ], - "filter": { - "term": { - "layer": "county" - } - } - } - }, - { - "bool": { - "_name": "fallback.macrocounty", - "must": [ - { - "multi_match": { - "query": "county value", - "type": "phrase", - "fields": [ - "parent.macrocounty", - "parent.macrocounty_a" - ] - } - }, - { - "multi_match": { - "query": "state value", - "type": "phrase", - "fields": [ - "parent.region", - "parent.region_a", - "parent.macroregion", - "parent.macroregion_a" - ] - } - }, - { - "multi_match": { - "query": "country value", - "type": "phrase", - "fields": [ - "parent.country", - "parent.country_a", - "parent.dependency", - "parent.dependency_a" - ] - } - } - ], - "filter": { - "term": { - "layer": "macrocounty" - } - } - } - }, - { - "bool": { - "_name": "fallback.region", - "must": [ - { - "multi_match": { - "query": "state value", - "type": "phrase", - "fields": [ - "parent.region", - "parent.region_a" - ] - } - }, - { - "multi_match": { - "query": "country value", - "type": "phrase", - "fields": [ - "parent.country", - "parent.country_a", - "parent.dependency", - "parent.dependency_a" - ] - } - } - ], - "filter": { - "term": { - "layer": "region" - } - } - } - }, - { - "bool": { - "_name": "fallback.macroregion", - "must": [ - { - "multi_match": { - "query": "state value", - "type": "phrase", - "fields": [ - "parent.macroregion", - "parent.macroregion_a" - ] - } - }, - { - "multi_match": { - "query": "country value", - "type": "phrase", - "fields": [ - "parent.country", - "parent.country_a", - "parent.dependency", - "parent.dependency_a" - ] - } - } - ], - "filter": { - "term": { - "layer": "macroregion" - } - } - } - }, - { - "bool": { - "_name": "fallback.dependency", - "must": [ - { - "multi_match": { - "query": "country value", - "type": "phrase", - "fields": [ - "parent.dependency", - "parent.dependency_a" - ] - } - } - ], - "filter": { - "term": { - "layer": "dependency" - } - } - } - }, - { - "bool": { - "_name": "fallback.country", - "must": [ - { - "multi_match": { - "query": "country value", - "type": "phrase", - "fields": [ - "parent.country", - "parent.country_a" - ] - } - } - ], - "filter": { - "term": { - "layer": "country" - } - } - } - } - ] - } - }, - "max_boost": 20, - "functions": [ - { - "field_value_factor": { - "modifier": "log1p", - "field": "popularity", - "missing": 1 - }, - "weight": 1 - }, - { - "field_value_factor": { - "modifier": "log1p", - "field": "population", - "missing": 1 - }, - "weight": 2 - } - ], - "score_mode": "avg", - "boost_mode": "multiply" - } - }, - "sort": [ - "_score" - ], - "size": 20, - "track_scores": true -} diff --git a/test/unit/fixture/structured_geocoding/linguistic_bbox.json b/test/unit/fixture/structured_geocoding/linguistic_bbox.json deleted file mode 100644 index 94acc81d..00000000 --- a/test/unit/fixture/structured_geocoding/linguistic_bbox.json +++ /dev/null @@ -1,62 +0,0 @@ -{ - "query": { - "function_score": { - "query": { - "bool": { - "minimum_should_match": 1, - "should": [], - "filter": { - "bool": { - "must": [ - { - "geo_bounding_box": { - "type": "indexed", - "center_point": { - "top": 11.51, - "right": -61.84, - "bottom": 47.47, - "left": -103.16 - } - } - }, - { - "terms": { - "layer": [ - "test" - ] - } - } - ] - } - } - } - }, - "max_boost": 20, - "functions": [ - { - "field_value_factor": { - "modifier": "log1p", - "field": "popularity", - "missing": 1 - }, - "weight": 1 - }, - { - "field_value_factor": { - "modifier": "log1p", - "field": "population", - "missing": 1 - }, - "weight": 2 - } - ], - "score_mode": "avg", - "boost_mode": "multiply" - } - }, - "sort": [ - "_score" - ], - "size": 10, - "track_scores": true -} diff --git a/test/unit/fixture/structured_geocoding/linguistic_focus.json b/test/unit/fixture/structured_geocoding/linguistic_focus.json deleted file mode 100644 index 0bb126d1..00000000 --- a/test/unit/fixture/structured_geocoding/linguistic_focus.json +++ /dev/null @@ -1,65 +0,0 @@ -{ - "query": { - "function_score": { - "query": { - "bool": { - "minimum_should_match": 1, - "should": [], - "filter": { - "bool": { - "must": [ - { - "terms": { - "layer": [ - "test" - ] - } - } - ] - } - } - } - }, - "max_boost": 20, - "functions": [ - { - "weight": 2, - "linear": { - "center_point": { - "origin": { - "lat": 29.49136, - "lon": -82.50622 - }, - "offset": "0km", - "scale": "50km", - "decay": 0.5 - } - } - }, - { - "field_value_factor": { - "modifier": "log1p", - "field": "popularity", - "missing": 1 - }, - "weight": 1 - }, - { - "field_value_factor": { - "modifier": "log1p", - "field": "population", - "missing": 1 - }, - "weight": 2 - } - ], - "score_mode": "avg", - "boost_mode": "multiply" - } - }, - "sort": [ - "_score" - ], - "size": 10, - "track_scores": true -} diff --git a/test/unit/fixture/structured_geocoding/linguistic_focus_bbox.json b/test/unit/fixture/structured_geocoding/linguistic_focus_bbox.json deleted file mode 100644 index 5b98f11f..00000000 --- a/test/unit/fixture/structured_geocoding/linguistic_focus_bbox.json +++ /dev/null @@ -1,76 +0,0 @@ -{ - "query": { - "function_score": { - "query": { - "bool": { - "minimum_should_match": 1, - "should": [], - "filter": { - "bool": { - "must": [ - { - "geo_bounding_box": { - "type": "indexed", - "center_point": { - "top": 11.51, - "right": -61.84, - "bottom": 47.47, - "left": -103.16 - } - } - }, - { - "terms": { - "layer": [ - "test" - ] - } - } - ] - } - } - } - }, - "max_boost": 20, - "functions": [ - { - "weight": 2, - "linear": { - "center_point": { - "origin": { - "lat": 29.49136, - "lon": -82.50622 - }, - "offset": "0km", - "scale": "50km", - "decay": 0.5 - } - } - }, - { - "field_value_factor": { - "modifier": "log1p", - "field": "popularity", - "missing": 1 - }, - "weight": 1 - }, - { - "field_value_factor": { - "modifier": "log1p", - "field": "population", - "missing": 1 - }, - "weight": 2 - } - ], - "score_mode": "avg", - "boost_mode": "multiply" - } - }, - "sort": [ - "_score" - ], - "size": 10, - "track_scores": true -} diff --git a/test/unit/fixture/structured_geocoding/linguistic_focus_null_island.json b/test/unit/fixture/structured_geocoding/linguistic_focus_null_island.json deleted file mode 100644 index d8723845..00000000 --- a/test/unit/fixture/structured_geocoding/linguistic_focus_null_island.json +++ /dev/null @@ -1,65 +0,0 @@ -{ - "query": { - "function_score": { - "query": { - "bool": { - "minimum_should_match": 1, - "should": [], - "filter": { - "bool": { - "must": [ - { - "terms": { - "layer": [ - "test" - ] - } - } - ] - } - } - } - }, - "max_boost": 20, - "functions": [ - { - "weight": 2, - "linear": { - "center_point": { - "origin": { - "lat": 0, - "lon": 0 - }, - "offset": "0km", - "scale": "50km", - "decay": 0.5 - } - } - }, - { - "field_value_factor": { - "modifier": "log1p", - "field": "popularity", - "missing": 1 - }, - "weight": 1 - }, - { - "field_value_factor": { - "modifier": "log1p", - "field": "population", - "missing": 1 - }, - "weight": 2 - } - ], - "score_mode": "avg", - "boost_mode": "multiply" - } - }, - "sort": [ - "_score" - ], - "size": 10, - "track_scores": true -} diff --git a/test/unit/fixture/structured_geocoding/linguistic_only.json b/test/unit/fixture/structured_geocoding/linguistic_only.json deleted file mode 100644 index 4de54360..00000000 --- a/test/unit/fixture/structured_geocoding/linguistic_only.json +++ /dev/null @@ -1,51 +0,0 @@ -{ - "query": { - "function_score": { - "query": { - "bool": { - "minimum_should_match": 1, - "should": [], - "filter": { - "bool": { - "must": [ - { - "terms": { - "layer": [ - "test" - ] - } - } - ] - } - } - } - }, - "max_boost": 20, - "functions": [ - { - "field_value_factor": { - "modifier": "log1p", - "field": "popularity", - "missing": 1 - }, - "weight": 1 - }, - { - "field_value_factor": { - "modifier": "log1p", - "field": "population", - "missing": 1 - }, - "weight": 2 - } - ], - "score_mode": "avg", - "boost_mode": "multiply" - } - }, - "sort": [ - "_score" - ], - "size": 10, - "track_scores": true -} diff --git a/test/unit/fixture/structured_geocoding/linguistic_viewport.json b/test/unit/fixture/structured_geocoding/linguistic_viewport.json deleted file mode 100644 index 4de54360..00000000 --- a/test/unit/fixture/structured_geocoding/linguistic_viewport.json +++ /dev/null @@ -1,51 +0,0 @@ -{ - "query": { - "function_score": { - "query": { - "bool": { - "minimum_should_match": 1, - "should": [], - "filter": { - "bool": { - "must": [ - { - "terms": { - "layer": [ - "test" - ] - } - } - ] - } - } - } - }, - "max_boost": 20, - "functions": [ - { - "field_value_factor": { - "modifier": "log1p", - "field": "popularity", - "missing": 1 - }, - "weight": 1 - }, - { - "field_value_factor": { - "modifier": "log1p", - "field": "population", - "missing": 1 - }, - "weight": 2 - } - ], - "score_mode": "avg", - "boost_mode": "multiply" - } - }, - "sort": [ - "_score" - ], - "size": 10, - "track_scores": true -} diff --git a/test/unit/fixture/structured_geocoding/linguistic_viewport_min_diagonal.json b/test/unit/fixture/structured_geocoding/linguistic_viewport_min_diagonal.json deleted file mode 100644 index 4de54360..00000000 --- a/test/unit/fixture/structured_geocoding/linguistic_viewport_min_diagonal.json +++ /dev/null @@ -1,51 +0,0 @@ -{ - "query": { - "function_score": { - "query": { - "bool": { - "minimum_should_match": 1, - "should": [], - "filter": { - "bool": { - "must": [ - { - "terms": { - "layer": [ - "test" - ] - } - } - ] - } - } - } - }, - "max_boost": 20, - "functions": [ - { - "field_value_factor": { - "modifier": "log1p", - "field": "popularity", - "missing": 1 - }, - "weight": 1 - }, - { - "field_value_factor": { - "modifier": "log1p", - "field": "population", - "missing": 1 - }, - "weight": 2 - } - ], - "score_mode": "avg", - "boost_mode": "multiply" - } - }, - "sort": [ - "_score" - ], - "size": 10, - "track_scores": true -} diff --git a/test/unit/fixture/structured_geocoding/postalcode_only.js b/test/unit/fixture/structured_geocoding/postalcode_only.js deleted file mode 100644 index 2eb10d30..00000000 --- a/test/unit/fixture/structured_geocoding/postalcode_only.js +++ /dev/null @@ -1,60 +0,0 @@ -module.exports = { - 'query': { - 'function_score': { - 'query': { - 'bool': { - 'minimum_should_match': 1, - 'should': [ - { - 'bool': { - '_name': 'fallback.postalcode', - 'must': [ - { - 'multi_match': { - 'query': 'postalcode value', - 'type': 'phrase', - 'fields': [ - 'parent.postalcode' - ] - } - } - ], - 'filter': { - 'term': { - 'layer': 'postalcode' - } - } - } - } - ] - } - }, - 'max_boost': 20, - 'functions': [ - { - 'field_value_factor': { - 'modifier': 'log1p', - 'field': 'popularity', - 'missing': 1 - }, - 'weight': 1 - }, - { - 'field_value_factor': { - 'modifier': 'log1p', - 'field': 'population', - 'missing': 1 - }, - 'weight': 2 - } - ], - 'score_mode': 'avg', - 'boost_mode': 'multiply' - } - }, - 'size': 20, - 'track_scores': true, - 'sort': [ - '_score' - ] -}; diff --git a/test/unit/fixture/structured_geocoding/with_source_filtering.json b/test/unit/fixture/structured_geocoding/with_source_filtering.json deleted file mode 100644 index 77e4fb87..00000000 --- a/test/unit/fixture/structured_geocoding/with_source_filtering.json +++ /dev/null @@ -1,52 +0,0 @@ -{ - "query": { - "function_score": { - "query": { - "bool": { - "minimum_should_match": 1, - "should": [ - ], - "filter": { - "bool": { - "must": [ - { - "terms": { - "source": [ - "test_source" - ] - } - } - ] - } - } - } - }, - "max_boost": 20, - "functions": [ - { - "field_value_factor": { - "modifier": "log1p", - "field": "popularity", - "missing": 1 - }, - "weight": 1 - }, - { - "field_value_factor": { - "modifier": "log1p", - "field": "population", - "missing": 1 - }, - "weight": 2 - } - ], - "score_mode": "avg", - "boost_mode": "multiply" - } - }, - "sort": [ - "_score" - ], - "size": 20, - "track_scores": true -} diff --git a/test/unit/query/structured_geocoding.js b/test/unit/query/structured_geocoding.js index d93a03aa..3fef87f0 100644 --- a/test/unit/query/structured_geocoding.js +++ b/test/unit/query/structured_geocoding.js @@ -1,253 +1,606 @@ -var generate = require('../../../query/structured_geocoding'); -var fs = require('fs'); +const generate = require('../../../query/structured_geocoding'); +const _ = require('lodash'); +const proxyquire = require('proxyquire').noCallThru(); +const MockQuery = require('./MockQuery'); + +// helper for canned views +const views = { + focus_only_function: () => 'focus_only_function view', + popularity_only_function: 'popularity_only_function view', + population_only_function: 'population_only_function view', + boundary_country: 'boundary_country view', + boundary_circle: 'boundary_circle view', + boundary_rect: 'boundary_rect view', + sources: 'sources view', + layers: 'layers view', + categories: 'categories view' +}; 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 generate, 'function', 'valid function'); t.end(); }); }; -module.exports.tests.query = function(test, common) { - test('valid search + focus + bbox', function(t) { - var clean = { - parsed_text: { - }, - text: 'test', - querySize: 10, - 'focus.point.lat': 29.49136, 'focus.point.lon': -82.50622, - 'boundary.rect.min_lat': 47.47, - 'boundary.rect.max_lon': -61.84, - 'boundary.rect.max_lat': 11.51, - 'boundary.rect.min_lon': -103.16, - layers: ['test'] +module.exports.tests.query = (test, common) => { + test('base no frills', t => { + const clean = { + text: 'text value', + sources: 'sources value', + layers: 'layers value' }; - var query = generate(clean); - - var compiled = JSON.parse( JSON.stringify( query ) ); - var expected = require('../fixture/structured_geocoding/linguistic_focus_bbox'); + const query = proxyquire('../../../query/structured_geocoding', { + 'pelias-query': { + layout: { + StructuredFallbackQuery: MockQuery + }, + view: views, + Vars: require('pelias-query').Vars + }, + './search_defaults': { + default_parameter_1: 'first default parameter', + default_parameter_2: 'second default parameter' + }, + './text_parser': () => { + t.fail('text_parser should not have been called'); + } + })(clean); + + t.equals(query.type, 'fallback', 'query type set'); + t.equals(query.body.vs.var('input:name').toString(), 'text value'); + t.equals(query.body.vs.var('sources').toString(), 'sources value'); + t.equals(query.body.vs.var('layers').toString(), 'layers value'); + t.deepEquals(query.body.vs.var('default_parameter_1').toString(), 'first default parameter'); + t.deepEquals(query.body.vs.var('default_parameter_2').toString(), 'second default parameter'); + t.notOk(query.body.vs.isset('size')); + t.notOk(query.body.vs.isset('focus:point:lat')); + t.notOk(query.body.vs.isset('focus:point:lon')); + t.notOk(query.body.vs.isset('boundary:rect:top')); + t.notOk(query.body.vs.isset('boundary:rect:right')); + t.notOk(query.body.vs.isset('boundary:rect:bottom')); + t.notOk(query.body.vs.isset('boundary:rect:left')); + t.notOk(query.body.vs.isset('boundary:circle:lat')); + t.notOk(query.body.vs.isset('boundary:circle:lon')); + t.notOk(query.body.vs.isset('boundary:circle:radius')); + t.notOk(query.body.vs.isset('boundary:country')); + + t.deepEquals(query.body.score_functions, [ + 'focus_only_function view', + 'popularity_only_function view', + 'population_only_function view' + ]); + + t.deepEquals(query.body.filter_functions, [ + 'boundary_country view', + 'boundary_circle view', + 'boundary_rect view', + 'sources view', + 'layers view', + 'categories view' + ]); - t.deepEqual(compiled.type, 'fallback', 'query type set'); - t.deepEqual(compiled.body, expected, 'search_linguistic_focus_bbox'); t.end(); }); - test('valid search + bbox', function(t) { - var clean = { - parsed_text: { + test('clean.parsed_text and vs should be passed to textParser', t => { + const clean = { + text: 'text value', + sources: 'sources value', + layers: 'layers value', + parsed_text: 'parsed_text value' + }; + + const query = proxyquire('../../../query/structured_geocoding', { + 'pelias-query': { + layout: { + StructuredFallbackQuery: MockQuery + }, + view: views, + Vars: require('pelias-query').Vars + }, + './search_defaults': { + default_parameter_1: 'first default parameter', + default_parameter_2: 'second default parameter' }, - text: 'test', - querySize: 10, - 'boundary.rect.min_lat': 47.47, - 'boundary.rect.max_lon': -61.84, - 'boundary.rect.max_lat': 11.51, - 'boundary.rect.min_lon': -103.16, - layers: ['test'] + './text_parser': (parsed_text, vs) => { + vs.var('text_parser:value', 'this value populated by text_parser'); + } + })(clean); + + t.deepEquals(query.body.vs.var('text_parser:value').toString(), 'this value populated by text_parser'); + t.end(); + + }); + +}; + +module.exports.tests.query_size = (test, common) => { + test('size should be set when querySize is available', t => { + const clean = { + text: 'text value', + sources: 'sources value', + layers: 'layers value', + querySize: 17 }; - var query = generate(clean); + const query = proxyquire('../../../query/structured_geocoding', { + 'pelias-query': { + layout: { + StructuredFallbackQuery: MockQuery + }, + view: views, + Vars: require('pelias-query').Vars + }, + './search_defaults': { }, + './text_parser': () => { + t.fail('text_parser should not have been called'); + } + })(clean); - var compiled = JSON.parse( JSON.stringify( query ) ); - var expected = require('../fixture/structured_geocoding/linguistic_bbox'); + t.equals(query.body.vs.var('size').toString(), 17); - t.deepEqual(compiled.type, 'fallback', 'query type set'); - t.deepEqual(compiled.body, expected, 'search_linguistic_bbox'); t.end(); + }); - test('valid lingustic-only search', function(t) { - var clean = { - parsed_text: { - }, - text: 'test', querySize: 10, - layers: ['test'] +}; + +module.exports.tests.focus_point_lat_lon = (test, common) => { + test('missing focus.point.lat shouldn\'t set focus:point:lat/lon', t => { + const clean = { + text: 'text value', + sources: 'sources value', + layers: 'layers value', + 'focus.point.lon': 21.212121 }; - var query = generate(clean); + const query = proxyquire('../../../query/structured_geocoding', { + 'pelias-query': { + layout: { + StructuredFallbackQuery: MockQuery + }, + view: views, + Vars: require('pelias-query').Vars + }, + './search_defaults': { }, + './text_parser': () => { + t.fail('text_parser should not have been called'); + } + })(clean); - var compiled = JSON.parse( JSON.stringify( query ) ); - var expected = require('../fixture/structured_geocoding/linguistic_only'); + t.notOk(query.body.vs.isset('focus:point:lat')); + t.notOk(query.body.vs.isset('focus:point:lon')); - t.deepEqual(compiled.type, 'fallback', 'query type set'); - t.deepEqual(compiled.body, expected, 'search_linguistic_only'); t.end(); + }); - test('search search + focus', function(t) { - var clean = { - parsed_text: { - }, - text: 'test', querySize: 10, - 'focus.point.lat': 29.49136, 'focus.point.lon': -82.50622, - layers: ['test'] + test('missing focus.point.lon shouldn\'t set focus:point:lat/lon', t => { + const clean = { + text: 'text value', + sources: 'sources value', + layers: 'layers value', + 'focus.point.lat': 12.121212 }; - var query = generate(clean); + const query = proxyquire('../../../query/structured_geocoding', { + 'pelias-query': { + layout: { + StructuredFallbackQuery: MockQuery + }, + view: views, + Vars: require('pelias-query').Vars + }, + './search_defaults': { }, + './text_parser': () => { + t.fail('text_parser should not have been called'); + } + })(clean); - var compiled = JSON.parse( JSON.stringify( query ) ); - var expected = require('../fixture/structured_geocoding/linguistic_focus'); + t.notOk(query.body.vs.isset('focus:point:lat')); + t.notOk(query.body.vs.isset('focus:point:lon')); - t.deepEqual(compiled.type, 'fallback', 'query type set'); - t.deepEqual(compiled.body, expected, 'search_linguistic_focus'); t.end(); + }); - test('search search + viewport', function(t) { - var clean = { - parsed_text: { - }, - text: 'test', querySize: 10, - 'focus.viewport.min_lat': 28.49136, - 'focus.viewport.max_lat': 30.49136, - 'focus.viewport.min_lon': -87.50622, - 'focus.viewport.max_lon': -77.50622, - layers: ['test'] + test('focus.point.lat/lon should set focus:point:lat/lon', t => { + const clean = { + text: 'text value', + sources: 'sources value', + layers: 'layers value', + 'focus.point.lat': 12.121212, + 'focus.point.lon': 21.212121 }; - var query = generate(clean); + const query = proxyquire('../../../query/structured_geocoding', { + 'pelias-query': { + layout: { + StructuredFallbackQuery: MockQuery + }, + view: views, + Vars: require('pelias-query').Vars + }, + './search_defaults': { }, + './text_parser': () => { + t.fail('text_parser should not have been called'); + } + })(clean); - var compiled = JSON.parse( JSON.stringify( query ) ); - var expected = require('../fixture/structured_geocoding/linguistic_viewport'); + t.equals(query.body.vs.var('focus:point:lat').toString(), 12.121212); + t.equals(query.body.vs.var('focus:point:lon').toString(), 21.212121); - t.deepEqual(compiled.type, 'fallback', 'query type set'); - t.deepEqual(compiled.body, expected, 'search_linguistic_viewport'); t.end(); + }); - // viewport scale sizing currently disabled. - // ref: https://github.com/pelias/api/pull/388 - test('search with viewport diagonal < 1km should set scale to 1km', function(t) { - var clean = { - parsed_text: { - }, - text: 'test', querySize: 10, - 'focus.viewport.min_lat': 28.49135, - 'focus.viewport.max_lat': 28.49137, - 'focus.viewport.min_lon': -87.50622, - 'focus.viewport.max_lon': -87.50624, - layers: ['test'] +}; + +module.exports.tests.boundary_rect = (test, common) => { + test('missing boundary.rect.min_lat shouldn\'t set boundary:rect fields', t => { + const clean = { + text: 'text value', + sources: 'sources value', + layers: 'layers value', + 'boundary.rect.max_lat': 13.131313, + 'boundary.rect.min_lon': 21.212121, + 'boundary.rect.max_lon': 31.313131 }; - var query = generate(clean); + const query = proxyquire('../../../query/structured_geocoding', { + 'pelias-query': { + layout: { + StructuredFallbackQuery: MockQuery + }, + view: views, + Vars: require('pelias-query').Vars + }, + './search_defaults': { }, + './text_parser': () => { + t.fail('text_parser should not have been called'); + } + })(clean); - var compiled = JSON.parse( JSON.stringify( query ) ); - var expected = require('../fixture/structured_geocoding/linguistic_viewport_min_diagonal'); + t.notOk(query.body.vs.isset('boundary:rect:top')); + t.notOk(query.body.vs.isset('boundary:rect:right')); + t.notOk(query.body.vs.isset('boundary:rect:bottom')); + t.notOk(query.body.vs.isset('boundary:rect:left')); - t.deepEqual(compiled.type, 'fallback', 'query type set'); - t.deepEqual(compiled.body, expected, 'valid search query'); t.end(); + }); - test('search search + focus on null island', function(t) { - var clean = { - parsed_text: { + test('missing boundary.rect.max_lat shouldn\'t set boundary:rect fields', t => { + const clean = { + text: 'text value', + sources: 'sources value', + layers: 'layers value', + 'boundary.rect.min_lat': 12.121212, + 'boundary.rect.min_lon': 21.212121, + 'boundary.rect.max_lon': 31.313131 + }; + + const query = proxyquire('../../../query/structured_geocoding', { + 'pelias-query': { + layout: { + StructuredFallbackQuery: MockQuery + }, + view: views, + Vars: require('pelias-query').Vars }, - text: 'test', querySize: 10, - 'focus.point.lat': 0, 'focus.point.lon': 0, - layers: ['test'] + './search_defaults': { }, + './text_parser': () => { + t.fail('text_parser should not have been called'); + } + })(clean); + + t.notOk(query.body.vs.isset('boundary:rect:top')); + t.notOk(query.body.vs.isset('boundary:rect:right')); + t.notOk(query.body.vs.isset('boundary:rect:bottom')); + t.notOk(query.body.vs.isset('boundary:rect:left')); + + t.end(); + + }); + + test('missing boundary.rect.min_lon shouldn\'t set boundary:rect fields', t => { + const clean = { + text: 'text value', + sources: 'sources value', + layers: 'layers value', + 'boundary.rect.min_lat': 12.121212, + 'boundary.rect.max_lat': 13.131313, + 'boundary.rect.max_lon': 31.313131 }; - var query = generate(clean); + const query = proxyquire('../../../query/structured_geocoding', { + 'pelias-query': { + layout: { + StructuredFallbackQuery: MockQuery + }, + view: views, + Vars: require('pelias-query').Vars + }, + './search_defaults': { }, + './text_parser': () => { + t.fail('text_parser should not have been called'); + } + })(clean); - var compiled = JSON.parse( JSON.stringify( query ) ); - var expected = require('../fixture/structured_geocoding/linguistic_focus_null_island'); + t.notOk(query.body.vs.isset('boundary:rect:top')); + t.notOk(query.body.vs.isset('boundary:rect:right')); + t.notOk(query.body.vs.isset('boundary:rect:bottom')); + t.notOk(query.body.vs.isset('boundary:rect:left')); - t.deepEqual(compiled.type, 'fallback', 'query type set'); - t.deepEqual(compiled.body, expected, 'search_linguistic_focus_null_island'); t.end(); + }); - test('parsed_text with all fields should use FallbackQuery', function(t) { - var clean = { - parsed_text: { - query: 'query value', - category: 'category value', - number: 'number value', - street: 'street value', - neighbourhood: 'neighbourhood value', - borough: 'borough value', - postalcode: 'postalcode value', - city: 'city value', - county: 'county value', - state: 'state value', - country: 'country value' + test('missing boundary.rect.max_lon shouldn\'t set boundary:rect fields', t => { + const clean = { + text: 'text value', + sources: 'sources value', + layers: 'layers value', + 'boundary.rect.min_lat': 12.121212, + 'boundary.rect.max_lat': 13.131313, + 'boundary.rect.min_lon': 21.212121 + }; + + const query = proxyquire('../../../query/structured_geocoding', { + 'pelias-query': { + layout: { + StructuredFallbackQuery: MockQuery + }, + view: views, + Vars: require('pelias-query').Vars + }, + './search_defaults': { }, + './text_parser': () => { + t.fail('text_parser should not have been called'); } + })(clean); + + t.notOk(query.body.vs.isset('boundary:rect:top')); + t.notOk(query.body.vs.isset('boundary:rect:right')); + t.notOk(query.body.vs.isset('boundary:rect:bottom')); + t.notOk(query.body.vs.isset('boundary:rect:left')); + + t.end(); + + }); + + test('focus.point.lat/lon should set focus:point:lat/lon', t => { + const clean = { + text: 'text value', + sources: 'sources value', + layers: 'layers value', + 'boundary.rect.min_lat': 12.121212, + 'boundary.rect.max_lat': 13.131313, + 'boundary.rect.min_lon': 21.212121, + 'boundary.rect.max_lon': 31.313131 }; - var query = generate(clean); + const query = proxyquire('../../../query/structured_geocoding', { + 'pelias-query': { + layout: { + StructuredFallbackQuery: MockQuery + }, + view: views, + Vars: require('pelias-query').Vars + }, + './search_defaults': { }, + './text_parser': () => { + t.fail('text_parser should not have been called'); + } + })(clean); - var compiled = JSON.parse(JSON.stringify(query)); - var expected = require('../fixture/structured_geocoding/fallback'); + t.equals(query.body.vs.var('boundary:rect:top').toString(), 13.131313); + t.equals(query.body.vs.var('boundary:rect:right').toString(), 31.313131); + t.equals(query.body.vs.var('boundary:rect:bottom').toString(), 12.121212); + t.equals(query.body.vs.var('boundary:rect:left').toString(), 21.212121); - t.deepEqual(compiled.type, 'fallback', 'query type set'); - t.deepEqual(compiled.body, expected, 'fallbackQuery'); t.end(); }); - test('parsed_text with all fields should use FallbackQuery', function(t) { - var clean = { - parsed_text: { - postalcode: 'postalcode value' +}; + +module.exports.tests.boundary_circle = (test, common) => { + test('missing boundary.circle.lat shouldn\'t set boundary:circle fields', t => { + const clean = { + text: 'text value', + sources: 'sources value', + layers: 'layers value', + 'boundary.circle.lon': 21.212121, + 'boundary.circle.radius': 17 + }; + + const query = proxyquire('../../../query/structured_geocoding', { + 'pelias-query': { + layout: { + StructuredFallbackQuery: MockQuery + }, + view: views, + Vars: require('pelias-query').Vars + }, + './search_defaults': { }, + './text_parser': () => { + t.fail('text_parser should not have been called'); } + })(clean); + + t.notOk(query.body.vs.isset('boundary:circle:lat')); + t.notOk(query.body.vs.isset('boundary:circle:lon')); + t.notOk(query.body.vs.isset('boundary:circle:radius')); + + t.end(); + + }); + + test('missing boundary.circle.lon shouldn\'t set boundary:circle fields', t => { + const clean = { + text: 'text value', + sources: 'sources value', + layers: 'layers value', + 'boundary.circle.lat': 12.121212, + 'boundary.circle.radius': 17 }; - var query = generate(clean); + const query = proxyquire('../../../query/structured_geocoding', { + 'pelias-query': { + layout: { + StructuredFallbackQuery: MockQuery + }, + view: views, + Vars: require('pelias-query').Vars + }, + './search_defaults': { }, + './text_parser': () => { + t.fail('text_parser should not have been called'); + } + })(clean); - var compiled = JSON.parse(JSON.stringify(query)); - var expected = require('../fixture/structured_geocoding/postalcode_only'); + t.notOk(query.body.vs.isset('boundary:circle:lat')); + t.notOk(query.body.vs.isset('boundary:circle:lon')); + t.notOk(query.body.vs.isset('boundary:circle:radius')); - t.deepEqual(compiled.type, 'fallback', 'query type set'); - t.deepEqual(compiled.body, expected, 'structured postalcode only'); t.end(); }); - test('valid boundary.country search', function(t) { - var clean = { - parsed_text: { + test('missing boundary.circle.radius should set lat/lon but not radius', t => { + const clean = { + text: 'text value', + sources: 'sources value', + layers: 'layers value', + 'boundary.circle.lat': 12.121212, + 'boundary.circle.lon': 21.212121 + }; + + const query = proxyquire('../../../query/structured_geocoding', { + 'pelias-query': { + layout: { + StructuredFallbackQuery: MockQuery + }, + view: views, + Vars: require('pelias-query').Vars }, - text: 'test', querySize: 10, - layers: ['test'], - 'boundary.country': 'ABC' + './search_defaults': { }, + './text_parser': () => { + t.fail('text_parser should not have been called'); + } + })(clean); + + t.equals(query.body.vs.var('boundary:circle:lat').toString(), 12.121212); + t.equals(query.body.vs.var('boundary:circle:lon').toString(), 21.212121); + t.notOk(query.body.vs.isset('boundary:circle:radius')); + + t.end(); + + }); + + test('boundary.circle.* should set lat/lon/radius with last rounded and in kilometers', t => { + const clean = { + text: 'text value', + sources: 'sources value', + layers: 'layers value', + 'boundary.circle.lat': 12.121212, + 'boundary.circle.lon': 21.212121, + 'boundary.circle.radius': 17.5 }; - var query = generate(clean); + const query = proxyquire('../../../query/structured_geocoding', { + 'pelias-query': { + layout: { + StructuredFallbackQuery: MockQuery + }, + view: views, + Vars: require('pelias-query').Vars + }, + './search_defaults': { }, + './text_parser': () => { + t.fail('text_parser should not have been called'); + } + })(clean); - var compiled = JSON.parse( JSON.stringify( query ) ); - var expected = require('../fixture/structured_geocoding/boundary_country'); + t.equals(query.body.vs.var('boundary:circle:lat').toString(), 12.121212); + t.equals(query.body.vs.var('boundary:circle:lon').toString(), 21.212121); + t.equals(query.body.vs.var('boundary:circle:radius').toString(), '18km'); - t.deepEqual(compiled.type, 'fallback', 'query type set'); - t.deepEqual(compiled.body, expected, 'search: valid boundary.country query'); t.end(); + }); - test('valid sources filter', function(t) { - var clean = { - parsed_text: { + test('missing boundary.circle.lat/lon but existing boundary.circle.radius should not set any', t => { + const clean = { + text: 'text value', + sources: 'sources value', + layers: 'layers value', + 'boundary.circle.radius': 17 + }; + + const query = proxyquire('../../../query/structured_geocoding', { + 'pelias-query': { + layout: { + StructuredFallbackQuery: MockQuery + }, + view: views, + Vars: require('pelias-query').Vars }, - 'text': 'test', - 'sources': ['test_source'] + './search_defaults': { }, + './text_parser': () => { + t.fail('text_parser should not have been called'); + } + })(clean); + + t.notOk(query.body.vs.isset('boundary:circle:lat')); + t.notOk(query.body.vs.isset('boundary:circle:lon')); + t.notOk(query.body.vs.isset('boundary:circle:radius')); + + t.end(); + + }); + +}; + +module.exports.tests.boundary_country = (test, common) => { + test('boundary.country available shoul set boundary:country', t => { + const clean = { + text: 'text value', + sources: 'sources value', + layers: 'layers value', + 'boundary.country': 'boundary country value' }; - var query = generate(clean); + const query = proxyquire('../../../query/structured_geocoding', { + 'pelias-query': { + layout: { + StructuredFallbackQuery: MockQuery + }, + view: views, + Vars: require('pelias-query').Vars + }, + './search_defaults': { }, + './text_parser': () => { + t.fail('text_parser should not have been called'); + } + })(clean); - var compiled = JSON.parse( JSON.stringify( query ) ); - var expected = require('../fixture/structured_geocoding/with_source_filtering'); + t.equals(query.body.vs.var('boundary:country').toString(), 'boundary country value'); - t.deepEqual(compiled.type, 'fallback', 'query type set'); - t.deepEqual(compiled.body, expected, 'search: valid search query with source filtering'); t.end(); + }); }; -module.exports.all = function (tape, common) { +module.exports.all = (tape, common) => { function test(name, testFunction) { - return tape('structured_geocoding query ' + name, testFunction); + return tape(`structured_geocoding query ${name}`, testFunction); } for( var testCase in module.exports.tests ){ From 1f3393a5c35e7b98c430ce0c270740a70c94a3fd Mon Sep 17 00:00:00 2001 From: "greenkeeper[bot]" Date: Tue, 10 Oct 2017 21:28:52 +0000 Subject: [PATCH 03/23] fix(package): update pelias-logger to version 0.2.1 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index fd567c54..dbd2409a 100644 --- a/package.json +++ b/package.json @@ -57,7 +57,7 @@ "pelias-categories": "1.2.0", "pelias-config": "2.12.1", "pelias-labels": "1.7.0", - "pelias-logger": "0.2.0", + "pelias-logger": "0.2.1", "pelias-microservice-wrapper": "1.2.1", "pelias-model": "5.1.0", "pelias-query": "9.1.0", From 5d7a0bfd1c3a42876b9c6149b6c181affe2bf71a Mon Sep 17 00:00:00 2001 From: "greenkeeper[bot]" Date: Wed, 11 Oct 2017 21:14:44 +0000 Subject: [PATCH 04/23] fix(package): update pelias-text-analyzer to version 1.9.5 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index fd567c54..9fc08508 100644 --- a/package.json +++ b/package.json @@ -62,7 +62,7 @@ "pelias-model": "5.1.0", "pelias-query": "9.1.0", "pelias-sorting": "1.0.1", - "pelias-text-analyzer": "1.9.2", + "pelias-text-analyzer": "1.9.5", "predicates": "^1.0.1", "retry": "^0.10.1", "stats-lite": "^2.0.4", From 2ec1af485603ed84c04251c99edc692a0a8f2631 Mon Sep 17 00:00:00 2001 From: "greenkeeper[bot]" Date: Sat, 14 Oct 2017 15:38:58 +0000 Subject: [PATCH 05/23] fix(package): update joi to version 12.0.0 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index dbd2409a..455e8371 100644 --- a/package.json +++ b/package.json @@ -49,7 +49,7 @@ "geolib": "^2.0.18", "iso-639-3": "^1.0.0", "iso3166-1": "^0.3.0", - "joi": "^11.0.1", + "joi": "^12.0.0", "locale": "^0.1.0", "lodash": "^4.17.4", "markdown": "0.5.0", From b7f110cfa46b6598335f013c401e1a3409a67d0a Mon Sep 17 00:00:00 2001 From: "greenkeeper[bot]" Date: Sat, 14 Oct 2017 17:47:29 +0000 Subject: [PATCH 06/23] fix(package): update predicates to version 2.0.0 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index dbd2409a..97297515 100644 --- a/package.json +++ b/package.json @@ -63,7 +63,7 @@ "pelias-query": "9.1.0", "pelias-sorting": "1.0.1", "pelias-text-analyzer": "1.9.2", - "predicates": "^1.0.1", + "predicates": "^2.0.0", "retry": "^0.10.1", "stats-lite": "^2.0.4", "through2": "^2.0.3" From 95551abb9d0e83be00aa5ecf2376e845d6049f40 Mon Sep 17 00:00:00 2001 From: Stephen Hess Date: Sun, 15 Oct 2017 22:02:07 -0400 Subject: [PATCH 07/23] updated for new location of interpolation service --- README.md | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 73e48d0d..4253d15e 100644 --- a/README.md +++ b/README.md @@ -44,7 +44,7 @@ The API recognizes the following properties under the top-level `api` key in you |`indexName`|*no*|*pelias*|name of the Elasticsearch index to be used when building queries| |`relativeScores`|*no*|true|if set to true, confidence scores will be normalized, realistically at this point setting this to false is not tested or desirable |`accessLog`|*no*||name of the format to use for access logs; may be any one of the [predefined values](https://github.com/expressjs/morgan#predefined-formats) in the `morgan` package. Defaults to `"common"`; if set to `false`, or an otherwise falsy value, disables access-logging entirely.| -|`services`|*no*||service definitions for [point-in-polygon](https://github.com/pelias/pip-service) and [placholder](https://github.com/pelias/placeholder) services. If missing (which is not recommended), the point-in-polygon and placeholder services will not be called.| +|`services`|*no*||service definitions for [point-in-polygon](https://github.com/pelias/pip-service), and [placeholder](https://github.com/pelias/placeholder), and [interpolation](https://github.com/pelias/interpolation) services. If missing (which is not recommended), the services will not be called.| |`defaultParameters.focus.point.lon`
`defaultParameters.focus.point.lat`|no | |default coordinates for focus point Example configuration file would look something like this: @@ -73,6 +73,10 @@ Example configuration file would look something like this: }, "placeholder": { "url": "http://myplaceholderservice.com:5000" + }, + "interpolation": { + "url": "http://myinterpolationservice.com:3000", + "timeout": 2500 } } "defaultParameters": { @@ -80,12 +84,6 @@ Example configuration file would look something like this: "focus.point.lon": 21.212121 } }, - "interpolation": { - "client": { - "adapter": "http", - "host": "internal-pelias-interpolation-dev-130430937.us-east-1.elb.amazonaws.com" - } - }, "logger": { "level": "debug" } From 66d4149f6a2f1741d7cc08a53db40da4d3bf4960 Mon Sep 17 00:00:00 2001 From: Stephen Hess Date: Mon, 16 Oct 2017 11:42:46 -0400 Subject: [PATCH 08/23] removed references to outdated request parameters --- .../fixture/search_linguistic_viewport.js | 71 ------------------- ...search_linguistic_viewport_min_diagonal.js | 71 ------------------- test/unit/query/search.js | 48 ------------- 3 files changed, 190 deletions(-) delete mode 100644 test/unit/fixture/search_linguistic_viewport.js delete mode 100644 test/unit/fixture/search_linguistic_viewport_min_diagonal.js diff --git a/test/unit/fixture/search_linguistic_viewport.js b/test/unit/fixture/search_linguistic_viewport.js deleted file mode 100644 index e30751ea..00000000 --- a/test/unit/fixture/search_linguistic_viewport.js +++ /dev/null @@ -1,71 +0,0 @@ -module.exports = { - 'query': { - 'function_score': { - 'query': { - 'bool': { - 'minimum_should_match': 1, - 'should': [ - { - 'bool': { - '_name': 'fallback.street', - 'boost': 5, - 'must': [ - { - 'match_phrase': { - 'address_parts.street': 'street value' - } - } - ], - 'should': [], - 'filter': { - 'term': { - 'layer': 'street' - } - } - } - } - ], - 'filter': { - 'bool': { - 'must': [ - { - 'terms': { - 'layer': [ - 'test' - ] - } - } - ] - } - } - } - }, - 'max_boost': 20, - 'functions': [ - { - 'field_value_factor': { - 'modifier': 'log1p', - 'field': 'popularity', - 'missing': 1 - }, - 'weight': 1 - }, - { - 'field_value_factor': { - 'modifier': 'log1p', - 'field': 'population', - 'missing': 1 - }, - 'weight': 2 - } - ], - 'score_mode': 'avg', - 'boost_mode': 'multiply' - } - }, - 'size': 10, - 'track_scores': true, - 'sort': [ - '_score' - ] -}; diff --git a/test/unit/fixture/search_linguistic_viewport_min_diagonal.js b/test/unit/fixture/search_linguistic_viewport_min_diagonal.js deleted file mode 100644 index e30751ea..00000000 --- a/test/unit/fixture/search_linguistic_viewport_min_diagonal.js +++ /dev/null @@ -1,71 +0,0 @@ -module.exports = { - 'query': { - 'function_score': { - 'query': { - 'bool': { - 'minimum_should_match': 1, - 'should': [ - { - 'bool': { - '_name': 'fallback.street', - 'boost': 5, - 'must': [ - { - 'match_phrase': { - 'address_parts.street': 'street value' - } - } - ], - 'should': [], - 'filter': { - 'term': { - 'layer': 'street' - } - } - } - } - ], - 'filter': { - 'bool': { - 'must': [ - { - 'terms': { - 'layer': [ - 'test' - ] - } - } - ] - } - } - } - }, - 'max_boost': 20, - 'functions': [ - { - 'field_value_factor': { - 'modifier': 'log1p', - 'field': 'popularity', - 'missing': 1 - }, - 'weight': 1 - }, - { - 'field_value_factor': { - 'modifier': 'log1p', - 'field': 'population', - 'missing': 1 - }, - 'weight': 2 - } - ], - 'score_mode': 'avg', - 'boost_mode': 'multiply' - } - }, - 'size': 10, - 'track_scores': true, - 'sort': [ - '_score' - ] -}; diff --git a/test/unit/query/search.js b/test/unit/query/search.js index c97fb876..ea8a4f17 100644 --- a/test/unit/query/search.js +++ b/test/unit/query/search.js @@ -99,54 +99,6 @@ module.exports.tests.query = function(test, common) { t.end(); }); - test('search search + viewport', function(t) { - var clean = { - parsed_text: { - street: 'street value' - }, - text: 'test', querySize: 10, - 'focus.viewport.min_lat': 28.49136, - 'focus.viewport.max_lat': 30.49136, - 'focus.viewport.min_lon': -87.50622, - 'focus.viewport.max_lon': -77.50622, - layers: ['test'] - }; - - var query = generate(clean); - - var compiled = JSON.parse( JSON.stringify( query ) ); - var expected = require('../fixture/search_linguistic_viewport'); - - t.deepEqual(compiled.type, 'fallback', 'query type set'); - t.deepEqual(compiled.body, expected, 'search_linguistic_viewport'); - t.end(); - }); - - // viewport scale sizing currently disabled. - // ref: https://github.com/pelias/api/pull/388 - test('search with viewport diagonal < 1km should set scale to 1km', function(t) { - var clean = { - parsed_text: { - street: 'street value' - }, - text: 'test', querySize: 10, - 'focus.viewport.min_lat': 28.49135, - 'focus.viewport.max_lat': 28.49137, - 'focus.viewport.min_lon': -87.50622, - 'focus.viewport.max_lon': -87.50624, - layers: ['test'] - }; - - var query = generate(clean); - - var compiled = JSON.parse( JSON.stringify( query ) ); - var expected = require('../fixture/search_linguistic_viewport_min_diagonal'); - - t.deepEqual(compiled.type, 'fallback', 'query type set'); - t.deepEqual(compiled.body, expected, 'valid search query'); - t.end(); - }); - test('search search + focus on null island', function(t) { var clean = { parsed_text: { From cb8f4017e55c2b7c6647d23e655e0e59d7af8da5 Mon Sep 17 00:00:00 2001 From: Stephen Hess Date: Mon, 16 Oct 2017 12:01:59 -0400 Subject: [PATCH 09/23] use host from HOST environmental variable --- index.js | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index 2bd4dcf5..4fffd846 100644 --- a/index.js +++ b/index.js @@ -1,7 +1,8 @@ -var app = require('./app'), - port = ( process.env.PORT || 3100 ); +const app = require('./app'), + port = ( process.env.PORT || 3100 ), + host = ( process.env.HOST || 'localhost' ); /** run server on the default setup (single core) **/ -console.log( 'pelias is now running on port ' + port ); -app.listen( port ); +console.log( `pelias is now running on ${host}:${port}` ); +app.listen( port, host ); From 9df6817652dd6499825c2e697eae692099da0806 Mon Sep 17 00:00:00 2001 From: "greenkeeper[bot]" Date: Tue, 17 Oct 2017 13:51:17 +0000 Subject: [PATCH 10/23] fix(package): update pelias-config to version 2.13.0 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 57629514..cc30ff90 100644 --- a/package.json +++ b/package.json @@ -55,7 +55,7 @@ "markdown": "0.5.0", "morgan": "^1.8.2", "pelias-categories": "1.2.0", - "pelias-config": "2.12.1", + "pelias-config": "2.13.0", "pelias-labels": "1.7.0", "pelias-logger": "0.2.1", "pelias-microservice-wrapper": "1.2.1", From 68eec006098d4398c4ed7660efa5d3d6fdef88ae Mon Sep 17 00:00:00 2001 From: "greenkeeper[bot]" Date: Tue, 17 Oct 2017 15:05:28 +0000 Subject: [PATCH 11/23] fix(package): update pelias-model to version 5.2.0 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 57629514..5bbc3155 100644 --- a/package.json +++ b/package.json @@ -59,7 +59,7 @@ "pelias-labels": "1.7.0", "pelias-logger": "0.2.1", "pelias-microservice-wrapper": "1.2.1", - "pelias-model": "5.1.0", + "pelias-model": "5.2.0", "pelias-query": "9.1.0", "pelias-sorting": "1.0.1", "pelias-text-analyzer": "1.9.5", From b7984a9e6acfbcfa0bb5c68b7acc8cd4c2790f5b Mon Sep 17 00:00:00 2001 From: "greenkeeper[bot]" Date: Tue, 17 Oct 2017 18:42:53 +0000 Subject: [PATCH 12/23] fix(package): update pelias-logger to version 0.3.0 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index a86318a7..b59d8aa4 100644 --- a/package.json +++ b/package.json @@ -57,7 +57,7 @@ "pelias-categories": "1.2.0", "pelias-config": "2.13.0", "pelias-labels": "1.7.0", - "pelias-logger": "0.2.1", + "pelias-logger": "0.3.0", "pelias-microservice-wrapper": "1.2.1", "pelias-model": "5.2.0", "pelias-query": "9.1.0", From d8ddfdc174a2b700b0f2a52149aa9e9d65dd5960 Mon Sep 17 00:00:00 2001 From: "greenkeeper[bot]" Date: Tue, 17 Oct 2017 20:02:30 +0000 Subject: [PATCH 13/23] fix(package): update pelias-text-analyzer to version 1.10.0 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index b59d8aa4..2d50759c 100644 --- a/package.json +++ b/package.json @@ -62,7 +62,7 @@ "pelias-model": "5.2.0", "pelias-query": "9.1.0", "pelias-sorting": "1.0.1", - "pelias-text-analyzer": "1.9.5", + "pelias-text-analyzer": "1.10.0", "predicates": "^2.0.0", "retry": "^0.10.1", "stats-lite": "^2.0.4", From d7e4ebdafb01a4b052981c375c6e521c201a1b57 Mon Sep 17 00:00:00 2001 From: "greenkeeper[bot]" Date: Fri, 20 Oct 2017 18:14:45 +0000 Subject: [PATCH 14/23] chore(package): update dependencies --- package.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 2d50759c..703658b4 100644 --- a/package.json +++ b/package.json @@ -49,7 +49,7 @@ "geolib": "^2.0.18", "iso-639-3": "^1.0.0", "iso3166-1": "^0.3.0", - "joi": "^12.0.0", + "joi": "^13.0.1", "locale": "^0.1.0", "lodash": "^4.17.4", "markdown": "0.5.0", @@ -62,7 +62,7 @@ "pelias-model": "5.2.0", "pelias-query": "9.1.0", "pelias-sorting": "1.0.1", - "pelias-text-analyzer": "1.10.0", + "pelias-text-analyzer": "1.10.1", "predicates": "^2.0.0", "retry": "^0.10.1", "stats-lite": "^2.0.4", From 393a51703d68c3461497fb57e664bf5e66b36819 Mon Sep 17 00:00:00 2001 From: Stephen Hess Date: Fri, 20 Oct 2017 15:20:39 -0400 Subject: [PATCH 15/23] revert joi version, keep pelias-text-analyzer --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 703658b4..c9d0b018 100644 --- a/package.json +++ b/package.json @@ -49,7 +49,7 @@ "geolib": "^2.0.18", "iso-639-3": "^1.0.0", "iso3166-1": "^0.3.0", - "joi": "^13.0.1", + "joi": "^12.0.0", "locale": "^0.1.0", "lodash": "^4.17.4", "markdown": "0.5.0", From ca993823947afd95376a212e4521d08abd4ab1f9 Mon Sep 17 00:00:00 2001 From: "greenkeeper[bot]" Date: Mon, 23 Oct 2017 13:23:57 +0000 Subject: [PATCH 16/23] fix(package): update pelias-text-analyzer to version 1.10.2 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index c9d0b018..147c87e4 100644 --- a/package.json +++ b/package.json @@ -62,7 +62,7 @@ "pelias-model": "5.2.0", "pelias-query": "9.1.0", "pelias-sorting": "1.0.1", - "pelias-text-analyzer": "1.10.1", + "pelias-text-analyzer": "1.10.2", "predicates": "^2.0.0", "retry": "^0.10.1", "stats-lite": "^2.0.4", From f9281190ac1da0d1c67ba8d54b144453262e1d11 Mon Sep 17 00:00:00 2001 From: Julian Simioni Date: Wed, 18 Oct 2017 11:35:32 -0400 Subject: [PATCH 17/23] Corrrectly filter /place queries by layer `/place` queries have been executing in a way where only the ID, but not the layer, has been considered when returning records from Elasticsearch. It turns out this bug was introduced almost a year and a half ago in https://github.com/pelias/api/pull/407. A little, relatively unimportant bit of code was looking for a property called `layers`: ``` const cmd = req.clean.ids.map( function(id) { return { _index: apiConfig.indexName, _type: id.layers, _id: id.id }; }); ``` The correct property was `layer`, so no filtering on layer was done in the resulting [mget](https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-multi-get.html) query. There was never an acceptance test for these sorts of queries, but there is now one in https://github.com/pelias/acceptance-tests/pull/446. The unit tests were enforcing the incorrect behavior. Fixes https://github.com/pelias/pelias/issues/643 --- controller/place.js | 2 +- test/unit/controller/place.js | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/controller/place.js b/controller/place.js index 2b5490aa..51ce58d9 100644 --- a/controller/place.js +++ b/controller/place.js @@ -38,7 +38,7 @@ function setup( apiConfig, esclient ){ const cmd = req.clean.ids.map( function(id) { return { _index: apiConfig.indexName, - _type: id.layers, + _type: id.layer, _id: id.id }; }); diff --git a/test/unit/controller/place.js b/test/unit/controller/place.js index 25d99463..b76d69c9 100644 --- a/test/unit/controller/place.js +++ b/test/unit/controller/place.js @@ -51,11 +51,11 @@ module.exports.tests.success = (test, common) => { ids: [ { id: 'id1', - layers: 'layer1' + layer: 'layer1' }, { id: 'id2', - layers: 'layer2' + layer: 'layer2' } ] }, From 110a258130c102ca8689da467282f171fb245785 Mon Sep 17 00:00:00 2001 From: Stephen Hess Date: Mon, 23 Oct 2017 11:14:29 -0400 Subject: [PATCH 18/23] added initial tests for using vs --- test/unit/query/MockQuery.js | 7 + test/unit/query/reverse.js | 335 +++++++++++++++++++---------------- test/unit/run.js | 188 ++++++++++---------- 3 files changed, 287 insertions(+), 243 deletions(-) diff --git a/test/unit/query/MockQuery.js b/test/unit/query/MockQuery.js index f97bfb27..e7202cc9 100644 --- a/test/unit/query/MockQuery.js +++ b/test/unit/query/MockQuery.js @@ -3,6 +3,7 @@ module.exports = class MockQuery { constructor() { this._score_functions = []; + this._sort_functions = []; this._filter_functions = []; } @@ -10,6 +11,7 @@ module.exports = class MockQuery { return { vs: vs, score_functions: this._score_functions, + sort_functions: this._sort_functions, filter_functions: this._filter_functions }; } @@ -19,6 +21,11 @@ module.exports = class MockQuery { return this; } + sort(view) { + this._sort_functions.push(view); + return this; + } + filter(view) { this._filter_functions.push(view); return this; diff --git a/test/unit/query/reverse.js b/test/unit/query/reverse.js index eab97188..0e47eaed 100644 --- a/test/unit/query/reverse.js +++ b/test/unit/query/reverse.js @@ -1,190 +1,227 @@ -var generate = require('../../../query/reverse'); +const generate = require('../../../query/reverse'); +const _ = require('lodash'); +const proxyquire = require('proxyquire').noCallThru(); +const MockQuery = require('./MockQuery'); + +// helper for canned views +const views = { + boundary_country: 'boundary_country view', + boundary_circle: 'boundary_circle view', + sources: 'sources view', + layers: 'layers view', + categories: 'categories view', + sort_distance: 'sort_distance view' +}; 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 generate, 'function', 'valid function'); t.end(); }); }; -module.exports.tests.query = function(test, common) { - test('valid query', function(t) { - var query = generate({ - 'point.lat': 29.49136, - 'point.lon': -82.50622, - 'boundary.circle.lat': 29.49136, - 'boundary.circle.lon': -82.50622, - 'boundary.circle.radius': 3 - }); - - var compiled = JSON.parse( JSON.stringify( query ) ); - var expected = require('../fixture/reverse_standard'); - - t.deepEqual(compiled.type, 'reverse', 'query type set'); - t.deepEqual(compiled.body, expected, 'reverse_standard'); +module.exports.tests.query = (test, common) => { + test('base no frills', t => { + const clean = {}; + + const query = proxyquire('../../../query/reverse', { + 'pelias-query': { + layout: { + FilteredBooleanQuery: MockQuery + }, + view: views, + Vars: require('pelias-query').Vars + }, + './reverse_defaults': { + default_parameter_1: 'first default parameter', + default_parameter_2: 'second default parameter' + } + })(clean); + + t.equals(query.type, 'reverse', 'query type set'); + t.deepEquals(query.body.vs.var('default_parameter_1').toString(), 'first default parameter'); + t.deepEquals(query.body.vs.var('default_parameter_2').toString(), 'second default parameter'); + t.notOk(query.body.vs.isset('size')); + t.notOk(query.body.vs.isset('sources')); + t.notOk(query.body.vs.isset('layers')); + t.notOk(query.body.vs.isset('focus:point:lat')); + t.notOk(query.body.vs.isset('focus:point:lon')); + t.notOk(query.body.vs.isset('boundary:circle:lat')); + t.notOk(query.body.vs.isset('boundary:circle:lon')); + t.notOk(query.body.vs.isset('boundary:circle:radius')); + t.notOk(query.body.vs.isset('boundary:country')); + t.notOk(query.body.vs.isset('input:categories')); + + t.deepEquals(query.body.score_functions, [ + 'boundary_country view' + ]); + + t.deepEquals(query.body.filter_functions, [ + 'boundary_circle view', + 'sources view', + 'layers view', + 'categories view' + ]); + + t.deepEquals(query.body.sort_functions, [ + 'sort_distance view' + ]); + t.end(); - }); - test('valid query - null island', function(t) { - var query = generate({ - 'point.lat': 0, - 'point.lon': 0, - 'boundary.circle.lat': 0, - 'boundary.circle.lon': 0, - 'boundary.circle.radius': 3 - }); + }); - var compiled = JSON.parse( JSON.stringify( query ) ); - var expected = require('../fixture/reverse_null_island'); + test('clean.querySize should set size parameter', t => { + const clean = { + querySize: 17 + }; - t.deepEqual(compiled.type, 'reverse', 'query type set'); - t.deepEqual(compiled.body, expected, 'reverse_null_island'); + const query = proxyquire('../../../query/reverse', { + 'pelias-query': { + layout: { + FilteredBooleanQuery: MockQuery + }, + view: views, + Vars: require('pelias-query').Vars + }, + './reverse_defaults': {} + })(clean); + + t.deepEquals(query.body.vs.var('size').toString(), 17); t.end(); + }); - test('valid query with radius', function(t) { - var query = generate({ - 'point.lat': 29.49136, - 'point.lon': -82.50622, - 'boundary.circle.lat': 29.49136, - 'boundary.circle.lon': -82.50622, - 'boundary.circle.radius': 123 - }); +}; - var compiled = JSON.parse( JSON.stringify( query ) ); - var expected = '123km'; +module.exports.tests.sources = (test, common) => { + test('non-array clean.sources should not set sources in vs', t => { + const clean = { + sources: 'this is not an array' + }; - t.deepEqual(compiled.type, 'reverse', 'query type set'); - t.deepEqual(compiled.body.query.bool.filter[0].geo_distance.distance, expected, 'distance set to boundary circle radius'); + const query = proxyquire('../../../query/reverse', { + 'pelias-query': { + layout: { + FilteredBooleanQuery: MockQuery + }, + view: views, + Vars: require('pelias-query').Vars + }, + './reverse_defaults': {} + })(clean); + + t.notOk(query.body.vs.isset('sources')); t.end(); - }); - // for coarse reverse cases where boundary circle radius isn't used - test('undefined radius set to default radius', function(t) { - var query = generate({ - 'point.lat': 12.12121, - 'point.lon': 21.21212, - 'boundary.circle.lat': 12.12121, - 'boundary.circle.lon': 21.21212 - }); + }); - var compiled = JSON.parse( JSON.stringify( query ) ); - var expected = '1km'; + test('empty array clean.sources should not set sources in vs', t => { + const clean = { + sources: [] + }; - t.deepEqual(compiled.type, 'reverse', 'query type set'); - t.deepEqual(compiled.body.query.bool.filter[0].geo_distance.distance, expected, 'distance set to default boundary circle radius'); + const query = proxyquire('../../../query/reverse', { + 'pelias-query': { + layout: { + FilteredBooleanQuery: MockQuery + }, + view: views, + Vars: require('pelias-query').Vars + }, + './reverse_defaults': {} + })(clean); + + t.notOk(query.body.vs.isset('sources')); t.end(); + }); - test('boundary.circle lat/lon/radius - overrides point.lat/lon when set', function(t) { - var clean = { - 'point.lat': 29.49136, - 'point.lon': -82.50622, - 'boundary.circle.lat': 111, - 'boundary.circle.lon': 333, - 'boundary.circle.radius': 3 + test('non-empty array clean.sources should set sources in vs', t => { + const clean = { + sources: ['source 1', 'source 2'] }; - var query = generate(clean); - var compiled = JSON.parse( JSON.stringify( query ) ); - - // this should not equal `point.lat` and `point.lon` as it was explitely specified - var expected = { lat: clean['boundary.circle.lat'], lon: clean['boundary.circle.lon'] }; - var centroid = compiled.body.query.bool.filter[0].geo_distance.center_point; - t.deepEqual(compiled.type, 'reverse', 'query type set'); - t.deepEqual(centroid, expected, 'reverse: boundary.circle/lon overrides point.lat/lon'); + const query = proxyquire('../../../query/reverse', { + 'pelias-query': { + layout: { + FilteredBooleanQuery: MockQuery + }, + view: views, + Vars: require('pelias-query').Vars + }, + './reverse_defaults': {} + })(clean); + + t.deepEquals(query.body.vs.var('sources').toString(), ['source 1', 'source 2']); t.end(); - }); - test('size fuzz test', function(t) { - // test different sizes - var sizes = [1,4,20,undefined,null]; - var expected = [1,4,20,1,1]; - sizes.forEach( function( size, index ){ - var query = generate({ - 'point.lat': 29.49136, 'point.lon': -82.50622, querySize: size - }); - - var compiled = JSON.parse( JSON.stringify( query ) ); - t.equal( compiled.body.size, expected[index], 'valid reverse query for size: '+ size); - }); - t.end(); }); - test('valid boundary.country reverse search', function(t) { - var query = generate({ - 'point.lat': 29.49136, - 'point.lon': -82.50622, - 'boundary.circle.lat': 29.49136, - 'boundary.circle.lon': -82.50622, - 'boundary.circle.radius': 3, - 'boundary.country': 'ABC' - }); - - var compiled = JSON.parse( JSON.stringify( query ) ); - var expected = require('../fixture/reverse_with_boundary_country'); - - t.deepEqual(compiled.type, 'reverse', 'query type set'); - t.deepEqual(compiled.body, expected, 'valid reverse query with boundary.country'); - t.end(); - }); +}; + +module.exports.tests.layers = (test, common) => { + test('non-array clean.layers should not set sources in vs', t => { + const clean = { + layers: 'this is not an array' + }; - test('valid sources filter', function(t) { - var query = generate({ - 'point.lat': 29.49136, - 'point.lon': -82.50622, - 'boundary.circle.lat': 29.49136, - 'boundary.circle.lon': -82.50622, - 'boundary.circle.radius': 3, - 'sources': ['test'] - }); - - var compiled = JSON.parse( JSON.stringify( query ) ); - var expected = require('../fixture/reverse_with_source_filtering'); - - t.deepEqual(compiled.type, 'reverse', 'query type set'); - t.deepEqual(compiled.body, expected, 'valid reverse query with source filtering'); + const query = proxyquire('../../../query/reverse', { + 'pelias-query': { + layout: { + FilteredBooleanQuery: MockQuery + }, + view: views, + Vars: require('pelias-query').Vars + }, + './reverse_defaults': {} + })(clean); + + t.notOk(query.body.vs.isset('layers')); t.end(); + }); - test('valid layers filter', (t) => { - const query = generate({ - 'point.lat': 29.49136, - 'point.lon': -82.50622, - 'boundary.circle.lat': 29.49136, - 'boundary.circle.lon': -82.50622, - 'boundary.circle.radius': 3, - // only venue, address, and street layers should be retained - 'layers': ['neighbourhood', 'venue', 'locality', 'address', 'region', 'street', 'country'] - }); - - const compiled = JSON.parse( JSON.stringify( query ) ); - const expected = require('../fixture/reverse_with_layer_filtering'); - - t.deepEqual(compiled.type, 'reverse', 'query type set'); - t.deepEqual(compiled.body, expected, 'valid reverse query with source filtering'); + test('empty array clean.layers should not set sources in vs', t => { + const clean = { + layers: [] + }; + + const query = proxyquire('../../../query/reverse', { + 'pelias-query': { + layout: { + FilteredBooleanQuery: MockQuery + }, + view: views, + Vars: require('pelias-query').Vars + }, + './reverse_defaults': {} + })(clean); + + t.notOk(query.body.vs.isset('layers')); t.end(); }); - test('valid layers filter - subset of non-coarse layers', (t) => { - const query = generate({ - 'point.lat': 29.49136, - 'point.lon': -82.50622, - 'boundary.circle.lat': 29.49136, - 'boundary.circle.lon': -82.50622, - 'boundary.circle.radius': 3, - // only venue, address, and street layers should be retained - 'layers': ['neighbourhood', 'venue', 'street', 'locality'] - }); - - const compiled = JSON.parse( JSON.stringify( query ) ); - const expected = require('../fixture/reverse_with_layer_filtering_non_coarse_subset'); - - t.deepEqual(compiled.type, 'reverse', 'query type set'); - t.deepEqual(compiled.body, expected, 'valid reverse query with source filtering'); + test('non-empty array clean.layers should only set non-coarse layers in vs', t => { + const clean = { + sources: ['source 1', 'source 2'] + }; + + const query = proxyquire('../../../query/reverse', { + 'pelias-query': { + layout: { + FilteredBooleanQuery: MockQuery + }, + view: views, + Vars: require('pelias-query').Vars + }, + './reverse_defaults': {} + })(clean); + + t.deepEquals(query.body.vs.var('sources').toString(), ['source 1', 'source 2']); t.end(); }); diff --git a/test/unit/run.js b/test/unit/run.js index 5631b9f3..cfe60255 100644 --- a/test/unit/run.js +++ b/test/unit/run.js @@ -9,101 +9,101 @@ var common = { }; var tests = [ - require('./app'), - require('./schema'), - require('./controller/coarse_reverse'), - require('./controller/index'), - require('./controller/libpostal'), - require('./controller/place'), - require('./controller/placeholder'), - require('./controller/search'), - require('./controller/search_with_ids'), - require('./controller/predicates/has_parsed_text_properties'), - require('./controller/predicates/has_request_parameter'), - require('./controller/predicates/has_response_data'), - require('./controller/predicates/has_results_at_layers'), - require('./controller/predicates/has_request_parameter'), - require('./controller/predicates/has_request_errors'), - require('./controller/predicates/is_addressit_parse'), - require('./controller/predicates/is_admin_only_analysis'), - require('./controller/predicates/is_coarse_reverse'), - require('./controller/predicates/is_only_non_admin_layers'), - require('./controller/predicates/is_request_sources_only_whosonfirst'), - require('./helper/debug'), - require('./helper/diffPlaces'), - require('./helper/geojsonify_place_details'), - require('./helper/geojsonify'), - require('./helper/logging'), - require('./helper/type_mapping'), - require('./helper/sizeCalculator'), - require('./helper/stackTraceLine'), - require('./middleware/access_log'), - require('./middleware/accuracy'), - require('./middleware/assignLabels'), - require('./middleware/confidenceScore'), - require('./middleware/confidenceScoreFallback'), - require('./middleware/confidenceScoreReverse'), - require('./middleware/changeLanguage'), - require('./middleware/distance'), - require('./middleware/interpolate'), - require('./middleware/localNamingConventions'), - require('./middleware/dedupe'), - require('./middleware/parseBBox'), - require('./middleware/sendJSON'), - require('./middleware/normalizeParentIds'), - require('./middleware/sortResponseData'), - require('./middleware/trimByGranularity'), - require('./middleware/trimByGranularityStructured'), - require('./middleware/requestLanguage'), - require('./query/address_search_using_ids'), - require('./query/autocomplete'), - require('./query/autocomplete_defaults'), - require('./query/search_defaults'), - require('./query/reverse_defaults'), + // require('./app'), + // require('./schema'), + // require('./controller/coarse_reverse'), + // require('./controller/index'), + // require('./controller/libpostal'), + // require('./controller/place'), + // require('./controller/placeholder'), + // require('./controller/search'), + // require('./controller/search_with_ids'), + // require('./controller/predicates/has_parsed_text_properties'), + // require('./controller/predicates/has_request_parameter'), + // require('./controller/predicates/has_response_data'), + // require('./controller/predicates/has_results_at_layers'), + // require('./controller/predicates/has_request_parameter'), + // require('./controller/predicates/has_request_errors'), + // require('./controller/predicates/is_addressit_parse'), + // require('./controller/predicates/is_admin_only_analysis'), + // require('./controller/predicates/is_coarse_reverse'), + // require('./controller/predicates/is_only_non_admin_layers'), + // require('./controller/predicates/is_request_sources_only_whosonfirst'), + // require('./helper/debug'), + // require('./helper/diffPlaces'), + // require('./helper/geojsonify_place_details'), + // require('./helper/geojsonify'), + // require('./helper/logging'), + // require('./helper/type_mapping'), + // require('./helper/sizeCalculator'), + // require('./helper/stackTraceLine'), + // require('./middleware/access_log'), + // require('./middleware/accuracy'), + // require('./middleware/assignLabels'), + // require('./middleware/confidenceScore'), + // require('./middleware/confidenceScoreFallback'), + // require('./middleware/confidenceScoreReverse'), + // require('./middleware/changeLanguage'), + // require('./middleware/distance'), + // require('./middleware/interpolate'), + // require('./middleware/localNamingConventions'), + // require('./middleware/dedupe'), + // require('./middleware/parseBBox'), + // require('./middleware/sendJSON'), + // require('./middleware/normalizeParentIds'), + // require('./middleware/sortResponseData'), + // require('./middleware/trimByGranularity'), + // require('./middleware/trimByGranularityStructured'), + // require('./middleware/requestLanguage'), + // require('./query/address_search_using_ids'), + // require('./query/autocomplete'), + // require('./query/autocomplete_defaults'), + // require('./query/search_defaults'), + // require('./query/reverse_defaults'), require('./query/reverse'), - require('./query/search'), - require('./query/search_original'), - require('./query/structured_geocoding'), - require('./query/text_parser'), - require('./sanitizer/_boundary_country'), - require('./sanitizer/_debug'), - require('./sanitizer/_flag_bool'), - require('./sanitizer/_geonames_deprecation'), - require('./sanitizer/_geonames_warnings'), - require('./sanitizer/_geo_common'), - require('./sanitizer/_geo_reverse'), - require('./sanitizer/_groups'), - require('./sanitizer/_ids'), - require('./sanitizer/_iso2_to_iso3'), - require('./sanitizer/_layers'), - require('./sanitizer/_location_bias'), - require('./sanitizer/_city_name_standardizer'), - require('./sanitizer/_request_language'), - require('./sanitizer/_single_scalar_parameters'), - require('./sanitizer/_size'), - require('./sanitizer/_sources'), - require('./sanitizer/_sources_and_layers'), - require('./sanitizer/_synthesize_analysis'), - require('./sanitizer/_text'), - require('./sanitizer/_text_addressit'), - require('./sanitizer/_tokenizer'), - require('./sanitizer/_deprecate_quattroshapes'), - require('./sanitizer/_categories'), - require('./sanitizer/nearby'), - require('./sanitizer/autocomplete'), - require('./sanitizer/structured_geocoding'), - require('./sanitizer/place'), - require('./sanitizer/reverse'), - require('./sanitizer/sanitizeAll'), - require('./sanitizer/search'), - require('./sanitizer/defer_to_addressit'), - require('./sanitizer/wrap'), - require('./service/configurations/Interpolation'), - require('./service/configurations/Language'), - require('./service/configurations/PlaceHolder'), - require('./service/configurations/PointInPolygon'), - require('./service/mget'), - require('./service/search') + // require('./query/search'), + // require('./query/search_original'), + // require('./query/structured_geocoding'), + // require('./query/text_parser'), + // require('./sanitizer/_boundary_country'), + // require('./sanitizer/_debug'), + // require('./sanitizer/_flag_bool'), + // require('./sanitizer/_geonames_deprecation'), + // require('./sanitizer/_geonames_warnings'), + // require('./sanitizer/_geo_common'), + // require('./sanitizer/_geo_reverse'), + // require('./sanitizer/_groups'), + // require('./sanitizer/_ids'), + // require('./sanitizer/_iso2_to_iso3'), + // require('./sanitizer/_layers'), + // require('./sanitizer/_location_bias'), + // require('./sanitizer/_city_name_standardizer'), + // require('./sanitizer/_request_language'), + // require('./sanitizer/_single_scalar_parameters'), + // require('./sanitizer/_size'), + // require('./sanitizer/_sources'), + // require('./sanitizer/_sources_and_layers'), + // require('./sanitizer/_synthesize_analysis'), + // require('./sanitizer/_text'), + // require('./sanitizer/_text_addressit'), + // require('./sanitizer/_tokenizer'), + // require('./sanitizer/_deprecate_quattroshapes'), + // require('./sanitizer/_categories'), + // require('./sanitizer/nearby'), + // require('./sanitizer/autocomplete'), + // require('./sanitizer/structured_geocoding'), + // require('./sanitizer/place'), + // require('./sanitizer/reverse'), + // require('./sanitizer/sanitizeAll'), + // require('./sanitizer/search'), + // require('./sanitizer/defer_to_addressit'), + // require('./sanitizer/wrap'), + // require('./service/configurations/Interpolation'), + // require('./service/configurations/Language'), + // require('./service/configurations/PlaceHolder'), + // require('./service/configurations/PointInPolygon'), + // require('./service/mget'), + // require('./service/search') ]; tests.map(function(t) { From 4d3ea56a397455ec819066ba1dd73c98c3a07503 Mon Sep 17 00:00:00 2001 From: Julian Simioni Date: Mon, 23 Oct 2017 13:00:03 -0400 Subject: [PATCH 19/23] Change default listen parameter to undefined This will ensure our default listen address is whatever the Node.js default is. It's currently set to listen on all addresses, and probably won't change. --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index 4fffd846..baefb894 100644 --- a/index.js +++ b/index.js @@ -1,7 +1,7 @@ const app = require('./app'), port = ( process.env.PORT || 3100 ), - host = ( process.env.HOST || 'localhost' ); + host = ( process.env.HOST || undefined ); /** run server on the default setup (single core) **/ console.log( `pelias is now running on ${host}:${port}` ); From 65507807acfe25da6b2db379cd72b01e3b304e62 Mon Sep 17 00:00:00 2001 From: Julian Simioni Date: Mon, 23 Oct 2017 13:03:10 -0400 Subject: [PATCH 20/23] Ask Node's `net.server` for the actual address its listening on This allows us to emit a helpful and correct message when Pelias starts. --- index.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index baefb894..f541c0cc 100644 --- a/index.js +++ b/index.js @@ -3,6 +3,8 @@ const app = require('./app'), port = ( process.env.PORT || 3100 ), host = ( process.env.HOST || undefined ); -/** run server on the default setup (single core) **/ -console.log( `pelias is now running on ${host}:${port}` ); -app.listen( port, host ); +const server = app.listen( port, host, () => { + // ask server for the actual address and port its listening on + const listenAddress = server.address(); + console.log( `pelias is now running on ${listenAddress.address}:${listenAddress.port}` ); +}); From d577d42d9feb019ae1f0982f147e78039abcfd73 Mon Sep 17 00:00:00 2001 From: Julian Simioni Date: Mon, 23 Oct 2017 14:58:54 -0400 Subject: [PATCH 21/23] Support geonames non-coarse reverse in deprecation sanitizer --- sanitizer/_geonames_deprecation.js | 15 +++++++- test/unit/sanitizer/_geonames_deprecation.js | 40 +++++++++++++++++--- 2 files changed, 48 insertions(+), 7 deletions(-) diff --git a/sanitizer/_geonames_deprecation.js b/sanitizer/_geonames_deprecation.js index b3cc8185..f6608e96 100644 --- a/sanitizer/_geonames_deprecation.js +++ b/sanitizer/_geonames_deprecation.js @@ -1,14 +1,25 @@ const _ = require('lodash'); /** -with the release of coarse reverse as a separate thing and ES reverse only -handling venues, addresses, and streets, geonames make no sense in the reverse context + * Now that we have the pip-service, we have stopped supporting returning Geonames for coarse reverse. + * + * However, until the `/nearby` endpoint is finalized, we still want to support Geonames for + * _non-coarse_ reverse. **/ function _sanitize( raw, clean, opts ) { // error & warning messages const messages = { errors: [], warnings: [] }; + // return taking no action unless this is a coarse-only reverse request + const non_coarse_layers = ['address', 'street', 'venue']; + const is_coarse_reverse = !_.isEmpty(clean.layers) && + _.isEmpty(_.intersection(clean.layers, non_coarse_layers)); + + if (!is_coarse_reverse) { + return messages; + } + if (_.isEqual(clean.sources, ['geonames']) || _.isEqual(clean.sources, ['gn'])) { messages.errors.push('/reverse does not support geonames'); diff --git a/test/unit/sanitizer/_geonames_deprecation.js b/test/unit/sanitizer/_geonames_deprecation.js index 41bd86c6..707c6774 100644 --- a/test/unit/sanitizer/_geonames_deprecation.js +++ b/test/unit/sanitizer/_geonames_deprecation.js @@ -27,13 +27,26 @@ module.exports.tests.no_warnings_or_errors_conditions = (test, common) => { }); + test('geonames/gn in sources with layers=venue should add neither warnings nor errors', (t) => { + const clean = { + sources: ['geonames'], + layers: ['venue'] + }; + + const messages = sanitizer.sanitize(undefined, clean); + + t.deepEquals(clean.sources, ['geonames']); + t.deepEquals(messages, { errors: [], warnings: [] }); + t.end(); + }); }; module.exports.tests.error_conditions = (test, common) => { - test('only geonames in sources should not modify clean.sources and add error message', (t) => { + test('only geonames in sources with layers=coarse should not modify clean.sources and add error message', (t) => { ['gn', 'geonames'].forEach((sources) => { const clean = { - sources: [sources] + sources: [sources], + layers: ['coarse'] }; const messages = sanitizer.sanitize(undefined, clean); @@ -45,16 +58,33 @@ module.exports.tests.error_conditions = (test, common) => { }); t.end(); - }); + test('only geonames in sources with layers=locality should not modify clean.sources and add error message', (t) => { + ['gn', 'geonames'].forEach((sources) => { + const clean = { + sources: [sources], + layers: ['locality'] + }; + + const messages = sanitizer.sanitize(undefined, clean); + + t.deepEquals(clean.sources, [sources]); + t.deepEquals(messages.errors, ['/reverse does not support geonames']); + t.deepEquals(messages.warnings, []); + + }); + + t.end(); + }); }; module.exports.tests.warning_conditions = (test, common) => { - test('only geonames in sources should not modify clean.sources and add error message', (t) => { + test('only geonames in sources and layers=coarse should not modify clean.sources and add error message', (t) => { ['gn', 'geonames'].forEach((sources) => { const clean = { - sources: ['another source', sources, 'yet another source'] + sources: ['another source', sources, 'yet another source'], + layers: ['coarse'] }; const messages = sanitizer.sanitize(undefined, clean); From 004e3c02b125d49487b369e4ffe4168079b82738 Mon Sep 17 00:00:00 2001 From: Stephen Hess Date: Mon, 23 Oct 2017 15:13:50 -0400 Subject: [PATCH 22/23] completed switch to vs tests over fixtures --- test/unit/fixture/reverse_null_island.js | 39 --- test/unit/fixture/reverse_standard.js | 39 --- .../fixture/reverse_with_boundary_country.js | 49 --- .../fixture/reverse_with_layer_filtering.js | 41 --- ..._with_layer_filtering_non_coarse_subset.js | 41 --- .../fixture/reverse_with_source_filtering.js | 46 --- test/unit/query/reverse.js | 280 +++++++++++++++++- test/unit/run.js | 188 ++++++------ 8 files changed, 372 insertions(+), 351 deletions(-) delete mode 100644 test/unit/fixture/reverse_null_island.js delete mode 100644 test/unit/fixture/reverse_standard.js delete mode 100644 test/unit/fixture/reverse_with_boundary_country.js delete mode 100644 test/unit/fixture/reverse_with_layer_filtering.js delete mode 100644 test/unit/fixture/reverse_with_layer_filtering_non_coarse_subset.js delete mode 100644 test/unit/fixture/reverse_with_source_filtering.js diff --git a/test/unit/fixture/reverse_null_island.js b/test/unit/fixture/reverse_null_island.js deleted file mode 100644 index 874518d4..00000000 --- a/test/unit/fixture/reverse_null_island.js +++ /dev/null @@ -1,39 +0,0 @@ -var vs = require('../../../query/reverse_defaults'); - -module.exports = { - 'query': { - 'bool': { - 'filter': [{ - 'geo_distance': { - 'distance': '3km', - 'distance_type': 'plane', - 'optimize_bbox': 'indexed', - 'center_point': { - 'lat': 0, - 'lon': 0 - } - } - }, - { - 'terms': { - 'layer': ['venue', 'address', 'street'] - } - }] - } - }, - 'sort': [ - '_score', - { - '_geo_distance': { - 'center_point': { - 'lat': 0, - 'lon': 0 - }, - 'order': 'asc', - 'distance_type': 'plane' - } - } - ], - 'size': vs.size, - 'track_scores': true -}; diff --git a/test/unit/fixture/reverse_standard.js b/test/unit/fixture/reverse_standard.js deleted file mode 100644 index 4a477ce1..00000000 --- a/test/unit/fixture/reverse_standard.js +++ /dev/null @@ -1,39 +0,0 @@ -var vs = require('../../../query/reverse_defaults'); - -module.exports = { - 'query': { - 'bool': { - 'filter': [{ - 'geo_distance': { - 'distance': '3km', - 'distance_type': 'plane', - 'optimize_bbox': 'indexed', - 'center_point': { - 'lat': 29.49136, - 'lon': -82.50622 - } - } - }, - { - 'terms': { - 'layer': ['venue', 'address', 'street'] - } - }] - } - }, - 'sort': [ - '_score', - { - '_geo_distance': { - 'center_point': { - 'lat': 29.49136, - 'lon': -82.50622 - }, - 'order': 'asc', - 'distance_type': 'plane' - } - } - ], - 'size': vs.size, - 'track_scores': true -}; diff --git a/test/unit/fixture/reverse_with_boundary_country.js b/test/unit/fixture/reverse_with_boundary_country.js deleted file mode 100644 index 6677247a..00000000 --- a/test/unit/fixture/reverse_with_boundary_country.js +++ /dev/null @@ -1,49 +0,0 @@ -var vs = require('../../../query/reverse_defaults'); - -module.exports = { - 'query': { - 'bool': { - 'must': [ - { - 'match': { - 'parent.country_a': { - 'analyzer': 'standard', - 'query': 'ABC' - } - } - } - ], - 'filter': [{ - 'geo_distance': { - 'distance': '3km', - 'distance_type': 'plane', - 'optimize_bbox': 'indexed', - 'center_point': { - 'lat': 29.49136, - 'lon': -82.50622 - } - } - }, - { - 'terms': { - 'layer': ['venue', 'address', 'street'] - } - }] - } - }, - 'sort': [ - '_score', - { - '_geo_distance': { - 'center_point': { - 'lat': 29.49136, - 'lon': -82.50622 - }, - 'order': 'asc', - 'distance_type': 'plane' - } - } - ], - 'size': vs.size, - 'track_scores': true -}; diff --git a/test/unit/fixture/reverse_with_layer_filtering.js b/test/unit/fixture/reverse_with_layer_filtering.js deleted file mode 100644 index 5d6c53b5..00000000 --- a/test/unit/fixture/reverse_with_layer_filtering.js +++ /dev/null @@ -1,41 +0,0 @@ -var vs = require('../../../query/reverse_defaults'); - -module.exports = { - 'query': { - 'bool': { - 'filter': [ - { - 'geo_distance': { - 'distance': '3km', - 'distance_type': 'plane', - 'optimize_bbox': 'indexed', - 'center_point': { - 'lat': 29.49136, - 'lon': -82.50622 - } - } - }, - { - 'terms': { - 'layer': ['venue', 'address', 'street'] - } - } - ] - } - }, - 'sort': [ - '_score', - { - '_geo_distance': { - 'center_point': { - 'lat': 29.49136, - 'lon': -82.50622 - }, - 'order': 'asc', - 'distance_type': 'plane' - } - } - ], - 'size': vs.size, - 'track_scores': true -}; diff --git a/test/unit/fixture/reverse_with_layer_filtering_non_coarse_subset.js b/test/unit/fixture/reverse_with_layer_filtering_non_coarse_subset.js deleted file mode 100644 index aba18b08..00000000 --- a/test/unit/fixture/reverse_with_layer_filtering_non_coarse_subset.js +++ /dev/null @@ -1,41 +0,0 @@ -var vs = require('../../../query/reverse_defaults'); - -module.exports = { - 'query': { - 'bool': { - 'filter': [ - { - 'geo_distance': { - 'distance': '3km', - 'distance_type': 'plane', - 'optimize_bbox': 'indexed', - 'center_point': { - 'lat': 29.49136, - 'lon': -82.50622 - } - } - }, - { - 'terms': { - 'layer': ['venue', 'street'] - } - } - ] - } - }, - 'sort': [ - '_score', - { - '_geo_distance': { - 'center_point': { - 'lat': 29.49136, - 'lon': -82.50622 - }, - 'order': 'asc', - 'distance_type': 'plane' - } - } - ], - 'size': vs.size, - 'track_scores': true -}; diff --git a/test/unit/fixture/reverse_with_source_filtering.js b/test/unit/fixture/reverse_with_source_filtering.js deleted file mode 100644 index b2bcb0d9..00000000 --- a/test/unit/fixture/reverse_with_source_filtering.js +++ /dev/null @@ -1,46 +0,0 @@ -var vs = require('../../../query/reverse_defaults'); - -module.exports = { - 'query': { - 'bool': { - 'filter': [ - { - 'geo_distance': { - 'distance': '3km', - 'distance_type': 'plane', - 'optimize_bbox': 'indexed', - 'center_point': { - 'lat': 29.49136, - 'lon': -82.50622 - } - } - }, - { - 'terms': { - 'source': ['test'] - } - }, - { - 'terms': { - 'layer': ['venue', 'address', 'street'] - } - } - ] - } - }, - 'sort': [ - '_score', - { - '_geo_distance': { - 'center_point': { - 'lat': 29.49136, - 'lon': -82.50622 - }, - 'order': 'asc', - 'distance_type': 'plane' - } - } - ], - 'size': vs.size, - 'track_scores': true -}; diff --git a/test/unit/query/reverse.js b/test/unit/query/reverse.js index 0e47eaed..78c98e0c 100644 --- a/test/unit/query/reverse.js +++ b/test/unit/query/reverse.js @@ -2,6 +2,7 @@ const generate = require('../../../query/reverse'); const _ = require('lodash'); const proxyquire = require('proxyquire').noCallThru(); const MockQuery = require('./MockQuery'); +const all_layers = require('../../../helper/type_mapping').layers; // helper for canned views const views = { @@ -207,7 +208,7 @@ module.exports.tests.layers = (test, common) => { test('non-empty array clean.layers should only set non-coarse layers in vs', t => { const clean = { - sources: ['source 1', 'source 2'] + layers: all_layers }; const query = proxyquire('../../../query/reverse', { @@ -221,7 +222,282 @@ module.exports.tests.layers = (test, common) => { './reverse_defaults': {} })(clean); - t.deepEquals(query.body.vs.var('sources').toString(), ['source 1', 'source 2']); + t.deepEquals(query.body.vs.var('layers').toString(), ['address', 'venue', 'street']); + t.end(); + + }); + +}; + +module.exports.tests.focus_point = (test, common) => { + test('numeric point.lat and non-numeric point.lon should not add focus:point:* fields', t => { + const clean = { + 'point.lat': 12.121212, + 'point.lon': 'this is non-numeric' + }; + + const query = proxyquire('../../../query/reverse', { + 'pelias-query': { + layout: { + FilteredBooleanQuery: MockQuery + }, + view: views, + Vars: require('pelias-query').Vars + }, + './reverse_defaults': {} + })(clean); + + t.notOk(query.body.vs.isset('focus:point:lat')); + t.notOk(query.body.vs.isset('focus:point:lon')); + t.end(); + + }); + + test('non-numeric point.lat and numeric point.lon should not add focus:point:* fields', t => { + const clean = { + 'point.lat': 'this is non-numeric', + 'point.lon': 21.212121 + }; + + const query = proxyquire('../../../query/reverse', { + 'pelias-query': { + layout: { + FilteredBooleanQuery: MockQuery + }, + view: views, + Vars: require('pelias-query').Vars + }, + './reverse_defaults': {} + })(clean); + + t.notOk(query.body.vs.isset('focus:point:lat')); + t.notOk(query.body.vs.isset('focus:point:lon')); + t.end(); + + }); + + test('numeric point.lat and point.lon should add focus:point:* fields', t => { + const clean = { + 'point.lat': 12.121212, + 'point.lon': 21.212121 + }; + + const query = proxyquire('../../../query/reverse', { + 'pelias-query': { + layout: { + FilteredBooleanQuery: MockQuery + }, + view: views, + Vars: require('pelias-query').Vars + }, + './reverse_defaults': {} + })(clean); + + t.deepEquals(query.body.vs.var('focus:point:lat').toString(), 12.121212); + t.deepEquals(query.body.vs.var('focus:point:lon').toString(), 21.212121); + t.end(); + + }); + +}; + +module.exports.tests.boundary_circle = (test, common) => { + test('numeric lat and non-numeric lon should not add boundary:circle:* fields', t => { + const clean = { + 'boundary.circle.lat': 12.121212, + 'boundary.circle.lon': 'this is non-numeric' + }; + + const query = proxyquire('../../../query/reverse', { + 'pelias-query': { + layout: { + FilteredBooleanQuery: MockQuery + }, + view: views, + Vars: require('pelias-query').Vars + }, + './reverse_defaults': {} + })(clean); + + t.notOk(query.body.vs.isset('boundary:circle:lat')); + t.notOk(query.body.vs.isset('boundary:circle:lon')); + t.notOk(query.body.vs.isset('boundary:circle:radius')); + t.end(); + + }); + + test('non-numeric lat and numeric lon should not add boundary:circle:* fields', t => { + const clean = { + 'boundary.circle.lat': 'this is non-numeric', + 'boundary.circle.lon': 21.212121 + }; + + const query = proxyquire('../../../query/reverse', { + 'pelias-query': { + layout: { + FilteredBooleanQuery: MockQuery + }, + view: views, + Vars: require('pelias-query').Vars + }, + './reverse_defaults': {} + })(clean); + + t.notOk(query.body.vs.isset('boundary:circle:lat')); + t.notOk(query.body.vs.isset('boundary:circle:lon')); + t.notOk(query.body.vs.isset('boundary:circle:radius')); + t.end(); + + }); + + test('radius not supplied should default to value from reverse_defaults', t => { + const clean = { + 'boundary.circle.lat': 12.121212, + 'boundary.circle.lon': 21.212121 + }; + + const query = proxyquire('../../../query/reverse', { + 'pelias-query': { + layout: { + FilteredBooleanQuery: MockQuery + }, + view: views, + Vars: require('pelias-query').Vars + }, + './reverse_defaults': { + 'boundary:circle:radius': 17 + } + })(clean); + + t.deepEquals(query.body.vs.var('boundary:circle:lat').toString(), 12.121212); + t.deepEquals(query.body.vs.var('boundary:circle:lon').toString(), 21.212121); + t.deepEquals(query.body.vs.var('boundary:circle:radius').toString(), 17); + t.end(); + + }); + + test('numeric radius supplied should be used instead of value from reverse_defaults', t => { + const clean = { + 'boundary.circle.lat': 12.121212, + 'boundary.circle.lon': 21.212121, + 'boundary.circle.radius': 17 + }; + + const query = proxyquire('../../../query/reverse', { + 'pelias-query': { + layout: { + FilteredBooleanQuery: MockQuery + }, + view: views, + Vars: require('pelias-query').Vars + }, + './reverse_defaults': { + 'boundary:circle:radius': 18 + } + })(clean); + + t.deepEquals(query.body.vs.var('boundary:circle:lat').toString(), 12.121212); + t.deepEquals(query.body.vs.var('boundary:circle:lon').toString(), 21.212121); + t.deepEquals(query.body.vs.var('boundary:circle:radius').toString(), '17km'); + t.end(); + + }); + + test('non-numeric radius supplied should not set any boundary:circle:radius', t => { + const clean = { + 'boundary.circle.lat': 12.121212, + 'boundary.circle.lon': 21.212121, + 'boundary.circle.radius': 'this is non-numeric' + }; + + const query = proxyquire('../../../query/reverse', { + 'pelias-query': { + layout: { + FilteredBooleanQuery: MockQuery + }, + view: views, + Vars: require('pelias-query').Vars + }, + './reverse_defaults': { + 'boundary:circle:radius': 18 + } + })(clean); + + t.deepEquals(query.body.vs.var('boundary:circle:lat').toString(), 12.121212); + t.deepEquals(query.body.vs.var('boundary:circle:lon').toString(), 21.212121); + t.deepEquals(query.body.vs.var('boundary:circle:radius').toString(), 18); + t.end(); + + }); + +}; + +module.exports.tests.boundary_country = (test, common) => { + test('non-string boundary.country should not set boundary:country', t => { + [17, undefined, {}, [], true, null].forEach(value => { + const clean = { + 'boundary.country': value + }; + + const query = proxyquire('../../../query/reverse', { + 'pelias-query': { + layout: { + FilteredBooleanQuery: MockQuery + }, + view: views, + Vars: require('pelias-query').Vars + }, + './reverse_defaults': {} + })(clean); + + t.notOk(query.body.vs.isset('boundary:country')); + }); + + t.end(); + + }); + + test('string boundary.country should set boundary:country', t => { + const clean = { + 'boundary.country': 'boundary country value' + }; + + const query = proxyquire('../../../query/reverse', { + 'pelias-query': { + layout: { + FilteredBooleanQuery: MockQuery + }, + view: views, + Vars: require('pelias-query').Vars + }, + './reverse_defaults': {} + })(clean); + + t.deepEquals(query.body.vs.var('boundary:country').toString(), 'boundary country value'); + t.end(); + + }); + +}; + +module.exports.tests.categories = (test, common) => { + test('categories supplied should set input:categories', t => { + const clean = { + categories: 'categories value' + }; + + const query = proxyquire('../../../query/reverse', { + 'pelias-query': { + layout: { + FilteredBooleanQuery: MockQuery + }, + view: views, + Vars: require('pelias-query').Vars + }, + './reverse_defaults': {} + })(clean); + + t.deepEquals(query.body.vs.var('input:categories').toString(), 'categories value'); t.end(); }); diff --git a/test/unit/run.js b/test/unit/run.js index cfe60255..5631b9f3 100644 --- a/test/unit/run.js +++ b/test/unit/run.js @@ -9,101 +9,101 @@ var common = { }; var tests = [ - // require('./app'), - // require('./schema'), - // require('./controller/coarse_reverse'), - // require('./controller/index'), - // require('./controller/libpostal'), - // require('./controller/place'), - // require('./controller/placeholder'), - // require('./controller/search'), - // require('./controller/search_with_ids'), - // require('./controller/predicates/has_parsed_text_properties'), - // require('./controller/predicates/has_request_parameter'), - // require('./controller/predicates/has_response_data'), - // require('./controller/predicates/has_results_at_layers'), - // require('./controller/predicates/has_request_parameter'), - // require('./controller/predicates/has_request_errors'), - // require('./controller/predicates/is_addressit_parse'), - // require('./controller/predicates/is_admin_only_analysis'), - // require('./controller/predicates/is_coarse_reverse'), - // require('./controller/predicates/is_only_non_admin_layers'), - // require('./controller/predicates/is_request_sources_only_whosonfirst'), - // require('./helper/debug'), - // require('./helper/diffPlaces'), - // require('./helper/geojsonify_place_details'), - // require('./helper/geojsonify'), - // require('./helper/logging'), - // require('./helper/type_mapping'), - // require('./helper/sizeCalculator'), - // require('./helper/stackTraceLine'), - // require('./middleware/access_log'), - // require('./middleware/accuracy'), - // require('./middleware/assignLabels'), - // require('./middleware/confidenceScore'), - // require('./middleware/confidenceScoreFallback'), - // require('./middleware/confidenceScoreReverse'), - // require('./middleware/changeLanguage'), - // require('./middleware/distance'), - // require('./middleware/interpolate'), - // require('./middleware/localNamingConventions'), - // require('./middleware/dedupe'), - // require('./middleware/parseBBox'), - // require('./middleware/sendJSON'), - // require('./middleware/normalizeParentIds'), - // require('./middleware/sortResponseData'), - // require('./middleware/trimByGranularity'), - // require('./middleware/trimByGranularityStructured'), - // require('./middleware/requestLanguage'), - // require('./query/address_search_using_ids'), - // require('./query/autocomplete'), - // require('./query/autocomplete_defaults'), - // require('./query/search_defaults'), - // require('./query/reverse_defaults'), + require('./app'), + require('./schema'), + require('./controller/coarse_reverse'), + require('./controller/index'), + require('./controller/libpostal'), + require('./controller/place'), + require('./controller/placeholder'), + require('./controller/search'), + require('./controller/search_with_ids'), + require('./controller/predicates/has_parsed_text_properties'), + require('./controller/predicates/has_request_parameter'), + require('./controller/predicates/has_response_data'), + require('./controller/predicates/has_results_at_layers'), + require('./controller/predicates/has_request_parameter'), + require('./controller/predicates/has_request_errors'), + require('./controller/predicates/is_addressit_parse'), + require('./controller/predicates/is_admin_only_analysis'), + require('./controller/predicates/is_coarse_reverse'), + require('./controller/predicates/is_only_non_admin_layers'), + require('./controller/predicates/is_request_sources_only_whosonfirst'), + require('./helper/debug'), + require('./helper/diffPlaces'), + require('./helper/geojsonify_place_details'), + require('./helper/geojsonify'), + require('./helper/logging'), + require('./helper/type_mapping'), + require('./helper/sizeCalculator'), + require('./helper/stackTraceLine'), + require('./middleware/access_log'), + require('./middleware/accuracy'), + require('./middleware/assignLabels'), + require('./middleware/confidenceScore'), + require('./middleware/confidenceScoreFallback'), + require('./middleware/confidenceScoreReverse'), + require('./middleware/changeLanguage'), + require('./middleware/distance'), + require('./middleware/interpolate'), + require('./middleware/localNamingConventions'), + require('./middleware/dedupe'), + require('./middleware/parseBBox'), + require('./middleware/sendJSON'), + require('./middleware/normalizeParentIds'), + require('./middleware/sortResponseData'), + require('./middleware/trimByGranularity'), + require('./middleware/trimByGranularityStructured'), + require('./middleware/requestLanguage'), + require('./query/address_search_using_ids'), + require('./query/autocomplete'), + require('./query/autocomplete_defaults'), + require('./query/search_defaults'), + require('./query/reverse_defaults'), require('./query/reverse'), - // require('./query/search'), - // require('./query/search_original'), - // require('./query/structured_geocoding'), - // require('./query/text_parser'), - // require('./sanitizer/_boundary_country'), - // require('./sanitizer/_debug'), - // require('./sanitizer/_flag_bool'), - // require('./sanitizer/_geonames_deprecation'), - // require('./sanitizer/_geonames_warnings'), - // require('./sanitizer/_geo_common'), - // require('./sanitizer/_geo_reverse'), - // require('./sanitizer/_groups'), - // require('./sanitizer/_ids'), - // require('./sanitizer/_iso2_to_iso3'), - // require('./sanitizer/_layers'), - // require('./sanitizer/_location_bias'), - // require('./sanitizer/_city_name_standardizer'), - // require('./sanitizer/_request_language'), - // require('./sanitizer/_single_scalar_parameters'), - // require('./sanitizer/_size'), - // require('./sanitizer/_sources'), - // require('./sanitizer/_sources_and_layers'), - // require('./sanitizer/_synthesize_analysis'), - // require('./sanitizer/_text'), - // require('./sanitizer/_text_addressit'), - // require('./sanitizer/_tokenizer'), - // require('./sanitizer/_deprecate_quattroshapes'), - // require('./sanitizer/_categories'), - // require('./sanitizer/nearby'), - // require('./sanitizer/autocomplete'), - // require('./sanitizer/structured_geocoding'), - // require('./sanitizer/place'), - // require('./sanitizer/reverse'), - // require('./sanitizer/sanitizeAll'), - // require('./sanitizer/search'), - // require('./sanitizer/defer_to_addressit'), - // require('./sanitizer/wrap'), - // require('./service/configurations/Interpolation'), - // require('./service/configurations/Language'), - // require('./service/configurations/PlaceHolder'), - // require('./service/configurations/PointInPolygon'), - // require('./service/mget'), - // require('./service/search') + require('./query/search'), + require('./query/search_original'), + require('./query/structured_geocoding'), + require('./query/text_parser'), + require('./sanitizer/_boundary_country'), + require('./sanitizer/_debug'), + require('./sanitizer/_flag_bool'), + require('./sanitizer/_geonames_deprecation'), + require('./sanitizer/_geonames_warnings'), + require('./sanitizer/_geo_common'), + require('./sanitizer/_geo_reverse'), + require('./sanitizer/_groups'), + require('./sanitizer/_ids'), + require('./sanitizer/_iso2_to_iso3'), + require('./sanitizer/_layers'), + require('./sanitizer/_location_bias'), + require('./sanitizer/_city_name_standardizer'), + require('./sanitizer/_request_language'), + require('./sanitizer/_single_scalar_parameters'), + require('./sanitizer/_size'), + require('./sanitizer/_sources'), + require('./sanitizer/_sources_and_layers'), + require('./sanitizer/_synthesize_analysis'), + require('./sanitizer/_text'), + require('./sanitizer/_text_addressit'), + require('./sanitizer/_tokenizer'), + require('./sanitizer/_deprecate_quattroshapes'), + require('./sanitizer/_categories'), + require('./sanitizer/nearby'), + require('./sanitizer/autocomplete'), + require('./sanitizer/structured_geocoding'), + require('./sanitizer/place'), + require('./sanitizer/reverse'), + require('./sanitizer/sanitizeAll'), + require('./sanitizer/search'), + require('./sanitizer/defer_to_addressit'), + require('./sanitizer/wrap'), + require('./service/configurations/Interpolation'), + require('./service/configurations/Language'), + require('./service/configurations/PlaceHolder'), + require('./service/configurations/PointInPolygon'), + require('./service/mget'), + require('./service/search') ]; tests.map(function(t) { From bc6401842c9046af3ea5984523f6b8d111becfb2 Mon Sep 17 00:00:00 2001 From: Julian Simioni Date: Mon, 23 Oct 2017 16:16:30 -0400 Subject: [PATCH 23/23] Add friendly, coarse specific error message to reverse This error message is updated to mention the coarse-reverse only nature of Geonames support removal. It also links to the [tracking ticket](https://github.com/pelias/acceptance-tests/pull/447) for Geonames removal which should help provide more context to users. --- sanitizer/_geonames_deprecation.js | 8 ++++---- test/unit/sanitizer/_geonames_deprecation.js | 8 +++++--- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/sanitizer/_geonames_deprecation.js b/sanitizer/_geonames_deprecation.js index f6608e96..2cdef1d1 100644 --- a/sanitizer/_geonames_deprecation.js +++ b/sanitizer/_geonames_deprecation.js @@ -7,6 +7,8 @@ const _ = require('lodash'); * _non-coarse_ reverse. **/ +const coarse_reverse_message ='coarse /reverse does not support geonames. See https://github.com/pelias/pelias/issues/675 for more info'; + function _sanitize( raw, clean, opts ) { // error & warning messages const messages = { errors: [], warnings: [] }; @@ -15,18 +17,16 @@ function _sanitize( raw, clean, opts ) { const non_coarse_layers = ['address', 'street', 'venue']; const is_coarse_reverse = !_.isEmpty(clean.layers) && _.isEmpty(_.intersection(clean.layers, non_coarse_layers)); - if (!is_coarse_reverse) { return messages; } if (_.isEqual(clean.sources, ['geonames']) || _.isEqual(clean.sources, ['gn'])) { - messages.errors.push('/reverse does not support geonames'); + messages.errors.push(coarse_reverse_message); } else if (_.includes(clean.sources, 'geonames') || _.includes(clean.sources, 'gn')) { clean.sources = _.without(clean.sources, 'geonames', 'gn'); - messages.warnings.push('/reverse does not support geonames'); - + messages.warnings.push(coarse_reverse_message); } return messages; diff --git a/test/unit/sanitizer/_geonames_deprecation.js b/test/unit/sanitizer/_geonames_deprecation.js index 707c6774..13461e15 100644 --- a/test/unit/sanitizer/_geonames_deprecation.js +++ b/test/unit/sanitizer/_geonames_deprecation.js @@ -2,6 +2,8 @@ const sanitizer = require('../../../sanitizer/_geonames_deprecation')(); module.exports.tests = {}; +const coarse_reverse_message ='coarse /reverse does not support geonames. See https://github.com/pelias/pelias/issues/675 for more info'; + module.exports.tests.no_warnings_or_errors_conditions = (test, common) => { test('undefined sources should add neither warnings nor errors', (t) => { const clean = {}; @@ -52,7 +54,7 @@ module.exports.tests.error_conditions = (test, common) => { const messages = sanitizer.sanitize(undefined, clean); t.deepEquals(clean.sources, [sources]); - t.deepEquals(messages.errors, ['/reverse does not support geonames']); + t.deepEquals(messages.errors, [ coarse_reverse_message ]); t.deepEquals(messages.warnings, []); }); @@ -70,7 +72,7 @@ module.exports.tests.error_conditions = (test, common) => { const messages = sanitizer.sanitize(undefined, clean); t.deepEquals(clean.sources, [sources]); - t.deepEquals(messages.errors, ['/reverse does not support geonames']); + t.deepEquals(messages.errors, [ coarse_reverse_message ]); t.deepEquals(messages.warnings, []); }); @@ -91,7 +93,7 @@ module.exports.tests.warning_conditions = (test, common) => { t.deepEquals(clean.sources, ['another source', 'yet another source']); t.deepEquals(messages.errors, []); - t.deepEquals(messages.warnings, ['/reverse does not support geonames']); + t.deepEquals(messages.warnings, [ coarse_reverse_message ]); });