Browse Source

Merge branch '112-support-boundary-country-parameter'

Conflicts:
	test/unit/sanitiser/reverse.js
	test/unit/sanitiser/search.js
pull/265/head
Julian Simioni 9 years ago
parent
commit
152b2a50f7
  1. 1
      package.json
  2. 10
      query/reverse.js
  3. 1
      query/search.js
  4. 42
      sanitiser/_boundary_country.js
  5. 4
      sanitiser/reverse.js
  6. 4
      sanitiser/search.js
  7. 6
      test/unit/fixture/reverse_standard.js
  8. 54
      test/unit/fixture/reverse_with_boundary_country.js
  9. 44
      test/unit/fixture/search_boundary_country.js
  10. 14
      test/unit/query/reverse.js
  11. 16
      test/unit/query/search.js
  12. 1
      test/unit/run.js
  13. 79
      test/unit/sanitiser/_boundary_country.js
  14. 9
      test/unit/sanitiser/reverse.js
  15. 2
      test/unit/sanitiser/search.js

1
package.json

@ -45,6 +45,7 @@
"geolib": "^2.0.18",
"geopipes-elasticsearch-backend": "^0.2.0",
"lodash": "^3.10.1",
"iso3166-1": "^0.2.3",
"markdown": "0.5.0",
"microtime": "1.4.0",
"morgan": "1.5.2",

10
query/reverse.js

@ -7,6 +7,9 @@ var peliasQuery = require('pelias-query'),
//------------------------------
var query = new peliasQuery.layout.FilteredBooleanQuery();
// mandatory matches
query.score( peliasQuery.view.boundary_country, 'must' );
// scoring boost
query.sort( peliasQuery.view.sort_distance );
@ -49,6 +52,13 @@ function generateQuery( clean ){
});
}
// boundary country
if( clean.boundary && clean.boundary.country ){
vs.set({
'boundary:country': clean.boundary.country
});
}
return query.render( vs );
}

1
query/search.js

@ -98,7 +98,6 @@ function generateQuery( clean ){
}
// boundary country
// @todo: change these to the correct request variable names
if( clean.boundary && clean.boundary.country ){
vs.set({
'boundary:country': clean.boundary.country

42
sanitiser/_boundary_country.js

@ -0,0 +1,42 @@
var check = require('check-types');
var iso3166 = require('iso3166-1');
function sanitize(raw, clean) {
// error & warning messages
var messages = { errors: [], warnings: [] };
// init clean.boundary (if not already init)
clean.boundary = clean.boundary || {};
if (check.assigned(raw['boundary.country'])) {
var country = raw['boundary.country'];
if (!check.string(country)) {
messages.errors.push('boundary.country is not a string');
delete clean.boundary.country;
}
else if (!containsIsoCode(country.toUpperCase())) {
messages.errors.push(country + ' is not a valid ISO2/ISO3 country code');
delete clean.boundary.country;
}
else {
// the only way for boundary.country to be assigned is if input is
// a string and a known ISO2 or ISO3
clean.boundary.country = iso3166.to3(country.toUpperCase());
}
} else {
delete clean.boundary.country;
}
return messages;
}
function containsIsoCode(isoCode) {
return iso3166.list().some(function(row) {
return row.alpha2 === isoCode || row.alpha3 === isoCode;
});
}
module.exports = sanitize;

4
sanitiser/reverse.js

@ -6,7 +6,8 @@ var sanitizeAll = require('../sanitiser/sanitizeAll'),
size: require('../sanitiser/_size'),
private: require('../sanitiser/_flag_bool')('private', false),
geo_reverse: require('../sanitiser/_geo_reverse'),
categories: require('../sanitiser/_categories')
categories: require('../sanitiser/_categories'),
boundary_country: require('../sanitiser/_boundary_country'),
};
var sanitize = function(req, cb) { sanitizeAll(req, sanitizers, cb); };
@ -25,4 +26,3 @@ module.exports.middleware = function( req, res, next ){
next();
});
};

4
sanitiser/search.js

@ -7,7 +7,8 @@ var sanitizeAll = require('../sanitiser/sanitizeAll'),
sources: require('../sanitiser/_targets')('sources', require( '../query/sources' )),
private: require('../sanitiser/_flag_bool')('private', false),
geo_search: require('../sanitiser/_geo_search'),
categories: require('../sanitiser/_categories')
categories: require('../sanitiser/_categories'),
boundary_country: require('../sanitiser/_boundary_country'),
};
var sanitize = function(req, cb) { sanitizeAll(req, sanitizers, cb); };
@ -26,4 +27,3 @@ module.exports.middleware = function( req, res, next ){
next();
});
};

