Browse Source

limit /reverse boundary.circle.* for non-coarse

pull/904/head
Stephen Hess 8 years ago
parent
commit
a247d7a1cd
  1. 25
      sanitizer/_geo_reverse.js
  2. 187
      test/unit/sanitizer/_geo_reverse.js

25
sanitizer/_geo_reverse.js

@ -4,6 +4,8 @@ var defaults = require('../query/reverse_defaults');
var LAT_LON_IS_REQUIRED = true,
CIRCLE_IS_REQUIRED = false;
const non_coarse_layers = ['venue', 'address', 'street'];
// validate inputs, convert types and apply defaults
module.exports = function sanitize( raw, clean ){
@ -23,22 +25,21 @@ module.exports = function sanitize( raw, clean ){
// first verify that point.lat/point.lon are valid
geo_common.sanitize_point( 'point', clean, raw, LAT_LON_IS_REQUIRED );
// overwrite boundary.circle.lat/lon with point.lat/lon
raw['boundary.circle.lat'] = clean['point.lat'];
raw['boundary.circle.lon'] = clean['point.lon'];
// only set boundary.circle things if the request is non-coarse
if (_.isEmpty(clean.layers) || !_.isEmpty(_.intersection(non_coarse_layers, clean.layers))) {
// overwrite boundary.circle.lat/lon with point.lat/lon
raw['boundary.circle.lat'] = clean['point.lat'];
raw['boundary.circle.lon'] = clean['point.lon'];
// if no radius was passed, set the default
if ( _.isUndefined( raw['boundary.circle.radius'] ) ) {
// the default is small unless layers other than venue or address were explicitly specified
if (clean.layers && clean.layers.length > 0 && !_.includes(clean.layers, 'venue') && !_.includes(clean.layers, 'address')) {
raw['boundary.circle.radius'] = defaults['boundary:circle:radius:coarse'];
} else {
// if no radius was passed, set the default
if ( _.isUndefined( raw['boundary.circle.radius'] ) ) {
raw['boundary.circle.radius'] = defaults['boundary:circle:radius'];
}
}
// santize the boundary.circle
geo_common.sanitize_circle( 'boundary.circle', clean, raw, CIRCLE_IS_REQUIRED );
// santize the boundary.circle
geo_common.sanitize_circle( 'boundary.circle', clean, raw, CIRCLE_IS_REQUIRED );
}
}
catch (err) {

187
test/unit/sanitizer/_geo_reverse.js

@ -1,17 +1,17 @@
var sanitize = require('../../../sanitizer/_geo_reverse');
var defaults = require('../../../query/reverse_defaults');
const sanitize = require('../../../sanitizer/_geo_reverse');
const defaults = require('../../../query/reverse_defaults');
module.exports.tests = {};
module.exports.tests.sanitize_boundary_country = function(test, common) {
test('raw with boundary.circle.lat should add warning about ignored boundary.circle', function(t) {
var raw = {
module.exports.tests.warning_situations = (test, common) => {
test('raw with boundary.circle.lat should add warning about ignored boundary.circle', (t) => {
const raw = {
'point.lat': '12.121212',
'point.lon': '21.212121',
'boundary.circle.lat': '13.131313'
};
var clean = {};
var errorsAndWarnings = sanitize(raw, clean);
const clean = {};
const errorsAndWarnings = sanitize(raw, clean);
t.equals(clean['boundary.circle.lat'], 12.121212, 'should be set to point.lat');
t.deepEquals(errorsAndWarnings, {
@ -19,16 +19,17 @@ module.exports.tests.sanitize_boundary_country = function(test, common) {
warnings: ['boundary.circle.lat/boundary.circle.lon are currently unsupported']
}, 'no warnings/errors');
t.end();
});
test('raw with boundary.circle.lon should add warning about ignored boundary.circle', function(t) {
var raw = {
test('raw with boundary.circle.lon should add warning about ignored boundary.circle', (t) => {
const raw = {
'point.lat': '12.121212',
'point.lon': '21.212121',
'boundary.circle.lon': '31.313131'
};
var clean = {};
var errorsAndWarnings = sanitize(raw, clean);
const clean = {};
const errorsAndWarnings = sanitize(raw, clean);
t.equals(clean['boundary.circle.lon'], 21.212121, 'should be set to point.lon');
t.deepEquals(errorsAndWarnings, {
@ -36,16 +37,17 @@ module.exports.tests.sanitize_boundary_country = function(test, common) {
warnings: ['boundary.circle.lat/boundary.circle.lon are currently unsupported']
}, 'no warnings/errors');
t.end();
});
test('raw with boundary.circle.radius shouldn\'t add warning about ignored boundary.circle', function(t) {
var raw = {
test('raw with boundary.circle.radius shouldn\'t add warning about ignored boundary.circle', (t) => {
const raw = {
'point.lat': '12.121212',
'point.lon': '21.212121',
'boundary.circle.radius': '17'
};
var clean = {};
var errorsAndWarnings = sanitize(raw, clean);
const clean = {};
const errorsAndWarnings = sanitize(raw, clean);
// t.equals(clean['boundary.circle.radius'], 12.121212, 'should be set to point.lat')
t.deepEquals(errorsAndWarnings, {
@ -53,80 +55,133 @@ module.exports.tests.sanitize_boundary_country = function(test, common) {
warnings: []
}, 'no warnings/errors');
t.end();
});
test('boundary.circle.lat/lon should be overridden with point.lat/lon', function(t) {
var raw = {
'point.lat': '12.121212',
'point.lon': '21.212121',
'boundary.circle.lat': '13.131313',
'boundary.circle.lon': '31.313131'
};
var clean = {};
var errorsAndWarnings = sanitize(raw, clean);
};
module.exports.tests.non_coarse_reverse = (test, common) => {
test('boundary.circle.lat/lon should be overridden with point.lat/lon when non-coarse layers', (t) => {
[[], ['venue', 'locality'], ['address', 'county'], ['street', 'country']].forEach((layers) => {
const raw = {
'point.lat': '12.121212',
'point.lon': '21.212121',
'boundary.circle.lat': '13.131313',
'boundary.circle.lon': '31.313131'
};
const clean = {
layers: layers
};
const errorsAndWarnings = sanitize(raw, clean);
t.equals(raw['boundary.circle.lat'], 12.121212, 'should be set to point.lat');
t.equals(raw['boundary.circle.lon'], 21.212121, 'should be set to point.lon');
t.equals(clean['boundary.circle.lat'], 12.121212, 'should be set to point.lat');
t.equals(clean['boundary.circle.lon'], 21.212121, 'should be set to point.lon');
});
t.equals(raw['boundary.circle.lat'], 12.121212, 'should be set to point.lat');
t.equals(raw['boundary.circle.lon'], 21.212121, 'should be set to point.lon');
t.equals(clean['boundary.circle.lat'], 12.121212, 'should be set to point.lat');
t.equals(clean['boundary.circle.lon'], 21.212121, 'should be set to point.lon');
t.end();
});
test('no boundary.circle.radius and no layers supplied should be set to default', function(t) {
var raw = {
'point.lat': '12.121212',
'point.lon': '21.212121'
};
var clean = {};
var errorsAndWarnings = sanitize(raw, clean);
test('no boundary.circle.radius supplied should be set to default when non-coarse layers', (t) => {
[[], ['venue', 'locality'], ['address', 'county'], ['street', 'country']].forEach((layers) => {
const raw = {
'point.lat': '12.121212',
'point.lon': '21.212121'
};
const clean = {
layers: layers
};
const errorsAndWarnings = sanitize(raw, clean);
t.equals(raw['boundary.circle.radius'], defaults['boundary:circle:radius'], 'should be from defaults');
t.equals(clean['boundary.circle.radius'], parseFloat(defaults['boundary:circle:radius']), 'should be same as raw');
});
t.equals(raw['boundary.circle.radius'], defaults['boundary:circle:radius'], 'should be from defaults');
t.equals(clean['boundary.circle.radius'], parseFloat(defaults['boundary:circle:radius']), 'should be same as raw');
t.end();
});
test('no boundary.circle.radius and coarse layers supplied should be set to coarse default', function(t) {
var raw = {
'point.lat': '12.121212',
'point.lon': '21.212121'
};
var clean = { layers: 'coarse' };
var errorsAndWarnings = sanitize(raw, clean);
test('explicit boundary.circle.radius should be used instead of default', (t) => {
[[], ['venue', 'locality'], ['address', 'county'], ['street', 'country']].forEach((layers) => {
const raw = {
'point.lat': '12.121212',
'point.lon': '21.212121',
'boundary.circle.radius': '3248732857km' // this will never be the default
};
const clean = {
layers: layers
};
const errorsAndWarnings = sanitize(raw, clean);
t.equals(raw['boundary.circle.radius'], '3248732857km', 'should be parsed float');
t.equals(clean['boundary.circle.radius'], 3248732857.0, 'should be copied from raw');
});
t.equals(raw['boundary.circle.radius'], defaults['boundary:circle:radius:coarse'], 'should be from defaults');
t.equals(clean['boundary.circle.radius'], parseFloat(defaults['boundary:circle:radius:coarse']), 'should be same as raw');
t.end();
});
test('no boundary.circle.radius and coarse layer supplied should be set to coarse default', function(t) {
var raw = {
'point.lat': '12.121212',
'point.lon': '21.212121'
};
var clean = { layers: 'locality' };
var errorsAndWarnings = sanitize(raw, clean);
};
module.exports.tests.coarse_reverse = (test, common) => {
test('coarse layers should not set boundary.circle things since they\'re not applicable', (t) => {
['coarse', 'neighbourhood', 'borough', 'locality', 'localadmin', 'county',
'macrocounty', 'region', 'macroregion', 'dependency', 'country'].forEach((layer) => {
const raw = {
'point.lat': '12.121212',
'point.lon': '21.212121'
};
const clean = { layers: [layer] };
const errorsAndWarnings = sanitize(raw, clean);
t.notOk(raw['boundary.circle.lat']);
t.notOk(raw['boundary.circle.lon']);
t.notOk(raw['boundary.circle.radius']);
t.notOk(clean['boundary.circle.lat']);
t.notOk(clean['boundary.circle.lon']);
t.notOk(clean['boundary.circle.radius']);
t.deepEquals(errorsAndWarnings, { errors: [], warnings: [] });
});
t.equals(raw['boundary.circle.radius'], defaults['boundary:circle:radius:coarse'], 'should be from defaults');
t.equals(clean['boundary.circle.radius'], parseFloat(defaults['boundary:circle:radius:coarse']), 'should be same as raw');
t.end();
});
test('explicit boundary.circle.radius should be used instead of default', function(t) {
var raw = {
'point.lat': '12.121212',
'point.lon': '21.212121',
'boundary.circle.radius': '3248732857km' // this will never be the default
};
var clean = {};
var errorsAndWarnings = sanitize(raw, clean);
test('coarse layers should not use explicit boundary.circle.radius since it\'s not applicable', (t) => {
['coarse', 'neighbourhood', 'borough', 'locality', 'localadmin', 'county',
'macrocounty', 'region', 'macroregion', 'dependency', 'country'].forEach((layer) => {
const raw = {
'point.lat': '12.121212',
'point.lon': '21.212121',
'boundary.circle.radius': '3248732857km' // this will never be the default
};
const clean = { layers: [layer] };
const errorsAndWarnings = sanitize(raw, clean);
t.notOk(raw['boundary.circle.lat'], 'should not have been copied');
t.notOk(raw['boundary.circle.lon'], 'should not have been copied');
t.notOk(clean['boundary.circle.lat'], 'should not have been copied');
t.notOk(clean['boundary.circle.lon'], 'should not have been copied');
t.notOk(clean['boundary.circle.radius'], 'should not have been copied');
t.deepEquals(errorsAndWarnings, { errors: [], warnings: [] });
});
t.equals(raw['boundary.circle.radius'], '3248732857km', 'should be parsed float');
t.equals(clean['boundary.circle.radius'], 3248732857.0, 'should be copied from raw');
t.end();
});
};
module.exports.all = function (tape, common) {
module.exports.all = (tape, common) => {
function test(name, testFunction) {
return tape('SANTIZE _geo_reverse ' + name, testFunction);
}

Loading…
Cancel
Save