diff --git a/controller/placeholder.js b/controller/placeholder.js index d5f7d169..d488f54c 100644 --- a/controller/placeholder.js +++ b/controller/placeholder.js @@ -5,6 +5,7 @@ const Document = require('pelias-model').Document; // composition of toNumber and isFinite, useful for single call to convert a value // to a number, then checking to see if it's finite const isFiniteNumber = _.flow(_.toNumber, _.isFinite); +const isNonNegativeFiniteNumber = _.flow(_.toNumber, (val) => { return val >= 0; }); // returns true if all 4 ,-delimited (max) substrings are parseable as finite numbers // '12.12,21.21,13.13,31.31' returns true @@ -63,8 +64,8 @@ function synthesizeDocs(boundaryCountry, result) { logger.error(`could not parse bbox for id ${result.id}: ${result.geom.bbox}`); } - if (_.conformsTo(result, { 'population': isFiniteNumber })) { - doc.setPopulation(result.population); + if (_.conformsTo(result, { 'population': isNonNegativeFiniteNumber })) { + doc.setPopulation(_.toNumber(result.population)); } _.defaultTo(result.lineage, []) diff --git a/test/unit/controller/placeholder.js b/test/unit/controller/placeholder.js index a4ccc272..ea30603d 100644 --- a/test/unit/controller/placeholder.js +++ b/test/unit/controller/placeholder.js @@ -357,6 +357,119 @@ module.exports.tests.success = (test, common) => { }); + test('results with string population should convert to number', (t) => { + const logger = require('pelias-mock-logger')(); + + const placeholder_service = (req, callback) => { + t.deepEqual(req, { param1: 'param1 value' }); + + const response = [ + { + id: 123, + name: 'name 1', + placetype: 'neighbourhood', + population: '123.4', + geom: { + area: 12.34 + } + } + ]; + + callback(null, response); + }; + + const controller = proxyquire('../../../controller/placeholder', { + 'pelias-logger': logger + })(placeholder_service, _.constant(true)); + + const req = { param1: 'param1 value' }; + const res = { }; + + controller(req, res, () => { + const expected_res = { + meta: {}, + data: [ + { + _id: '123', + _type: 'neighbourhood', + layer: 'neighbourhood', + source: 'whosonfirst', + source_id: '123', + population: 123.4, + name: { + 'default': 'name 1' + }, + phrase: { + 'default': 'name 1' + }, + parent: { } + } + ] + }; + + t.deepEquals(res, expected_res); + t.ok(logger.isInfoMessage('[controller:placeholder] [result_count:1]')); + t.end(); + }); + + }); + + test('results with negative population should not set population', (t) => { + const logger = require('pelias-mock-logger')(); + + const placeholder_service = (req, callback) => { + t.deepEqual(req, { param1: 'param1 value' }); + + const response = [ + { + id: 123, + name: 'name 1', + placetype: 'neighbourhood', + population: -1, + geom: { + area: 12.34 + } + } + ]; + + callback(null, response); + }; + + const controller = proxyquire('../../../controller/placeholder', { + 'pelias-logger': logger + })(placeholder_service, _.constant(true)); + + const req = { param1: 'param1 value' }; + const res = { }; + + controller(req, res, () => { + const expected_res = { + meta: {}, + data: [ + { + _id: '123', + _type: 'neighbourhood', + layer: 'neighbourhood', + source: 'whosonfirst', + source_id: '123', + name: { + 'default': 'name 1' + }, + phrase: { + 'default': 'name 1' + }, + parent: { } + } + ] + }; + + t.deepEquals(res, expected_res); + t.ok(logger.isInfoMessage('[controller:placeholder] [result_count:1]')); + t.end(); + }); + + }); + test('results with undefined or empty name should be skipped', (t) => { [undefined, '', ' \t '].forEach((invalid_name) => { const logger = require('pelias-mock-logger')();