@ -1,20 +1,16 @@
var sanitize = require ( '../../../sanitiser/_ids' ) ;
var sanitize = require ( '../../../sanitiser/_ids' ) ;
var delimiter = ':' ;
var delimiter = ':' ;
var types = require ( '../../../query/types ' ) ;
var type _mapping = require ( '../../../helper/type_mapping ' ) ;
var inputs = {
var inputs = {
valid : [ 'geoname:1' , 'osmnode:2' , 'admin0:53' , 'osmway:44' , 'geoname:5' ] ,
valid : [ 'geoname:1' , 'osmnode:2' , 'admin0:53' , 'osmway:44' , 'geoname:5' ] ,
invalid : [ ':' , '' , '::' , 'geoname:' , ':234' , 'gibberish:23' ]
invalid : [ ':' , '' , '::' , 'geoname:' , ':234' , 'gibberish:23' ]
} ;
} ;
var formatError = function ( input ) {
var formatError = function ( input ) {
return 'id `' + input + 'is invalid: must be of the format type:id for ex: \'geonam e:4163334\'' ;
return 'id `' + input + ' is invalid: must be of the format source:layer:id for ex: \'geonames:venu e:4163334\'' ;
} ;
} ;
var lengthError = 'invalid param \'ids\': length must be >0' ;
var lengthError = 'invalid param \'ids\': length must be >0' ;
var defaultMissingTypeError = function ( input ) {
var type = input . split ( delimiter ) [ 0 ] ;
return type + ' is invalid. It must be one of these values - [' + types . join ( ', ' ) + ']' ;
} ;
module . exports . tests = { } ;
module . exports . tests = { } ;
@ -74,50 +70,58 @@ module.exports.tests.invalid_ids = function(test, common) {
t . end ( ) ;
t . end ( ) ;
} ) ;
} ) ;
test ( 'invalid id: typ e name invalid' , function ( t ) {
test ( 'invalid id: sourc e name invalid' , function ( t ) {
var raw = { ids : 'gibberish :23' } ;
var raw = { ids : 'invalidsource:venue :23' } ;
var clean = { } ;
var clean = { } ;
var expected _error = 'invalidsource is invalid. It must be one of these values - [' + type _mapping . sources . join ( ', ' ) + ']' ;
var messages = sanitize ( raw , clean ) ;
var messages = sanitize ( raw , clean ) ;
t . equal ( messages . errors [ 0 ] , defaultMissingTypeError ( 'gibberish:23' ) , 'format error returned' ) ;
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' ) ;
t . equal ( clean . ids , undefined , 'ids unset in clean object' ) ;
t . equal ( clean . ids , undefined , 'ids unset in clean object' ) ;
t . end ( ) ;
t . end ( ) ;
} ) ;
} ) ;
} ;
} ;
module . exports . tests . valid _ids = function ( test , common ) {
module . exports . tests . valid _ids = function ( test , common ) {
test ( 'ids: valid input' , function ( t ) {
test ( 'ids: valid input (openaddresses) ' , function ( t ) {
inputs . valid . forEach ( function ( input ) {
var raw = { ids : 'openaddresses:address:20' } ;
var input _parts = input . split ( delimiter ) ;
var clean = { } ;
var expected _clean = { ids : [ { id : input _parts [ 1 ] , type : input _parts [ 0 ] } ] } ;
var expected _ids = [ {
var raw = { ids : input } ;
id : '20' ,
var clean = { } ;
types : [ 'openaddresses' ]
} ] ;
var messages = sanitize ( raw , clean ) ;
var messages = sanitize ( raw , clean ) ;
t . deepEqual ( messages . errors , [ ] , 'no error (' + input + ')' ) ;
t . deepEqual ( clean , expected _clean , 'clean set correctly (' + input + ') ') ;
t . deepEqual ( messages . errors , [ ] , ' no errors ') ;
} ) ;
t . deepEqual ( clean . ids , expected _ids , 'single type value returned' ) ;
t . end ( ) ;
t . end ( ) ;
} ) ;
} ) ;
test ( 'ids: valid input with multiple values' , function ( t ) {
test ( 'ids: valid input (osm)' , function ( t ) {
var raw = { ids : inputs . valid . join ( ',' ) } ;
var raw = { ids : 'osm:venue:500' } ;
var clean = { } ;
var clean = { } ;
var expected _clean = {
var expected _ids = [ {
ids : [ ] ,
id : '500' ,
} ;
types : [ 'osmnode' , 'osmway' ]
// construct the expected id and type for each valid input
} ] ;
inputs . valid . forEach ( function ( input ) {
var input _parts = input . split ( delimiter ) ;
expected _clean . ids . push ( { id : input _parts [ 1 ] , type : input _parts [ 0 ] } ) ;
} ) ;
var messages = sanitize ( raw , clean ) ;
var messages = sanitize ( raw , clean ) ;
t . deepEqual ( messages . errors , [ ] , 'no errors' ) ;
t . deepEqual ( messages . errors , [ ] , ' no errors' ) ;
t . deepEqual ( clean , expected _clean , 'clean set correctly' ) ;
t . deepEqual ( clean . ids , expected _ids , 'osm could be two types, but that\'s ok' ) ;
t . end ( ) ;
t . end ( ) ;
} ) ;
} ) ;
} ;
} ;
@ -137,10 +141,10 @@ module.exports.tests.array_of_ids = function(test, common) {
} ;
} ;
module . exports . tests . multiple _ids = function ( test , common ) {
module . exports . tests . multiple _ids = function ( test , common ) {
test ( 'duplicate ids' , function ( t ) {
test ( 'multiple ids' , function ( t ) {
var expected _clean = { ids : [ { id : '1' , type : 'geoname' } , { id : '2' , type : 'osmnode' } ] } ;
var raw = { ids : 'geonames:venue:1,osm:venue:2' } ;
var raw = { ids : 'geoname:1,osmnode:2' } ;
var clean = { } ;
var clean = { } ;
var expected _clean = { ids : [ { id : '1' , types : [ 'geoname' ] } , { id : '2' , types : [ 'osmnode' , 'osmway' ] } ] } ;
var messages = sanitize ( raw , clean ) ;
var messages = sanitize ( raw , clean ) ;
@ -153,8 +157,8 @@ module.exports.tests.multiple_ids = function(test, common) {
module . exports . tests . de _dupe = function ( test , common ) {
module . exports . tests . de _dupe = function ( test , common ) {
test ( 'duplicate ids' , function ( t ) {
test ( 'duplicate ids' , function ( t ) {
var expected _clean = { ids : [ { id : '1' , type : 'geoname' } , { id : '2' , type : 'osmnode' } ] } ;
var expected _clean = { ids : [ { id : '1' , types : [ 'geoname' ] } , { id : '2' , types : [ 'osmnode', 'osmway' ] } ] } ;
var raw = { ids : 'geoname:1,osmnode:2,geonam e:1' } ;
var raw = { ids : 'geonames:venue:1,osm:venue:2,geonames:venu e:1' } ;
var clean = { } ;
var clean = { } ;
var messages = sanitize ( raw , clean ) ;
var messages = sanitize ( raw , clean ) ;