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) {