Browse Source

Merge pull request #1044 from pelias/update-reverse-query-tests

Update reverse query tests
pull/1047/head v3.32.13
Julian Simioni 7 years ago committed by GitHub
parent
commit
c045693257
  1. 39
      test/unit/fixture/reverse_null_island.js
  2. 39
      test/unit/fixture/reverse_standard.js
  3. 49
      test/unit/fixture/reverse_with_boundary_country.js
  4. 41
      test/unit/fixture/reverse_with_layer_filtering.js
  5. 41
      test/unit/fixture/reverse_with_layer_filtering_non_coarse_subset.js
  6. 46
      test/unit/fixture/reverse_with_source_filtering.js
  7. 7
      test/unit/query/MockQuery.js
  8. 573
      test/unit/query/reverse.js

39
test/unit/fixture/reverse_null_island.js

@ -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
};

39
test/unit/fixture/reverse_standard.js

@ -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
};

49
test/unit/fixture/reverse_with_boundary_country.js

@ -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
};

41
test/unit/fixture/reverse_with_layer_filtering.js

@ -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
};

41
test/unit/fixture/reverse_with_layer_filtering_non_coarse_subset.js

@ -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
};

46
test/unit/fixture/reverse_with_source_filtering.js

@ -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
};

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;

573
test/unit/query/reverse.js

