Browse Source

Merge branch 'master' of github.com:pelias/api into missinglink

pull/529/head
missinglink 9 years ago
parent
commit
b80efff0ea
  1. 5
      bin/units
  2. 8
      controller/search.js
  3. 45
      helper/labelGenerator.js
  4. 79
      helper/labelSchema.js
  5. 26
      helper/text_parser.js
  6. 4
      helper/type_mapping.js
  7. 3
      middleware/localNamingConventions.js
  8. 2
      package.json
  9. 3
      test/ciao/autocomplete/layers_alias_coarse.coffee
  10. 2
      test/ciao/autocomplete/layers_invalid.coffee
  11. 2
      test/ciao/autocomplete/layers_mix_invalid_valid.coffee
  12. 3
      test/ciao/reverse/layers_alias_coarse.coffee
  13. 2
      test/ciao/reverse/layers_invalid.coffee
  14. 2
      test/ciao/reverse/layers_mix_invalid_valid.coffee
  15. 3
      test/ciao/search/layers_alias_coarse.coffee
  16. 2
      test/ciao/search/layers_invalid.coffee
  17. 2
      test/ciao/search/layers_mix_invalid_valid.coffee
  18. 17
      test/unit/controller/search.js
  19. 24
      test/unit/helper/geojsonify.js
  20. 205
      test/unit/helper/labelGenerator_CAN.js
  21. 190
      test/unit/helper/labelGenerator_GBR.js
  22. 51
      test/unit/helper/labelGenerator_SGP.js
  23. 53
      test/unit/helper/labelGenerator_SWE.js
  24. 287
      test/unit/helper/labelGenerator_USA.js
  25. 356
      test/unit/helper/labelGenerator_default.js
  26. 116
      test/unit/helper/labelGenerator_examples.js
  27. 770
      test/unit/helper/labelSchema.js
  28. 20
      test/unit/helper/text_parser.js
  29. 4
      test/unit/helper/type_mapping.js
  30. 19
      test/unit/middleware/localNamingConventions.js
  31. 8
      test/unit/mock/backend.js
  32. 4
      test/unit/run.js
  33. 14
      test/unit/sanitiser/_layers.js

5
bin/units

@ -0,0 +1,5 @@
#!/bin/bash
set -euo pipefail
node test/unit/run.js | ./node_modules/.bin/tap-dot

8
controller/search.js