6
test/unit/fixture/reverse_standard.js

@ -3,7 +3,9 @@ module.exports = {
'query': {
'filtered': {
'query': {
'bool': {}
'bool': {
'must': []
}
},
'filter': {
'bool': {
@ -40,4 +42,4 @@ module.exports = {
],
'size': 1,
'track_scores': true
};
};

54
test/unit/fixture/reverse_with_boundary_country.js

@ -0,0 +1,54 @@
module.exports = {
'query': {
'filtered': {
'query': {
'bool': {
'must': [
{
'match': {
'alpha3': {
'analyzer': 'standard',
'query': 'ABC'
}
}
}
]
}
},
'filter': {
'bool': {
'must': [
{
'geo_distance': {
'distance': '500km',
'distance_type': 'plane',
'optimize_bbox': 'indexed',
'_cache': true,
'center_point': {
'lat': 29.49136,
'lon': -82.50622
}
}
}
]
}
}
}
},
'sort': [
'_score',
{
'_geo_distance': {
'center_point': {
'lat': 29.49136,
'lon': -82.50622
},
'order': 'asc',
'distance_type': 'plane'
}
}
],
'size': 1,
'track_scores': true
};

44
test/unit/fixture/search_boundary_country.js

@ -0,0 +1,44 @@
module.exports = {
'query': {
'filtered': {
'query': {
'bool': {
'must': [
{
'match': {
'alpha3': {
'analyzer': 'standard',
'query': 'ABC'
}
}
},
{
'match': {
'name.default': {
'query': 'test',
'boost': 1,
'analyzer': 'peliasOneEdgeGram'
}
}
}
],
'should': [{
'match': {
'phrase.default': {
'query': 'test',
'analyzer': 'peliasPhrase',
'type': 'phrase',
'boost': 1,
'slop': 2
}
}
}]
}
}
}
},
'sort': [ '_score' ],
'size': 10,
'track_scores': true
};

14
test/unit/query/reverse.js

@ -60,6 +60,20 @@ module.exports.tests.query = function(test, common) {
});
t.end();
});
test('valid boundary.country reverse search', function(t) {
var query = generate({
lat: 29.49136, lon: -82.50622,
boundary: { country: 'ABC' }
});
var compiled = JSON.parse( JSON.stringify( query ) );
var expected = require('../fixture/reverse_with_boundary_country');
t.deepEqual(compiled, expected, 'valid reverse query with boundary.country');
t.end();
});
};
module.exports.all = function (tape, common) {

16
test/unit/query/search.js

@ -133,6 +133,22 @@ module.exports.tests.query = function(test, common) {
t.deepEqual(compiled, expected, 'valid search query');
t.end();
});
test('valid boundary.country search', function(t) {
var query = generate({
text: 'test', size: 10,
layers: ['test'],
boundary: { country: 'ABC' }
});
var compiled = JSON.parse( JSON.stringify( query ) );
var expected = require('../fixture/search_boundary_country');
expected.sort = sort;
t.deepEqual(compiled, expected, 'valid boundary.country query');
t.end();
});
};
module.exports.all = function (tape, common) {

1
test/unit/run.js

@ -11,6 +11,7 @@ var tests = [
require('./sanitiser/_flag_bool'),
require('./sanitiser/autocomplete'),
require('./sanitiser/_sources'),
require('./sanitiser/_boundary_country'),
require('./sanitiser/search'),
require('./sanitiser/_layers'),
require('./sanitiser/reverse'),

79
test/unit/sanitiser/_boundary_country.js

@ -0,0 +1,79 @@
var sanitize = require('../../../sanitiser/_boundary_country');
module.exports.tests = {};
module.exports.tests.sanitize_boundary_country = function(test, common) {
test('raw w/o boundary should set boundary.country undefined', function(t) {
var raw = { };
var clean = {};
var errorsAndWarnings = sanitize(raw, clean);
t.equals(clean.boundary.country, undefined, 'should be undefined');
t.deepEquals(errorsAndWarnings, { errors: [], warnings: [] }, 'no warnings or errors');
t.end();
});
test('boundary.country explicitly undefined in raw should leave boundary.country undefined', function(t) {
var raw = { 'boundary.country': undefined };
var clean = {};
var errorsAndWarnings = sanitize(raw, clean);
t.equals(clean.boundary.country, undefined, 'should be undefined');
t.deepEquals(errorsAndWarnings, { errors: [], warnings: [] }, 'no warnings or errors');
t.end();
});
test('non-string boundary.country should set boundary.country to undefined and return warning', function(t) {
var raw = { 'boundary.country': ['this isn\'t a string primitive'] };
var clean = {};
var errorsAndWarnings = sanitize(raw, clean);
t.equals(clean.boundary.country, undefined, 'should be undefined');
t.deepEquals(errorsAndWarnings, { errors: ['boundary.country is not a string'], warnings: [] }, 'non-string country warning');
t.end();
});
test('iso2 boundary.country in raw should set boundary.country to ISO3 uppercased', function(t) {
var raw = { 'boundary.country': 'aq' };
var clean = {};
var errorsAndWarnings = sanitize(raw, clean);
t.equals(clean.boundary.country, 'ATA', 'should be uppercased ISO3');
t.deepEquals(errorsAndWarnings, { errors: [], warnings: [] }, 'no warnings or errors');
t.end();
});
test('iso3 boundary.country in raw should set boundary.country to matching ISO3 uppercased', function(t) {
var raw = { 'boundary.country': 'aTa' };
var clean = {};
var errorsAndWarnings = sanitize(raw, clean);
t.equals(clean.boundary.country, 'ATA', 'should be uppercased ISO3');
t.deepEquals(errorsAndWarnings, { errors: [], warnings: [] }, 'no warnings or errors');
t.end();
});
test('unknown 2-character boundary.country should set boundary.country to undefined', function(t) {
var raw = { 'boundary.country': 'zq' };
var clean = {};
var errorsAndWarnings = sanitize(raw, clean);
t.equals(clean.boundary.country, undefined, 'should be undefined');
t.deepEquals(errorsAndWarnings, { errors: ['zq is not a valid ISO2/ISO3 country code'], warnings: [] }, 'country not found warning`');
t.end();
});
test('unknown 3-character boundary.country should set boundary.country to undefined', function(t) {
var raw = { 'boundary.country': 'zqx' };
var clean = {};
var errorsAndWarnings = sanitize(raw, clean);
t.equals(clean.boundary.country, undefined, 'should be undefined');
t.deepEquals(errorsAndWarnings, { errors: ['zqx is not a valid ISO2/ISO3 country code'], warnings: [] }, 'country not found warning`');
t.end();
});
};
module.exports.all = function (tape, common) {
function test(name, testFunction) {
return tape('SANTIZE _boundary_country ' + name, testFunction);
}
for( var testCase in module.exports.tests ){
module.exports.tests[testCase](test, common);
}
};

9
test/unit/sanitiser/reverse.js

@ -9,7 +9,8 @@ var reverse = require('../../../sanitiser/reverse'),
lon: 0,
size: 10,
private: false,
categories: []
categories: [],
boundary: { }
},
sanitize = function(query, cb) { _sanitize({'query':query}, cb); };
@ -30,7 +31,7 @@ module.exports.tests.interface = function(test, common) {
module.exports.tests.sanitisers = function(test, common) {
test('check sanitiser list', function (t) {
var expected = ['layers', 'sources', 'size', 'private', 'geo_reverse', 'categories'];
var expected = ['layers', 'sources', 'size', 'private', 'geo_reverse', 'categories', 'boundary_country'];
t.deepEqual(Object.keys(reverse.sanitiser_list), expected);
t.end();
});
@ -57,7 +58,7 @@ module.exports.tests.sanitize_lat = function(test, common) {
var expected = JSON.parse(JSON.stringify( defaultClean ));
expected.lat = parseFloat( lat );
t.equal(err, undefined, 'no error');
t.deepEqual(clean, expected, 'clean set correctly (' + lat + ')');
t.equal(clean.lat, parseFloat(lat), 'clean set correctly (' + lat + ')');
});
});
t.end();
@ -84,7 +85,7 @@ module.exports.tests.sanitize_lon = function(test, common) {
var expected = JSON.parse(JSON.stringify( defaultClean ));
expected.lon = parseFloat( lon );
t.equal(err, undefined, 'no error');
t.deepEqual(clean, expected, 'clean set correctly (' + lon + ')');
t.equal(clean.lon, parseFloat(lon), 'clean set correctly (' + lon + ')');
});
});
t.end();

2
test/unit/sanitiser/search.js

@ -31,7 +31,7 @@ module.exports.tests.interface = function(test, common) {
module.exports.tests.sanitisers = function(test, common) {
test('check sanitiser list', function (t) {
var expected = ['text', 'size', 'layers', 'sources', 'private', 'geo_search', 'categories' ];
var expected = ['text', 'size', 'layers', 'sources', 'private', 'geo_search', 'categories', 'boundary_country' ];
t.deepEqual(Object.keys(search.sanitiser_list), expected);
t.end();
});

Loading…
Cancel
Save