From e46c17a84aa9f2d9005d24bfb4f74f2b23ebf9db Mon Sep 17 00:00:00 2001 From: Diana Shkolnikov Date: Tue, 4 Apr 2017 13:41:37 -0400 Subject: [PATCH] Sort interpolated results to show address first --- middleware/interpolate.js | 7 +++ test/unit/middleware/interpolate.js | 76 +++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+) diff --git a/middleware/interpolate.js b/middleware/interpolate.js index a6d69649..7ab541cd 100644 --- a/middleware/interpolate.js +++ b/middleware/interpolate.js @@ -43,6 +43,13 @@ function setup() { res.data = results; } + // sort the results to ensure that addresses show up higher than street centroids + res.data = res.data.sort((a, b) => { + if (a.layer === 'address' && b.layer !== 'address') { return -1; } + if (a.layer !== 'address' && b.layer === 'address') { return 1; } + return 0; + }); + // log the execution time, continue logger.info( '[interpolation] [took]', (new Date()).getTime() - timer, 'ms' ); next(); diff --git a/test/unit/middleware/interpolate.js b/test/unit/middleware/interpolate.js index 26bf1e52..3548f3b3 100644 --- a/test/unit/middleware/interpolate.js +++ b/test/unit/middleware/interpolate.js @@ -190,6 +190,82 @@ module.exports.tests.hit = function(test, common) { }); }; +// check the service is called and response mapped correctly +module.exports.tests.hit = function(test, common) { + test('hit', function(t) { + + var req = { clean: { + parsed_text: { + number: '1', + street: 'sesame st' + }} + }; + var res = { data: [ + { + layer: 'street', + center_point: { lat: 1, lon: 1 }, + address_parts: { street: 'sesame rd' }, + name: { default: 'street name' }, + source_id: '123456' + }, + { + layer: 'street', + center_point: { lat: 2, lon: 2 }, + address_parts: { street: 'sesame rd' }, + name: { default: 'street name' }, + source_id: '654321' + } + ]}; + + var middleware = load(); + + // mock out the transport + middleware.transport.query = function mock(coord, number, street, cb) { + if (coord.lat === 2) { + t.deepEqual(coord, res.data[1].center_point); + t.deepEqual(number, req.clean.parsed_text.number); + t.deepEqual(street, res.data[1].address_parts.street); + t.equal(typeof cb, 'function'); + return cb(null, { + properties: { + number: '100A', + source: 'OSM', + source_id: 'way:111111', + lat: 22.2, + lon: -33.3, + } + }); + } + else { + return cb('miss'); + } + }; + + middleware(req, res, function(){ + t.deepEqual( res, { data: [ + { + layer: 'address', + match_type: 'interpolated', + center_point: { lat: 22.2, lon: -33.3 }, + address_parts: { street: 'sesame rd', number: '100A' }, + name: { default: '100A street name' }, + source: 'openstreetmap', + source_id: 'way:111111' + }, + { + layer: 'street', + center_point: { lat: 1, lon: 1 }, + address_parts: { street: 'sesame rd' }, + name: { default: 'street name' }, + source_id: '123456' + } + ]}); + t.end(); + }); + }); +}; + + module.exports.all = function (tape, common) { function test(name, testFunction) {