From 67360a4d0bf9ca8d7266e2df3521040029afcf61 Mon Sep 17 00:00:00 2001 From: Stephen Hess Date: Tue, 6 Jun 2017 16:13:39 -0400 Subject: [PATCH] added geonames_deprecation to reverse sanitizing --- query/reverse_defaults.js | 1 - sanitizer/_geonames_deprecation.js | 25 ++++++ sanitizer/reverse.js | 1 + test/unit/run.js | 1 + test/unit/sanitizer/_geonames_deprecation.js | 82 ++++++++++++++++++++ test/unit/sanitizer/nearby.js | 3 +- test/unit/sanitizer/reverse.js | 3 +- 7 files changed, 113 insertions(+), 3 deletions(-) create mode 100644 sanitizer/_geonames_deprecation.js create mode 100644 test/unit/sanitizer/_geonames_deprecation.js diff --git a/query/reverse_defaults.js b/query/reverse_defaults.js index d8b72292..bce72057 100644 --- a/query/reverse_defaults.js +++ b/query/reverse_defaults.js @@ -14,7 +14,6 @@ module.exports = _.merge({}, peliasQuery.defaults, { 'sort:distance:distance_type': 'plane', 'boundary:circle:radius': '1km', - 'boundary:circle:radius:coarse': '500km', 'boundary:circle:distance_type': 'plane', 'boundary:circle:optimize_bbox': 'indexed', diff --git a/sanitizer/_geonames_deprecation.js b/sanitizer/_geonames_deprecation.js new file mode 100644 index 00000000..ceb4e591 --- /dev/null +++ b/sanitizer/_geonames_deprecation.js @@ -0,0 +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 +**/ + +function sanitize( raw, clean, opts ) { + // error & warning messages + const messages = { errors: [], warnings: [] }; + + if (_.isEqual(clean.sources, ['geonames']) || _.isEqual(clean.sources, ['gn'])) { + messages.errors.push('/reverse does not support geonames'); + + } 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'); + + } + + return messages; + +} + +module.exports = sanitize; diff --git a/sanitizer/reverse.js b/sanitizer/reverse.js index 285ab56c..e5ef3272 100644 --- a/sanitizer/reverse.js +++ b/sanitizer/reverse.js @@ -8,6 +8,7 @@ var sanitizeAll = require('../sanitizer/sanitizeAll'), sources: require('../sanitizer/_targets')('sources', type_mapping.source_mapping), // depends on the layers and sources sanitizers, must be run after them sources_and_layers: require('../sanitizer/_sources_and_layers'), + geonames_deprecation: require('../sanitizer/_geonames_deprecation'), size: require('../sanitizer/_size')(/* use defaults*/), private: require('../sanitizer/_flag_bool')('private', false), geo_reverse: require('../sanitizer/_geo_reverse'), diff --git a/test/unit/run.js b/test/unit/run.js index c7647eee..92d258fa 100644 --- a/test/unit/run.js +++ b/test/unit/run.js @@ -56,6 +56,7 @@ var tests = [ require('./query/text_parser'), require('./sanitizer/_boundary_country'), require('./sanitizer/_flag_bool'), + require('./sanitizer/_geonames_deprecation'), require('./sanitizer/_geonames_warnings'), require('./sanitizer/_geo_common'), require('./sanitizer/_geo_reverse'), diff --git a/test/unit/sanitizer/_geonames_deprecation.js b/test/unit/sanitizer/_geonames_deprecation.js new file mode 100644 index 00000000..f7676db3 --- /dev/null +++ b/test/unit/sanitizer/_geonames_deprecation.js @@ -0,0 +1,82 @@ +const geonames_deprecation = require('../../../sanitizer/_geonames_deprecation'); + +module.exports.tests = {}; + +module.exports.tests.no_warnings_or_errors_conditions = (test, common) => { + test('undefined sources should add neither warnings nor errors', (t) => { + const clean = {}; + + const messages = geonames_deprecation(undefined, clean); + + t.deepEquals(clean, {}); + t.deepEquals(messages, { errors: [], warnings: [] }); + t.end(); + + }); + + test('geonames/gn not in sources should add neither warnings nor errors', (t) => { + const clean = { + sources: ['source 1', 'source 2'], + }; + + const messages = geonames_deprecation(undefined, clean); + + t.deepEquals(clean.sources, ['source 1', 'source 2']); + 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) => { + ['gn', 'geonames'].forEach((sources) => { + const clean = { + sources: [sources] + }; + + const messages = geonames_deprecation(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) => { + ['gn', 'geonames'].forEach((sources) => { + const clean = { + sources: ['another source', sources, 'yet another source'] + }; + + const messages = geonames_deprecation(undefined, clean); + + t.deepEquals(clean.sources, ['another source', 'yet another source']); + t.deepEquals(messages.errors, []); + t.deepEquals(messages.warnings, ['/reverse does not support geonames']); + + }); + + t.end(); + + }); + +}; + +module.exports.all = (tape, common) => { + function test(name, testFunction) { + return tape(`SANTIZE _geonames_deprecation ${name}`, testFunction); + } + + for( var testCase in module.exports.tests ){ + module.exports.tests[testCase](test, common); + } +}; diff --git a/test/unit/sanitizer/nearby.js b/test/unit/sanitizer/nearby.js index 4b5507e0..1ba41f67 100644 --- a/test/unit/sanitizer/nearby.js +++ b/test/unit/sanitizer/nearby.js @@ -31,7 +31,8 @@ module.exports.tests.interface = function(test, common) { module.exports.tests.sanitizers = function(test, common) { test('check sanitizer list', function (t) { var expected = ['singleScalarParameters', 'quattroshapes_deprecation', 'layers', - 'sources', 'sources_and_layers', 'size', 'private', 'geo_reverse', 'boundary_country', 'categories']; + 'sources', 'sources_and_layers', 'geonames_deprecation', 'size', 'private', + 'geo_reverse', 'boundary_country', 'categories']; t.deepEqual(Object.keys(nearby.sanitizer_list), expected); t.end(); }); diff --git a/test/unit/sanitizer/reverse.js b/test/unit/sanitizer/reverse.js index b3bbdb95..430d3ffa 100644 --- a/test/unit/sanitizer/reverse.js +++ b/test/unit/sanitizer/reverse.js @@ -37,7 +37,8 @@ module.exports.tests.interface = function(test, common) { module.exports.tests.sanitizers = function(test, common) { test('check sanitizer list', function (t) { var expected = ['singleScalarParameters', 'quattroshapes_deprecation', 'layers', - 'sources', 'sources_and_layers', 'size', 'private', 'geo_reverse', 'boundary_country']; + 'sources', 'sources_and_layers', 'geonames_deprecation', 'size', 'private', + 'geo_reverse', 'boundary_country']; t.deepEqual(Object.keys(reverse.sanitizer_list), expected); t.end(); });