@ -1,190 +1,503 @@
var generate = require('../../../query/reverse'); 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 = {
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,
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();
var compiled = JSON.parse( JSON.stringify( query ) ); });
var expected = require('../fixture/reverse_standard');
t.deepEqual(compiled.type, 'reverse', 'query type set'); test('clean.querySize should set size parameter', t => {
t.deepEqual(compiled.body, expected, 'reverse_standard'); const clean = {
querySize: 17
};
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(); 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 ) ); module.exports.tests.sources = (test, common) => {
var expected = require('../fixture/reverse_null_island'); 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, expected, 'reverse_null_island'); '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('valid query with radius', function(t) { test('empty array clean.sources should not set sources in vs', t => {
var query = generate({ const clean = {
'point.lat': 29.49136, sources: []
'point.lon': -82.50622, };
'boundary.circle.lat': 29.49136,
'boundary.circle.lon': -82.50622, const query = proxyquire('../../../query/reverse', {
'boundary.circle.radius': 123 'pelias-query': {
}); layout: {
FilteredBooleanQuery: MockQuery
},
view: views,
Vars: require('pelias-query').Vars
},
'./reverse_defaults': {}
})(clean);
t.notOk(query.body.vs.isset('sources'));
t.end();
var compiled = JSON.parse( JSON.stringify( query ) ); });
var expected = '123km';
test('non-empty array clean.sources should set sources in vs', t => {
const clean = {
sources: ['source 1', 'source 2']
};
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.deepEquals(query.body.vs.var('sources').toString(), ['source 1', 'source 2']);
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 ) ); module.exports.tests.layers = (test, common) => {
var expected = '1km'; test('non-array clean.layers should not set sources in vs', t => {
const clean = {
layers: '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 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('layers'));
t.end(); t.end();
}); });
test('boundary.circle lat/lon/radius - overrides point.lat/lon when set', function(t) { test('empty array clean.layers should not set sources in vs', t => {
var clean = { const clean = {
'point.lat': 29.49136, layers: []
'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 const query = proxyquire('../../../query/reverse', {
var expected = { lat: clean['boundary.circle.lat'], lon: clean['boundary.circle.lon'] }; 'pelias-query': {
var centroid = compiled.body.query.bool.filter[0].geo_distance.center_point; layout: {
FilteredBooleanQuery: MockQuery
},
view: views,
Vars: require('pelias-query').Vars
},
'./reverse_defaults': {}
})(clean);
t.notOk(query.body.vs.isset('layers'));
t.end();
});
test('non-empty array clean.layers should only set non-coarse layers in vs', t => {
const clean = {
layers: all_layers
};
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('layers').toString(), ['address', 'venue', 'street']);
t.end(); t.end();
}); });
test('size fuzz test', function(t) { };
// test different sizes
var sizes = [1,4,20,undefined,null]; module.exports.tests.focus_point = (test, common) => {
var expected = [1,4,20,1,1]; test('numeric point.lat and non-numeric point.lon should not add focus:point:* fields', t => {
sizes.forEach( function( size, index ){ const clean = {
var query = generate({ 'point.lat': 12.121212,
'point.lat': 29.49136, 'point.lon': -82.50622, querySize: size 'point.lon': 'this is non-numeric'
}); };
var compiled = JSON.parse( JSON.stringify( query ) ); const query = proxyquire('../../../query/reverse', {
t.equal( compiled.body.size, expected[index], 'valid reverse query for size: '+ size); '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(); t.end();
}); });
test('valid boundary.country reverse search', function(t) { test('non-numeric point.lat and numeric point.lon should not add focus:point:* fields', t => {
var query = generate({ const clean = {
'point.lat': 29.49136, 'point.lat': 'this is non-numeric',
'point.lon': -82.50622, 'point.lon': 21.212121
'boundary.circle.lat': 29.49136, };
'boundary.circle.lon': -82.50622,
'boundary.circle.radius': 3,
'boundary.country': 'ABC'
});
var compiled = JSON.parse( JSON.stringify( query ) ); const query = proxyquire('../../../query/reverse', {
var expected = require('../fixture/reverse_with_boundary_country'); '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();
t.deepEqual(compiled.type, 'reverse', 'query type set'); });
t.deepEqual(compiled.body, expected, 'valid reverse query with boundary.country');
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(); t.end();
}); });
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 ) ); module.exports.tests.boundary_circle = (test, common) => {
var expected = require('../fixture/reverse_with_source_filtering'); 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'
};
t.deepEqual(compiled.type, 'reverse', 'query type set'); const query = proxyquire('../../../query/reverse', {
t.deepEqual(compiled.body, expected, 'valid reverse query with source filtering'); '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(); t.end();
}); });
test('valid layers filter', (t) => { test('non-numeric lat and numeric lon should not add boundary:circle:* fields', t => {
const query = generate({ const clean = {
'point.lat': 29.49136, 'boundary.circle.lat': 'this is non-numeric',
'point.lon': -82.50622, 'boundary.circle.lon': 21.212121
'boundary.circle.lat': 29.49136, };
'boundary.circle.lon': -82.50622,
'boundary.circle.radius': 3, const query = proxyquire('../../../query/reverse', {
// only venue, address, and street layers should be retained 'pelias-query': {
'layers': ['neighbourhood', 'venue', 'locality', 'address', 'region', 'street', 'country'] 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 compiled = JSON.parse( JSON.stringify( query ) ); const query = proxyquire('../../../query/reverse', {
const expected = require('../fixture/reverse_with_layer_filtering'); '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'
};
t.deepEqual(compiled.type, 'reverse', 'query type set'); const query = proxyquire('../../../query/reverse', {
t.deepEqual(compiled.body, expected, 'valid reverse query with source filtering'); '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(); t.end();
}); });
test('valid layers filter - subset of non-coarse layers', (t) => { };
const query = generate({
'point.lat': 29.49136, module.exports.tests.boundary_country = (test, common) => {
'point.lon': -82.50622, test('non-string boundary.country should not set boundary:country', t => {
'boundary.circle.lat': 29.49136, [17, undefined, {}, [], true, null].forEach(value => {
'boundary.circle.lon': -82.50622, const clean = {
'boundary.circle.radius': 3, 'boundary.country': value
// only venue, address, and street layers should be retained };
'layers': ['neighbourhood', 'venue', 'street', 'locality']
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'));
}); });
const compiled = JSON.parse( JSON.stringify( query ) ); t.end();
const expected = require('../fixture/reverse_with_layer_filtering_non_coarse_subset');
});
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'
};
t.deepEqual(compiled.type, 'reverse', 'query type set'); const query = proxyquire('../../../query/reverse', {
t.deepEqual(compiled.body, expected, 'valid reverse query with source filtering'); '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(); t.end();
}); });

Loading…
Cancel
Save