Browse Source

added guard clause for string/negative population

pull/850/head
Stephen Hess 8 years ago
parent
commit
2ad3bec3b4
  1. 5
      controller/placeholder.js
  2. 113
      test/unit/controller/placeholder.js

5
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 // composition of toNumber and isFinite, useful for single call to convert a value
// to a number, then checking to see if it's finite // to a number, then checking to see if it's finite
const isFiniteNumber = _.flow(_.toNumber, _.isFinite); 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 // returns true if all 4 ,-delimited (max) substrings are parseable as finite numbers
// '12.12,21.21,13.13,31.31' returns true // '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}`); logger.error(`could not parse bbox for id ${result.id}: ${result.geom.bbox}`);
} }
if (_.conformsTo(result, { 'population': isFiniteNumber })) { if (_.conformsTo(result, { 'population': isNonNegativeFiniteNumber })) {
doc.setPopulation(result.population); doc.setPopulation(_.toNumber(result.population));
} }
_.defaultTo(result.lineage, []) _.defaultTo(result.lineage, [])

113
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) => { test('results with undefined or empty name should be skipped', (t) => {
[undefined, '', ' \t '].forEach((invalid_name) => { [undefined, '', ' \t '].forEach((invalid_name) => {
const logger = require('pelias-mock-logger')(); const logger = require('pelias-mock-logger')();

Loading…
Cancel
Save