diff --git a/service/configurations/Interpolation.js b/service/configurations/Interpolation.js new file mode 100644 index 00000000..1bb08d77 --- /dev/null +++ b/service/configurations/Interpolation.js @@ -0,0 +1,30 @@ +'use strict'; + +const url = require('url'); + +const _ = require('lodash'); + +const ServiceConfiguration = require('pelias-microservice-wrapper').ServiceConfiguration; + +class Language extends ServiceConfiguration { + constructor(o) { + super('interpolation', o); + } + + getParameters(req, hit) { + return { + number: req.clean.parsed_text.number, + street: hit.address_parts.street || req.clean.parsed_text.street, + lat: hit.center_point.lat, + lon: hit.center_point.lon + }; + + } + + getUrl(req) { + return url.resolve(this.baseUrl, 'search/geojson'); + } + +} + +module.exports = Language; diff --git a/test/unit/run.js b/test/unit/run.js index fc14194f..6c987085 100644 --- a/test/unit/run.js +++ b/test/unit/run.js @@ -96,6 +96,7 @@ var tests = [ require('./sanitizer/search'), require('./sanitizer/defer_to_addressit'), require('./sanitizer/wrap'), + require('./service/configurations/Interpolation'), require('./service/configurations/Language'), require('./service/configurations/PlaceHolder'), require('./service/configurations/PointInPolygon'), diff --git a/test/unit/service/configurations/Interpolation.js b/test/unit/service/configurations/Interpolation.js new file mode 100644 index 00000000..c71df69e --- /dev/null +++ b/test/unit/service/configurations/Interpolation.js @@ -0,0 +1,140 @@ +module.exports.tests = {}; + +const Interpolation = require('../../../../service/configurations/Interpolation'); + +module.exports.tests.all = (test, common) => { + test('getName should return \'interpolation\'', t => { + const configBlob = { + url: 'http://localhost:1234', + timeout: 17, + retries: 19 + }; + + const interpolation = new Interpolation(configBlob); + + t.equals(interpolation.getName(), 'interpolation'); + t.equals(interpolation.getBaseUrl(), 'http://localhost:1234/'); + t.equals(interpolation.getTimeout(), 17); + t.equals(interpolation.getRetries(), 19); + t.end(); + + }); + + test('getUrl should return value passed to constructor', t => { + const configBlob = { + url: 'http://localhost:1234' + }; + + const interpolation = new Interpolation(configBlob); + + t.equals(interpolation.getUrl(), 'http://localhost:1234/search/geojson'); + t.end(); + + }); + + test('getHeaders should return empty object', t => { + const configBlob = { + url: 'base url' + }; + + const interpolation = new Interpolation(configBlob); + + t.deepEquals(interpolation.getHeaders(), {}); + t.end(); + + }); + + test('getParameters should return hit.address_parts.street over req.clean.parsed_text.street', t => { + const configBlob = { + url: 'base url' + }; + + const interpolation = new Interpolation(configBlob); + + const req = { + clean: { + parsed_text: { + number: 'parsed number value', + street: 'parsed street value' + } + } + }; + + const hit = { + address_parts: { + street: 'hit street value' + }, + center_point: { + lat: 12.121212, + lon: 21.212121 + } + }; + + t.deepEquals(interpolation.getParameters(req, hit), { + number: 'parsed number value', + street: 'hit street value', + lat: 12.121212, + lon: 21.212121 + }); + t.end(); + + }); + + test('getParameters should return req.clean.parsed_text.street when hit.address_parts.street unavailable', t => { + const configBlob = { + url: 'base url' + }; + + const interpolation = new Interpolation(configBlob); + + const req = { + clean: { + parsed_text: { + number: 'parsed number value', + street: 'parsed street value' + } + } + }; + + const hit = { + address_parts: { + }, + center_point: { + lat: 12.121212, + lon: 21.212121 + } + }; + + t.deepEquals(interpolation.getParameters(req, hit), { + number: 'parsed number value', + street: 'parsed street value', + lat: 12.121212, + lon: 21.212121 + }); + t.end(); + + }); + + test('baseUrl ending in / should not have double /\'s return by getUrl', t => { + const configBlob = { + url: 'http://localhost:1234/' + }; + + const interpolation = new Interpolation(configBlob); + + t.deepEquals(interpolation.getUrl(), 'http://localhost:1234/search/geojson'); + t.end(); + + }); + +}; + +module.exports.all = (tape, common) => { + function test(name, testFunction) { + return tape(`SERVICE CONFIGURATION /Interpolation ${name}`, testFunction); + } + + for( var testCase in module.exports.tests ){ + module.exports.tests[testCase](test, common); + } +};