From 7670eef3fb9d589eff4a65fedf9f24d89a175301 Mon Sep 17 00:00:00 2001 From: Stephen Hess Date: Fri, 18 Nov 2016 00:29:26 -0500 Subject: [PATCH] added support for city+country inputs --- query/search.js | 20 ++++- test/unit/query/search.js | 158 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 175 insertions(+), 3 deletions(-) diff --git a/query/search.js b/query/search.js index 945276e9..41e5775f 100644 --- a/query/search.js +++ b/query/search.js @@ -120,7 +120,7 @@ function generateQuery( clean ){ } function getQuery(vs) { - if (hasStreet(vs) || isCityStateOnlyWithOptionalCountry(vs)) { + if (hasStreet(vs) || isCityStateOnlyWithOptionalCountry(vs) || isCityCountryOnly(vs)) { return { type: 'fallback', body: fallbackQuery.render(vs) @@ -138,8 +138,8 @@ function hasStreet(vs) { } function isCityStateOnlyWithOptionalCountry(vs) { - var isSet = function(layer) { - return vs.isset('input:' + layer); + var isSet = (layer) => { + return vs.isset(`input:${layer}`); }; var allowedFields = ['locality', 'region']; @@ -150,4 +150,18 @@ function isCityStateOnlyWithOptionalCountry(vs) { } +function isCityCountryOnly(vs) { + var isSet = (layer) => { + return vs.isset(`input:${layer}`); + }; + + var allowedFields = ['locality', 'country']; + var disallowedFields = ['query', 'category', 'housenumber', 'street', + 'neighbourhood', 'borough', 'postcode', 'county', 'region']; + + return allowedFields.every(isSet) && + !disallowedFields.some(isSet); + +} + module.exports = generateQuery; diff --git a/test/unit/query/search.js b/test/unit/query/search.js index f16a79ae..1f312d0d 100644 --- a/test/unit/query/search.js +++ b/test/unit/query/search.js @@ -447,6 +447,164 @@ module.exports.tests.city_state = function(test, common) { }; +module.exports.tests.city_country = function(test, common) { + test('only city and country set should return query', function(t) { + var clean = { + parsed_text: { + 'city': 'city value', + 'country': 'country value' + } + }; + + var query = generate(clean); + + t.notEqual(query, undefined, 'should not have returned undefined'); + t.end(); + + }); + + test('city-only should return undefined', function(t) { + var clean = { + parsed_text: { + city: 'city value' + } + }; + + var query = generate(clean); + + t.equals(query, undefined, 'should have returned undefined'); + t.end(); + + }); + + test('country-only should return undefined', function(t) { + var clean = { + parsed_text: { + country: 'country value' + } + }; + + var query = generate(clean); + + t.equals(query, undefined, 'should have returned undefined'); + t.end(); + + }); + + test('city/country with query should return undefined', function(t) { + var clean = { + parsed_text: { + city: 'city value', + country: 'country value', + query: 'query value' + } + }; + + var query = generate(clean); + + t.equals(query, undefined, 'should have returned undefined'); + t.end(); + + }); + + test('city/country with category should return undefined', function(t) { + var clean = { + parsed_text: { + city: 'city value', + country: 'country value', + category: 'category value' + } + }; + + var query = generate(clean); + + t.equals(query, undefined, 'should have returned undefined'); + t.end(); + + }); + + test('city/country with number should return undefined', function(t) { + var clean = { + parsed_text: { + city: 'city value', + country: 'country value', + number: 'number value' + } + }; + + var query = generate(clean); + + t.equals(query, undefined, 'should have returned undefined'); + t.end(); + + }); + + test('city/country with neighbourhood should return undefined', function(t) { + var clean = { + parsed_text: { + city: 'city value', + country: 'country value', + neighbourhood: 'neighbourhood value' + } + }; + + var query = generate(clean); + + t.equals(query, undefined, 'should have returned undefined'); + t.end(); + + }); + + test('city/country with borough should return undefined', function(t) { + var clean = { + parsed_text: { + city: 'city value', + country: 'country value', + borough: 'borough value' + } + }; + + var query = generate(clean); + + t.equals(query, undefined, 'should have returned undefined'); + t.end(); + + }); + + test('city/country with postalcode should return undefined', function(t) { + var clean = { + parsed_text: { + city: 'city value', + country: 'country value', + postalcode: 'postalcode value' + } + }; + + var query = generate(clean); + + t.equals(query, undefined, 'should have returned undefined'); + t.end(); + + }); + + test('city/country with county should return undefined', function(t) { + var clean = { + parsed_text: { + city: 'city value', + country: 'country value', + county: 'county value' + } + }; + + var query = generate(clean); + + t.equals(query, undefined, 'should have returned undefined'); + t.end(); + + }); + +}; + module.exports.all = function (tape, common) { function test(name, testFunction) { return tape('search query ' + name, testFunction);