Browse Source

Merge pull request #430 from pelias/add-iso3-to-USA-results

Add iso3 to usa results
pull/434/merge
Julian Simioni 9 years ago
parent
commit
f3386399ae
  1. 72
      helper/labelGenerator.js
  2. 66
      helper/labelSchema.js
  3. 22
      helper/labelSchema.json
  4. 2
      test/unit/helper/geojsonify.js
  5. 70
      test/unit/helper/labelGenerator_GBR.js
  6. 51
      test/unit/helper/labelGenerator_SGP.js
  7. 53
      test/unit/helper/labelGenerator_SWE.js
  8. 229
      test/unit/helper/labelGenerator_USA.js
  9. 201
      test/unit/helper/labelGenerator_default.js
  10. 792
      test/unit/helper/labelSchema.js
  11. 6
      test/unit/run.js

72
helper/labelGenerator.js

@ -1,35 +1,59 @@
var _ = require('lodash'),
check = require('check-types'),
schemas = require('./labelSchema.json');
schemas = require('./labelSchema');
module.exports = function( record ){
var schema = getSchema(record.country_a);
var labelParts = [ record.name.default ];
var labelParts = getInitialLabel(record);
for (var key in schema) {
var valueFunction = schema[key];
labelParts = valueFunction(record, labelParts);
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.nonEmptyString(fieldValue) && !_.includes(parts, fieldValue)) {
parts.push( fieldValue );
return parts;
}
}
return parts;
};
for (var key in schema) {
labelParts = buildOutput(labelParts, schema[key], record);
// NOTE: while it may seem odd to call `uniq` on the list of label parts,
// the effect is quite subtle. Take, for instance, a result for "Lancaster, PA"
// the pseudo-object is:
// {
// 'name': 'Lancaster',
// 'locality': 'Lancaster',
// 'region_a': 'PA',
// 'country_a': 'USA'
// }
//
// the code up to this point generates the label:
// `Lancaster, Lancaster, PA, USA`
//
// 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();
};
function getSchema(country_a) {
if (country_a && country_a.length && schemas[country_a]) {
return schemas[country_a];
}
return schemas.default;
}
function getInitialLabel(record) {
if ('region' === record.layer && ('geonames' === record.source || 'whosonfirst' === record.source)) {
return [];
}
// de-dupe outputs
labelParts = _.uniq( labelParts );
return [record.name.default];
return labelParts.join(', ').trim();
};
}

66
helper/labelSchema.js

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

22
helper/labelSchema.json

@ -1,22 +0,0 @@
{
"USA": {
"local": ["localadmin", "locality", "neighbourhood", "county"],
"regional": ["region_a", "region", "country"]
},
"GBR": {
"local": ["neighbourhood", "county", "localadmin", "locality", "region"],
"regional": ["county","country","region"]
},
"SGP": {
"local": ["neighbourhood", "region", "county", "localadmin", "locality"],
"regional": ["county","country","region"]
},
"SWE": {
"local": ["neighbourhood", "region", "county", "localadmin", "locality"],
"regional": ["country"]
},
"default": {
"local": ["localadmin", "locality", "neighbourhood", "county", "region"],
"regional": ["country"]
}
}

2
test/unit/helper/geojsonify.js

@ -199,7 +199,7 @@ module.exports.tests.search = function(test, common) {
'gid': 'osm:venue:34633854',
'layer': 'venue',
'source': 'osm',
'label': 'Empire State Building, Manhattan, NY',
'label': 'Empire State Building, Manhattan, NY, USA',
'name': 'Empire State Building',
'country_a': 'USA',
'country': 'United States',

70
test/unit/helper/labelGenerator_GBR.js

@ -0,0 +1,70 @@
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();
});
};
// 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();
});
};
// GBR country
module.exports.tests.wales = function(test, common) {
test('wales', function(t) {
var doc = {
'name': { 'default': 'Wales' },
'country_a': 'GBR',
'country': 'United Kingdom',
'region': 'Wales'
};
t.equal(generator(doc),'Wales, United Kingdom');
t.end();
});
};
module.exports.all = function (tape, common) {
function test(name, testFunction) {
return tape('label generator (GBR): ' + name, testFunction);
}
for( var testCase in module.exports.tests ){
module.exports.tests[testCase](test, common);
}
};

