Browse Source

added warning if any of boundary.circle.lat/lon/radius are supplied + tests

pull/295/head
Stephen Hess 9 years ago
parent
commit
947797f41e
  1. 3
      sanitiser/_geo_common.js
  2. 11
      sanitiser/_geo_reverse.js
  3. 1
      test/unit/run.js
  4. 67
      test/unit/sanitiser/_geo_reverse.js

3
sanitiser/_geo_common.js

@ -55,7 +55,6 @@ function sanitize_rect( key_prefix, clean, raw, bbox_is_required ) {
* @param {bool} circle_is_required
*/
function sanitize_circle( key_prefix, clean, raw, circle_is_required ) {
// the names we use to define the centroid
var mandatoryProps = [ 'lat', 'lon' ];
@ -86,7 +85,7 @@ function sanitize_circle( key_prefix, clean, raw, circle_is_required ) {
// radius was specified without lat or lon
else if( raw.hasOwnProperty( key_prefix + '.radius' ) ){
var format2 = 'missing circle param \'%s\' requires all of: \'%s\' to be present';
throw new Error( util.format( format2, key_prefix, mandatoryProps.join('\',\'') ) );
throw new Error( util.format( format2, key_prefix, mandatoryProps.join('\',\'') ) );
}
// fields required, eg. ( totalFieldsSpecified === 0 && bbox_is_required === true )
else if( circle_is_required ){

11
sanitiser/_geo_reverse.js

@ -10,11 +10,22 @@ module.exports = function sanitize( raw, clean ){
// error & warning messages
var messages = { errors: [], warnings: [] };
// helper function to determine if raw has a boundary.circle property
var hasBoundaryCircleField = function(field) {
return raw.hasOwnProperty('boundary.circle.' + field);
};
if (['lat', 'lon', 'radius'].some(hasBoundaryCircleField)) {
messages.warnings.push('boundary.circle is currently unsupported and being ignored');
}
try {
geo_common.sanitize_point( 'point', clean, raw, LAT_LON_IS_REQUIRED );
// this hack is to allow point.lat/point.lon to be used interchanagbly
// with boundary.circle.lat/boundary.circle.lon
//
// if clean doesn't have boundary.circle.lat but clean has point.lat, set boundary.circle.lat to point.lat
if( !clean.hasOwnProperty('boundary.circle.lat') && clean.hasOwnProperty('point.lat') ){
raw['boundary.circle.lat'] = clean['point.lat'];
}

1
test/unit/run.js

@ -30,6 +30,7 @@ var tests = [
require('./middleware/distance'),
require('./middleware/confidenceScoreReverse'),
require('./sanitiser/_size'),
require('./sanitiser/_geo_reverse'),
];
tests.map(function(t) {

67
test/unit/sanitiser/_geo_reverse.js

@ -0,0 +1,67 @@
var sanitize = require('../../../sanitiser/_geo_reverse');
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 = {
'point.lat': '12.121212',
'point.lon': '21.212121',
'boundary.circle.lat': '13.131313'
};
var clean = {};
var errorsAndWarnings = sanitize(raw, clean);
t.equals(clean['boundary.circle.lat'], 12.121212, 'should be set to point.lat');
t.deepEquals(errorsAndWarnings, {
errors: [],
warnings: ['boundary.circle is currently unsupported and being ignored']
}, 'no warnings/errors');
t.end();
});
test('raw with boundary.circle.lon should add warning about ignored boundary.circle', function(t) {
var raw = {
'point.lat': '12.121212',
'point.lon': '21.212121',
'boundary.circle.lon': '31.313131'
};
var clean = {};
var errorsAndWarnings = sanitize(raw, clean);
t.equals(clean['boundary.circle.lon'], 21.212121, 'should be set to point.lon');
t.deepEquals(errorsAndWarnings, {
errors: [],
warnings: ['boundary.circle is currently unsupported and being ignored']
}, 'no warnings/errors');
t.end();
});
test('raw with boundary.circle.radius should add warning about ignored boundary.circle', function(t) {
var raw = {
'point.lat': '12.121212',
'point.lon': '21.212121',
'boundary.circle.radius': '17'
};
var clean = {};
var errorsAndWarnings = sanitize(raw, clean);
// t.equals(clean['boundary.circle.radius'], 12.121212, 'should be set to point.lat')
t.deepEquals(errorsAndWarnings, {
errors: [],
warnings: ['boundary.circle is currently unsupported and being ignored']
}, 'no warnings/errors');
t.end();
});
};
module.exports.all = function (tape, common) {
function test(name, testFunction) {
return tape('SANTIZE _geo_reverse ' + name, testFunction);
}
for( var testCase in module.exports.tests ){
module.exports.tests[testCase](test, common);
}
};
Loading…
Cancel
Save