Browse Source

moved assignLabels to fire before geojsonify

pull/680/head
Stephen Hess 8 years ago
parent
commit
beb309ca07
  1. 1
      helper/geojsonify.js
  2. 6
      middleware/assignLabels.js
  3. 2
      routes/v1.js
  4. 30
      test/unit/helper/geojsonify.js
  5. 64
      test/unit/middleware/assignLabels.js

1
helper/geojsonify.js

@ -46,6 +46,7 @@ function geojsonifyPlace(params, labelGenerator, place) {
addMetaData(place, output); addMetaData(place, output);
addName(place, output); addName(place, output);
addDetails(params, place, output); addDetails(params, place, output);
output.label = place.label;
// map center_point for GeoJSON to work properly // map center_point for GeoJSON to work properly
// these should not show up in the final feature properties // these should not show up in the final feature properties

6
middleware/assignLabels.js

@ -9,12 +9,12 @@ function setup(labelGenerator) {
function assignLabel(req, res, next, labelGenerator) { function assignLabel(req, res, next, labelGenerator) {
// do nothing if there's nothing to process // do nothing if there's nothing to process
if (!res || !res.body || !res.body.features) { if (!res || !res.data) {
return next(); return next();
} }
res.body.features.forEach(function (feature) { res.data.forEach(function (result) {
feature.properties.label = labelGenerator(feature.properties); result.label = labelGenerator(result.parent);
}); });
next(); next();

2
routes/v1.js

@ -88,8 +88,8 @@ function addRoutes(app, peliasConfig) {
postProc.renamePlacenames(), postProc.renamePlacenames(),
postProc.parseBoundingBox(), postProc.parseBoundingBox(),
postProc.normalizeParentIds(), postProc.normalizeParentIds(),
postProc.geocodeJSON(peliasConfig, base),
postProc.assignLabels(require('pelias-labels')), postProc.assignLabels(require('pelias-labels')),
postProc.geocodeJSON(peliasConfig, base),
postProc.sendJSON postProc.sendJSON
]), ]),
autocomplete: createRouter([ autocomplete: createRouter([

30
test/unit/helper/geojsonify.js

@ -69,7 +69,8 @@ module.exports.tests.geojsonify = function(test, common) {
'category': [ 'category': [
'food', 'food',
'nightlife' 'nightlife'
] ],
'label': 'label for id id1'
}, },
{ {
'_id': 'id2', '_id': 'id2',
@ -93,7 +94,8 @@ module.exports.tests.geojsonify = function(test, common) {
'county': 'Smithfield', 'county': 'Smithfield',
'localadmin': 'test1', 'localadmin': 'test1',
'locality': 'test2', 'locality': 'test2',
'neighbourhood': 'test3' 'neighbourhood': 'test3',
'label': 'label for id id2'
}, },
{ {
'_id': 'node:34633854', '_id': 'node:34633854',
@ -120,7 +122,8 @@ module.exports.tests.geojsonify = function(test, common) {
'category': [ 'category': [
'tourism', 'tourism',
'transport' 'transport'
] ],
'label': 'label for id node:34633854'
} }
]; ];
@ -160,7 +163,8 @@ module.exports.tests.geojsonify = function(test, common) {
'category': [ 'category': [
'food', 'food',
'nightlife' 'nightlife'
] ],
'label': 'label for id id1'
} }
}, },
{ {
@ -188,7 +192,8 @@ module.exports.tests.geojsonify = function(test, common) {
'county': 'Smithfield', 'county': 'Smithfield',
'localadmin': 'test1', 'localadmin': 'test1',
'locality': 'test2', 'locality': 'test2',
'neighbourhood': 'test3' 'neighbourhood': 'test3',
'label': 'label for id id2'
} }
}, },
{ {
@ -219,7 +224,8 @@ module.exports.tests.geojsonify = function(test, common) {
'category': [ 'category': [
'tourism', 'tourism',
'transport' 'transport'
] ],
'label': 'label for id node:34633854'
} }
} }
] ]
@ -330,7 +336,8 @@ module.exports.tests.geojsonify = function(test, common) {
null null
], ],
'neighbourhood': [], 'neighbourhood': [],
'neighbourhood_gid': [] 'neighbourhood_gid': [],
'label': 'label for id 85816607'
} }
]; ];
@ -369,7 +376,8 @@ module.exports.tests.geojsonify = function(test, common) {
'county_gid': '102082361', 'county_gid': '102082361',
'localadmin_gid': '404521211', 'localadmin_gid': '404521211',
'locality': 'New York', 'locality': 'New York',
'locality_gid': '85977539' 'locality_gid': '85977539',
'label': 'label for id 85816607'
}, },
'bbox': [-73.8967895508,40.6514712164,-73.8665771484,40.6737320588], 'bbox': [-73.8967895508,40.6514712164,-73.8665771484,40.6737320588],
'geometry': { 'geometry': {
@ -411,7 +419,8 @@ module.exports.tests.categories = function (test, common) {
'default': 'East New York' 'default': 'East New York'
}, },
'source_id': '85816607', 'source_id': '85816607',
'category': ['government'] 'category': ['government'],
'label': 'label for id 85816607'
} }
]; ];
@ -428,7 +437,8 @@ module.exports.tests.categories = function (test, common) {
'source': 'whosonfirst', 'source': 'whosonfirst',
'source_id': '85816607', 'source_id': '85816607',
'name': 'East New York', 'name': 'East New York',
'category': ['government'] 'category': ['government'],
'label': 'label for id 85816607'
}, },
'bbox': [-73.8967895508,40.6514712164,-73.8665771484,40.6737320588], 'bbox': [-73.8967895508,40.6514712164,-73.8665771484,40.6737320588],
'geometry': { 'geometry': {

64
test/unit/middleware/assignLabels.js

@ -15,7 +15,7 @@ module.exports.tests.serialization = function(test, common) {
}); });
test('res without body should not throw an exception', function(t) { test('res without data should not throw an exception', function(t) {
var assignLabels = require('../../../middleware/assignLabels')(function(){}); var assignLabels = require('../../../middleware/assignLabels')(function(){});
function testIt() { function testIt() {
@ -27,24 +27,12 @@ module.exports.tests.serialization = function(test, common) {
}); });
test('res.body without features should not throw an exception', function(t) {
var assignLabels = require('../../../middleware/assignLabels')(function(){});
function testIt() {
assignLabels({ body: {} }, {}, function() {});
}
t.doesNotThrow(testIt, 'an exception should not have been thrown');
t.end();
});
test('labels should be assigned to all results', function(t) { test('labels should be assigned to all results', function(t) {
var labelGenerator = function(properties) { var labelGenerator = function(result) {
if (properties.id === 1) { if (result.id === 1) {
return 'label 1'; return 'label 1';
} }
if (properties.id === 2) { if (result.id === 2) {
return 'label 2'; return 'label 2';
} }
@ -53,39 +41,35 @@ module.exports.tests.serialization = function(test, common) {
var assignLabels = require('../../../middleware/assignLabels')(labelGenerator); var assignLabels = require('../../../middleware/assignLabels')(labelGenerator);
var input = { var input = {
body: { data: [
features: [
{ {
properties: { parent: {
id: 1 id: 1
} }
}, },
{ {
properties: { parent: {
id: 2 id: 2
} }
} }
] ]
}
}; };
var expected = { var expected = {
body: { data: [
features: [
{ {
properties: { parent: {
id: 1, id: 1
},
label: 'label 1' label: 'label 1'
}
}, },
{ {
properties: { parent: {
id: 2, id: 2
},
label: 'label 2' label: 'label 2'
} }
}
] ]
}
}; };
assignLabels({}, input, function () { assignLabels({}, input, function () {
@ -97,36 +81,32 @@ module.exports.tests.serialization = function(test, common) {
test('no explicit labelGenerator supplied should use pelias-labels module', function(t) { test('no explicit labelGenerator supplied should use pelias-labels module', function(t) {
var assignLabels = proxyquire('../../../middleware/assignLabels', { var assignLabels = proxyquire('../../../middleware/assignLabels', {
'pelias-labels': function(properties) { 'pelias-labels': function(result) {
if (properties.id === 1) { if (result.id === 1) {
return 'label 1'; return 'label 1';
} }
} }
})(); })();
var input = { var input = {
body: { data: [
features: [
{ {
properties: { parent: {
id: 1 id: 1
} }
} }
] ]
}
}; };
var expected = { var expected = {
body: { data: [
features: [
{ {
properties: { parent: {
id: 1, id: 1
},
label: 'label 1' label: 'label 1'
} }
}
] ]
}
}; };
assignLabels({}, input, function () { assignLabels({}, input, function () {

Loading…
Cancel
Save