From beb309ca076ee4fd1fd433daaf2f220efb034a8f Mon Sep 17 00:00:00 2001 From: Stephen Hess Date: Mon, 10 Oct 2016 16:17:16 -0400 Subject: [PATCH] moved assignLabels to fire before geojsonify --- helper/geojsonify.js | 1 + middleware/assignLabels.js | 6 +- routes/v1.js | 2 +- test/unit/helper/geojsonify.js | 30 +++++--- test/unit/middleware/assignLabels.js | 108 +++++++++++---------------- 5 files changed, 69 insertions(+), 78 deletions(-) diff --git a/helper/geojsonify.js b/helper/geojsonify.js index 7144d318..99d148dd 100644 --- a/helper/geojsonify.js +++ b/helper/geojsonify.js @@ -46,6 +46,7 @@ function geojsonifyPlace(params, labelGenerator, place) { addMetaData(place, output); addName(place, output); addDetails(params, place, output); + output.label = place.label; // map center_point for GeoJSON to work properly // these should not show up in the final feature properties diff --git a/middleware/assignLabels.js b/middleware/assignLabels.js index 8ee52acc..d10cad69 100644 --- a/middleware/assignLabels.js +++ b/middleware/assignLabels.js @@ -9,12 +9,12 @@ function setup(labelGenerator) { function assignLabel(req, res, next, labelGenerator) { // do nothing if there's nothing to process - if (!res || !res.body || !res.body.features) { + if (!res || !res.data) { return next(); } - res.body.features.forEach(function (feature) { - feature.properties.label = labelGenerator(feature.properties); + res.data.forEach(function (result) { + result.label = labelGenerator(result.parent); }); next(); diff --git a/routes/v1.js b/routes/v1.js index 59936d57..99a54a96 100644 --- a/routes/v1.js +++ b/routes/v1.js @@ -88,8 +88,8 @@ function addRoutes(app, peliasConfig) { postProc.renamePlacenames(), postProc.parseBoundingBox(), postProc.normalizeParentIds(), - postProc.geocodeJSON(peliasConfig, base), postProc.assignLabels(require('pelias-labels')), + postProc.geocodeJSON(peliasConfig, base), postProc.sendJSON ]), autocomplete: createRouter([ diff --git a/test/unit/helper/geojsonify.js b/test/unit/helper/geojsonify.js index 1035901b..22e65b75 100644 --- a/test/unit/helper/geojsonify.js +++ b/test/unit/helper/geojsonify.js @@ -69,7 +69,8 @@ module.exports.tests.geojsonify = function(test, common) { 'category': [ 'food', 'nightlife' - ] + ], + 'label': 'label for id id1' }, { '_id': 'id2', @@ -93,7 +94,8 @@ module.exports.tests.geojsonify = function(test, common) { 'county': 'Smithfield', 'localadmin': 'test1', 'locality': 'test2', - 'neighbourhood': 'test3' + 'neighbourhood': 'test3', + 'label': 'label for id id2' }, { '_id': 'node:34633854', @@ -120,7 +122,8 @@ module.exports.tests.geojsonify = function(test, common) { 'category': [ 'tourism', 'transport' - ] + ], + 'label': 'label for id node:34633854' } ]; @@ -160,7 +163,8 @@ module.exports.tests.geojsonify = function(test, common) { 'category': [ 'food', 'nightlife' - ] + ], + 'label': 'label for id id1' } }, { @@ -188,7 +192,8 @@ module.exports.tests.geojsonify = function(test, common) { 'county': 'Smithfield', 'localadmin': 'test1', 'locality': 'test2', - 'neighbourhood': 'test3' + 'neighbourhood': 'test3', + 'label': 'label for id id2' } }, { @@ -219,7 +224,8 @@ module.exports.tests.geojsonify = function(test, common) { 'category': [ 'tourism', 'transport' - ] + ], + 'label': 'label for id node:34633854' } } ] @@ -330,7 +336,8 @@ module.exports.tests.geojsonify = function(test, common) { null ], 'neighbourhood': [], - 'neighbourhood_gid': [] + 'neighbourhood_gid': [], + 'label': 'label for id 85816607' } ]; @@ -369,7 +376,8 @@ module.exports.tests.geojsonify = function(test, common) { 'county_gid': '102082361', 'localadmin_gid': '404521211', 'locality': 'New York', - 'locality_gid': '85977539' + 'locality_gid': '85977539', + 'label': 'label for id 85816607' }, 'bbox': [-73.8967895508,40.6514712164,-73.8665771484,40.6737320588], 'geometry': { @@ -411,7 +419,8 @@ module.exports.tests.categories = function (test, common) { 'default': 'East New York' }, '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_id': '85816607', 'name': 'East New York', - 'category': ['government'] + 'category': ['government'], + 'label': 'label for id 85816607' }, 'bbox': [-73.8967895508,40.6514712164,-73.8665771484,40.6737320588], 'geometry': { diff --git a/test/unit/middleware/assignLabels.js b/test/unit/middleware/assignLabels.js index 305f99f6..2fae4678 100644 --- a/test/unit/middleware/assignLabels.js +++ b/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(){}); 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) { - var labelGenerator = function(properties) { - if (properties.id === 1) { + var labelGenerator = function(result) { + if (result.id === 1) { return 'label 1'; } - if (properties.id === 2) { + if (result.id === 2) { return 'label 2'; } @@ -53,39 +41,35 @@ module.exports.tests.serialization = function(test, common) { var assignLabels = require('../../../middleware/assignLabels')(labelGenerator); var input = { - body: { - features: [ - { - properties: { - id: 1 - } - }, - { - properties: { - id: 2 - } + data: [ + { + parent: { + id: 1 } - ] - } + }, + { + parent: { + id: 2 + } + } + ] }; var expected = { - body: { - features: [ - { - properties: { - id: 1, - label: 'label 1' - } + data: [ + { + parent: { + id: 1 }, - { - properties: { - id: 2, - label: 'label 2' - } - } - ] - } + label: 'label 1' + }, + { + parent: { + id: 2 + }, + label: 'label 2' + } + ] }; 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) { var assignLabels = proxyquire('../../../middleware/assignLabels', { - 'pelias-labels': function(properties) { - if (properties.id === 1) { + 'pelias-labels': function(result) { + if (result.id === 1) { return 'label 1'; } } })(); var input = { - body: { - features: [ - { - properties: { - id: 1 - } + data: [ + { + parent: { + id: 1 } - ] - } + } + ] }; var expected = { - body: { - features: [ - { - properties: { - id: 1, - label: 'label 1' - } - } - ] - } + data: [ + { + parent: { + id: 1 + }, + label: 'label 1' + } + ] }; assignLabels({}, input, function () {