51
test/unit/helper/labelGenerator_SGP.js

@ -0,0 +1,51 @@
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': { 'default': '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': { '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);
}
};

53
test/unit/helper/labelGenerator_SWE.js

@ -0,0 +1,53 @@
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': { 'default': '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': { 'default': '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);
}
};

229
test/unit/helper/labelGenerator_USA.js

@ -0,0 +1,229 @@
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.localadmin = function(test, common) {
test('localadmin should trump locality, neighbourhood, and county', function(t) {
var doc = {
'name': { 'default': 'Default Name' },
'country_a': 'USA',
'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.end();
});
};
module.exports.tests.locality = function(test, common) {
test('locality should trump neighbourhood and county when localadmin not available', function(t) {
var doc = {
'name': { 'default': 'Default Name' },
'country_a': 'USA',
'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.end();
});
};
module.exports.tests.neighbourhood = function(test, common) {
test('neighbourhood should trump county when neither localadmin nor locality', function(t) {
var doc = {
'name': { 'default': 'Default Name' },
'country_a': 'USA',
'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.end();
});
};
module.exports.tests.county = function(test, common) {
test('county should be used when localadmin, locality, and neighbourhood are not available', function(t) {
var doc = {
'name': { 'default': 'Default Name' },
'country_a': 'USA',
'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.end();
});
};
module.exports.tests.region = function(test, common) {
test('region should be used when region_a is not available', function(t) {
var doc = {
'name': { 'default': 'Default Name' },
'country_a': 'USA',
'country': 'United States',
'region': 'Region Name'
};
t.equal(generator(doc),'Default Name, Region Name, USA');
t.end();
});
};
// USA geonames state
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 = {
'name': { 'default': 'Region Name' },
'country_a': 'USA',
'country': 'United States',
'region': 'Region Name',
'region_a': 'Region Abbr',
'source': 'geonames',
'layer': 'region'
};
t.equal(generator(doc),'Region Name, USA');
t.end();
});
};
// USA whosonfirst state
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 = {
'name': { 'default': 'Region Name' },
'country_a': 'USA',
'country': 'United States',
'region': 'Region Name',
'region_a': 'Region Abbr',
'source': 'whosonfirst',
'layer': 'region'
};
t.equal(generator(doc),'Region Name, USA');
t.end();
});
};
// USA non-geonames/whosonfirst state
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 = {
'name': { 'default': 'Default Name' },
'country_a': 'USA',
'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.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, USA');
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, USA');
t.end();
});
};
// USA NYC eatery
module.exports.tests.nyc_bakery = function(test, common) {
test('New York Bakery', function(t) {
var doc = {
'name': { 'default': 'New York Bakery' },
'housenumber': '51 W',
'street': '29th',
'country_a': 'USA',
'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.end();
});
};
// USA SFC building
module.exports.tests.ferry_building = function(test, common) {
test('Ferry Building', function(t) {
var doc = {
'name': { 'default': 'Ferry Building' },
'country_a': 'USA',
'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.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);
}
};

201
test/unit/helper/labelGenerator.js → test/unit/helper/labelGenerator_default.js

@ -10,84 +10,6 @@ module.exports.tests.interface = function(test, common) {
});
};
// 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();
});
};
// USA NYC eatery
module.exports.tests.nyc_bakery = function(test, common) {
test('New York Bakery', function(t) {
var doc = {
'name': { 'default': 'New York Bakery' },
'housenumber': '51 W',
'street': '29th',
'country_a': 'USA',
'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');
t.end();
});
};
// USA SFC building
module.exports.tests.ferry_building = function(test, common) {
test('Ferry Building', function(t) {
var doc = {
'name': { 'default': 'Ferry Building' },
'country_a': 'USA',
'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');
t.end();
});
};
// AUS state
module.exports.tests.new_south_wales = function(test, common) {
test('new south wales', function(t) {
@ -102,21 +24,6 @@ module.exports.tests.new_south_wales = function(test, common) {
});
};
// 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) {
@ -194,20 +101,6 @@ module.exports.tests.wellington_victoria = function(test, common) {
});
};
// 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) {
@ -236,71 +129,6 @@ module.exports.tests.madrid = function(test, common) {
});
};
// SWE city
module.exports.tests.skane1 = function(test, common) {
test('skåne 1', function(t) {
var doc = {
'name': { 'default': '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': { 'default': 'Malmö' },
'country_a': 'SWE',
'country': 'Sweden',
'region': 'Skåne',
'county': 'Malmö',
'locality': 'Malmö'
};
t.equal(generator(doc),'Malmö, Skåne, Sweden');
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) {
@ -334,20 +162,6 @@ module.exports.tests.new_zealand = function(test, common) {
});
};
// GBR country
module.exports.tests.wales = function(test, common) {
test('wales', function(t) {
var doc = {
'name': { 'default': 'Wales' },
'country_a': 'GBR',
'country': 'United Kingdom',
'region': 'Wales'
};
t.equal(generator(doc),'Wales, United Kingdom');
t.end();
});
};
// IRL country
module.exports.tests.republic_of_ireland = function(test, common) {
test('northern ireland', function(t) {
@ -362,21 +176,6 @@ module.exports.tests.republic_of_ireland = function(test, common) {
});
};
// 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();
});
};
// THA province
module.exports.tests.krabi_province = function(test, common) {
test('Krabi Provence', function(t) {

792
test/unit/helper/labelSchema.js

@ -1,5 +1,5 @@
var schemas = require('../../../helper/labelSchema.json');
var schemas = require('../../../helper/labelSchema');
var alpha3 = require('../mock/alpha3.json');
module.exports.tests = {};
@ -12,36 +12,766 @@ module.exports.tests.interface = function(test, common) {
});
};
module.exports.tests.valid = function(test, common) {
var valid_keys = ['localadmin', 'locality', 'neighbourhood', 'county', 'region_a', 'region', 'country'];
var default_schema = {
local: ['localadmin', 'locality', 'neighbourhood', 'county', 'region'],
regional: ['country']
};
var isValid = function(keys, schema) {
test('valid key/object (' + keys + ')' , function(t) {
if (keys === 'default') {
t.deepEqual(schema, default_schema, 'valid default schema');
} else {
t.equal(alpha3.hasOwnProperty(keys), true, 'valid key');
}
t.equal(typeof schema, 'object', 'valid object');
t.notEqual(Object.getOwnPropertyNames(schema).length, 0, 'object not empty');
for (var levels in schema) {
t.equal(Object.prototype.toString.call(schema[levels]), '[object Array]', levels+' is an array');
for (var i=0;i<schema[levels].length;i++) {
var key = schema[levels][i];
t.notEqual(valid_keys.indexOf(key), -1, key + ' is valid');
}
}
t.end();
});
};
for (var keys in schemas) {
isValid(keys, schemas[keys]);
}
module.exports.tests.supported_countries = function(test, common) {
test('support countries', function(t) {
var supported_countries = Object.keys(schemas);
t.notEquals(supported_countries.indexOf('USA'), -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.equals(supported_countries.length, 5);
t.equals(Object.keys(schemas.USA).length, 3);
t.equals(Object.keys(schemas.GBR).length, 2);
t.equals(Object.keys(schemas.SGP).length, 2);
t.equals(Object.keys(schemas.SWE).length, 2);
t.equals(Object.keys(schemas.default).length, 2);
t.end();
});
};
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();
});
};
module.exports.all = function (tape, common) {

6
test/unit/run.js

@ -7,7 +7,11 @@ var tests = [
require('./controller/place'),
require('./controller/search'),
require('./helper/geojsonify'),
require('./helper/labelGenerator'),
require('./helper/labelGenerator_default'),
require('./helper/labelGenerator_GBR'),
require('./helper/labelGenerator_SGP'),
require('./helper/labelGenerator_SWE'),
require('./helper/labelGenerator_USA'),
require('./helper/labelSchema'),
require('./helper/text_parser'),
require('./helper/type_mapping'),

Loading…
Cancel
Save