Browse Source

handle case where result has data but no lineage

pull/850/head
Stephen Hess 8 years ago
parent
commit
fa95fe510a
  1. 104
      controller/placeholder.js
  2. 77
      test/unit/controller/placeholder.js

104
controller/placeholder.js

@ -6,48 +6,86 @@ const logger = require('pelias-logger').get('api');
const logging = require( '../helper/logging' );
const Document = require('pelias-model').Document;
function synthesizeDocs(result) {
return result.lineage.map((hierarchy) => {
const doc = new Document('whosonfirst', result.placetype, result.id.toString());
doc.setName('default', result.name);
doc.setCentroid( { lat: result.geom.lat, lon: result.geom.lon } );
const parsedBoundingBox = result.geom.bbox.split(',').map(parseFloat);
doc.setBoundingBox({
upperLeft: {
lat: parsedBoundingBox[3],
lon: parsedBoundingBox[0]
},
lowerRight: {
lat: parsedBoundingBox[1],
lon: parsedBoundingBox[2]
}
});
function createDocWithoutLineage(result) {
const doc = new Document('whosonfirst', result.placetype, result.id.toString());
doc.setName('default', result.name);
doc.setCentroid( { lat: result.geom.lat, lon: result.geom.lon } );
const parsedBoundingBox = result.geom.bbox.split(',').map(parseFloat);
doc.setBoundingBox({
upperLeft: {
lat: parsedBoundingBox[3],
lon: parsedBoundingBox[0]
},
lowerRight: {
lat: parsedBoundingBox[1],
lon: parsedBoundingBox[2]
}
});
Object.keys(hierarchy)
.filter(doc.isSupportedParent)
.filter((placetype) => { return !_.isEmpty(_.trim(hierarchy[placetype].name)); } )
.forEach((placetype) => {
if (hierarchy[placetype].hasOwnProperty('abbr') && placetype === 'country') {
doc.setAlpha3(hierarchy[placetype].abbr);
}
const esDoc = doc.toESDocument();
esDoc.data._id = esDoc._id;
esDoc.data._type = esDoc._type;
doc.addParent(
placetype,
hierarchy[placetype].name,
hierarchy[placetype].id.toString(),
hierarchy[placetype].abbr);
return esDoc.data;
});
}
function synthesizeDocs(result) {
const doc = new Document('whosonfirst', result.placetype, result.id.toString());
doc.setName('default', result.name);
doc.setCentroid( { lat: result.geom.lat, lon: result.geom.lon } );
const parsedBoundingBox = result.geom.bbox.split(',').map(parseFloat);
doc.setBoundingBox({
upperLeft: {
lat: parsedBoundingBox[3],
lon: parsedBoundingBox[0]
},
lowerRight: {
lat: parsedBoundingBox[1],
lon: parsedBoundingBox[2]
}
});
if (_.isEmpty(result.lineage)) {
// there are no hierarchies so just return what's been assembled so far
const esDoc = doc.toESDocument();
esDoc.data._id = esDoc._id;
esDoc.data._type = esDoc._type;
return esDoc.data;
});
} else {
return result.lineage.map((hierarchy) => {
// clear out the effects of the last hierarchy array (this effectively clones the base Document)
doc.clearAlpha3().clearAllParents();
Object.keys(hierarchy)
.filter(doc.isSupportedParent)
.filter((placetype) => { return !_.isEmpty(_.trim(hierarchy[placetype].name)); } )
.forEach((placetype) => {
if (hierarchy[placetype].hasOwnProperty('abbr') && placetype === 'country') {
doc.setAlpha3(hierarchy[placetype].abbr);
}
doc.addParent(
placetype,
hierarchy[placetype].name,
hierarchy[placetype].id.toString(),
hierarchy[placetype].abbr);
});
const esDoc = doc.toESDocument();
esDoc.data._id = esDoc._id;
esDoc.data._type = esDoc._type;
return esDoc.data;
});
}
}
function setup(placeholderService, should_execute) {
@ -67,6 +105,10 @@ function setup(placeholderService, should_execute) {
} else {
res.meta = {};
res.data = _.flatten(results.map((result) => {
if (_.isEmpty(result.lineage)) {
return createDocWithoutLineage(result);
}
return synthesizeDocs(result);
}));
}

77
test/unit/controller/placeholder.js

@ -92,7 +92,8 @@ module.exports.tests.success = function(test, common) {
{
country: {
id: 1,
name: 'country name 1'
name: 'country name 1',
abbr: 'ABC'
},
dependency: {
id: 2,
@ -250,6 +251,7 @@ module.exports.tests.success = function(test, common) {
phrase: {
'default': 'name 1'
},
alpha3: 'ABC',
parent: {
neighbourhood: ['neighbourhood name 1'],
neighbourhood_id: ['10'],
@ -280,7 +282,7 @@ module.exports.tests.success = function(test, common) {
dependency_a: [null],
country: ['country name 1'],
country_id: ['1'],
country_a: [null]
country_a: ['ABC']
}
},
{
@ -364,6 +366,77 @@ module.exports.tests.success = function(test, common) {
});
test('results with no lineage should no set any parent fields', (t) => {
const placeholder_response = [
{
id: 123,
name: 'name 1',
placetype: 'neighbourhood',
geom: {
area: 12.34,
bbox: '21.212121,12.121212,31.313131,13.131313',
lat: 14.141414,
lon: 41.414141
}
}
];
const placeholderService = {
search: (text, language, do_not_track, callback) => {
t.equals(text, 'query value');
t.equals(language, 'language value');
callback(null, placeholder_response);
}
};
const should_execute = (req, res) => {
return true;
};
const controller = placeholder(placeholderService, should_execute);
const req = {
clean: {
text: 'query value',
lang: {
iso6393: 'language value'
}
}
};
const res = { };
const expected_res = {
meta: {},
data: [
{
_id: '123',
_type: 'neighbourhood',
layer: 'neighbourhood',
source: 'whosonfirst',
source_id: '123',
center_point: {
lat: 14.141414,
lon: 41.414141
},
bounding_box: '{"min_lat":12.121212,"max_lat":13.131313,"min_lon":21.212121,"max_lon":31.313131}',
name: {
'default': 'name 1'
},
phrase: {
'default': 'name 1'
},
parent: { }
}
]
};
controller(req, res, () => {
t.deepEquals(res, expected_res);
t.end();
});
});
};
module.exports.tests.do_not_track = function(test, common) {

Loading…
Cancel
Save