mirror of https://github.com/pelias/api.git
Diana Shkolnikov
9 years ago
79 changed files with 1678 additions and 591 deletions
@ -0,0 +1,35 @@
|
||||
|
||||
var _ = require('lodash'), |
||||
check = require('check-types'), |
||||
schemas = require('./labelSchema.json'); |
||||
|
||||
module.exports = function( record ){ |
||||
|
||||
var labelParts = [ record.name.default ]; |
||||
|
||||
var schema = schemas.default; |
||||
|
||||
if (record.country_a && record.country_a.length && schemas[record.country_a]) { |
||||
schema = schemas[record.country_a]; |
||||
} |
||||
|
||||
var buildOutput = function(parts, schemaArr, record) { |
||||
for (var i=0; i<schemaArr.length; i++) { |
||||
var fieldValue = record[schemaArr[i]]; |
||||
if (check.unemptyString(fieldValue) && !_.contains(parts, fieldValue)) { |
||||
parts.push( fieldValue ); |
||||
return parts; |
||||
} |
||||
} |
||||
return parts; |
||||
}; |
||||
|
||||
for (var key in schema) { |
||||
labelParts = buildOutput(labelParts, schema[key], record); |
||||
} |
||||
|
||||
// de-dupe outputs
|
||||
labelParts = _.unique( labelParts ); |
||||
|
||||
return labelParts.join(', ').trim(); |
||||
}; |
@ -1,37 +0,0 @@
|
||||
|
||||
var schemas = require('./outputSchema.json'); |
||||
|
||||
module.exports = function( record ){ |
||||
|
||||
var adminParts = []; |
||||
|
||||
var schema = schemas.default; |
||||
|
||||
if (record.country_a && record.country_a.length && schemas[record.country_a]) { |
||||
schema = schemas[record.country_a]; |
||||
} |
||||
|
||||
var buildOutput = function(parts, schemaArr, record) { |
||||
for (var i=0; i<schemaArr.length; i++) { |
||||
var rec = record[schemaArr[i]]; |
||||
if (rec && rec.length) { |
||||
parts.push( rec ); |
||||
return parts; |
||||
} |
||||
} |
||||
return parts; |
||||
}; |
||||
|
||||
for (var key in schema) { |
||||
adminParts = buildOutput(adminParts, schema[key], record);
|
||||
} |
||||
|
||||
var outputs = [ record.name.default ].concat( adminParts ); |
||||
|
||||
// de-dupe outputs
|
||||
outputs = outputs.filter( function( i, pos ) { |
||||
return outputs.indexOf( i ) === pos; |
||||
}); |
||||
|
||||
return outputs.join(', ').trim(); |
||||
}; |
@ -0,0 +1,87 @@
|
||||
var extend = require('extend'), |
||||
_ = require('lodash'); |
||||
|
||||
var TYPE_TO_SOURCE = { |
||||
'geoname': 'gn', |
||||
'osmnode': 'osm', |
||||
'osmway': 'osm', |
||||
'admin0': 'qs', |
||||
'admin1': 'qs', |
||||
'admin2': 'qs', |
||||
'neighborhood': 'qs', |
||||
'locality': 'qs', |
||||
'local_admin': 'qs', |
||||
'osmaddress': 'osm', |
||||
'openaddresses': 'oa' |
||||
}; |
||||
|
||||
/* |
||||
* This doesn't include alias layers such as coarse |
||||
*/ |
||||
var TYPE_TO_LAYER = { |
||||
'geoname': 'venue', |
||||
'osmnode': 'venue', |
||||
'osmway': 'venue', |
||||
'admin0': 'country', |
||||
'admin1': 'region', |
||||
'admin2': 'county', |
||||
'neighborhood': 'neighbourhood', |
||||
'locality': 'locality', |
||||
'local_admin': 'localadmin', |
||||
'osmaddress': 'address', |
||||
'openaddresses': 'address' |
||||
}; |
||||
|
||||
var SOURCE_TO_TYPE = { |
||||
'gn' : ['geoname'], |
||||
'geonames' : ['geoname'], |
||||
'oa' : ['openaddresses'], |
||||
'openaddresses' : ['openaddresses'], |
||||
'qs' : ['admin0', 'admin1', 'admin2', 'neighborhood', 'locality', 'local_admin'], |
||||
'quattroshapes' : ['admin0', 'admin1', 'admin2', 'neighborhood', 'locality', 'local_admin'], |
||||
'osm' : ['osmaddress', 'osmnode', 'osmway'], |
||||
'openstreetmap' : ['osmaddress', 'osmnode', 'osmway'] |
||||
}; |
||||
|
||||
/** |
||||
* This does not included alias layers, those are built separately |
||||
*/ |
||||
var LAYER_TO_TYPE = { |
||||
'venue': ['geoname','osmnode','osmway'], |
||||
'address': ['osmaddress','openaddresses', 'geoname'], |
||||
'country': ['admin0', 'geoname'], |
||||
'region': ['admin1', 'geoname'], |
||||
'county': ['admin2', 'geoname'], |
||||
'locality': ['locality', 'geoname'], |
||||
'localadmin': ['local_admin'], |
||||
'neighbourhood': ['neighborhood', 'geoname'] |
||||
}; |
||||
|
||||
var LAYER_ALIASES = { |
||||
'coarse': ['admin0','admin1','admin2','neighborhood','locality','local_admin'] |
||||
}; |
||||
|
||||
var LAYER_WITH_ALIASES_TO_TYPE = extend({}, LAYER_ALIASES, LAYER_TO_TYPE); |
||||
|
||||
/* |
||||
* derive the list of types, sources, and layers from above mappings |
||||
*/ |
||||
var TYPES = Object.keys(TYPE_TO_SOURCE); |
||||
var SOURCES = Object.keys(SOURCE_TO_TYPE); |
||||
var LAYERS = Object.keys(LAYER_TO_TYPE); |
||||
|
||||
var sourceAndLayerToType = function sourceAndLayerToType(source, layer) { |
||||
return _.intersection(SOURCE_TO_TYPE[source], LAYER_WITH_ALIASES_TO_TYPE[layer]); |
||||
}; |
||||
|
||||
module.exports = { |
||||
types: TYPES, |
||||
sources: SOURCES, |
||||
layers: LAYERS, |
||||
type_to_source: TYPE_TO_SOURCE, |
||||
type_to_layer: TYPE_TO_LAYER, |
||||
source_to_type: SOURCE_TO_TYPE, |
||||
layer_to_type: LAYER_TO_TYPE, |
||||
layer_with_aliases_to_type: LAYER_WITH_ALIASES_TO_TYPE, |
||||
source_and_layer_to_type: sourceAndLayerToType |
||||
}; |
@ -1,15 +0,0 @@
|
||||
/* |
||||
* Mapping from data layers to type values |
||||
*/ |
||||
|
||||
module.exports = { |
||||
'venue': ['geoname','osmnode','osmway'], |
||||
'address': ['osmaddress','openaddresses'], |
||||
'country': ['admin0'], |
||||
'region': ['admin1'], |
||||
'county': ['admin2'], |
||||
'locality': ['locality'], |
||||
'localadmin': ['local_admin'], |
||||
'neighbourhood': ['neighborhood'], |
||||
'coarse': ['admin0','admin1','admin2','neighborhood','locality','local_admin'], |
||||
}; |
@ -1,14 +0,0 @@
|
||||
/* |
||||
* Mapping from data sources to type values |
||||
*/ |
||||
|
||||
module.exports = { |
||||
'gn' : ['geoname'], |
||||
'geonames' : ['geoname'], |
||||
'oa' : ['openaddresses'], |
||||
'openaddresses' : ['openaddresses'], |
||||
'qs' : ['admin0', 'admin1', 'admin2', 'neighborhood', 'locality', 'local_admin'], |
||||
'quattroshapes' : ['admin0', 'admin1', 'admin2', 'neighborhood', 'locality', 'local_admin'], |
||||
'osm' : ['osmaddress', 'osmnode', 'osmway'], |
||||
'openstreetmap' : ['osmaddress', 'osmnode', 'osmway'] |
||||
}; |
@ -1,16 +0,0 @@
|
||||
|
||||
// querable types
|
||||
|
||||
module.exports = [ |
||||
'geoname', |
||||
'osmnode', |
||||
'osmway', |
||||
'admin0', |
||||
'admin1', |
||||
'admin2', |
||||
'neighborhood', |
||||
'locality', |
||||
'local_admin', |
||||
'osmaddress', |
||||
'openaddresses' |
||||
]; |
@ -0,0 +1,23 @@
|
||||
|
||||
var _ = require('lodash'), |
||||
check = require('check-types'); |
||||
|
||||
// validate inputs
|
||||
function sanitize( raw, clean ){ |
||||
// error & warning messages
|
||||
var messages = { errors: [], warnings: [] }; |
||||
|
||||
Object.keys(raw).forEach(function(key) { |
||||
if (_.isArray(raw[key])) { |
||||
messages.errors.push('\'' + key + '\' parameter can only have one value'); |
||||
} else if (_.isObject(raw[key])) { |
||||
messages.errors.push('\'' + key + '\' parameter must be a scalar'); |
||||
} |
||||
|
||||
}); |
||||
|
||||
return messages; |
||||
} |
||||
|
||||
// export function
|
||||
module.exports = sanitize; |
@ -0,0 +1,30 @@
|
||||
|
||||
#> set size |
||||
path: '/v1/reverse?point.lat=1&point.lon=1¶m=value1¶m=value2' |
||||
|
||||
#? 200 ok |
||||
response.statusCode.should.be.equal 200 |
||||
response.should.have.header 'charset', 'utf8' |
||||
response.should.have.header 'content-type', 'application/json; charset=utf-8' |
||||
|
||||
#? valid geocoding block |
||||
should.exist json.geocoding |
||||
should.exist json.geocoding.version |
||||
should.exist json.geocoding.attribution |
||||
should.exist json.geocoding.query |
||||
should.exist json.geocoding.engine |
||||
should.exist json.geocoding.engine.name |
||||
should.exist json.geocoding.engine.author |
||||
should.exist json.geocoding.engine.version |
||||
should.exist json.geocoding.timestamp |
||||
|
||||
#? valid geojson |
||||
json.type.should.be.equal 'FeatureCollection' |
||||
json.features.should.be.instanceof Array |
||||
|
||||
#? expected warnings |
||||
should.not.exist json.geocoding.warnings |
||||
|
||||
#? expected errors |
||||
should.exist json.geocoding.errors |
||||
json.geocoding.errors.should.eql [ '\'param\' parameter can only have one value' ] |
@ -0,0 +1,30 @@
|
||||
|
||||
#> set size |
||||
path: '/v1/reverse?point.lat=1&point.lon=1¶meter[idx]=value' |
||||
|
||||
#? 200 ok |
||||
response.statusCode.should.be.equal 200 |
||||
response.should.have.header 'charset', 'utf8' |
||||
response.should.have.header 'content-type', 'application/json; charset=utf-8' |
||||
|
||||
#? valid geocoding block |
||||
should.exist json.geocoding |
||||
should.exist json.geocoding.version |
||||
should.exist json.geocoding.attribution |
||||
should.exist json.geocoding.query |
||||
should.exist json.geocoding.engine |
||||
should.exist json.geocoding.engine.name |
||||
should.exist json.geocoding.engine.author |
||||
should.exist json.geocoding.engine.version |
||||
should.exist json.geocoding.timestamp |
||||
|
||||
#? valid geojson |
||||
json.type.should.be.equal 'FeatureCollection' |
||||
json.features.should.be.instanceof Array |
||||
|
||||
#? expected warnings |
||||
should.not.exist json.geocoding.warnings |
||||
|
||||
#? expected errors |
||||
should.exist json.geocoding.errors |
||||
json.geocoding.errors.should.eql [ '\'parameter\' parameter must be a scalar' ] |
@ -0,0 +1,228 @@
|
||||
|
||||
var generator = require('../../../helper/labelGenerator'); |
||||
|
||||
module.exports.tests = {}; |
||||
|
||||
module.exports.tests.interface = function(test, common) { |
||||
test('interface', function(t) { |
||||
t.equal(typeof generator, 'function', 'valid function'); |
||||
t.end(); |
||||
}); |
||||
}; |
||||
|
||||
// major USA city
|
||||
module.exports.tests.san_francisco = function(test, common) { |
||||
test('san francisco', function(t) { |
||||
var doc = { |
||||
'name': { 'default': 'San Francisco' }, |
||||
'country_a': 'USA', |
||||
'country': 'United States', |
||||
'region': 'California', |
||||
'region_a': 'CA', |
||||
'county': 'San Francisco County', |
||||
'locality': 'San Francisco' |
||||
}; |
||||
t.equal(generator(doc),'San Francisco, San Francisco County, CA'); |
||||
t.end(); |
||||
}); |
||||
}; |
||||
|
||||
// USA venue
|
||||
module.exports.tests.nyc_office = function(test, common) { |
||||
test('30 West 26th Street', function(t) { |
||||
var doc = { |
||||
'name': { 'default': '30 West 26th Street' }, |
||||
'housenumber': '30', |
||||
'street': 'West 26th Street', |
||||
'postalcode': '10010', |
||||
'country_a': 'USA', |
||||
'country': 'United States', |
||||
'region': 'New York', |
||||
'region_a': 'NY', |
||||
'county': 'New York County', |
||||
'localadmin': 'Manhattan', |
||||
'locality': 'New York', |
||||
'neighbourhood': 'Flatiron District' |
||||
}; |
||||
t.equal(generator(doc),'30 West 26th Street, Manhattan, NY'); |
||||
t.end(); |
||||
}); |
||||
}; |
||||
|
||||
// AUS state
|
||||
module.exports.tests.new_south_wales = function(test, common) { |
||||
test('new south wales', function(t) { |
||||
var doc = { |
||||
'name': { 'default': 'New South Wales' }, |
||||
'country_a': 'AUS', |
||||
'country': 'Australia', |
||||
'region': 'New South Wales' |
||||
}; |
||||
t.equal(generator(doc),'New South Wales, Australia'); |
||||
t.end(); |
||||
}); |
||||
}; |
||||
|
||||
// USA state
|
||||
module.exports.tests.california = function(test, common) { |
||||
test('california', function(t) { |
||||
var doc = { |
||||
'name': { 'default': 'California' }, |
||||
'country_a': 'USA', |
||||
'country': 'United States', |
||||
'region': 'California', |
||||
'region_a': 'CA' |
||||
}; |
||||
t.equal(generator(doc),'California, CA'); |
||||
t.end(); |
||||
}); |
||||
}; |
||||
|
||||
// IND state
|
||||
module.exports.tests.west_bengal = function(test, common) { |
||||
test('west bengal', function(t) { |
||||
var doc = { |
||||
'name': { 'default': 'West Bengal' }, |
||||
'country_a': 'IND', |
||||
'country': 'India', |
||||
'region': 'West Bengal' |
||||
}; |
||||
t.equal(generator(doc),'West Bengal, India'); |
||||
t.end(); |
||||
}); |
||||
}; |
||||
|
||||
// SGP region
|
||||
module.exports.tests.north_west_singapore = function(test, common) { |
||||
test('north west singapore', function(t) { |
||||
var doc = { |
||||
'name': { 'default': 'North West' }, |
||||
'country_a': 'SGP', |
||||
'country': 'Singapore', |
||||
'region': 'North West' |
||||
}; |
||||
t.equal(generator(doc),'North West, Singapore'); |
||||
t.end(); |
||||
}); |
||||
}; |
||||
|
||||
// IRQ region
|
||||
module.exports.tests.arbil = function(test, common) { |
||||
test('arbil', function(t) { |
||||
var doc = { |
||||
'name': { 'default': 'Arbil' }, |
||||
'country_a': 'IRQ', |
||||
'country': 'Iraq', |
||||
'region': 'Arbil' |
||||
}; |
||||
t.equal(generator(doc),'Arbil, Iraq'); |
||||
t.end(); |
||||
}); |
||||
}; |
||||
|
||||
// ESP city
|
||||
module.exports.tests.madrid = function(test, common) { |
||||
test('madrid', function(t) { |
||||
var doc = { |
||||
'name': { 'default': 'Madrid' }, |
||||
'country_a': 'ESP', |
||||
'country': 'Spain', |
||||
'region': 'Madrid' |
||||
}; |
||||
t.equal(generator(doc),'Madrid, Spain'); |
||||
t.end(); |
||||
}); |
||||
}; |
||||
|
||||
// GBR street address
|
||||
module.exports.tests.one_main_street_uk = function(test, common) { |
||||
test('one main street uk', function(t) { |
||||
var doc = { |
||||
'name': { 'default': '1 Main St' }, |
||||
'housenumber': '1', |
||||
'street': 'Main St', |
||||
'postalcode': 'BT77 0BG', |
||||
'country_a': 'GBR', |
||||
'country': 'United Kingdom', |
||||
'region': 'Dungannon' |
||||
}; |
||||
t.equal(generator(doc),'1 Main St, Dungannon, United Kingdom'); |
||||
t.end(); |
||||
}); |
||||
}; |
||||
|
||||
// GBR venue
|
||||
module.exports.tests.hackney_city_farm = function(test, common) { |
||||
test('hackney city farm', function(t) { |
||||
var doc = { |
||||
'name': { 'default': 'Hackney City Farm' }, |
||||
'country_a': 'GBR', |
||||
'country': 'United Kingdom', |
||||
'region': 'Hackney', |
||||
'county': 'Greater London', |
||||
'locality': 'London', |
||||
'neighbourhood': 'Haggerston' |
||||
}; |
||||
t.equal(generator(doc),'Hackney City Farm, Haggerston, Greater London'); |
||||
t.end(); |
||||
}); |
||||
}; |
||||
|
||||
// DEU street address
|
||||
module.exports.tests.one_grolmanstrasse = function(test, common) { |
||||
test('one grolmanstrasse', function(t) { |
||||
var doc = { |
||||
'name': { 'default': '1 Grolmanstraße' }, |
||||
'housenumber': '1', |
||||
'street': 'Grolmanstraße', |
||||
'postalcode': '10623', |
||||
'country_a': 'DEU', |
||||
'country': 'Germany', |
||||
'region': 'Berlin', |
||||
'county': 'Berlin', |
||||
'locality': 'Berlin', |
||||
'neighbourhood': 'Halensee' |
||||
}; |
||||
t.equal(generator(doc),'1 Grolmanstraße, Berlin, Germany'); |
||||
t.end(); |
||||
}); |
||||
}; |
||||
|
||||
// NZD country
|
||||
module.exports.tests.new_zealand = function(test, common) { |
||||
test('new zealand', function(t) { |
||||
var doc = { |
||||
'name': { 'default': 'New Zealand' }, |
||||
'country_a': 'NZL', |
||||
'country': 'New Zealand' |
||||
}; |
||||
t.equal(generator(doc),'New Zealand'); |
||||
t.end(); |
||||
}); |
||||
}; |
||||
|
||||
// SGP venue
|
||||
module.exports.tests.singapore_mcdonalds = function(test, common) { |
||||
test('singapore_mcdonalds', function(t) { |
||||
var doc = { |
||||
'name': { 'default': 'McDonald\'s' }, |
||||
'country_a': 'SGP', |
||||
'country': 'Singapore', |
||||
'region': 'Central Singapore', |
||||
'locality': 'Singapore' |
||||
}; |
||||
t.equal(generator(doc),'McDonald\'s, Central Singapore, Singapore'); |
||||
t.end(); |
||||
}); |
||||
}; |
||||
|
||||
module.exports.all = function (tape, common) { |
||||
|
||||
function test(name, testFunction) { |
||||
return tape('label generator: ' + name, testFunction); |
||||
} |
||||
|
||||
for( var testCase in module.exports.tests ){ |
||||
module.exports.tests[testCase](test, common); |
||||
} |
||||
}; |
@ -1,5 +1,5 @@
|
||||
|
||||
var schemas = require('../../../helper/outputSchema.json'); |
||||
var schemas = require('../../../helper/labelSchema.json'); |
||||
var alpha3 = require('../mock/alpha3.json'); |
||||
|
||||
module.exports.tests = {}; |
@ -0,0 +1,52 @@
|
||||
var check = require('check-types'); |
||||
var type_mapping = require('../../../helper/type_mapping'); |
||||
|
||||
module.exports.tests = {}; |
||||
|
||||
module.exports.tests.interfaces = function(test, common) { |
||||
test('types list', function(t) { |
||||
t.ok(check.array(type_mapping.types), 'is array'); |
||||
t.ok(check.hasLength(type_mapping.types, 11), 'has correct number of elements'); |
||||
t.end(); |
||||
}); |
||||
|
||||
test('type to source mapping', function(t) { |
||||
t.ok(check.object(type_mapping.type_to_source), 'is object'); |
||||
t.ok(check.hasLength(Object.keys(type_mapping.type_to_source), 11), 'has correct number of elements'); |
||||
t.end(); |
||||
}); |
||||
|
||||
test('type to layer mapping', function(t) { |
||||
t.ok(check.object(type_mapping.type_to_layer), 'is object'); |
||||
t.ok(check.hasLength(Object.keys(type_mapping.type_to_layer), 11), 'has correct number of elements'); |
||||
t.end(); |
||||
}); |
||||
|
||||
test('source to type mapping', function(t) { |
||||
t.ok(check.object(type_mapping.source_to_type), 'is object'); |
||||
t.ok(check.hasLength(Object.keys(type_mapping.source_to_type), 8), 'has correct number of elements'); |
||||
t.end(); |
||||
}); |
||||
|
||||
test('layer to type mapping', function(t) { |
||||
t.ok(check.object(type_mapping.layer_to_type), 'is object'); |
||||
t.equal(Object.keys(type_mapping.layer_to_type).length, 8, 'has correct number of elements'); |
||||
t.end(); |
||||
}); |
||||
|
||||
test('layer to type mapping (with aliases)', function(t) { |
||||
t.ok(check.object(type_mapping.layer_with_aliases_to_type), 'is object'); |
||||
t.ok(check.hasLength(Object.keys(type_mapping.layer_with_aliases_to_type), 9), 'has correct number of elements'); |
||||
t.end(); |
||||
}); |
||||
}; |
||||
|
||||
module.exports.all = function (tape, common) { |
||||
function test(name, testFunction) { |
||||
return tape('type_mapping: ' + name, testFunction); |
||||
} |
||||
|
||||
for( var testCase in module.exports.tests ){ |
||||
module.exports.tests[testCase](test, common); |
||||
} |
||||
}; |
@ -0,0 +1,82 @@
|
||||
var confidenceScore = require('../../../middleware/confidenceScore')(); |
||||
|
||||
module.exports.tests = {}; |
||||
|
||||
module.exports.tests.confidenceScore = function(test, common) { |
||||
|
||||
test('empty res and req should not throw exception', function(t) { |
||||
try { |
||||
confidenceScore({}, {}, function() {}); |
||||
t.pass('no exception'); |
||||
} |
||||
catch (e) { |
||||
t.fail('an exception should not have been thrown'); |
||||
} |
||||
finally { |
||||
t.end(); |
||||
} |
||||
|
||||
}); |
||||
|
||||
test('res.results without parsed_text should not throw exception', function(t) { |
||||
var req = {}; |
||||
var res = { |
||||
data: [{ |
||||
name: 'foo' |
||||
}], |
||||
meta: [10] |
||||
}; |
||||
|
||||
try { |
||||
confidenceScore(req, res, function() {}); |
||||
t.pass('no exception'); |
||||
} |
||||
catch (e) { |
||||
t.fail('an exception should not have been thrown'); |
||||
console.log(e.stack); |
||||
} |
||||
finally { |
||||
t.end(); |
||||
} |
||||
|
||||
}); |
||||
|
||||
|
||||
test('res.results without parsed_text should not throw exception', function(t) { |
||||
var req = { |
||||
clean: { text: 'test name1' } |
||||
}; |
||||
var res = { |
||||
data: [{ |
||||
_score: 10, |
||||
found: true, |
||||
value: 1, |
||||
center_point: { lat: 100.1, lon: -50.5 }, |
||||
name: { default: 'test name1' }, |
||||
admin0: 'country1', admin1: 'state1', admin2: 'city1' |
||||
}, { |
||||
value: 2, |
||||
center_point: { lat: 100.2, lon: -51.5 }, |
||||
name: { default: 'test name2' }, |
||||
admin0: 'country2', admin1: 'state2', admin2: 'city2', |
||||
_score: 20 |
||||
}], |
||||
meta: {scores: [10]} |
||||
}; |
||||
|
||||
confidenceScore(req, res, function() {}); |
||||
t.equal(res.data[0].confidence, 0.6, 'score was set'); |
||||
t.end(); |
||||
}); |
||||
|
||||
}; |
||||
|
||||
module.exports.all = function (tape, common) { |
||||
function test(name, testFunction) { |
||||
return tape('[middleware] confidenceScore: ' + name, testFunction); |
||||
} |
||||
|
||||
for( var testCase in module.exports.tests ){ |
||||
module.exports.tests[testCase](test, common); |
||||
} |
||||
}; |
@ -1,23 +0,0 @@
|
||||
|
||||
var types = require('../../../query/types'); |
||||
|
||||
module.exports.tests = {}; |
||||
|
||||
module.exports.tests.interface = function(test, common) { |
||||
test('valid interface', function(t) { |
||||
t.true(Array.isArray(types), 'valid array'); |
||||
t.equal(types.length, 11, 'valid array'); |
||||
t.end(); |
||||
}); |
||||
}; |
||||
|
||||
module.exports.all = function (tape, common) { |
||||
|
||||
function test(name, testFunction) { |
||||
return tape('types ' + name, testFunction); |
||||
} |
||||
|
||||
for( var testCase in module.exports.tests ){ |
||||
module.exports.tests[testCase](test, common); |
||||
} |
||||
}; |
@ -0,0 +1,114 @@
|
||||
var sanitize = require('../../../sanitiser/_geo_reverse'); |
||||
var defaults = require('../../../query/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 = { |
||||
'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'] |
||||
}, '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'] |
||||
}, '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'] |
||||
}, '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); |
||||
|
||||
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 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); |
||||
|
||||
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('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); |
||||
|
||||
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) { |
||||
function test(name, testFunction) { |
||||
return tape('SANTIZE _geo_reverse ' + name, testFunction); |
||||
} |
||||
|
||||
for( var testCase in module.exports.tests ){ |
||||
module.exports.tests[testCase](test, common); |
||||
} |
||||
}; |
@ -0,0 +1,60 @@
|
||||
var sanitize = require('../../../sanitiser/_single_scalar_parameters'); |
||||
|
||||
module.exports.tests = {}; |
||||
|
||||
module.exports.tests.single_scalar_parameters = function(test, common) { |
||||
test('all duplicate parameters should have error messages returned', function(t) { |
||||
var raw = { |
||||
arrayParameter1: ['value1', 'value2'], |
||||
scalarParameter: 'value', |
||||
arrayParameter2: ['value3'] |
||||
}; |
||||
var clean = {}; |
||||
var errorsAndWarnings = sanitize(raw, clean); |
||||
t.deepEquals(errorsAndWarnings, { |
||||
errors: [ |
||||
'\'arrayParameter1\' parameter can only have one value', |
||||
'\'arrayParameter2\' parameter can only have one value', |
||||
], |
||||
warnings: [] |
||||
}); |
||||
t.end(); |
||||
}); |
||||
|
||||
test('object parameters should have error messages returned', function(t) { |
||||
var raw = { |
||||
objectParameter1: { key1: 'value1', key2: 'value2'}, |
||||
scalarParameter: 'value', |
||||
objectParameter2: { } |
||||
}; |
||||
var clean = {}; |
||||
var errorsAndWarnings = sanitize(raw, clean); |
||||
t.deepEquals(errorsAndWarnings, { |
||||
errors: [ |
||||
'\'objectParameter1\' parameter must be a scalar', |
||||
'\'objectParameter2\' parameter must be a scalar' |
||||
], |
||||
warnings: [] |
||||
}); |
||||
t.end(); |
||||
}); |
||||
|
||||
test('request with all scalar parameters should return empty errors', function(t) { |
||||
var raw = { scalarParameter1: 'value1', scalarParameter2: 2, scalarParameter3: true }; |
||||
var clean = {}; |
||||
var errorsAndWarnings = sanitize(raw, clean); |
||||
t.deepEquals(errorsAndWarnings, { errors: [], warnings: [] }); |
||||
t.end(); |
||||
}); |
||||
|
||||
}; |
||||
|
||||
module.exports.all = function (tape, common) { |
||||
function test(name, testFunction) { |
||||
return tape('SANTIZE _single_scalar_parameters ' + name, testFunction); |
||||
} |
||||
|
||||
for( var testCase in module.exports.tests ){ |
||||
module.exports.tests[testCase](test, common); |
||||
} |
||||
}; |
@ -1,127 +0,0 @@
|
||||
var sanitize = require( '../../../sanitiser/_source' ); |
||||
|
||||
var success_response = { error: false }; |
||||
|
||||
module.exports.tests = {}; |
||||
|
||||
module.exports.tests.no_sources = function(test, common) { |
||||
test('source is not set', function(t) { |
||||
var req = { |
||||
query: { }, |
||||
clean: { } |
||||
}; |
||||
|
||||
var response = sanitize(req.query, req.clean); |
||||
|
||||
t.deepEqual(req.clean.types, {}, 'clean.types should be empty object'); |
||||
t.deepEqual(response.errors, [], 'no error returned'); |
||||
t.deepEqual(response.warnings, [], 'no warnings returned'); |
||||
t.end(); |
||||
}); |
||||
|
||||
test('source is empty string', function(t) { |
||||
var req = { |
||||
query: { |
||||
source: '' |
||||
}, |
||||
clean: { } |
||||
}; |
||||
|
||||
var response = sanitize(req.query, req.clean); |
||||
|
||||
t.deepEqual(req.clean.types, {}, 'clean.types should be empty object'); |
||||
t.deepEqual(response.errors, [], 'no error returned'); |
||||
t.deepEqual(response.warnings, [], 'no warnings returned'); |
||||
t.end(); |
||||
}); |
||||
}; |
||||
|
||||
module.exports.tests.valid_sources = function(test, common) { |
||||
test('geonames source', function(t) { |
||||
var req = { |
||||
query: { |
||||
source: 'geonames' |
||||
}, |
||||
clean: { } |
||||
}; |
||||
|
||||
var response = sanitize(req.query, req.clean); |
||||
|
||||
t.deepEqual(req.clean.types, { from_source: ['geoname'] }, 'clean.types should contain from_source entry with geonames'); |
||||
t.deepEqual(response.errors, [], 'no error returned'); |
||||
t.deepEqual(response.warnings, [], 'no warnings returned'); |
||||
t.end(); |
||||
}); |
||||
|
||||
test('openstreetmap source', function(t) { |
||||
var req = { |
||||
query: { |
||||
source: 'openstreetmap' |
||||
}, |
||||
clean: { } |
||||
}; |
||||
var expected_types = { |
||||
from_source: ['osmaddress', 'osmnode', 'osmway'] |
||||
}; |
||||
|
||||
var response = sanitize(req.query, req.clean); |
||||
|
||||
t.deepEqual(req.clean.types, expected_types, 'clean.types should contain from_source entry with multiple entries for openstreetmap'); |
||||
t.deepEqual(response.errors, [], 'no error returned'); |
||||
t.deepEqual(response.warnings, [], 'no warnings returned'); |
||||
t.end(); |
||||
}); |
||||
|
||||
test('multiple sources', function(t) { |
||||
var req = { |
||||
query: { |
||||
source: 'openstreetmap,openaddresses' |
||||
}, |
||||
clean: { } |
||||
}; |
||||
var expected_types = { |
||||
from_source: ['osmaddress', 'osmnode', 'osmway', 'openaddresses'] |
||||
}; |
||||
|
||||
var response = sanitize(req.query, req.clean); |
||||
|
||||
t.deepEqual(req.clean.types, expected_types, |
||||
'clean.types should contain from_source entry with multiple entries for openstreetmap and openadresses'); |
||||
t.deepEqual(response.errors, [], 'no error returned'); |
||||
t.deepEqual(response.warnings, [], 'no warnings returned'); |
||||
t.end(); |
||||
}); |
||||
}; |
||||
|
||||
module.exports.tests.invalid_sources = function(test, common) { |
||||
test('geonames source', function(t) { |
||||
var req = { |
||||
query: { |
||||
source: 'notasource' |
||||
}, |
||||
clean: { } |
||||
}; |
||||
var expected_response = { |
||||
errors: [ |
||||
'\'notasource\' is an invalid source parameter. Valid options: geonames,openaddresses,quattroshapes,openstreetmap' |
||||
], |
||||
warnings: [] |
||||
}; |
||||
|
||||
var response = sanitize(req.query, req.clean); |
||||
|
||||
t.deepEqual(response, expected_response, 'error with message returned'); |
||||
t.deepEqual(req.clean.types, { }, 'clean.types should remain empty'); |
||||
t.end(); |
||||
}); |
||||
}; |
||||
|
||||
module.exports.all = function (tape, common) { |
||||
function test(name, testFunction) { |
||||
return tape('SANTIZE _source ' + name, testFunction); |
||||
} |
||||
|
||||
for( var testCase in module.exports.tests ){ |
||||
module.exports.tests[testCase](test, common); |
||||
} |
||||
}; |
Loading…
Reference in new issue