@ -1,3 +1,5 @@
var _ = require('lodash');
var service = { search: require('../service/search') }; var service = { search: require('../service/search') };
var logger = require('pelias-logger').get('api:controller:search'); var logger = require('pelias-logger').get('api:controller:search');
@ -36,7 +38,11 @@ function setup( backend, query ){
// error handler // error handler
if( err ){ if( err ){
req.errors.push( err ); if (_.isObject(err) && err.message) {
req.errors.push( err.message );
} else {
req.errors.push( err );
}
} }
// set response data // set response data
else { else {

45
helper/labelGenerator.js

@ -4,37 +4,28 @@ var _ = require('lodash'),
module.exports = function( record ){ module.exports = function( record ){
var schema = getSchema(record.country_a); var schema = getSchema(record.country_a);
// in virtually all cases, this will be the `name` field
var labelParts = getInitialLabel(record); var labelParts = getInitialLabel(record);
for (var key in schema) { // iterate the schema
var valueFunction = schema[key]; for (var field in schema) {
var valueFunction = schema[field];
labelParts = valueFunction(record, labelParts); labelParts.push(valueFunction(record));
} }
// NOTE: while it may seem odd to call `uniq` on the list of label parts, // retain only things that are truthy
// the effect is quite subtle. Take, for instance, a result for "Lancaster, PA" labelParts = _.compact(labelParts);
// the pseudo-object is:
// { // first, dedupe the name and 1st label array elements
// 'name': 'Lancaster', // this is used to ensure that the `name` and first admin hierarchy elements aren't repeated
// 'locality': 'Lancaster', // eg - `["Lancaster", "Lancaster", "PA", "United States"]` -> `["Lancaster", "PA", "United States"]`
// 'region_a': 'PA', var dedupedNameAndFirstLabelElement = _.uniq([labelParts.shift(), labelParts.shift()]);
// 'country_a': 'USA'
// } // second, unshift the deduped parts back onto the labelParts
// labelParts.unshift.apply(labelParts, dedupedNameAndFirstLabelElement);
// the code up to this point generates the label:
// `Lancaster, Lancaster, PA, USA` // third, join with a comma and return
// return labelParts.join(', ');
// then the `unique` call reduces this to:
// `Lancaster, PA, USA`
//
// this code doesn't have the same effect in the case of a venue or address
// where the `name` field would contain the address or name of a point-of-interest
//
// Also see https://github.com/pelias/api/issues/429 for other ways that this is bad
//
// de-dupe, join, trim
return _.uniq( labelParts ).join(', ').trim();
}; };

79
helper/labelSchema.js

@ -1,66 +1,73 @@
var _ = require('lodash'), var _ = require('lodash');
check = require('check-types');
module.exports = { module.exports = {
'USA': { 'default': {
'local': getFirstProperty(['borough', 'localadmin', 'locality', 'neighbourhood', 'county']), 'local': getFirstProperty(['locality', 'localadmin']),
'regional': getUsState, 'country': getFirstProperty(['country'])
'country': getFirstProperty(['country_a'])
}, },
'GBR': { 'GBR': {
'local': getFirstProperty(['neighbourhood', 'county', 'localadmin', 'locality', 'macroregion', 'region']), 'local': getFirstProperty(['locality', 'localadmin']),
'regional': getFirstProperty(['county','country','region']) 'regional': getFirstProperty(['macroregion']),
}, 'country': getFirstProperty(['country'])
'SGP': {
'local': getFirstProperty(['neighbourhood', 'region', 'county', 'localadmin', 'locality']),
'regional': getFirstProperty(['county','country','region'])
}, },
'SWE': { 'USA': {
'local': getFirstProperty(['neighbourhood', 'region', 'county', 'localadmin', 'locality']), 'borough': getFirstProperty(['borough']),
'regional': getFirstProperty(['country']) 'local': getFirstProperty(['locality', 'localadmin']),
'regional': getUsOrCaState,
'country': getUSACountryValue
}, },
'default': { 'CAN': {
'local': getFirstProperty(['localadmin', 'locality', 'neighbourhood', 'county', 'macroregion', 'region']), 'local': getFirstProperty(['locality']), // no localadmins in CAN
'regional': getFirstProperty(['country']) 'regional': getUsOrCaState,
'country': getFirstProperty(['country'])
} }
}; };
// find the first field of record that has a non-empty value that's not already in labelParts // find the first field of record that has a non-empty value that's not already in labelParts
function getFirstProperty(fields) { function getFirstProperty(fields) {
return function(record, labelParts) { return function(record) {
for (var i = 0; i < fields.length; i++) { for (var i = 0; i < fields.length; i++) {
var fieldValue = record[fields[i]]; var fieldValue = record[fields[i]];
if (check.nonEmptyString(fieldValue) && !_.includes(labelParts, fieldValue)) { if (!_.isEmpty(fieldValue)) {
labelParts.push( fieldValue ); return fieldValue;
return labelParts;
} }
} }
return labelParts;
}; };
} }
// this function is exclusively used for figuring out which field to use for US States // this function is exclusively used for figuring out which field to use for US/CA States
// 1. if a US state is the most granular bit of info entered, the label should contain // 1. if a US/CA state is the most granular bit of info entered, the label should contain
// the full state name, eg: Pennsylvania, USA // the full state name, eg: Pennsylvania, USA and Ontario, CA
// 2. otherwise, the state abbreviation should be used, eg: Lancaster, PA, USA // 2. otherwise, the state abbreviation should be used, eg: Lancaster, PA, USA and Bruce, ON, CA
// 3. if for some reason the abbreviation isn't available, use the full state name // 3. if for some reason the abbreviation isn't available, use the full state name
function getUsState(record, labelParts) { function getUsOrCaState(record) {
if ('region' === record.layer && record.region) { if ('region' === record.layer && record.region) {
// add full state name when state is the most granular piece of info // return full state name when state is the most granular piece of info
labelParts.push(record.region); return record.region;
} else if (record.region_a) { } else if (record.region_a) {
// otherwise just add the region code when available // otherwise just return the region code when available
labelParts.push(record.region_a); return record.region_a;
} else if (record.region) { } else if (record.region) {
// add the full name when there's no region code available () // return the full name when there's no region code available
labelParts.push(record.region); return record.region;
} }
return labelParts; }
// this function returns the full name of a country if the result is in the
// country layer (for "United States" record). It returns the abbreviation
// otherwise (eg - Lancaster, PA, USA).
function getUSACountryValue(record) {
if ('country' === record.layer && record.country) {
return record.country;
}
return record.country_a;
} }

26
helper/text_parser.js

@ -2,11 +2,10 @@
var parser = require('addressit'); var parser = require('addressit');
var extend = require('extend'); var extend = require('extend');
var type_mapping = require('../helper/type_mapping'); var type_mapping = require('../helper/type_mapping');
var delim = ',';
var check = require('check-types'); var check = require('check-types');
var logger = require('pelias-logger').get('api'); var logger = require('pelias-logger').get('api');
module.exports = {}; var DELIM = ',';
/* /*
* For performance, and to prefer POI and admin records, express a preference * For performance, and to prefer POI and admin records, express a preference
@ -21,14 +20,21 @@ module.exports.get_layers = function get_layers(query) {
module.exports.get_parsed_address = function get_parsed_address(query) { module.exports.get_parsed_address = function get_parsed_address(query) {
var getAdminPartsBySplittingOnDelim = function(query) { var getAdminPartsBySplittingOnDelim = function(queryParts) {
// naive approach - for admin matching during query time // naive approach - for admin matching during query time
// split 'flatiron, new york, ny' into 'flatiron' and 'new york, ny' // split 'flatiron, new york, ny' into 'flatiron' and 'new york, ny'
var delimIndex = query.indexOf(delim);
var address = {}; var address = {};
if ( delimIndex !== -1 ) {
address.name = query.substring(0, delimIndex); if (queryParts.length > 1) {
address.admin_parts = query.substring(delimIndex + 1).trim(); address.name = queryParts[0].trim();
// 1. slice away all parts after the first one
// 2. trim spaces from each part just in case
// 3. join the parts back together with appropriate delimiter and spacing
address.admin_parts = queryParts.slice(1)
.map(function (part) { return part.trim(); })
.join(DELIM + ' ');
} }
return address; return address;
@ -42,8 +48,10 @@ module.exports.get_parsed_address = function get_parsed_address(query) {
} }
}; };
var addressWithAdminParts = getAdminPartsBySplittingOnDelim(query); var queryParts = query.split(DELIM);
var addressWithAddressParts= getAddressParts(query);
var addressWithAdminParts = getAdminPartsBySplittingOnDelim(queryParts);
var addressWithAddressParts= getAddressParts(queryParts.join(DELIM + ' '));
var parsedAddress = extend(addressWithAdminParts, var parsedAddress = extend(addressWithAdminParts,
addressWithAddressParts); addressWithAddressParts);

4
helper/type_mapping.js

@ -48,8 +48,8 @@ var LAYERS_BY_SOURCE = {
openstreetmap: [ 'address', 'venue' ], openstreetmap: [ 'address', 'venue' ],
openaddresses: [ 'address' ], openaddresses: [ 'address' ],
geonames: [ 'country', 'region', 'county', 'locality', 'venue' ], geonames: [ 'country', 'region', 'county', 'locality', 'venue' ],
whosonfirst: [ 'continent', 'macrocountry', 'country', 'dependency', 'region', whosonfirst: [ 'continent', 'country', 'dependency', 'macroregion', 'region',
'locality', 'localadmin', 'county', 'macrohood', 'neighbourhood', 'microhood', 'disputed'] 'locality', 'localadmin', 'macrocounty', 'county', 'macrohood', 'neighbourhood', 'microhood', 'disputed']
}; };
/* /*

3
middleware/localNamingConventions.js

@ -23,6 +23,9 @@ function applyLocalNamingConventions(req, res, next) {
// loop through data items and flip relevant number/street // loop through data items and flip relevant number/street
res.data.filter(function(place){ res.data.filter(function(place){
// do nothing for records with no admin info
if (!place.parent || !place.parent.country_a) { return false; }
// relevant for some countries // relevant for some countries
var flip = place.parent.country_a.some(function(country) { var flip = place.parent.country_a.some(function(country) {
return _.includes(flipNumberAndStreetCountries, country); return _.includes(flipNumberAndStreetCountries, country);

2
package.json

@ -9,7 +9,7 @@
"scripts": { "scripts": {
"start": "node index.js", "start": "node index.js",
"test": "npm run unit", "test": "npm run unit",
"unit": "node test/unit/run.js | tap-dot", "unit": "./bin/units",
"ciao": "node node_modules/ciao/bin/ciao -c test/ciao.json test/ciao", "ciao": "node node_modules/ciao/bin/ciao -c test/ciao.json test/ciao",
"coverage": "node_modules/.bin/istanbul cover test/unit/run.js", "coverage": "node_modules/.bin/istanbul cover test/unit/run.js",
"audit": "npm shrinkwrap; node node_modules/nsp/bin/nspCLI.js audit-shrinkwrap; rm npm-shrinkwrap.json;", "audit": "npm shrinkwrap; node node_modules/nsp/bin/nspCLI.js audit-shrinkwrap; rm npm-shrinkwrap.json;",

3
test/ciao/autocomplete/layers_alias_coarse.coffee

@ -32,12 +32,13 @@ should.not.exist json.geocoding.warnings
json.geocoding.query['text'].should.eql 'a' json.geocoding.query['text'].should.eql 'a'
json.geocoding.query['size'].should.eql 10 json.geocoding.query['size'].should.eql 10
json.geocoding.query.layers.should.eql [ "continent", json.geocoding.query.layers.should.eql [ "continent",
"macrocountry",
"country", "country",
"dependency", "dependency",
"macroregion",
"region", "region",
"locality", "locality",
"localadmin", "localadmin",
"macrocounty",
"county", "county",
"macrohood", "macrohood",
"neighbourhood", "neighbourhood",

2
test/ciao/autocomplete/layers_invalid.coffee

@ -24,7 +24,7 @@ json.features.should.be.instanceof Array
#? expected errors #? expected errors
should.exist json.geocoding.errors should.exist json.geocoding.errors
json.geocoding.errors.should.eql [ '\'notlayer\' is an invalid layers parameter. Valid options: coarse,address,venue,country,region,county,locality,continent,macrocountry,dependency,localadmin,macrohood,neighbourhood,microhood,disputed' ] json.geocoding.errors.should.eql [ '\'notlayer\' is an invalid layers parameter. Valid options: coarse,address,venue,country,macroregion,region,county,locality,continent,macrocounty,dependency,localadmin,macrohood,neighbourhood,microhood,disputed' ]
#? expected warnings #? expected warnings
should.not.exist json.geocoding.warnings should.not.exist json.geocoding.warnings

2
test/ciao/autocomplete/layers_mix_invalid_valid.coffee

@ -24,7 +24,7 @@ json.features.should.be.instanceof Array
#? expected errors #? expected errors
should.exist json.geocoding.errors should.exist json.geocoding.errors
json.geocoding.errors.should.eql [ '\'notlayer\' is an invalid layers parameter. Valid options: coarse,address,venue,country,region,county,locality,continent,macrocountry,dependency,localadmin,macrohood,neighbourhood,microhood,disputed' ] json.geocoding.errors.should.eql [ '\'notlayer\' is an invalid layers parameter. Valid options: coarse,address,venue,country,macroregion,region,county,locality,continent,macrocounty,dependency,localadmin,macrohood,neighbourhood,microhood,disputed' ]
#? expected warnings #? expected warnings
should.not.exist json.geocoding.warnings should.not.exist json.geocoding.warnings

3
test/ciao/reverse/layers_alias_coarse.coffee

@ -31,12 +31,13 @@ should.not.exist json.geocoding.warnings
#? inputs #? inputs
json.geocoding.query['size'].should.eql 10 json.geocoding.query['size'].should.eql 10
json.geocoding.query.layers.should.eql [ "continent", json.geocoding.query.layers.should.eql [ "continent",
"macrocountry",
"country", "country",
"dependency", "dependency",
"macroregion",
"region", "region",
"locality", "locality",
"localadmin", "localadmin",
"macrocounty",
"county", "county",
"macrohood", "macrohood",
"neighbourhood", "neighbourhood",

2
test/ciao/reverse/layers_invalid.coffee

@ -24,7 +24,7 @@ json.features.should.be.instanceof Array
#? expected errors #? expected errors
should.exist json.geocoding.errors should.exist json.geocoding.errors
json.geocoding.errors.should.eql [ '\'notlayer\' is an invalid layers parameter. Valid options: coarse,address,venue,country,region,county,locality,continent,macrocountry,dependency,localadmin,macrohood,neighbourhood,microhood,disputed' ] json.geocoding.errors.should.eql [ '\'notlayer\' is an invalid layers parameter. Valid options: coarse,address,venue,country,macroregion,region,county,locality,continent,macrocounty,dependency,localadmin,macrohood,neighbourhood,microhood,disputed' ]
#? expected warnings #? expected warnings
should.not.exist json.geocoding.warnings should.not.exist json.geocoding.warnings

2
test/ciao/reverse/layers_mix_invalid_valid.coffee

@ -24,7 +24,7 @@ json.features.should.be.instanceof Array
#? expected errors #? expected errors
should.exist json.geocoding.errors should.exist json.geocoding.errors
json.geocoding.errors.should.eql [ '\'notlayer\' is an invalid layers parameter. Valid options: coarse,address,venue,country,region,county,locality,continent,macrocountry,dependency,localadmin,macrohood,neighbourhood,microhood,disputed' ] json.geocoding.errors.should.eql [ '\'notlayer\' is an invalid layers parameter. Valid options: coarse,address,venue,country,macroregion,region,county,locality,continent,macrocounty,dependency,localadmin,macrohood,neighbourhood,microhood,disputed' ]
#? expected warnings #? expected warnings
should.not.exist json.geocoding.warnings should.not.exist json.geocoding.warnings

3
test/ciao/search/layers_alias_coarse.coffee

@ -32,12 +32,13 @@ should.not.exist json.geocoding.warnings
json.geocoding.query['text'].should.eql 'a' json.geocoding.query['text'].should.eql 'a'
json.geocoding.query['size'].should.eql 10 json.geocoding.query['size'].should.eql 10
json.geocoding.query.layers.should.eql [ "continent", json.geocoding.query.layers.should.eql [ "continent",
"macrocountry",
"country", "country",
"dependency", "dependency",
"macroregion",
"region", "region",
"locality", "locality",
"localadmin", "localadmin",
"macrocounty",
"county", "county",
"macrohood", "macrohood",
"neighbourhood", "neighbourhood",

2
test/ciao/search/layers_invalid.coffee

@ -24,7 +24,7 @@ json.features.should.be.instanceof Array
#? expected errors #? expected errors
should.exist json.geocoding.errors should.exist json.geocoding.errors
json.geocoding.errors.should.eql [ '\'notlayer\' is an invalid layers parameter. Valid options: coarse,address,venue,country,region,county,locality,continent,macrocountry,dependency,localadmin,macrohood,neighbourhood,microhood,disputed' ] json.geocoding.errors.should.eql [ '\'notlayer\' is an invalid layers parameter. Valid options: coarse,address,venue,country,macroregion,region,county,locality,continent,macrocounty,dependency,localadmin,macrohood,neighbourhood,microhood,disputed' ]
#? expected warnings #? expected warnings
should.not.exist json.geocoding.warnings should.not.exist json.geocoding.warnings

2
test/ciao/search/layers_mix_invalid_valid.coffee

@ -24,7 +24,7 @@ json.features.should.be.instanceof Array
#? expected errors #? expected errors
should.exist json.geocoding.errors should.exist json.geocoding.errors
json.geocoding.errors.should.eql [ '\'notlayer\' is an invalid layers parameter. Valid options: coarse,address,venue,country,region,county,locality,continent,macrocountry,dependency,localadmin,macrohood,neighbourhood,microhood,disputed' ] json.geocoding.errors.should.eql [ '\'notlayer\' is an invalid layers parameter. Valid options: coarse,address,venue,country,macroregion,region,county,locality,continent,macrocounty,dependency,localadmin,macrohood,neighbourhood,microhood,disputed' ]
#? expected warnings #? expected warnings
should.not.exist json.geocoding.warnings should.not.exist json.geocoding.warnings

17
test/unit/controller/search.js

@ -123,6 +123,21 @@ module.exports.tests.functional_failure = function(test, common) {
}); });
}; };
module.exports.tests.timeout = function(test, common) {
test('timeout', function(t) {
var backend = mockBackend( 'client/search/timeout/1', function( cmd ){
t.deepEqual(cmd, { body: { a: 'b' }, index: 'pelias', searchType: 'dfs_query_then_fetch' }, 'correct backend command');
});
var controller = setup( backend, mockQuery() );
var req = { clean: { a: 'b' }, errors: [], warnings: [] };
var next = function(){
t.equal(req.errors[0],'Request Timeout after 5000ms');
t.end();
};
controller(req, undefined, next );
});
};
module.exports.all = function (tape, common) { module.exports.all = function (tape, common) {
function test(name, testFunction) { function test(name, testFunction) {
@ -132,4 +147,4 @@ module.exports.all = function (tape, common) {
for( var testCase in module.exports.tests ){ for( var testCase in module.exports.tests ){
module.exports.tests[testCase](test, common); module.exports.tests[testCase](test, common);
} }
}; };

24
test/unit/helper/geojsonify.js

@ -59,6 +59,7 @@ module.exports.tests.search = function(test, common) {
'country': 'United Kingdom', 'country': 'United Kingdom',
'region': 'Islington', 'region': 'Islington',
'region_a': 'ISL', 'region_a': 'ISL',
'macroregion': 'England',
'county': 'Angel', 'county': 'Angel',
'localadmin': 'test1', 'localadmin': 'test1',
'locality': 'test2', 'locality': 'test2',
@ -84,6 +85,7 @@ module.exports.tests.search = function(test, common) {
'country': 'United Kingdom', 'country': 'United Kingdom',
'region': 'City And County Of The City Of London', 'region': 'City And County Of The City Of London',
'region_a': 'COL', 'region_a': 'COL',
'macroregion': 'England',
'county': 'Smithfield', 'county': 'Smithfield',
'localadmin': 'test1', 'localadmin': 'test1',
'locality': 'test2', 'locality': 'test2',
@ -106,7 +108,7 @@ module.exports.tests.search = function(test, common) {
'region': 'New York', 'region': 'New York',
'region_a': 'NY', 'region_a': 'NY',
'county': 'New York', 'county': 'New York',
'localadmin': 'Manhattan', 'borough': 'Manhattan',
'locality': 'New York', 'locality': 'New York',
'neighbourhood': 'Koreatown', 'neighbourhood': 'Koreatown',
'category': [ 'category': [
@ -134,10 +136,11 @@ module.exports.tests.search = function(test, common) {
'gid': 'source1:layer1:id1', 'gid': 'source1:layer1:id1',
'layer': 'layer1', 'layer': 'layer1',
'source': 'source1', 'source': 'source1',
'label': '\'Round Midnight Jazz and Blues Bar, test3, Angel', 'label': '\'Round Midnight Jazz and Blues Bar, test2, England, United Kingdom',
'name': '\'Round Midnight Jazz and Blues Bar', 'name': '\'Round Midnight Jazz and Blues Bar',
'country_a': 'GBR', 'country_a': 'GBR',
'country': 'United Kingdom', 'country': 'United Kingdom',
'macroregion': 'England',
'region': 'Islington', 'region': 'Islington',
'region_a': 'ISL', 'region_a': 'ISL',
'county': 'Angel', 'county': 'Angel',
@ -163,10 +166,11 @@ module.exports.tests.search = function(test, common) {
'gid': 'source2:layer2:id2', 'gid': 'source2:layer2:id2',
'layer': 'layer2', 'layer': 'layer2',
'source': 'source2', 'source': 'source2',
'label': 'Blues Cafe, test3, Smithfield', 'label': 'Blues Cafe, test2, England, United Kingdom',
'name': 'Blues Cafe', 'name': 'Blues Cafe',
'country_a': 'GBR', 'country_a': 'GBR',
'country': 'United Kingdom', 'country': 'United Kingdom',
'macroregion': 'England',
'region': 'City And County Of The City Of London', 'region': 'City And County Of The City Of London',
'region_a': 'COL', 'region_a': 'COL',
'county': 'Smithfield', 'county': 'Smithfield',
@ -189,14 +193,14 @@ module.exports.tests.search = function(test, common) {
'gid': 'openstreetmap:venue:node:34633854', 'gid': 'openstreetmap:venue:node:34633854',
'layer': 'venue', 'layer': 'venue',
'source': 'openstreetmap', 'source': 'openstreetmap',
'label': 'Empire State Building, Manhattan, NY, USA', 'label': 'Empire State Building, Manhattan, New York, NY, USA',
'name': 'Empire State Building', 'name': 'Empire State Building',
'country_a': 'USA', 'country_a': 'USA',
'country': 'United States', 'country': 'United States',
'region': 'New York', 'region': 'New York',
'region_a': 'NY', 'region_a': 'NY',
'county': 'New York', 'county': 'New York',
'localadmin': 'Manhattan', 'borough': 'Manhattan',
'locality': 'New York', 'locality': 'New York',
'neighbourhood': 'Koreatown' 'neighbourhood': 'Koreatown'
} }
@ -206,6 +210,7 @@ module.exports.tests.search = function(test, common) {
test('geojsonify.search(doc)', function(t) { test('geojsonify.search(doc)', function(t) {
var json = geojsonify.search( input ); var json = geojsonify.search( input );
t.deepEqual(json, expected, 'all docs mapped'); t.deepEqual(json, expected, 'all docs mapped');
t.end(); t.end();
}); });
@ -284,13 +289,13 @@ module.exports.tests.search = function(test, common) {
'county_a': [ 'county_a': [
null null
], ],
'localadmin': [ 'borough': [
'Brooklyn' 'Brooklyn'
], ],
'localadmin_gid': [ 'localadmin_gid': [
'404521211' '404521211'
], ],
'localadmin_a': [ 'borough_a': [
null null
], ],
'locality_gid': [ 'locality_gid': [
@ -330,12 +335,12 @@ module.exports.tests.search = function(test, common) {
'macrocounty_gid': 'MacroCounty Id', 'macrocounty_gid': 'MacroCounty Id',
'macrocounty_a': 'MacroCounty Abbreviation', 'macrocounty_a': 'MacroCounty Abbreviation',
'county': 'Kings County', 'county': 'Kings County',
'borough': 'Brooklyn',
'county_gid': '102082361', 'county_gid': '102082361',
'localadmin': 'Brooklyn',
'localadmin_gid': '404521211', 'localadmin_gid': '404521211',
'locality': 'New York', 'locality': 'New York',
'locality_gid': '85977539', 'locality_gid': '85977539',
'label': 'East New York, Brooklyn, NY, USA' 'label': 'East New York, Brooklyn, New York, NY, USA'
}, },
'bbox': [-73.8967895508,40.6514712164,-73.8665771484,40.6737320588], 'bbox': [-73.8967895508,40.6514712164,-73.8665771484,40.6737320588],
'geometry': { 'geometry': {
@ -350,6 +355,7 @@ module.exports.tests.search = function(test, common) {
}; };
var json = geojsonify.search( input ); var json = geojsonify.search( input );
t.deepEqual(json, expected, 'all wanted properties exposed'); t.deepEqual(json, expected, 'all wanted properties exposed');
t.end(); t.end();
}); });

205
test/unit/helper/labelGenerator_CAN.js

@ -0,0 +1,205 @@
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();
});
};
module.exports.tests.canada = function(test, common) {
test('venue', function(t) {
var doc = {
'name': 'venue name',
'layer': 'venue',
'housenumber': 'house number',
'street': 'street name',
'neighbourhood': 'neighbourhood name',
'locality': 'locality name',
'localadmin': 'localadmin name',
'county': 'county name',
'macrocounty': 'macrocounty name',
'region_a': 'region abbr',
'region': 'region name',
'macroregion': 'macroregion name',
'country_a': 'CAN',
'country': 'Canada'
};
t.equal(generator(doc),'venue name, locality name, region abbr, Canada');
t.end();
});
test('street', function(t) {
var doc = {
'name': 'house number street name',
'layer': 'address',
'housenumber': 'house number',
'street': 'street name',
'neighbourhood': 'neighbourhood name',
'locality': 'locality name',
'localadmin': 'localadmin name',
'county': 'county name',
'macrocounty': 'macrocounty name',
'region_a': 'region abbr',
'region': 'region name',
'macroregion': 'macroregion name',
'country_a': 'CAN',
'country': 'Canada'
};
t.equal(generator(doc),'house number street name, locality name, region abbr, Canada');
t.end();
});
test('neighbourhood', function(t) {
var doc = {
'name': 'neighbourhood name',
'layer': 'neighbourhood',
'neighbourhood': 'neighbourhood name',
'locality': 'locality name',
'localadmin': 'localadmin name',
'county': 'county name',
'macrocounty': 'macrocounty name',
'region_a': 'region abbr',
'region': 'region name',
'macroregion': 'macroregion name',
'country_a': 'CAN',
'country': 'Canada'
};
t.equal(generator(doc),'neighbourhood name, locality name, region abbr, Canada');
t.end();
});
test('locality', function(t) {
var doc = {
'name': 'locality name',
'layer': 'locality',
'locality': 'locality name',
'localadmin': 'localadmin name',
'county': 'county name',
'macrocounty': 'macrocounty name',
'region_a': 'region abbr',
'region': 'region name',
'macroregion': 'macroregion name',
'country_a': 'CAN',
'country': 'Canada'
};
t.equal(generator(doc),'locality name, region abbr, Canada');
t.end();
});
test('localadmin', function(t) {
var doc = {
'name': 'localadmin name',
'layer': 'localadmin',
'localadmin': 'localadmin name',
'county': 'county name',
'macrocounty': 'macrocounty name',
'region_a': 'region abbr',
'region': 'region name',
'macroregion': 'macroregion name',
'country_a': 'CAN',
'country': 'Canada'
};
t.equal(generator(doc),'localadmin name, region abbr, Canada');
t.end();
});
test('county', function(t) {
var doc = {
'name': 'county name',
'layer': 'county',
'county': 'county name',
'macrocounty': 'macrocounty name',
'region_a': 'region abbr',
'region': 'region name',
'macroregion': 'macroregion name',
'country_a': 'CAN',
'country': 'Canada'
};
t.equal(generator(doc),'county name, region abbr, Canada');
t.end();
});
test('macrocounty', function(t) {
var doc = {
'name': 'macrocounty name',
'layer': 'macrocounty',
'macrocounty': 'macrocounty name',
'region_a': 'region abbr',
'region': 'region name',
'macroregion': 'macroregion name',
'country_a': 'CAN',
'country': 'Canada'
};
t.equal(generator(doc),'macrocounty name, region abbr, Canada');
t.end();
});
test('region', function(t) {
var doc = {
'name': 'region name',
'layer': 'region',
'region_a': 'region abbr',
'region': 'region name',
'macroregion': 'macroregion name',
'country_a': 'CAN',
'country': 'Canada'
};
t.equal(generator(doc),'region name, Canada');
t.end();
});
test('macroregion', function(t) {
var doc = {
'name': 'macroregion name',
'layer': 'macroregion',
'macroregion': 'macroregion name',
'country_a': 'CAN',
'country': 'Canada'
};
t.equal(generator(doc),'macroregion name, Canada');
t.end();
});
test('country', function(t) {
var doc = {
'name': 'Canada',
'layer': 'country',
'country_a': 'CAN',
'country': 'Canada'
};
t.equal(generator(doc),'Canada');
t.end();
});
test('region should be used when region_a is not available', function(t) {
var doc = {
'name': 'locality name',
'layer': 'region',
'locality': 'locality name',
'localadmin': 'localadmin name',
'county': 'county name',
'macrocounty': 'macrocounty name',
'region': 'region name',
'macroregion': 'macroregion name',
'country_a': 'CAN',
'country': 'Canada'
};
t.equal(generator(doc),'locality name, region name, Canada', 'region should be used');
t.end();
});
};
module.exports.all = function (tape, common) {
function test(name, testFunction) {
return tape('label generator (CAN): ' + name, testFunction);
}
for( var testCase in module.exports.tests ){
module.exports.tests[testCase](test, common);
}
};

190
test/unit/helper/labelGenerator_GBR.js

@ -10,69 +10,183 @@ module.exports.tests.interface = function(test, common) {
}); });
}; };
// GBR street address module.exports.tests.united_kingdom = function(test, common) {
module.exports.tests.one_main_street_uk = function(test, common) { test('venue', function(t) {
test('one main street uk', function(t) {
var doc = { var doc = {
'name': '1 Main St', 'name': 'venue name',
'housenumber': '1', 'layer': 'venue',
'street': 'Main St', 'housenumber': 'house number',
'postalcode': 'BT77 0BG', 'street': 'street name',
'neighbourhood': 'neighbourhood name',
'locality': 'locality name',
'localadmin': 'localadmin name',
'county': 'county name',
'macrocounty': 'macrocounty name',
'region': 'region name',
'macroregion': 'macroregion name',
'country_a': 'GBR', 'country_a': 'GBR',
'country': 'United Kingdom', 'country': 'United Kingdom'
'region': 'Dungannon'
}; };
t.equal(generator(doc),'1 Main St, Dungannon, United Kingdom'); t.equal(generator(doc),'venue name, locality name, macroregion name, United Kingdom');
t.end(); t.end();
}); });
};
// GBR venue test('localadmin value should be used when locality is not available', function(t) {
module.exports.tests.hackney_city_farm = function(test, common) {
test('hackney city farm', function(t) {
var doc = { var doc = {
'name': 'Hackney City Farm', 'name': 'venue name',
'layer': 'venue',
'housenumber': 'house number',
'street': 'street name',
'neighbourhood': 'neighbourhood name',
'localadmin': 'localadmin name',
'county': 'county name',
'macrocounty': 'macrocounty name',
'region': 'region name',
'macroregion': 'macroregion name',
'country_a': 'GBR', 'country_a': 'GBR',
'country': 'United Kingdom', 'country': 'United Kingdom'
'region': 'Hackney',
'county': 'Greater London',
'locality': 'London',
'neighbourhood': 'Haggerston'
}; };
t.equal(generator(doc),'Hackney City Farm, Haggerston, Greater London'); t.equal(generator(doc),'venue name, localadmin name, macroregion name, United Kingdom');
t.end(); t.end();
}); });
};
// GBR country test('street', function(t) {
module.exports.tests.wales = function(test, common) {
test('wales', function(t) {
var doc = { var doc = {
'name': 'Wales', 'name': 'house number street name',
'layer': 'address',
'housenumber': 'house number',
'street': 'street name',
'neighbourhood': 'neighbourhood name',
'locality': 'locality name',
'localadmin': 'localadmin name',
'county': 'county name',
'macrocounty': 'macrocounty name',
'region': 'region name',
'macroregion': 'macroregion name',
'country_a': 'GBR', 'country_a': 'GBR',
'country': 'United Kingdom', 'country': 'United Kingdom'
'region': 'Wales'
}; };
t.equal(generator(doc),'Wales, United Kingdom'); t.equal(generator(doc),'house number street name, locality name, macroregion name, United Kingdom');
t.end();
});
test('neighbourhood', function(t) {
var doc = {
'name': 'neighbourhood name',
'layer': 'neighbourhood',
'neighbourhood': 'neighbourhood name',
'locality': 'locality name',
'localadmin': 'localadmin name',
'county': 'county name',
'macrocounty': 'macrocounty name',
'region': 'region name',
'macroregion': 'macroregion name',
'country_a': 'GBR',
'country': 'United Kingdom'
};
t.equal(generator(doc),'neighbourhood name, locality name, macroregion name, United Kingdom');
t.end();
});
test('locality', function(t) {
var doc = {
'name': 'locality name',
'layer': 'locality',
'locality': 'locality name',
'localadmin': 'localadmin name',
'county': 'county name',
'macrocounty': 'macrocounty name',
'region': 'region name',
'macroregion': 'macroregion name',
'country_a': 'GBR',
'country': 'United Kingdom'
};
t.equal(generator(doc),'locality name, macroregion name, United Kingdom');
t.end();
});
test('localadmin', function(t) {
var doc = {
'name': 'localadmin name',
'layer': 'localadmin',
'localadmin': 'localadmin name',
'county': 'county name',
'macrocounty': 'macrocounty name',
'region': 'region name',
'macroregion': 'macroregion name',
'country_a': 'GBR',
'country': 'United Kingdom'
};
t.equal(generator(doc),'localadmin name, macroregion name, United Kingdom');
t.end();
});
test('county', function(t) {
var doc = {
'name': 'county name',
'layer': 'county',
'county': 'county name',
'macrocounty': 'macrocounty name',
'region': 'region name',
'macroregion': 'macroregion name',
'country_a': 'GBR',
'country': 'United Kingdom'
};
t.equal(generator(doc),'county name, macroregion name, United Kingdom');
t.end();
});
test('macrocounty', function(t) {
var doc = {
'name': 'macrocounty name',
'layer': 'macrocounty',
'macrocounty': 'macrocounty name',
'region': 'region name',
'macroregion': 'macroregion name',
'country_a': 'GBR',
'country': 'United Kingdom'
};
t.equal(generator(doc),'macrocounty name, macroregion name, United Kingdom');
t.end(); t.end();
}); });
};
// GBR macroregion test('region', function(t) {
module.exports.tests.macroregion_trumps_region = function(test, common) {
test('macroregion should trump region when none of neighbourhood, county, localadmin, locality are available', function(t) {
var doc = { var doc = {
'name': 'Name', 'name': 'region name',
'layer': 'region',
'region': 'region name',
'macroregion': 'macroregion name',
'country_a': 'GBR', 'country_a': 'GBR',
'country': 'Country Name', 'country': 'United Kingdom'
'macroregion': 'Macroregion Name',
'region': 'Region Name'
}; };
t.equal(generator(doc),'region name, macroregion name, United Kingdom');
t.end();
});
t.equal(generator(doc), 'Name, Macroregion Name, Country Name'); test('macroregion', function(t) {
var doc = {
'name': 'macroregion name',
'layer': 'macroregion',
'macroregion': 'macroregion name',
'country_a': 'GBR',
'country': 'United Kingdom'
};
t.equal(generator(doc),'macroregion name, United Kingdom');
t.end(); t.end();
});
test('country', function(t) {
var doc = {
'name': 'United Kingdom',
'layer': 'country',
'postalcode': 'postalcode',
'country_a': 'GBR',
'country': 'United Kingdom'
};
t.equal(generator(doc),'United Kingdom');
t.end();
}); });
}; };
module.exports.all = function (tape, common) { module.exports.all = function (tape, common) {

51
test/unit/helper/labelGenerator_SGP.js

@ -1,51 +0,0 @@
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();
});
};
// SGP region
module.exports.tests.north_west_singapore = function(test, common) {
test('north west singapore', function(t) {
var doc = {
'name': 'North West',
'country_a': 'SGP',
'country': 'Singapore',
'region': 'North West'
};
t.equal(generator(doc),'North West, Singapore');
t.end();
});
};
// SGP venue
module.exports.tests.singapore_mcdonalds = function(test, common) {
test('singapore_mcdonalds', function(t) {
var doc = {
'name': '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);
}
};

53
test/unit/helper/labelGenerator_SWE.js

@ -1,53 +0,0 @@
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();
});
};
// SWE city
module.exports.tests.skane1 = function(test, common) {
test('skĂĄne 1', function(t) {
var doc = {
'name': 'Malmö',
'country_a': 'SWE',
'country': 'Sweden',
'region': 'SkĂĄne',
'county': 'Malmö'
};
t.equal(generator(doc),'Malmö, Skåne, Sweden');
t.end();
});
};
// SWE city
module.exports.tests.skane2 = function(test, common) {
test('skĂĄne 2', function(t) {
var doc = {
'name': 'Malmö',
'country_a': 'SWE',
'country': 'Sweden',
'region': 'SkĂĄne',
'county': 'Malmö',
'locality': 'Malmö'
};
t.equal(generator(doc),'Malmö, Skåne, Sweden');
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);
}
};

287
test/unit/helper/labelGenerator_USA.js

@ -9,218 +9,233 @@ module.exports.tests.interface = function(test, common) {
}); });
}; };
module.exports.tests.localadmin = function(test, common) { module.exports.tests.united_states = function(test, common) {
test('localadmin should trump locality, neighbourhood, and county', function(t) { test('venue', function(t) {
var doc = { var doc = {
'name': 'Default Name', 'name': 'venue name',
'layer': 'venue',
'housenumber': 'house number',
'street': 'street name',
'neighbourhood': 'neighbourhood name',
'locality': 'locality name',
'localadmin': 'localadmin name',
'county': 'county name',
'macrocounty': 'macrocounty name',
'region_a': 'region abbr',
'region': 'region name',
'macroregion': 'macroregion name',
'country_a': 'USA', 'country_a': 'USA',
'country': 'United States', 'country': 'United States'
'region': 'Region Name',
'region_a': 'Region Abbr',
'county': 'County Name',
'localadmin': 'LocalAdmin Name',
'locality': 'Locality Name',
'neighbourhood': 'Neighbourhood Name'
}; };
t.equal(generator(doc),'Default Name, LocalAdmin Name, Region Abbr, USA'); t.equal(generator(doc),'venue name, locality name, region abbr, USA');
t.end(); t.end();
}); });
};
module.exports.tests.locality = function(test, common) { test('localadmin value should be used when there is no locality', function(t) {
test('locality should trump neighbourhood and county when localadmin not available', function(t) {
var doc = { var doc = {
'name': 'Default Name', 'name': 'venue name',
'layer': 'venue',
'housenumber': 'house number',
'street': 'street name',
'neighbourhood': 'neighbourhood name',
'localadmin': 'localadmin name',
'county': 'county name',
'macrocounty': 'macrocounty name',
'region_a': 'region abbr',
'region': 'region name',
'macroregion': 'macroregion name',
'country_a': 'USA', 'country_a': 'USA',
'country': 'United States', 'country': 'United States'
'region': 'Region Name',
'region_a': 'Region Abbr',
'county': 'County Name',
'locality': 'Locality Name',
'neighbourhood': 'Neighbourhood Name'
}; };
t.equal(generator(doc),'Default Name, Locality Name, Region Abbr, USA'); t.equal(generator(doc),'venue name, localadmin name, region abbr, USA');
t.end(); t.end();
}); });
};
module.exports.tests.neighbourhood = function(test, common) { test('street', function(t) {
test('neighbourhood should trump county when neither localadmin nor locality', function(t) {
var doc = { var doc = {
'name': 'Default Name', 'name': 'house number street name',
'layer': 'address',
'housenumber': 'house number',
'street': 'street name',
'neighbourhood': 'neighbourhood name',
'locality': 'locality name',
'localadmin': 'localadmin name',
'county': 'county name',
'macrocounty': 'macrocounty name',
'region_a': 'region abbr',
'region': 'region name',
'macroregion': 'macroregion name',
'country_a': 'USA', 'country_a': 'USA',
'country': 'United States', 'country': 'United States'
'region': 'Region Name',
'region_a': 'Region Abbr',
'county': 'County Name',
'neighbourhood': 'Neighbourhood Name'
}; };
t.equal(generator(doc),'Default Name, Neighbourhood Name, Region Abbr, USA'); t.equal(generator(doc),'house number street name, locality name, region abbr, USA');
t.end(); t.end();
}); });
};
module.exports.tests.county = function(test, common) { test('neighbourhood', function(t) {
test('county should be used when localadmin, locality, and neighbourhood are not available', function(t) {
var doc = { var doc = {
'name': 'Default Name', 'name': 'neighbourhood name',
'layer': 'neighbourhood',
'neighbourhood': 'neighbourhood name',
'locality': 'locality name',
'localadmin': 'localadmin name',
'county': 'county name',
'macrocounty': 'macrocounty name',
'region_a': 'region abbr',
'region': 'region name',
'macroregion': 'macroregion name',
'country_a': 'USA', 'country_a': 'USA',
'country': 'United States', 'country': 'United States'
'region': 'Region Name',
'region_a': 'Region Abbr',
'county': 'County Name'
}; };
t.equal(generator(doc),'Default Name, County Name, Region Abbr, USA'); t.equal(generator(doc),'neighbourhood name, locality name, region abbr, USA');
t.end(); t.end();
}); });
};
module.exports.tests.region = function(test, common) { test('venue in borough', function(t) {
test('region should be used when region_a is not available', function(t) {
var doc = { var doc = {
'name': 'Default Name', 'name': 'venue name',
'layer': 'borough',
'neighbourhood': 'neighbourhood name',
'borough': 'borough name',
'locality': 'locality name',
'localadmin': 'localadmin name',
'county': 'county name',
'macrocounty': 'macrocounty name',
'region_a': 'region abbr',
'region': 'region name',
'macroregion': 'macroregion name',
'country_a': 'USA', 'country_a': 'USA',
'country': 'United States', 'country': 'United States'
'region': 'Region Name'
}; };
t.equal(generator(doc),'Default Name, Region Name, USA'); t.equal(generator(doc),'venue name, borough name, locality name, region abbr, USA');
t.end(); t.end();
}); });
};
// USA geonames state test('locality', function(t) {
module.exports.tests.region_geonames = function(test, common) {
test('default name should not be prepended when source=geonames and layer=region', function(t) {
var doc = { var doc = {
'name': 'Region Name', 'name': 'locality name',
'layer': 'locality',
'locality': 'locality name',
'localadmin': 'localadmin name',
'county': 'county name',
'macrocounty': 'macrocounty name',
'region_a': 'region abbr',
'region': 'region name',
'macroregion': 'macroregion name',
'country_a': 'USA', 'country_a': 'USA',
'country': 'United States', 'country': 'United States'
'region': 'Region Name',
'region_a': 'Region Abbr',
'source': 'geonames',
'layer': 'region'
}; };
t.equal(generator(doc),'Region Name, USA'); t.equal(generator(doc),'locality name, region abbr, USA');
t.end(); t.end();
}); });
};
// USA whosonfirst state test('localadmin', function(t) {
module.exports.tests.region_whosonfirst = function(test, common) {
test('default name should not be prepended when source=whosonfirst and layer=region', function(t) {
var doc = { var doc = {
'name': 'Region Name', 'name': 'localadmin name',
'layer': 'localadmin',
'localadmin': 'localadmin name',
'county': 'county name',
'macrocounty': 'macrocounty name',
'region_a': 'region abbr',
'region': 'region name',
'macroregion': 'macroregion name',
'country_a': 'USA', 'country_a': 'USA',
'country': 'United States', 'country': 'United States'
'region': 'Region Name',
'region_a': 'Region Abbr',
'source': 'whosonfirst',
'layer': 'region'
}; };
t.equal(generator(doc),'Region Name, USA'); t.equal(generator(doc),'localadmin name, region abbr, USA');
t.end(); t.end();
}); });
};
// USA non-geonames/whosonfirst state test('county', function(t) {
module.exports.tests.region_other_source = function(test, common) {
test('default name should be prepended when layer=region and source is not whosonfirst or geonames', function(t) {
var doc = { var doc = {
'name': 'Default Name', 'name': 'county name',
'layer': 'county',
'county': 'county name',
'macrocounty': 'macrocounty name',
'region_a': 'region abbr',
'region': 'region name',
'macroregion': 'macroregion name',
'country_a': 'USA', 'country_a': 'USA',
'country': 'United States', 'country': 'United States'
'region': 'Region Name',
'region_a': 'Region Abbr',
'source': 'not geonames or whosonfirst',
'layer': 'region'
}; };
t.equal(generator(doc),'Default Name, Region Name, USA',generator(doc)); t.equal(generator(doc),'county name, region abbr, USA');
t.end(); t.end();
}); });
};
// major USA city test('macrocounty', function(t) {
module.exports.tests.san_francisco = function(test, common) {
test('san francisco', function(t) {
var doc = { var doc = {
'name': 'San Francisco', 'name': 'macrocounty name',
'layer': 'macrocounty',
'macrocounty': 'macrocounty name',
'region_a': 'region abbr',
'region': 'region name',
'macroregion': 'macroregion name',
'country_a': 'USA', 'country_a': 'USA',
'country': 'United States', '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, USA'); t.equal(generator(doc),'macrocounty name, region abbr, USA');
t.end(); t.end();
}); });
};
// USA venue test('region', function(t) {
module.exports.tests.nyc_office = function(test, common) {
test('30 West 26th Street', function(t) {
var doc = { var doc = {
'name': '30 West 26th Street', 'name': 'region name',
'housenumber': '30', 'layer': 'region',
'street': 'West 26th Street', 'region_a': 'region abbr',
'postalcode': '10010', 'region': 'region name',
'macroregion': 'macroregion name',
'country_a': 'USA', 'country_a': 'USA',
'country': 'United States', '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, USA'); t.equal(generator(doc),'region name, USA');
t.end(); t.end();
}); });
};
// USA NYC eatery test('macroregion', function(t) {
module.exports.tests.nyc_bakery = function(test, common) {
test('New York Bakery', function(t) {
var doc = { var doc = {
'name': 'New York Bakery', 'name': 'macroregion name',
'housenumber': '51 W', 'layer': 'macroregion',
'street': '29th', 'macroregion': 'macroregion name',
'country_a': 'USA', 'country_a': 'USA',
'country': 'United States', 'country': 'United States'
'region': 'New York',
'region_a': 'NY',
'county': 'New York County',
'localadmin': 'Manhattan',
'locality': 'New York',
'neighbourhood': 'Koreatown'
}; };
t.equal(generator(doc),'New York Bakery, Manhattan, NY, USA'); t.equal(generator(doc),'macroregion name, USA');
t.end();
});
test('country', function(t) {
var doc = {
'name': 'United States',
'layer': 'country',
'country_a': 'USA',
'country': 'United States'
};
t.equal(generator(doc),'United States');
t.end(); t.end();
}); });
};
// USA SFC building test('region should be used when region_a is not available', function(t) {
module.exports.tests.ferry_building = function(test, common) {
test('Ferry Building', function(t) {
var doc = { var doc = {
'name': 'Ferry Building', 'name': 'locality name',
'locality': 'locality name',
'localadmin': 'localadmin name',
'county': 'county name',
'macrocounty': 'macrocounty name',
'region': 'region name',
'macroregion': 'macroregion name',
'country_a': 'USA', 'country_a': 'USA',
'country': 'United States', 'country': 'United States'
'region': 'California',
'region_a': 'CA',
'county': 'San Francisco County',
'locality': 'San Francisco',
'neighbourhood': 'Financial District'
}; };
t.equal(generator(doc),'Ferry Building, San Francisco, CA, USA'); t.equal(generator(doc),'locality name, region name, USA', 'region should be used');
t.end(); t.end();
}); });
}; };
module.exports.all = function (tape, common) { module.exports.all = function (tape, common) {
function test(name, testFunction) { function test(name, testFunction) {
return tape('label generator: ' + name, testFunction); return tape('label generator (USA): ' + name, testFunction);
} }
for( var testCase in module.exports.tests ){ for( var testCase in module.exports.tests ){

356
test/unit/helper/labelGenerator_default.js

@ -10,296 +10,182 @@ module.exports.tests.interface = function(test, common) {
}); });
}; };
// AUS state module.exports.tests.default_country = function(test, common) {
module.exports.tests.new_south_wales = function(test, common) { test('venue', function(t) {
test('new south wales', function(t) {
var doc = { var doc = {
'name': 'New South Wales', 'name': 'venue name',
'country_a': 'AUS', 'layer': 'venue',
'country': 'Australia', 'housenumber': 'house number',
'region': 'New South Wales' 'street': 'street name',
'neighbourhood': 'neighbourhood name',
'locality': 'locality name',
'localadmin': 'localadmin name',
'county': 'county name',
'macrocounty': 'macrocounty name',
'region': 'region name',
'macroregion': 'macroregion name',
'country_a': 'country code',
'country': 'country name'
}; };
t.equal(generator(doc),'New South Wales, Australia'); t.equal(generator(doc),'venue name, locality name, country name');
t.end(); t.end();
}); });
};
// IND state
module.exports.tests.west_bengal = function(test, common) {
test('west bengal', function(t) {
var doc = {
'name': 'West Bengal',
'country_a': 'IND',
'country': 'India',
'region': 'West Bengal'
};
t.equal(generator(doc),'West Bengal, India');
t.end();
});
};
// IND city
module.exports.tests.bangalore = function(test, common) {
test('bangalore', function(t) {
var doc = {
'name': 'Bangalore',
'country_a': 'IND',
'country': 'India',
'region': 'Karnataka',
'county': 'Bangalore',
'locality': 'Bangalore'
};
t.equal(generator(doc),'Bangalore, Karnataka, India');
t.end();
});
};
// IND region of city
module.exports.tests.sarjapur = function(test, common) {
test('Sarjapur', function(t) {
var doc = {
'name': 'Sarjapur',
'country_a': 'IND',
'country': 'India',
'region': 'Karnataka'
};
t.equal(generator(doc),'Sarjapur, Karnataka, India');
t.end();
});
};
// IND region of city 2 test('localadmin value should be used when locality is not available', function(t) {
module.exports.tests.bengaluru_east = function(test, common) {
test('Bengaluru East', function(t) {
var doc = { var doc = {
'name': 'Bengaluru East', 'name': 'venue name',
'country_a': 'IND', 'layer': 'venue',
'country': 'India', 'housenumber': 'house number',
'region': 'Karnataka', 'street': 'street name',
'county': 'Bangalore', 'neighbourhood': 'neighbourhood name',
'locality': 'Bangalore', 'localadmin': 'localadmin name',
'neighbourhood': 'Fraser Town' 'county': 'county name',
'macrocounty': 'macrocounty name',
'region': 'region name',
'macroregion': 'macroregion name',
'country_a': 'country code',
'country': 'country name'
}; };
t.equal(generator(doc),'Bengaluru East, Bangalore, India'); t.equal(generator(doc),'venue name, localadmin name, country name');
t.end(); t.end();
}); });
};
// AUS area test('street', function(t) {
// https://en.wikipedia.org/wiki/Shire_of_Wellington
module.exports.tests.wellington_victoria = function(test, common) {
test('Wellington, Victoria, Australia', function(t) {
var doc = { var doc = {
'name': 'Wellington', 'name': 'house number street name',
'country_a': 'AUS', 'layer': 'address',
'country': 'Australia', 'housenumber': 'house number',
'region': 'Victoria', 'street': 'street name',
'county': 'Wellington' 'neighbourhood': 'neighbourhood name',
'locality': 'locality name',
'localadmin': 'localadmin name',
'county': 'county name',
'macrocounty': 'macrocounty name',
'region': 'region name',
'macroregion': 'macroregion name',
'country_a': 'country code',
'country': 'country name'
}; };
t.equal(generator(doc),'Wellington, Victoria, Australia'); t.equal(generator(doc),'house number street name, locality name, country name');
t.end(); t.end();
}); });
};
// IRQ region test('neighbourhood', function(t) {
module.exports.tests.arbil = function(test, common) {
test('arbil', function(t) {
var doc = { var doc = {
'name': 'Arbil', 'name': 'neighbourhood name',
'country_a': 'IRQ', 'layer': 'neighbourhood',
'country': 'Iraq', 'neighbourhood': 'neighbourhood name',
'region': 'Arbil' 'locality': 'locality name',
'localadmin': 'localadmin name',
'county': 'county name',
'macrocounty': 'macrocounty name',
'region': 'region name',
'macroregion': 'macroregion name',
'country_a': 'country code',
'country': 'country name'
}; };
t.equal(generator(doc),'Arbil, Iraq'); t.equal(generator(doc),'neighbourhood name, locality name, country name');
t.end(); t.end();
}); });
};
// ESP city test('locality', function(t) {
module.exports.tests.madrid = function(test, common) {
test('madrid', function(t) {
var doc = { var doc = {
'name': 'Madrid', 'name': 'locality name',
'country_a': 'ESP', 'layer': 'locality',
'country': 'Spain', 'locality': 'locality name',
'region': 'Madrid' 'localadmin': 'localadmin name',
'county': 'county name',
'macrocounty': 'macrocounty name',
'region': 'region name',
'macroregion': 'macroregion name',
'country_a': 'country code',
'country': 'country name'
}; };
t.equal(generator(doc),'Madrid, Spain'); t.equal(generator(doc),'locality name, country name');
t.end(); t.end();
}); });
};
// DEU street address test('localadmin', function(t) {
module.exports.tests.one_grolmanstrasse = function(test, common) {
test('one grolmanstrasse', function(t) {
var doc = { var doc = {
'name': '1 GrolmanstraĂźe', 'name': 'localadmin name',
'housenumber': '1', 'layer': 'localadmin',
'street': 'GrolmanstraĂźe', 'localadmin': 'localadmin name',
'postalcode': '10623', 'county': 'county name',
'country_a': 'DEU', 'macrocounty': 'macrocounty name',
'country': 'Germany', 'region': 'region name',
'region': 'Berlin', 'macroregion': 'macroregion name',
'county': 'Berlin', 'country_a': 'country code',
'locality': 'Berlin', 'country': 'country name'
'neighbourhood': 'Halensee'
}; };
t.equal(generator(doc),'1 GrolmanstraĂźe, Berlin, Germany'); t.equal(generator(doc),'localadmin name, country name');
t.end(); t.end();
}); });
};
// NZD country test('county', function(t) {
module.exports.tests.new_zealand = function(test, common) {
test('new zealand', function(t) {
var doc = { var doc = {
'name': 'New Zealand', 'name': 'county name',
'country_a': 'NZL', 'layer': 'county',
'country': 'New Zealand' 'county': 'county name',
'macrocounty': 'macrocounty name',
'region': 'region name',
'macroregion': 'macroregion name',
'country_a': 'country code',
'country': 'country name'
}; };
t.equal(generator(doc),'New Zealand'); t.equal(generator(doc),'county name, country name');
t.end(); t.end();
}); });
};
// IRL country test('macrocounty', function(t) {
module.exports.tests.republic_of_ireland = function(test, common) {
test('northern ireland', function(t) {
var doc = { var doc = {
'name': 'Ireland', 'name': 'macrocounty name',
'country_a': 'IRL', 'layer': 'macrocounty',
'country': 'Ireland' 'macrocounty': 'macrocounty name',
'region': 'region name',
'macroregion': 'macroregion name',
'country_a': 'country code',
'country': 'country name'
}; };
// !! this is not part of the UK !! t.equal(generator(doc),'macrocounty name, country name');
t.equal(generator(doc),'Ireland');
t.end(); t.end();
}); });
};
// THA province test('region', function(t) {
module.exports.tests.krabi_province = function(test, common) {
test('Krabi Provence', function(t) {
var doc = { var doc = {
'name': 'Krabi', 'name': 'region name',
'country_a': 'THA',
'country': 'Thailand',
'region': 'Krabi'
};
t.equal(generator(doc),'Krabi, Thailand');
t.end();
});
};
// THA island
module.exports.tests.koh_lanta = function(test, common) {
test('Koh Lanta', function(t) {
var doc = {
'name': 'Ko Lanta',
'country_a': 'THA',
'country': 'Thailand',
'region': 'Krabi'
};
t.equal(generator(doc),'Ko Lanta, Krabi, Thailand');
t.end();
});
};
// NZD cafe
module.exports.tests.black_dog_cafe = function(test, common) {
test('Black Dog Cafe', function(t) {
var doc = {
'name': 'Black Dog Cafe',
'country_a': 'NZL',
'country': 'New Zealand',
'region': 'Auckland Region',
'county': 'Auckland'
};
t.equal(generator(doc),'Black Dog Cafe, Auckland, New Zealand');
t.end();
});
};
// NZD cafe 2
module.exports.tests.beach_bablyon = function(test, common) {
test('Beach Bablyon', function(t) {
var doc = {
'name': 'Beach Bablyon',
'country_a': 'NZL',
'country': 'New Zealand',
'region': 'Wellington Region',
'county': 'Wellington City',
'locality': 'Wellington',
'neighbourhood': 'Oriental Bay'
};
t.equal(generator(doc),'Beach Bablyon, Wellington, New Zealand');
t.end();
});
};
// NZD tourism
module.exports.tests.waiotapu = function(test, common) {
test('Waiotapu', function(t) {
var doc = {
'name': 'Waiotapu',
'country_a': 'NZL',
'country': 'New Zealand',
'region': 'Bay of Plenty',
'county': 'Rotorua District'
};
t.equal(generator(doc),'Waiotapu, Rotorua District, New Zealand');
t.end();
});
};
module.exports.tests.non_us_or_ca_region = function(test, common) {
test('geonames US', function(t) {
var doc = {
'name': 'Default Name',
'country_a': 'XYZ',
'country': 'Full Country Name',
'region': 'Full Region Name',
'layer': 'region', 'layer': 'region',
'source': 'geonames' 'region': 'region name',
'macroregion': 'macroregion name',
'country_a': 'country code',
'country': 'country name'
}; };
t.equal(generator(doc),'region name, country name');
t.equal(generator(doc), 'Default Name, Full Region Name, Full Country Name');
t.end(); t.end();
}); });
test('whosonfirst US', function(t) { test('macroregion', function(t) {
var doc = { var doc = {
'name': 'Default Name', 'name': 'macroregion name',
'country_a': 'XYZ', 'layer': 'macroregion',
'country': 'Full Country Name', 'macroregion': 'macroregion name',
'region': 'Full Region Name', 'country_a': 'country code',
'layer': 'region', 'country': 'country name'
'source': 'whosonfirst'
}; };
t.equal(generator(doc),'macroregion name, country name');
t.equal(generator(doc), 'Default Name, Full Region Name, Full Country Name');
t.end(); t.end();
}); });
}; test('country', function(t) {
// macroregion
module.exports.tests.macroregion_trumps_region = function(test, common) {
test('macroregion should trump region when none of localadmin, locality, neighbourhood, county are available', function(t) {
var doc = { var doc = {
'name': 'Name', 'name': 'country name',
'country_a': 'Country abbreviation', 'layer': 'country',
'country': 'Country Name', 'country_a': 'country code',
'macroregion': 'Macroregion Name', 'country': 'country name'
'region': 'Region Name'
}; };
t.equal(generator(doc),'country name');
t.equal(generator(doc), 'Name, Macroregion Name, Country Name');
t.end(); t.end();
}); });
}; };
module.exports.all = function (tape, common) { module.exports.all = function (tape, common) {

116
test/unit/helper/labelGenerator_examples.js

@ -0,0 +1,116 @@
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();
});
};
module.exports.tests.canada = function(test, common) {
test('venue', function(t) {
var doc = {
'name': 'Tim Horton\'s',
'layer': 'venue',
'housenumber': '1',
'street': 'Main St',
'neighbourhood': 'College Heights',
'locality': 'Thunder Bay',
'region_a': 'ON',
'region': 'Ontario',
'country_a': 'CAN',
'country': 'Canada'
};
t.equal(generator(doc),'Tim Horton\'s, Thunder Bay, ON, Canada');
t.end();
});
test('address', function(t) {
var doc = {
'name': '1 Main St',
'layer': 'venue',
'housenumber': '1',
'street': 'Main St',
'locality': 'Truth or Consequences',
'region_a': 'NM',
'region': 'New Mexico',
'country_a': 'USA',
'country': 'United States'
};
t.equal(generator(doc),'1 Main St, Truth or Consequences, NM, USA');
t.end();
});
};
module.exports.tests.france = function(test, common) {
test('eiffel tower', function(t) {
var doc = {
'name': 'Tour Eiffel',
'layer': 'venue',
'neighbourhood': 'Quartier du Gros-Caillou',
'locality': 'Paris',
'region': 'Paris',
'country_a': 'FRA',
'country': 'France'
};
t.equal(generator(doc),'Tour Eiffel, Paris, France');
t.end();
});
test('France street address', function(t) {
var doc = {
'name': '74 rue de rivoli',
'layer': 'address',
'housenumber': '74',
'street': 'Rue de Rivoli',
'neighbourhood': 'Quartier Saint-Merri',
'locality': 'Paris',
'region': 'Paris',
'country_a': 'FRA',
'country': 'France'
};
t.equal(generator(doc),'74 rue de rivoli, Paris, France');
t.end();
});
test('France neighbourhood', function(t) {
var doc = {
'name': 'Grange aux Belles Terrage',
'layer': 'neighbourhood',
'neighbourhood': 'Grange aux Belles Terrage',
'locality': 'Paris',
'region': 'Paris',
'country_a': 'FRA',
'country': 'France'
};
t.equal(generator(doc),'Grange aux Belles Terrage, Paris, France');
t.end();
});
test('Luxembourg (the city) in Luxembourg', function(t) {
var doc = {
'name': 'Luxembourg',
'layer': 'locality',
'locality': 'Luxembourg',
'country_a': 'LUX',
'country': 'Luxembourg'
};
// console.error(generator(doc));
t.equal(generator(doc),'Luxembourg, Luxembourg');
t.end();
});
};
module.exports.all = function (tape, common) {
function test(name, testFunction) {
return tape('label generator (CAN): ' + name, testFunction);
}
for( var testCase in module.exports.tests ){
module.exports.tests[testCase](test, common);
}
};

770
test/unit/helper/labelSchema.js

@ -1,4 +1,3 @@
var schemas = require('../../../helper/labelSchema'); var schemas = require('../../../helper/labelSchema');
var alpha3 = require('../mock/alpha3.json'); var alpha3 = require('../mock/alpha3.json');
@ -13,20 +12,18 @@ module.exports.tests.interface = function(test, common) {
}; };
module.exports.tests.supported_countries = function(test, common) { module.exports.tests.supported_countries = function(test, common) {
test('support countries', function(t) { test('supported countries', function(t) {
var supported_countries = Object.keys(schemas); var supported_countries = Object.keys(schemas);
t.notEquals(supported_countries.indexOf('USA'), -1); t.notEquals(supported_countries.indexOf('USA'), -1);
t.notEquals(supported_countries.indexOf('CAN'), -1);
t.notEquals(supported_countries.indexOf('GBR'), -1); t.notEquals(supported_countries.indexOf('GBR'), -1);
t.notEquals(supported_countries.indexOf('SGP'), -1);
t.notEquals(supported_countries.indexOf('SWE'), -1);
t.notEquals(supported_countries.indexOf('default'), -1); t.notEquals(supported_countries.indexOf('default'), -1);
t.equals(supported_countries.length, 5); t.equals(supported_countries.length, 4);
t.equals(Object.keys(schemas.USA).length, 3); t.equals(Object.keys(schemas.USA).length, 4);
t.equals(Object.keys(schemas.GBR).length, 2); t.equals(Object.keys(schemas.CAN).length, 3);
t.equals(Object.keys(schemas.SGP).length, 2); t.equals(Object.keys(schemas.GBR).length, 3);
t.equals(Object.keys(schemas.SWE).length, 2);
t.equals(Object.keys(schemas.default).length, 2); t.equals(Object.keys(schemas.default).length, 2);
t.end(); t.end();
@ -34,761 +31,6 @@ module.exports.tests.supported_countries = function(test, common) {
}); });
}; };
module.exports.tests.usa = function(test, common) {
test('USA.local should use localadmin value over locality, neighbourhood, and county', function(t) {
var record = {
localadmin: 'localadmin value',
locality: 'locality value',
neighbourhood: 'neighbourhood value',
county: 'county value'
};
var labelParts = ['initial value'];
var f = schemas.USA.local;
t.deepEqual(f(record, labelParts), ['initial value', 'localadmin value']);
t.end();
});
test('USA.local should use locality value over neighbourhood and county when no localadmin', function(t) {
var record = {
locality: 'locality value',
neighbourhood: 'neighbourhood value',
county: 'county value'
};
var labelParts = ['initial value'];
var f = schemas.USA.local;
t.deepEqual(f(record, labelParts), ['initial value', 'locality value']);
t.end();
});
test('USA.local should use neighbourhood value over county when no localadmin or locality', function(t) {
var record = {
neighbourhood: 'neighbourhood value',
county: 'county value'
};
var labelParts = ['initial value'];
var f = schemas.USA.local;
t.deepEqual(f(record, labelParts), ['initial value', 'neighbourhood value']);
t.end();
});
test('USA.local should use county value when no localadmin, locality, or neighbourhood', function(t) {
var record = {
county: 'county value'
};
var labelParts = ['initial value'];
var f = schemas.USA.local;
t.deepEqual(f(record, labelParts), ['initial value', 'county value']);
t.end();
});
test('USA.local should not modify labelParts if none of localadmin, locality, neighbourhood, or county is available', function(t) {
var record = {};
var labelParts = ['initial value'];
var f = schemas.USA.local;
t.deepEqual(f(record, labelParts), ['initial value']);
t.end();
});
test('USA.regional should use region when layer=region and region is available', function(t) {
var record = {
layer: 'region',
region: 'region name',
region_a: 'region_a name'
};
var labelParts = ['initial value'];
var f = schemas.USA.regional;
t.deepEqual(f(record, labelParts), ['initial value', 'region name']);
t.end();
});
test('USA.regional should use region_a when layer=region and region is unavailable', function(t) {
var record = {
layer: 'region',
region_a: 'region_a name'
};
var labelParts = ['initial value'];
var f = schemas.USA.regional;
t.deepEqual(f(record, labelParts), ['initial value', 'region_a name']);
t.end();
});
test('USA.regional should use region_a when layer!=region and both region and region_a are available', function(t) {
var record = {
layer: 'not region',
region: 'region name',
region_a: 'region_a name'
};
var labelParts = ['initial value'];
var f = schemas.USA.regional;
t.deepEqual(f(record, labelParts), ['initial value', 'region_a name']);
t.end();
});
test('USA.regional should use region when layer!=region and region_a is unavailable', function(t) {
var record = {
layer: 'region',
region: 'region name',
region_a: 'region_a name'
};
var labelParts = ['initial value'];
var f = schemas.USA.regional;
t.deepEqual(f(record, labelParts), ['initial value', 'region name'], 'region should have been appended');
t.end();
});
test('USA.regional should not append anything when neither region nor region_a are available', function(t) {
var record = {
layer: 'region',
};
var labelParts = ['initial value'];
var f = schemas.USA.regional;
t.deepEqual(f(record, labelParts), ['initial value'], 'no USA.region should have appended');
t.end();
});
test('USA.country should append country_a when available', function(t) {
var record = {
country_a: 'country_a name',
country: 'country name'
};
var labelParts = ['initial value'];
var f = schemas.USA.country;
t.deepEqual(f(record, labelParts), ['initial value', 'country_a name'], 'country_a should have appended');
t.end();
});
test('USA.country should not append anything when country_a is unavailable', function(t) {
var record = {
country: 'country name'
};
var labelParts = ['initial value'];
var f = schemas.USA.country;
t.deepEqual(f(record, labelParts), ['initial value'], 'no USA.country should have appended');
t.end();
});
};
module.exports.tests.gbr = function(test, common) {
test('GBR.local should use neighbourhood value over county, localadmin, locality, region', function(t) {
var record = {
neighbourhood: 'neighbourhood value',
county: 'county value',
localadmin: 'localadmin value',
locality: 'locality value',
region: 'region value'
};
var labelParts = ['initial value'];
var f = schemas.GBR.local;
t.deepEqual(f(record, labelParts), ['initial value', 'neighbourhood value']);
t.end();
});
test('GBR.local should use county value over county, localadmin, locality, region', function(t) {
var record = {
county: 'county value',
localadmin: 'localadmin value',
locality: 'locality value',
region: 'region value'
};
var labelParts = ['initial value'];
var f = schemas.GBR.local;
t.deepEqual(f(record, labelParts), ['initial value', 'county value']);
t.end();
});
test('GBR.local should use localadmin value over locality, region', function(t) {
var record = {
localadmin: 'localadmin value',
locality: 'locality value',
region: 'region value'
};
var labelParts = ['initial value'];
var f = schemas.GBR.local;
t.deepEqual(f(record, labelParts), ['initial value', 'localadmin value']);
t.end();
});
test('GBR.local should use locality value over region', function(t) {
var record = {
locality: 'locality value',
region: 'region value'
};
var labelParts = ['initial value'];
var f = schemas.GBR.local;
t.deepEqual(f(record, labelParts), ['initial value', 'locality value']);
t.end();
});
test('GBR.local should use region value when nothing else is available', function(t) {
var record = {
region: 'region value'
};
var labelParts = ['initial value'];
var f = schemas.GBR.local;
t.deepEqual(f(record, labelParts), ['initial value', 'region value']);
t.end();
});
test('GBR.local should not append anything when none of neighbourhood, county, localadmin, locality, region are available', function(t) {
var record = {};
var labelParts = ['initial value'];
var f = schemas.GBR.local;
t.deepEqual(f(record, labelParts), ['initial value']);
t.end();
});
test('GBR.regional should use county over country and region', function(t) {
var record = {
county: 'county value',
country: 'country value',
region: 'region value'
};
var labelParts = ['initial value'];
var f = schemas.GBR.regional;
t.deepEqual(f(record, labelParts), ['initial value', 'county value']);
t.end();
});
test('GBR.regional should use country over region', function(t) {
var record = {
country: 'country value',
region: 'region value'
};
var labelParts = ['initial value'];
var f = schemas.GBR.regional;
t.deepEqual(f(record, labelParts), ['initial value', 'country value']);
t.end();
});
test('GBR.regional should use region when county and country aren not available', function(t) {
var record = {
region: 'region value'
};
var labelParts = ['initial value'];
var f = schemas.GBR.regional;
t.deepEqual(f(record, labelParts), ['initial value', 'region value']);
t.end();
});
test('GBR.regional should not append anything when none of county, country, or region are available', function(t) {
var record = {};
var labelParts = ['initial value'];
var f = schemas.GBR.regional;
t.deepEqual(f(record, labelParts), ['initial value']);
t.end();
});
};
module.exports.tests.sgp = function(test, common) {
test('SGP.local should use neighbourhood value over region, county, localadmin, locality', function(t) {
var record = {
neighbourhood: 'neighbourhood value',
county: 'county value',
localadmin: 'localadmin value',
locality: 'locality value',
region: 'region value'
};
var labelParts = ['initial value'];
var f = schemas.SGP.local;
t.deepEqual(f(record, labelParts), ['initial value', 'neighbourhood value']);
t.end();
});
test('SGP.local should use region value over county, localadmin, locality', function(t) {
var record = {
county: 'county value',
localadmin: 'localadmin value',
locality: 'locality value',
region: 'region value'
};
var labelParts = ['initial value'];
var f = schemas.SGP.local;
t.deepEqual(f(record, labelParts), ['initial value', 'region value']);
t.end();
});
test('SGP.local should use county value over localadmin, locality', function(t) {
var record = {
localadmin: 'localadmin value',
locality: 'locality value',
county: 'county value'
};
var labelParts = ['initial value'];
var f = schemas.SGP.local;
t.deepEqual(f(record, labelParts), ['initial value', 'county value']);
t.end();
});
test('SGP.local should use localadmin value over locality', function(t) {
var record = {
localadmin: 'localadmin value',
locality: 'locality value'
};
var labelParts = ['initial value'];
var f = schemas.SGP.local;
t.deepEqual(f(record, labelParts), ['initial value', 'localadmin value']);
t.end();
});
test('SGP.local should use locality value when nothing else is available', function(t) {
var record = {
locality: 'locality value'
};
var labelParts = ['initial value'];
var f = schemas.SGP.local;
t.deepEqual(f(record, labelParts), ['initial value', 'locality value']);
t.end();
});
test('SGP.local should not append anything when none of neighbourhood, region, county, localadmin, locality are available', function(t) {
var record = {};
var labelParts = ['initial value'];
var f = schemas.SGP.local;
t.deepEqual(f(record, labelParts), ['initial value']);
t.end();
});
test('SGP.regional should use county over country and region', function(t) {
var record = {
county: 'county value',
country: 'country value',
region: 'region value'
};
var labelParts = ['initial value'];
var f = schemas.SGP.regional;
t.deepEqual(f(record, labelParts), ['initial value', 'county value']);
t.end();
});
test('SGP.regional should use country over region', function(t) {
var record = {
country: 'country value',
region: 'region value'
};
var labelParts = ['initial value'];
var f = schemas.SGP.regional;
t.deepEqual(f(record, labelParts), ['initial value', 'country value']);
t.end();
});
test('SGP.regional should use region when county and country aren not available', function(t) {
var record = {
region: 'region value'
};
var labelParts = ['initial value'];
var f = schemas.SGP.regional;
t.deepEqual(f(record, labelParts), ['initial value', 'region value']);
t.end();
});
test('SGP.regional should not append anything when none of county, country, or region are available', function(t) {
var record = {};
var labelParts = ['initial value'];
var f = schemas.SGP.regional;
t.deepEqual(f(record, labelParts), ['initial value']);
t.end();
});
};
module.exports.tests.swe = function(test, common) {
test('SWE.local should use neighbourhood value over region, county, localadmin, locality', function(t) {
var record = {
neighbourhood: 'neighbourhood value',
county: 'county value',
localadmin: 'localadmin value',
locality: 'locality value',
region: 'region value'
};
var labelParts = ['initial value'];
var f = schemas.SWE.local;
t.deepEqual(f(record, labelParts), ['initial value', 'neighbourhood value']);
t.end();
});
test('SWE.local should use region value over county, localadmin, locality', function(t) {
var record = {
county: 'county value',
localadmin: 'localadmin value',
locality: 'locality value',
region: 'region value'
};
var labelParts = ['initial value'];
var f = schemas.SWE.local;
t.deepEqual(f(record, labelParts), ['initial value', 'region value']);
t.end();
});
test('SWE.local should use county value over localadmin, locality', function(t) {
var record = {
localadmin: 'localadmin value',
locality: 'locality value',
county: 'county value'
};
var labelParts = ['initial value'];
var f = schemas.SWE.local;
t.deepEqual(f(record, labelParts), ['initial value', 'county value']);
t.end();
});
test('SWE.local should use localadmin value over locality', function(t) {
var record = {
localadmin: 'localadmin value',
locality: 'locality value'
};
var labelParts = ['initial value'];
var f = schemas.SWE.local;
t.deepEqual(f(record, labelParts), ['initial value', 'localadmin value']);
t.end();
});
test('SWE.local should use locality value when nothing else is available', function(t) {
var record = {
locality: 'locality value'
};
var labelParts = ['initial value'];
var f = schemas.SWE.local;
t.deepEqual(f(record, labelParts), ['initial value', 'locality value']);
t.end();
});
test('SWE.local should not append anything when none of neighbourhood, region, county, localadmin, locality are available', function(t) {
var record = {};
var labelParts = ['initial value'];
var f = schemas.SWE.local;
t.deepEqual(f(record, labelParts), ['initial value']);
t.end();
});
test('SGP.regional should use country when available', function(t) {
var record = {
country: 'country value',
country_a: 'country_a value',
};
var labelParts = ['initial value'];
var f = schemas.SGP.regional;
t.deepEqual(f(record, labelParts), ['initial value', 'country value']);
t.end();
});
test('SGP.regional should not append anything when country is not available', function(t) {
var record = {
country_a: 'country_a value'
};
var labelParts = ['initial value'];
var f = schemas.SGP.regional;
t.deepEqual(f(record, labelParts), ['initial value']);
t.end();
});
};
module.exports.tests.default = function(test, common) {
test('default.local should use localadmin value over locality, neighbourhood, county, region', function(t) {
var record = {
neighbourhood: 'neighbourhood value',
county: 'county value',
localadmin: 'localadmin value',
locality: 'locality value',
region: 'region value'
};
var labelParts = ['initial value'];
var f = schemas.default.local;
t.deepEqual(f(record, labelParts), ['initial value', 'localadmin value']);
t.end();
});
test('default.local should use locality value over neighbourhood, county, region', function(t) {
var record = {
neighbourhood: 'neighbourhood value',
county: 'county value',
locality: 'locality value',
region: 'region value'
};
var labelParts = ['initial value'];
var f = schemas.default.local;
t.deepEqual(f(record, labelParts), ['initial value', 'locality value']);
t.end();
});
test('default.local should use neighbourhood value over county, region', function(t) {
var record = {
neighbourhood: 'neighbourhood value',
county: 'county value',
region: 'region value'
};
var labelParts = ['initial value'];
var f = schemas.default.local;
t.deepEqual(f(record, labelParts), ['initial value', 'neighbourhood value']);
t.end();
});
test('default.local should use county value over region', function(t) {
var record = {
county: 'county value',
region: 'region value'
};
var labelParts = ['initial value'];
var f = schemas.default.local;
t.deepEqual(f(record, labelParts), ['initial value', 'county value']);
t.end();
});
test('default.local should use region value when nothing else is available', function(t) {
var record = {
region: 'region value'
};
var labelParts = ['initial value'];
var f = schemas.default.local;
t.deepEqual(f(record, labelParts), ['initial value', 'region value']);
t.end();
});
test('default.local should not append anything when none of neighbourhood, region, county, localadmin, ' +
'locality are available', function(t) {
var record = {};
var labelParts = ['initial value'];
var f = schemas.default.local;
t.deepEqual(f(record, labelParts), ['initial value']);
t.end();
});
test('default.regional should use country over region, region_a, or country_a', function(t) {
var record = {
region: 'region value',
region_a: 'region_a value',
country: 'country value',
country_a: 'country_a value'
};
var labelParts = ['initial value'];
var f = schemas.default.regional;
t.deepEqual(f(record, labelParts), ['initial value', 'country value']);
t.end();
});
test('default.regional should not append any value if country is not available', function(t) {
var record = {
region: 'region value',
region_a: 'region_a value',
country_a: 'country_a value'
};
var labelParts = ['initial value'];
var f = schemas.default.regional;
t.deepEqual(f(record, labelParts), ['initial value']);
t.end();
});
test('USA.local borough should be shown if available', function(t) {
var record = {
borough: 'borough value',
region: 'region value',
region_a: 'region_a value',
country_a: 'country_a value'
};
var labelParts = ['initial value'];
var f = schemas.USA.local;
t.deepEqual(f(record, labelParts), ['initial value', 'borough value']);
t.end();
});
};
module.exports.all = function (tape, common) { module.exports.all = function (tape, common) {
function test(name, testFunction) { function test(name, testFunction) {

20
test/unit/helper/text_parser.js

@ -29,6 +29,15 @@ module.exports.tests.split_on_comma = function(test, common) {
t.equal(address.admin_parts, query.admin_parts, 'admin_parts set correctly to ' + address.admin_parts); t.equal(address.admin_parts, query.admin_parts, 'admin_parts set correctly to ' + address.admin_parts);
t.end(); t.end();
}); });
test('naive parsing ' + query + 'without spaces', function(t) {
var address = parser.get_parsed_address(query.name + ',' + query.admin_parts);
t.equal(typeof address, 'object', 'valid object');
t.equal(address.name, query.name, 'name set correctly to ' + address.name);
t.equal(address.admin_parts, query.admin_parts, 'admin_parts set correctly to ' + address.admin_parts);
t.end();
});
}); });
}; };
@ -115,6 +124,17 @@ module.exports.tests.parse_address = function(test, common) {
t.equal(address.postalcode, '06410', 'parsed zip'); t.equal(address.postalcode, '06410', 'parsed zip');
t.end(); t.end();
}); });
test('valid address without spaces after commas', function(t) {
var query_string = '339 W Main St,Lancaster,PA';
var address = parser.get_parsed_address(query_string);
t.equal(typeof address, 'object', 'valid object for the address');
t.equal(address.number, '339', 'parsed house number');
t.equal(address.street, 'W Main St', 'parsed street');
t.deepEqual(address.regions, ['Lancaster'], 'parsed city');
t.deepEqual(address.state, 'PA', 'parsed state');
t.end();
});
}; };

4
test/unit/helper/type_mapping.js

@ -12,8 +12,8 @@ module.exports.tests.interfaces = function(test, common) {
test('alias layer mapping', function(t) { test('alias layer mapping', function(t) {
t.deepEquals(type_mapping.layer_mapping.coarse, t.deepEquals(type_mapping.layer_mapping.coarse,
[ 'continent', 'macrocountry', 'country', 'dependency', [ 'continent', 'country', 'dependency', 'macroregion',
'region', 'locality', 'localadmin', 'county', 'macrohood', 'region', 'locality', 'localadmin', 'macrocounty', 'county', 'macrohood',
'neighbourhood', 'microhood', 'disputed' ]); 'neighbourhood', 'microhood', 'disputed' ]);
t.end(); t.end();
}); });

19
test/unit/middleware/localNamingConventions.js

@ -77,8 +77,21 @@ module.exports.tests.flipNumberAndStreet = function(test, common) {
} }
}; };
var unknownCountryAddress = {
'_id': 'test4',
'_type': 'test',
'name': { 'default': '123 Main Street' },
'center_point': { 'lon': 30.1, 'lat': -50 },
'address_parts': {
'number': '123',
'street': 'Main Street'
},
'parent': {
}
};
var req = {}, var req = {},
res = { data: [ ukAddress, deAddress, nlAddress ] }, res = { data: [ ukAddress, deAddress, nlAddress, unknownCountryAddress ] },
middleware = localNamingConventions(); middleware = localNamingConventions();
test('flipNumberAndStreet', function(t) { test('flipNumberAndStreet', function(t) {
@ -96,6 +109,10 @@ module.exports.tests.flipNumberAndStreet = function(test, common) {
// this definition comes from pelias configuration // this definition comes from pelias configuration
t.equal( res.data[2].name.default, 'Keizersgracht 117', 'flipped name' ); t.equal( res.data[2].name.default, 'Keizersgracht 117', 'flipped name' );
// addresses without a known country (either due to missing data or admin lookup
// being disabled), don't have the name flipped
t.equal( res.data[3].name.default, '123 Main Street', 'standard name');
t.end(); t.end();
}); });
}); });

8
test/unit/mock/backend.js

@ -33,6 +33,14 @@ responses['client/search/fail/1'] = function( cmd, cb ){
return cb( 'a backend error occurred' ); return cb( 'a backend error occurred' );
}; };
responses['client/search/timeout/1'] = function( cmd, cb) {
// timeout errors are objects
return cb({
status: 408,
message: 'Request Timeout after 5000ms'
});
};
responses['client/mget/ok/1'] = function( cmd, cb ){ responses['client/mget/ok/1'] = function( cmd, cb ){
return cb( undefined, mgetEnvelope([{ return cb( undefined, mgetEnvelope([{
_id: 'myid1', _id: 'myid1',

4
test/unit/run.js

@ -13,10 +13,10 @@ var tests = [
require('./controller/place'), require('./controller/place'),
require('./controller/search'), require('./controller/search'),
require('./helper/geojsonify'), require('./helper/geojsonify'),
require('./helper/labelGenerator_examples'),
require('./helper/labelGenerator_default'), require('./helper/labelGenerator_default'),
require('./helper/labelGenerator_CAN'),
require('./helper/labelGenerator_GBR'), require('./helper/labelGenerator_GBR'),
require('./helper/labelGenerator_SGP'),
require('./helper/labelGenerator_SWE'),
require('./helper/labelGenerator_USA'), require('./helper/labelGenerator_USA'),
require('./helper/labelSchema'), require('./helper/labelSchema'),
require('./helper/text_parser'), require('./helper/text_parser'),

14
test/unit/sanitiser/_layers.js

@ -41,8 +41,8 @@ module.exports.tests.sanitize_layers = function(test, common) {
sanitize(raw, clean); sanitize(raw, clean);
var admin_layers = [ 'continent', 'macrocountry', 'country', 'dependency', var admin_layers = [ 'continent', 'country', 'dependency',
'region', 'locality', 'localadmin', 'county', 'macrohood', 'neighbourhood', 'macroregion', 'region', 'locality', 'localadmin', 'macrocounty', 'county', 'macrohood', 'neighbourhood',
'microhood', 'disputed' ]; 'microhood', 'disputed' ];
t.deepEqual(clean.layers, admin_layers, 'coarse layers set'); t.deepEqual(clean.layers, admin_layers, 'coarse layers set');
@ -76,8 +76,8 @@ module.exports.tests.sanitize_layers = function(test, common) {
sanitize(raw, clean); sanitize(raw, clean);
var expected_layers = [ 'continent', 'macrocountry', 'country', 'dependency', var expected_layers = [ 'continent', 'country', 'dependency',
'region', 'locality', 'localadmin', 'county', 'macrohood', 'neighbourhood', 'macroregion', 'region', 'locality', 'localadmin', 'macrocounty', 'county', 'macrohood', 'neighbourhood',
'microhood', 'disputed' ]; 'microhood', 'disputed' ];
t.deepEqual(clean.layers, expected_layers, 'coarse + regular layers set'); t.deepEqual(clean.layers, expected_layers, 'coarse + regular layers set');
@ -112,9 +112,9 @@ module.exports.tests.sanitize_layers = function(test, common) {
sanitize(raw, clean); sanitize(raw, clean);
var coarse_layers = [ 'continent', 'macrocountry', var coarse_layers = [ 'continent',
'country', 'dependency', 'region', 'locality', 'localadmin', 'country', 'dependency', 'macroregion', 'region', 'locality', 'localadmin',
'county', 'macrohood', 'neighbourhood', 'microhood', 'macrocounty', 'county', 'macrohood', 'neighbourhood', 'microhood',
'disputed' ]; 'disputed' ];
var venue_layers = [ 'venue' ]; var venue_layers = [ 'venue' ];

Loading…
Cancel
Save