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.
216 lines
5.9 KiB
216 lines
5.9 KiB
8 years ago
|
var sanitize = require('../../../sanitizer/_ids');
|
||
9 years ago
|
|
||
|
var delimiter = ':';
|
||
9 years ago
|
var type_mapping = require('../../../helper/type_mapping');
|
||
9 years ago
|
|
||
|
var formatError = function(input) {
|
||
9 years ago
|
return 'id `' + input + ' is invalid: must be of the format source:layer:id for ex: \'geonames:venue:4163334\'';
|
||
9 years ago
|
};
|
||
9 years ago
|
var lengthError = 'invalid param \'ids\': length must be >0';
|
||
9 years ago
|
|
||
|
module.exports.tests = {};
|
||
|
|
||
9 years ago
|
module.exports.tests.invalid_ids = function(test, common) {
|
||
|
test('invalid id: empty string', function(t) {
|
||
|
var raw = { ids: '' };
|
||
|
var clean = {};
|
||
9 years ago
|
|
||
9 years ago
|
var messages = sanitize(raw, clean);
|
||
|
|
||
9 years ago
|
t.equal(messages.errors[0], lengthError, 'ids length error returned');
|
||
9 years ago
|
t.equal(clean.ids, undefined, 'ids unset in clean object');
|
||
|
t.end();
|
||
|
});
|
||
|
|
||
|
test('invalid id: single colon', function(t) {
|
||
|
var raw = { ids: ':' };
|
||
|
var clean = {};
|
||
|
|
||
|
var messages = sanitize(raw, clean);
|
||
|
|
||
9 years ago
|
t.equal(messages.errors[0], formatError(':'), 'format error returned');
|
||
9 years ago
|
t.equal(clean.ids, undefined, 'ids unset in clean object');
|
||
9 years ago
|
t.end();
|
||
|
});
|
||
|
|
||
9 years ago
|
test('invalid id: double colon', function(t) {
|
||
|
var raw = { ids: '::' };
|
||
|
var clean = {};
|
||
|
|
||
|
var messages = sanitize(raw, clean);
|
||
|
|
||
9 years ago
|
t.equal(messages.errors[0], formatError('::'), 'format error returned');
|
||
9 years ago
|
t.equal(clean.ids, undefined, 'ids unset in clean object');
|
||
|
t.end();
|
||
|
});
|
||
|
|
||
|
test('invalid id: only type section present', function(t) {
|
||
|
var raw = { ids: 'geoname:' };
|
||
|
var clean = {};
|
||
|
|
||
|
var messages = sanitize(raw, clean);
|
||
|
|
||
9 years ago
|
t.equal(messages.errors[0], formatError('geoname:'), 'format error returned');
|
||
9 years ago
|
t.equal(clean.ids, undefined, 'ids unset in clean object');
|
||
|
t.end();
|
||
|
});
|
||
|
|
||
|
test('invalid id: only type id section present', function(t) {
|
||
|
var raw = { ids: ':234' };
|
||
|
var clean = {};
|
||
|
|
||
|
var messages = sanitize(raw, clean);
|
||
|
|
||
9 years ago
|
t.equal(messages.errors[0], formatError(':234'), 'format error returned');
|
||
9 years ago
|
t.equal(clean.ids, undefined, 'ids unset in clean object');
|
||
|
t.end();
|
||
|
});
|
||
|
|
||
9 years ago
|
test('invalid id: source name invalid', function(t) {
|
||
|
var raw = { ids: 'invalidsource:venue:23' };
|
||
9 years ago
|
var clean = {};
|
||
9 years ago
|
var expected_error = 'invalidsource is invalid. It must be one of these values - [' +
|
||
|
Object.keys(type_mapping.source_mapping).join(', ') + ']';
|
||
9 years ago
|
|
||
|
var messages = sanitize(raw, clean);
|
||
|
|
||
9 years ago
|
t.equal(messages.errors[0], expected_error, 'format error returned');
|
||
|
t.equal(clean.ids, undefined, 'ids unset in clean object');
|
||
|
t.end();
|
||
|
});
|
||
|
|
||
|
test('invalid id: old style 2 part id', function(t) {
|
||
|
var raw = { ids: 'geonames:23' };
|
||
|
var clean = {};
|
||
|
|
||
|
var messages = sanitize(raw, clean);
|
||
|
|
||
|
t.equal(messages.errors[0], formatError('geonames:23'), 'format error returned');
|
||
9 years ago
|
t.equal(clean.ids, undefined, 'ids unset in clean object');
|
||
|
t.end();
|
||
|
});
|
||
|
};
|
||
|
|
||
|
module.exports.tests.valid_ids = function(test, common) {
|
||
9 years ago
|
test('ids: valid input (openaddresses)', function(t) {
|
||
|
var raw = { ids: 'openaddresses:address:20' };
|
||
|
var clean = {};
|
||
|
|
||
|
var messages = sanitize( raw, clean );
|
||
|
|
||
9 years ago
|
var expected_ids = [{
|
||
|
source: 'openaddresses',
|
||
|
layer: 'address',
|
||
|
id: '20',
|
||
|
}];
|
||
9 years ago
|
t.deepEqual( messages.errors, [], ' no errors');
|
||
|
t.deepEqual( clean.ids, expected_ids, 'single type value returned');
|
||
9 years ago
|
t.end();
|
||
|
});
|
||
|
|
||
9 years ago
|
test('ids: valid short input (openaddresses)', function(t) {
|
||
|
var raw = { ids: 'oa:address:20' };
|
||
|
var clean = {};
|
||
|
|
||
|
var messages = sanitize( raw, clean );
|
||
|
|
||
|
var expected_ids = [{
|
||
|
source: 'openaddresses',
|
||
|
layer: 'address',
|
||
|
id: '20',
|
||
|
}];
|
||
|
t.deepEqual( messages.errors, [], ' no errors');
|
||
|
t.deepEqual( clean.ids, expected_ids, 'single type value returned');
|
||
|
t.end();
|
||
|
});
|
||
|
|
||
9 years ago
|
test('ids: valid input (osm)', function(t) {
|
||
9 years ago
|
var raw = { ids: 'openstreetmap:venue:node:500' };
|
||
9 years ago
|
var clean = {};
|
||
9 years ago
|
var expected_ids = [{
|
||
9 years ago
|
source: 'openstreetmap',
|
||
|
layer: 'venue',
|
||
|
id: 'node:500',
|
||
9 years ago
|
}];
|
||
9 years ago
|
|
||
|
var messages = sanitize( raw, clean );
|
||
|
|
||
9 years ago
|
t.deepEqual( messages.errors, [], ' no errors');
|
||
9 years ago
|
t.deepEqual( clean.ids, expected_ids, 'osm has node: or way: in id field');
|
||
9 years ago
|
t.end();
|
||
|
});
|
||
9 years ago
|
|
||
|
test('ids: valid short input (osm)', function(t) {
|
||
|
var raw = { ids: 'osm:venue:node:500' };
|
||
|
var clean = {};
|
||
|
var expected_ids = [{
|
||
|
source: 'openstreetmap',
|
||
|
layer: 'venue',
|
||
|
id: 'node:500',
|
||
|
}];
|
||
|
|
||
|
var messages = sanitize( raw, clean );
|
||
|
|
||
|
t.deepEqual( messages.errors, [], ' no errors');
|
||
|
t.deepEqual( clean.ids, expected_ids, 'osm has node: or way: in id field');
|
||
|
t.end();
|
||
|
});
|
||
9 years ago
|
};
|
||
|
|
||
|
module.exports.tests.multiple_ids = function(test, common) {
|
||
9 years ago
|
test('multiple ids', function(t) {
|
||
9 years ago
|
var raw = { ids: 'geonames:venue:1,openstreetmap:address:way:2' };
|
||
9 years ago
|
var clean = {};
|
||
|
|
||
|
var messages = sanitize( raw, clean);
|
||
|
|
||
9 years ago
|
var expected_ids = [ {
|
||
|
source: 'geonames',
|
||
|
layer: 'venue',
|
||
|
id: '1'
|
||
|
}, {
|
||
|
source: 'openstreetmap',
|
||
|
layer: 'address',
|
||
|
id: 'way:2'
|
||
|
} ];
|
||
|
|
||
9 years ago
|
t.deepEqual( messages.errors, [], 'no errors' );
|
||
|
t.deepEqual( messages.warnings, [], 'no warnings' );
|
||
9 years ago
|
t.deepEqual(clean.ids, expected_ids, 'clean set correctly');
|
||
9 years ago
|
t.end();
|
||
|
});
|
||
|
};
|
||
|
|
||
|
module.exports.tests.de_dupe = function(test, common) {
|
||
|
test('duplicate ids', function(t) {
|
||
9 years ago
|
var raw = { ids: 'geonames:venue:1,openstreetmap:venue:node:2,geonames:venue:1' };
|
||
9 years ago
|
var clean = {};
|
||
|
|
||
|
var messages = sanitize( raw, clean );
|
||
|
|
||
9 years ago
|
var expected_ids = [ {
|
||
|
source: 'geonames',
|
||
|
layer: 'venue',
|
||
|
id: '1'
|
||
|
}, {
|
||
|
source: 'openstreetmap',
|
||
|
layer: 'venue',
|
||
|
id: 'node:2'
|
||
|
} ];
|
||
9 years ago
|
t.deepEqual( messages.errors, [], 'no errors' );
|
||
|
t.deepEqual( messages.warnings, [], 'no warnings' );
|
||
9 years ago
|
t.deepEqual(clean.ids, expected_ids, 'clean set correctly');
|
||
9 years ago
|
t.end();
|
||
|
});
|
||
|
};
|
||
|
|
||
|
module.exports.all = function (tape, common) {
|
||
|
function test(name, testFunction) {
|
||
|
return tape('SANTIZE _ids ' + name, testFunction);
|
||
|
}
|
||
|
|
||
|
for( var testCase in module.exports.tests ){
|
||
|
module.exports.tests[testCase](test, common);
|
||
|
}
|
||
|
};
|