Browse Source

added initial tests for using vs

pull/1044/head
Stephen Hess 7 years ago
parent
commit
110a258130
  1. 7
      test/unit/query/MockQuery.js
  2. 335
      test/unit/query/reverse.js
  3. 188
      test/unit/run.js

7
test/unit/query/MockQuery.js

@ -3,6 +3,7 @@
module.exports = class MockQuery { module.exports = class MockQuery {
constructor() { constructor() {
this._score_functions = []; this._score_functions = [];
this._sort_functions = [];
this._filter_functions = []; this._filter_functions = [];
} }
@ -10,6 +11,7 @@ module.exports = class MockQuery {
return { return {
vs: vs, vs: vs,
score_functions: this._score_functions, score_functions: this._score_functions,
sort_functions: this._sort_functions,
filter_functions: this._filter_functions filter_functions: this._filter_functions
}; };
} }
@ -19,6 +21,11 @@ module.exports = class MockQuery {
return this; return this;
} }
sort(view) {
this._sort_functions.push(view);
return this;
}
filter(view) { filter(view) {
this._filter_functions.push(view); this._filter_functions.push(view);
return this; return this;

335
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 = {};
module.exports.tests.interface = function(test, common) { module.exports.tests.interface = (test, common) => {
test('valid interface', function(t) { test('valid interface', t => {
t.equal(typeof generate, 'function', 'valid function'); t.equal(typeof generate, 'function', 'valid function');
t.end(); t.end();
}); });
}; };
module.exports.tests.query = function(test, common) { module.exports.tests.query = (test, common) => {
test('valid query', function(t) { test('base no frills', t => {
var query = generate({ const clean = {};
'point.lat': 29.49136,
'point.lon': -82.50622, const query = proxyquire('../../../query/reverse', {
'boundary.circle.lat': 29.49136, 'pelias-query': {
'boundary.circle.lon': -82.50622, layout: {
'boundary.circle.radius': 3 FilteredBooleanQuery: MockQuery
}); },
view: views,
var compiled = JSON.parse( JSON.stringify( query ) ); Vars: require('pelias-query').Vars
var expected = require('../fixture/reverse_standard'); },
'./reverse_defaults': {
t.deepEqual(compiled.type, 'reverse', 'query type set'); default_parameter_1: 'first default parameter',
t.deepEqual(compiled.body, expected, 'reverse_standard'); 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(); 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 ) ); test('clean.querySize should set size parameter', t => {
var expected = require('../fixture/reverse_null_island'); const clean = {
querySize: 17
};
t.deepEqual(compiled.type, 'reverse', 'query type set'); const query = proxyquire('../../../query/reverse', {
t.deepEqual(compiled.body, expected, 'reverse_null_island'); '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(); 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 ) ); module.exports.tests.sources = (test, common) => {
var expected = '123km'; 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'); const query = proxyquire('../../../query/reverse', {
t.deepEqual(compiled.body.query.bool.filter[0].geo_distance.distance, expected, 'distance set to boundary circle radius'); 'pelias-query': {
layout: {
FilteredBooleanQuery: MockQuery
},
view: views,
Vars: require('pelias-query').Vars
},
'./reverse_defaults': {}
})(clean);
t.notOk(query.body.vs.isset('sources'));
t.end(); 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 ) ); test('empty array clean.sources should not set sources in vs', t => {
var expected = '1km'; const clean = {
sources: []
};
t.deepEqual(compiled.type, 'reverse', 'query type set'); const query = proxyquire('../../../query/reverse', {
t.deepEqual(compiled.body.query.bool.filter[0].geo_distance.distance, expected, 'distance set to default boundary circle radius'); 'pelias-query': {
layout: {
FilteredBooleanQuery: MockQuery
},
view: views,
Vars: require('pelias-query').Vars
},
'./reverse_defaults': {}
})(clean);
t.notOk(query.body.vs.isset('sources'));
t.end(); t.end();
}); });
test('boundary.circle lat/lon/radius - overrides point.lat/lon when set', function(t) { test('non-empty array clean.sources should set sources in vs', t => {
var clean = { const clean = {
'point.lat': 29.49136, sources: ['source 1', 'source 2']
'point.lon': -82.50622,
'boundary.circle.lat': 111,
'boundary.circle.lon': 333,
'boundary.circle.radius': 3
}; };
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'); const query = proxyquire('../../../query/reverse', {
t.deepEqual(centroid, expected, 'reverse: boundary.circle/lon overrides point.lat/lon'); '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(); 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, module.exports.tests.layers = (test, common) => {
'point.lon': -82.50622, test('non-array clean.layers should not set sources in vs', t => {
'boundary.circle.lat': 29.49136, const clean = {
'boundary.circle.lon': -82.50622, layers: 'this is not an array'
'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();
});
test('valid sources filter', function(t) { const query = proxyquire('../../../query/reverse', {
var query = generate({ 'pelias-query': {
'point.lat': 29.49136, layout: {
'point.lon': -82.50622, FilteredBooleanQuery: MockQuery
'boundary.circle.lat': 29.49136, },
'boundary.circle.lon': -82.50622, view: views,
'boundary.circle.radius': 3, Vars: require('pelias-query').Vars
'sources': ['test'] },
}); './reverse_defaults': {}
})(clean);
var compiled = JSON.parse( JSON.stringify( query ) );
var expected = require('../fixture/reverse_with_source_filtering'); t.notOk(query.body.vs.isset('layers'));
t.deepEqual(compiled.type, 'reverse', 'query type set');
t.deepEqual(compiled.body, expected, 'valid reverse query with source filtering');
t.end(); t.end();
}); });
test('valid layers filter', (t) => { test('empty array clean.layers should not set sources in vs', t => {
const query = generate({ const clean = {
'point.lat': 29.49136, layers: []
'point.lon': -82.50622, };
'boundary.circle.lat': 29.49136,
'boundary.circle.lon': -82.50622, const query = proxyquire('../../../query/reverse', {
'boundary.circle.radius': 3, 'pelias-query': {
// only venue, address, and street layers should be retained layout: {
'layers': ['neighbourhood', 'venue', 'locality', 'address', 'region', 'street', 'country'] FilteredBooleanQuery: MockQuery
}); },
view: views,
const compiled = JSON.parse( JSON.stringify( query ) ); Vars: require('pelias-query').Vars
const expected = require('../fixture/reverse_with_layer_filtering'); },
'./reverse_defaults': {}
t.deepEqual(compiled.type, 'reverse', 'query type set'); })(clean);
t.deepEqual(compiled.body, expected, 'valid reverse query with source filtering');
t.notOk(query.body.vs.isset('layers'));
t.end(); t.end();
}); });
test('valid layers filter - subset of non-coarse layers', (t) => { test('non-empty array clean.layers should only set non-coarse layers in vs', t => {
const query = generate({ const clean = {
'point.lat': 29.49136, sources: ['source 1', 'source 2']
'point.lon': -82.50622, };
'boundary.circle.lat': 29.49136,
'boundary.circle.lon': -82.50622, const query = proxyquire('../../../query/reverse', {
'boundary.circle.radius': 3, 'pelias-query': {
// only venue, address, and street layers should be retained layout: {
'layers': ['neighbourhood', 'venue', 'street', 'locality'] FilteredBooleanQuery: MockQuery
}); },
view: views,
const compiled = JSON.parse( JSON.stringify( query ) ); Vars: require('pelias-query').Vars
const expected = require('../fixture/reverse_with_layer_filtering_non_coarse_subset'); },
'./reverse_defaults': {}
t.deepEqual(compiled.type, 'reverse', 'query type set'); })(clean);
t.deepEqual(compiled.body, expected, 'valid reverse query with source filtering');
t.deepEquals(query.body.vs.var('sources').toString(), ['source 1', 'source 2']);
t.end(); t.end();
}); });

188
test/unit/run.js

@ -9,101 +9,101 @@ var common = {
}; };
var tests = [ var tests = [
require('./app'), // require('./app'),
require('./schema'), // require('./schema'),
require('./controller/coarse_reverse'), // require('./controller/coarse_reverse'),
require('./controller/index'), // require('./controller/index'),
require('./controller/libpostal'), // require('./controller/libpostal'),
require('./controller/place'), // require('./controller/place'),
require('./controller/placeholder'), // require('./controller/placeholder'),
require('./controller/search'), // require('./controller/search'),
require('./controller/search_with_ids'), // require('./controller/search_with_ids'),
require('./controller/predicates/has_parsed_text_properties'), // require('./controller/predicates/has_parsed_text_properties'),
require('./controller/predicates/has_request_parameter'), // require('./controller/predicates/has_request_parameter'),
require('./controller/predicates/has_response_data'), // require('./controller/predicates/has_response_data'),
require('./controller/predicates/has_results_at_layers'), // require('./controller/predicates/has_results_at_layers'),
require('./controller/predicates/has_request_parameter'), // require('./controller/predicates/has_request_parameter'),
require('./controller/predicates/has_request_errors'), // require('./controller/predicates/has_request_errors'),
require('./controller/predicates/is_addressit_parse'), // require('./controller/predicates/is_addressit_parse'),
require('./controller/predicates/is_admin_only_analysis'), // require('./controller/predicates/is_admin_only_analysis'),
require('./controller/predicates/is_coarse_reverse'), // require('./controller/predicates/is_coarse_reverse'),
require('./controller/predicates/is_only_non_admin_layers'), // require('./controller/predicates/is_only_non_admin_layers'),
require('./controller/predicates/is_request_sources_only_whosonfirst'), // require('./controller/predicates/is_request_sources_only_whosonfirst'),
require('./helper/debug'), // require('./helper/debug'),
require('./helper/diffPlaces'), // require('./helper/diffPlaces'),
require('./helper/geojsonify_place_details'), // require('./helper/geojsonify_place_details'),
require('./helper/geojsonify'), // require('./helper/geojsonify'),
require('./helper/logging'), // require('./helper/logging'),
require('./helper/type_mapping'), // require('./helper/type_mapping'),
require('./helper/sizeCalculator'), // require('./helper/sizeCalculator'),
require('./helper/stackTraceLine'), // require('./helper/stackTraceLine'),
require('./middleware/access_log'), // require('./middleware/access_log'),
require('./middleware/accuracy'), // require('./middleware/accuracy'),
require('./middleware/assignLabels'), // require('./middleware/assignLabels'),
require('./middleware/confidenceScore'), // require('./middleware/confidenceScore'),
require('./middleware/confidenceScoreFallback'), // require('./middleware/confidenceScoreFallback'),
require('./middleware/confidenceScoreReverse'), // require('./middleware/confidenceScoreReverse'),
require('./middleware/changeLanguage'), // require('./middleware/changeLanguage'),
require('./middleware/distance'), // require('./middleware/distance'),
require('./middleware/interpolate'), // require('./middleware/interpolate'),
require('./middleware/localNamingConventions'), // require('./middleware/localNamingConventions'),
require('./middleware/dedupe'), // require('./middleware/dedupe'),
require('./middleware/parseBBox'), // require('./middleware/parseBBox'),
require('./middleware/sendJSON'), // require('./middleware/sendJSON'),
require('./middleware/normalizeParentIds'), // require('./middleware/normalizeParentIds'),
require('./middleware/sortResponseData'), // require('./middleware/sortResponseData'),
require('./middleware/trimByGranularity'), // require('./middleware/trimByGranularity'),
require('./middleware/trimByGranularityStructured'), // require('./middleware/trimByGranularityStructured'),
require('./middleware/requestLanguage'), // require('./middleware/requestLanguage'),
require('./query/address_search_using_ids'), // require('./query/address_search_using_ids'),
require('./query/autocomplete'), // require('./query/autocomplete'),
require('./query/autocomplete_defaults'), // require('./query/autocomplete_defaults'),
require('./query/search_defaults'), // require('./query/search_defaults'),
require('./query/reverse_defaults'), // require('./query/reverse_defaults'),
require('./query/reverse'), require('./query/reverse'),
require('./query/search'), // require('./query/search'),
require('./query/search_original'), // require('./query/search_original'),
require('./query/structured_geocoding'), // require('./query/structured_geocoding'),
require('./query/text_parser'), // require('./query/text_parser'),
require('./sanitizer/_boundary_country'), // require('./sanitizer/_boundary_country'),
require('./sanitizer/_debug'), // require('./sanitizer/_debug'),
require('./sanitizer/_flag_bool'), // require('./sanitizer/_flag_bool'),
require('./sanitizer/_geonames_deprecation'), // require('./sanitizer/_geonames_deprecation'),
require('./sanitizer/_geonames_warnings'), // require('./sanitizer/_geonames_warnings'),
require('./sanitizer/_geo_common'), // require('./sanitizer/_geo_common'),
require('./sanitizer/_geo_reverse'), // require('./sanitizer/_geo_reverse'),
require('./sanitizer/_groups'), // require('./sanitizer/_groups'),
require('./sanitizer/_ids'), // require('./sanitizer/_ids'),
require('./sanitizer/_iso2_to_iso3'), // require('./sanitizer/_iso2_to_iso3'),
require('./sanitizer/_layers'), // require('./sanitizer/_layers'),
require('./sanitizer/_location_bias'), // require('./sanitizer/_location_bias'),
require('./sanitizer/_city_name_standardizer'), // require('./sanitizer/_city_name_standardizer'),
require('./sanitizer/_request_language'), // require('./sanitizer/_request_language'),
require('./sanitizer/_single_scalar_parameters'), // require('./sanitizer/_single_scalar_parameters'),
require('./sanitizer/_size'), // require('./sanitizer/_size'),
require('./sanitizer/_sources'), // require('./sanitizer/_sources'),
require('./sanitizer/_sources_and_layers'), // require('./sanitizer/_sources_and_layers'),
require('./sanitizer/_synthesize_analysis'), // require('./sanitizer/_synthesize_analysis'),
require('./sanitizer/_text'), // require('./sanitizer/_text'),
require('./sanitizer/_text_addressit'), // require('./sanitizer/_text_addressit'),
require('./sanitizer/_tokenizer'), // require('./sanitizer/_tokenizer'),
require('./sanitizer/_deprecate_quattroshapes'), // require('./sanitizer/_deprecate_quattroshapes'),
require('./sanitizer/_categories'), // require('./sanitizer/_categories'),
require('./sanitizer/nearby'), // require('./sanitizer/nearby'),
require('./sanitizer/autocomplete'), // require('./sanitizer/autocomplete'),
require('./sanitizer/structured_geocoding'), // require('./sanitizer/structured_geocoding'),
require('./sanitizer/place'), // require('./sanitizer/place'),
require('./sanitizer/reverse'), // require('./sanitizer/reverse'),
require('./sanitizer/sanitizeAll'), // require('./sanitizer/sanitizeAll'),
require('./sanitizer/search'), // require('./sanitizer/search'),
require('./sanitizer/defer_to_addressit'), // require('./sanitizer/defer_to_addressit'),
require('./sanitizer/wrap'), // require('./sanitizer/wrap'),
require('./service/configurations/Interpolation'), // require('./service/configurations/Interpolation'),
require('./service/configurations/Language'), // require('./service/configurations/Language'),
require('./service/configurations/PlaceHolder'), // require('./service/configurations/PlaceHolder'),
require('./service/configurations/PointInPolygon'), // require('./service/configurations/PointInPolygon'),
require('./service/mget'), // require('./service/mget'),
require('./service/search') // require('./service/search')
]; ];
tests.map(function(t) { tests.map(function(t) {

Loading…
Cancel
Save