mirror of https://github.com/pelias/api.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
464 lines
12 KiB
464 lines
12 KiB
8 years ago
|
var sanitize = require('../../../sanitizer/_geo_common');
|
||
9 years ago
|
|
||
|
module.exports.tests = {};
|
||
|
|
||
|
module.exports.tests.interface = function(test, common) {
|
||
|
test('valid interface', function(t) {
|
||
9 years ago
|
t.equal(typeof sanitize.sanitize_rect, 'function', 'sanitize_rect is a valid function');
|
||
9 years ago
|
t.equal(typeof sanitize.sanitize_coord, 'function', 'sanitize_coord is a valid function');
|
||
9 years ago
|
t.equal(typeof sanitize.sanitize_circle, 'function', 'sanitize_circle is a valid function');
|
||
|
t.equal(typeof sanitize.sanitize_point, 'function', 'sanitize_point is a valid function');
|
||
9 years ago
|
t.end();
|
||
|
});
|
||
|
};
|
||
|
|
||
8 years ago
|
// @note: for better coverage see unit tests for 'wrap.js'.
|
||
8 years ago
|
module.exports.tests.wrapping = function(test, common) {
|
||
|
test('control - no wrapping required', function (t) {
|
||
|
var clean = {};
|
||
|
var params = {
|
||
8 years ago
|
'point.lat': +1.1,
|
||
|
'point.lon': -1.1
|
||
8 years ago
|
};
|
||
|
sanitize.sanitize_point( 'point', clean, params, false );
|
||
8 years ago
|
t.equal(clean['point.lat'], +1.1, 'not changed');
|
||
|
t.equal(clean['point.lon'], -1.1, 'not changed');
|
||
8 years ago
|
t.end();
|
||
|
});
|
||
|
test('positive longitude wrapping', function (t) {
|
||
|
var clean = {};
|
||
|
var params = {
|
||
8 years ago
|
'point.lat': +1.1,
|
||
|
'point.lon': +181.1
|
||
8 years ago
|
};
|
||
|
sanitize.sanitize_point( 'point', clean, params, false );
|
||
8 years ago
|
t.equal(clean['point.lat'], +1.1, 'not changed');
|
||
|
t.equal(clean['point.lon'], -178.9, 'equal to (-180 + 1.1)');
|
||
8 years ago
|
t.end();
|
||
|
});
|
||
|
test('negative longitude wrapping', function (t) {
|
||
|
var clean = {};
|
||
|
var params = {
|
||
8 years ago
|
'point.lat': -1.1,
|
||
|
'point.lon': -181.1
|
||
8 years ago
|
};
|
||
|
sanitize.sanitize_point( 'point', clean, params, false );
|
||
8 years ago
|
t.equal(clean['point.lat'], -1.1, 'not changed');
|
||
|
t.equal(clean['point.lon'], +178.9, 'equal to (+180 - 1.1)');
|
||
8 years ago
|
t.end();
|
||
|
});
|
||
|
test('positive latitudinal wrapping', function (t) {
|
||
|
var clean = {};
|
||
|
var params = {
|
||
8 years ago
|
'point.lat': 91.1,
|
||
|
'point.lon': 1.1
|
||
8 years ago
|
};
|
||
|
sanitize.sanitize_point( 'point', clean, params, false );
|
||
8 years ago
|
t.equal(clean['point.lat'], +88.9, 'equal to (+90 - 1.1)');
|
||
8 years ago
|
t.equal(clean['point.lon'], -178.9, 'equal to (-180 + 1.1)'); // polar flip
|
||
8 years ago
|
t.end();
|
||
|
});
|
||
|
test('negative latitudinal wrapping', function (t) {
|
||
|
var clean = {};
|
||
|
var params = {
|
||
8 years ago
|
'point.lat': -91.1,
|
||
|
'point.lon': -1.1
|
||
8 years ago
|
};
|
||
|
sanitize.sanitize_point( 'point', clean, params, false );
|
||
8 years ago
|
t.equal(clean['point.lat'], -88.9, 'equal to (-90 + 1.1)');
|
||
|
t.equal(clean['point.lon'], +178.9, 'equal to (+180 - 1.1)'); // polar flip
|
||
8 years ago
|
t.end();
|
||
|
});
|
||
|
};
|
||
|
|
||
9 years ago
|
module.exports.tests.coord = function(test, common) {
|
||
|
test('valid coord', function (t) {
|
||
|
var clean = {};
|
||
|
var params = {
|
||
|
'foo': -40.659
|
||
|
};
|
||
|
var mandatory = false;
|
||
|
|
||
9 years ago
|
sanitize.sanitize_coord( 'foo', clean, params, mandatory );
|
||
9 years ago
|
t.equal(clean.foo, params.foo);
|
||
|
t.end();
|
||
|
});
|
||
|
|
||
|
test('invalid coord', function (t) {
|
||
|
var clean = {};
|
||
|
var params = {
|
||
|
'foo': 'bar'
|
||
|
};
|
||
|
var mandatory = false;
|
||
|
|
||
9 years ago
|
sanitize.sanitize_coord( 'foo', clean, params, mandatory );
|
||
9 years ago
|
t.equal(clean.foo, undefined, 'not set');
|
||
|
t.end();
|
||
|
});
|
||
|
|
||
|
test('nothing specified', function (t) {
|
||
|
var clean = {};
|
||
|
var params = {};
|
||
|
var mandatory = false;
|
||
|
|
||
|
t.doesNotThrow( function(){
|
||
9 years ago
|
sanitize.sanitize_coord( 'foo', clean, params, mandatory );
|
||
9 years ago
|
});
|
||
|
t.end();
|
||
|
});
|
||
|
|
||
|
test('nothing specified - mandatory', function (t) {
|
||
|
var clean = {};
|
||
|
var params = {};
|
||
|
var mandatory = true;
|
||
|
|
||
|
t.throws( function(){
|
||
9 years ago
|
sanitize.sanitize_coord( 'foo', clean, params, mandatory );
|
||
9 years ago
|
});
|
||
|
t.end();
|
||
|
});
|
||
|
|
||
|
};
|
||
|
|
||
|
module.exports.tests.point = function(test, common) {
|
||
|
test('valid point duo', function (t) {
|
||
|
var clean = {};
|
||
|
var params = {
|
||
|
'foo.bar.lat': 11,
|
||
|
'foo.bar.lon': 22
|
||
|
};
|
||
|
var mandatory = false;
|
||
|
|
||
|
sanitize.sanitize_point( 'foo.bar', clean, params, mandatory );
|
||
|
t.equal(clean['foo.bar.lat'], params['foo.bar.lat'], 'lat approved');
|
||
|
t.equal(clean['foo.bar.lon'], params['foo.bar.lon'], 'lon approved');
|
||
|
t.end();
|
||
|
});
|
||
|
|
||
|
test('invalid point, lat only', function (t) {
|
||
|
var clean = {};
|
||
|
var params = {
|
||
|
'foo.bar.lat': 11
|
||
|
};
|
||
|
var mandatory = false;
|
||
|
|
||
|
t.throws( function(){
|
||
|
sanitize.sanitize_point( 'foo.bar', clean, params, mandatory );
|
||
|
});
|
||
|
t.end();
|
||
|
});
|
||
|
|
||
|
test('invalid point, lon only', function (t) {
|
||
|
var clean = {};
|
||
|
var params = {
|
||
|
'foo.bar.lon': 22,
|
||
|
};
|
||
|
var mandatory = false;
|
||
|
|
||
|
t.throws( function(){
|
||
|
sanitize.sanitize_point( 'foo.bar', clean, params, mandatory );
|
||
|
});
|
||
|
t.end();
|
||
|
});
|
||
|
|
||
|
test('invalid lat value specified', function (t) {
|
||
|
var clean = {};
|
||
|
var params = {
|
||
|
'foo.bar.lat': 'foobar',
|
||
|
'foo.bar.lon': 22,
|
||
|
};
|
||
|
var mandatory = false;
|
||
|
|
||
|
t.throws( function(){
|
||
|
sanitize.sanitize_point( 'foo.bar', clean, params, mandatory );
|
||
|
});
|
||
|
t.end();
|
||
|
});
|
||
|
|
||
|
test('invalid lon value specified', function (t) {
|
||
|
var clean = {};
|
||
|
var params = {
|
||
|
'foo.bar.lat': 11,
|
||
|
'foo.bar.lon': 'foobar'
|
||
|
};
|
||
|
var mandatory = false;
|
||
|
|
||
|
t.throws( function(){
|
||
|
sanitize.sanitize_point( 'foo.bar', clean, params, mandatory );
|
||
|
});
|
||
|
t.end();
|
||
|
});
|
||
|
|
||
|
test('nothing specified', function (t) {
|
||
|
var clean = {};
|
||
|
var params = {};
|
||
|
var mandatory = false;
|
||
|
|
||
|
t.doesNotThrow( function(){
|
||
|
sanitize.sanitize_point( 'foo.bar', clean, params, mandatory );
|
||
|
});
|
||
|
t.end();
|
||
|
});
|
||
|
|
||
|
test('nothing specified - mandatory', function (t) {
|
||
|
var clean = {};
|
||
|
var params = {};
|
||
|
var mandatory = true;
|
||
|
|
||
|
t.throws( function(){
|
||
|
sanitize.sanitize_point( 'foo.bar', clean, params, mandatory );
|
||
|
});
|
||
|
t.end();
|
||
|
});
|
||
|
};
|
||
|
|
||
|
module.exports.tests.rect = function(test, common) {
|
||
|
test('valid rect quad', function (t) {
|
||
|
var clean = {};
|
||
|
var params = {
|
||
|
'boundary.rect.min_lat': -40.659,
|
||
|
'boundary.rect.max_lat': -41.614,
|
||
|
'boundary.rect.min_lon': 174.612,
|
||
|
'boundary.rect.max_lon': 176.333
|
||
|
};
|
||
|
var mandatory = false;
|
||
|
|
||
|
sanitize.sanitize_rect( 'boundary.rect', clean, params, mandatory );
|
||
|
t.equal(clean['boundary.rect.min_lat'], params['boundary.rect.min_lat'], 'min_lat approved');
|
||
|
t.equal(clean['boundary.rect.max_lat'], params['boundary.rect.max_lat'], 'min_lat approved');
|
||
|
t.equal(clean['boundary.rect.min_lon'], params['boundary.rect.min_lon'], 'min_lat approved');
|
||
|
t.equal(clean['boundary.rect.max_lon'], params['boundary.rect.max_lon'], 'min_lat approved');
|
||
|
t.end();
|
||
|
});
|
||
|
|
||
9 years ago
|
test('valid rect quad, null island', function (t) {
|
||
|
var clean = {};
|
||
|
var params = {
|
||
|
'boundary.rect.min_lat': 0,
|
||
|
'boundary.rect.max_lat': 0,
|
||
|
'boundary.rect.min_lon': 0,
|
||
|
'boundary.rect.max_lon': 0
|
||
|
};
|
||
|
var mandatory = false;
|
||
|
|
||
|
sanitize.sanitize_rect( 'boundary.rect', clean, params, mandatory );
|
||
|
t.equal(clean['boundary.rect.min_lat'], params['boundary.rect.min_lat'], 'min_lat approved');
|
||
|
t.equal(clean['boundary.rect.max_lat'], params['boundary.rect.max_lat'], 'min_lat approved');
|
||
|
t.equal(clean['boundary.rect.min_lon'], params['boundary.rect.min_lon'], 'min_lat approved');
|
||
|
t.equal(clean['boundary.rect.max_lon'], params['boundary.rect.max_lon'], 'min_lat approved');
|
||
|
t.end();
|
||
|
});
|
||
9 years ago
|
test('invalid rect - partially specified', function (t) {
|
||
|
var clean = {};
|
||
|
var params = {
|
||
|
'boundary.rect.min_lat': -40.659
|
||
|
};
|
||
|
var mandatory = false;
|
||
|
|
||
|
t.throws( function(){
|
||
|
sanitize.sanitize_rect( 'boundary.rect', clean, params, mandatory );
|
||
|
});
|
||
|
t.end();
|
||
|
});
|
||
|
|
||
|
test('nothing specified', function (t) {
|
||
|
var clean = {};
|
||
|
var params = {};
|
||
|
var mandatory = false;
|
||
|
|
||
|
t.doesNotThrow( function(){
|
||
|
sanitize.sanitize_rect( 'boundary.rect', clean, params, mandatory );
|
||
|
});
|
||
|
t.end();
|
||
|
});
|
||
|
|
||
|
test('nothing specified - mandatory', function (t) {
|
||
|
var clean = {};
|
||
|
var params = {};
|
||
|
var mandatory = true;
|
||
|
|
||
|
t.throws( function(){
|
||
|
sanitize.sanitize_rect( 'boundary.rect', clean, params, mandatory );
|
||
|
});
|
||
|
t.end();
|
||
|
});
|
||
|
|
||
|
};
|
||
|
|
||
|
module.exports.tests.circle = function(test, common) {
|
||
9 years ago
|
test('valid circle trio', function (t) {
|
||
|
var clean = {};
|
||
|
var params = {
|
||
|
'boundary.circle.lat': 11,
|
||
|
'boundary.circle.lon': 22,
|
||
|
'boundary.circle.radius': 33
|
||
|
};
|
||
9 years ago
|
var mandatory = false;
|
||
9 years ago
|
|
||
9 years ago
|
sanitize.sanitize_circle( 'boundary.circle', clean, params, mandatory );
|
||
9 years ago
|
t.equal(clean['boundary.circle.lat'], params['boundary.circle.lat'], 'lat approved');
|
||
|
t.equal(clean['boundary.circle.lon'], params['boundary.circle.lon'], 'lon approved');
|
||
|
t.equal(clean['boundary.circle.radius'], params['boundary.circle.radius'], 'radius approved');
|
||
9 years ago
|
t.end();
|
||
|
});
|
||
9 years ago
|
|
||
9 years ago
|
test('valid circle duo', function (t) {
|
||
|
var clean = {};
|
||
|
var params = {
|
||
|
'boundary.circle.lat': 11,
|
||
|
'boundary.circle.lon': 22
|
||
|
};
|
||
|
var mandatory = false;
|
||
|
|
||
|
sanitize.sanitize_circle( 'boundary.circle', clean, params, mandatory );
|
||
|
t.equal(clean['boundary.circle.lat'], params['boundary.circle.lat'], 'lat approved');
|
||
|
t.equal(clean['boundary.circle.lon'], params['boundary.circle.lon'], 'lon approved');
|
||
|
t.end();
|
||
|
});
|
||
|
|
||
|
test('invalid circle, lat only', function (t) {
|
||
|
var clean = {};
|
||
|
var params = {
|
||
|
'boundary.circle.lat': 11,
|
||
|
};
|
||
|
var mandatory = false;
|
||
|
|
||
|
t.throws( function(){
|
||
|
sanitize.sanitize_circle( 'boundary.circle', clean, params, mandatory );
|
||
|
});
|
||
|
t.end();
|
||
|
});
|
||
|
|
||
|
test('invalid circle, lon only', function (t) {
|
||
|
var clean = {};
|
||
|
var params = {
|
||
|
'boundary.circle.lon': 22,
|
||
|
};
|
||
|
var mandatory = false;
|
||
|
|
||
|
t.throws( function(){
|
||
|
sanitize.sanitize_circle( 'boundary.circle', clean, params, mandatory );
|
||
|
});
|
||
|
t.end();
|
||
|
});
|
||
|
|
||
|
test('invalid circle, radius only', function (t) {
|
||
9 years ago
|
var clean = {};
|
||
|
var params = {
|
||
|
'boundary.circle.radius': 33
|
||
|
};
|
||
9 years ago
|
var mandatory = false;
|
||
9 years ago
|
|
||
9 years ago
|
t.throws( function(){
|
||
|
sanitize.sanitize_circle( 'boundary.circle', clean, params, mandatory );
|
||
|
});
|
||
|
t.end();
|
||
|
});
|
||
|
|
||
|
test('invalid circle, lon and radius only', function (t) {
|
||
|
var clean = {};
|
||
|
var params = {
|
||
|
'boundary.circle.lon': 22,
|
||
|
'boundary.circle.radius': 33
|
||
|
};
|
||
|
var mandatory = false;
|
||
|
|
||
|
t.throws( function(){
|
||
|
sanitize.sanitize_circle( 'boundary.circle', clean, params, mandatory );
|
||
|
});
|
||
|
t.end();
|
||
|
});
|
||
|
|
||
|
test('invalid circle, lat and radius only', function (t) {
|
||
|
var clean = {};
|
||
|
var params = {
|
||
|
'boundary.circle.lat': 11,
|
||
|
'boundary.circle.radius': 33
|
||
|
};
|
||
|
var mandatory = false;
|
||
|
|
||
|
t.throws( function(){
|
||
|
sanitize.sanitize_circle( 'boundary.circle', clean, params, mandatory );
|
||
|
});
|
||
|
t.end();
|
||
|
});
|
||
|
|
||
|
test('invalid lat value specified', function (t) {
|
||
|
var clean = {};
|
||
|
var params = {
|
||
|
'boundary.circle.lat': 'foobar',
|
||
|
'boundary.circle.lon': 22,
|
||
|
'boundary.circle.radius': 33
|
||
|
};
|
||
|
var mandatory = false;
|
||
|
|
||
|
t.throws( function(){
|
||
|
sanitize.sanitize_circle( 'boundary.circle', clean, params, mandatory );
|
||
|
});
|
||
|
t.end();
|
||
|
});
|
||
|
|
||
|
test('invalid lon value specified', function (t) {
|
||
|
var clean = {};
|
||
|
var params = {
|
||
|
'boundary.circle.lat': 11,
|
||
|
'boundary.circle.lon': 'foobar',
|
||
|
'boundary.circle.radius': 33
|
||
|
};
|
||
|
var mandatory = false;
|
||
|
|
||
|
t.throws( function(){
|
||
|
sanitize.sanitize_circle( 'boundary.circle', clean, params, mandatory );
|
||
|
});
|
||
|
t.end();
|
||
|
});
|
||
|
|
||
|
test('invalid radius value specified', function (t) {
|
||
|
var clean = {};
|
||
|
var params = {
|
||
|
'boundary.circle.lat': 11,
|
||
|
'boundary.circle.lon': 22,
|
||
|
'boundary.circle.radius': 'foobar'
|
||
|
};
|
||
|
var mandatory = false;
|
||
|
|
||
|
t.throws( function(){
|
||
|
sanitize.sanitize_circle( 'boundary.circle', clean, params, mandatory );
|
||
|
});
|
||
|
t.end();
|
||
|
});
|
||
|
|
||
|
test('nothing specified', function (t) {
|
||
|
var clean = {};
|
||
|
var params = {};
|
||
|
var mandatory = false;
|
||
|
|
||
|
t.doesNotThrow( function(){
|
||
|
sanitize.sanitize_circle( 'boundary.circle', clean, params, mandatory );
|
||
|
});
|
||
|
t.end();
|
||
|
});
|
||
|
|
||
|
test('nothing specified - mandatory', function (t) {
|
||
|
var clean = {};
|
||
|
var params = {};
|
||
|
var mandatory = true;
|
||
|
|
||
|
t.throws( function(){
|
||
|
sanitize.sanitize_circle( 'boundary.circle', clean, params, mandatory );
|
||
|
});
|
||
9 years ago
|
t.end();
|
||
|
});
|
||
|
};
|
||
|
|
||
|
module.exports.all = function (tape, common) {
|
||
|
|
||
|
function test(name, testFunction) {
|
||
|
return tape('SANITIZER _geo_common ' + name, testFunction);
|
||
|
}
|
||
|
|
||
|
for( var testCase in module.exports.tests ){
|
||
|
module.exports.tests[testCase](test, common);
|
||
|
}
|
||
|
};
|