diff --git a/service/configurations/Language.js b/service/configurations/Language.js new file mode 100644 index 00000000..e0c2f04a --- /dev/null +++ b/service/configurations/Language.js @@ -0,0 +1,33 @@ +'use strict'; + +const url = require('url'); + +const _ = require('lodash'); + +const ServiceConfiguration = require('pelias-microservice-wrapper').ServiceConfiguration; + +class Language extends ServiceConfiguration { + constructor(o) { + super('placeholder.language', o); + } + + getParameters(req, res) { + // find all the values for all keys with names that end with '_id' + const ids = _.get(res, 'data', []).reduce((acc, doc) => { + Array.prototype.push.apply(acc, _.values(_.pickBy(doc.parent, (v, k) => _.endsWith(k, '_id') ) ) ); + return acc; + }, []); + + return { + ids: _.uniq(ids) + }; + + } + + getUrl(req) { + return url.resolve(this.baseUrl, 'parser/findbyid'); + } + +} + +module.exports = Language; diff --git a/test/unit/run.js b/test/unit/run.js index e616231e..1071f474 100644 --- a/test/unit/run.js +++ b/test/unit/run.js @@ -95,6 +95,7 @@ var tests = [ require('./sanitizer/search'), require('./sanitizer/defer_to_addressit'), require('./sanitizer/wrap'), + require('./service/configurations/Language'), require('./service/configurations/PlaceHolder'), require('./service/configurations/PointInPolygon'), require('./service/mget'), diff --git a/test/unit/service/configurations/Language.js b/test/unit/service/configurations/Language.js new file mode 100644 index 00000000..bf3b62e6 --- /dev/null +++ b/test/unit/service/configurations/Language.js @@ -0,0 +1,157 @@ +module.exports.tests = {}; + +const Language = require('../../../../service/configurations/Language'); + +module.exports.tests.all = (test, common) => { + test('getName should return \'placeholder.language\'', (t) => { + const configBlob = { + url: 'http://localhost:1234', + timeout: 17, + retries: 19 + }; + + const language = new Language(configBlob); + + t.equals(language.getName(), 'placeholder.language'); + t.equals(language.getBaseUrl(), 'http://localhost:1234/'); + t.equals(language.getTimeout(), 17); + t.equals(language.getRetries(), 19); + t.end(); + + }); + + test('getUrl should return value passed to constructor', (t) => { + const configBlob = { + url: 'http://localhost:1234', + timeout: 17, + retries: 19 + }; + + const language = new Language(configBlob); + + t.equals(language.getUrl(), 'http://localhost:1234/parser/findbyid'); + t.end(); + + }); + + test('getParameters should return object with all deduped ids extracted from res', (t) => { + const configBlob = { + url: 'http://localhost:1234', + timeout: 17, + retries: 19 + }; + + const language = new Language(configBlob); + + const res = { + data: [ + { + parent: { + layer1_name: 'layer1 name', + layer1_id: 1, + layer2_name: 'layer2 name', + layer2_id: 2 + } + }, + { + // empty parent + parent: {} + }, + { + // no parent + }, + { + parent: { + layer3_name: 'layer3 name', + layer3_id: 3, + layer4_name: 'layer4 name', + // doesn't end with '_id', will be ignored + layer4id: 4 + } + }, + { + parent: { + layer1_name: 'layer1 name', + // this is a duplicate id + layer1_id: 1, + layer5_name: 'layer5 name', + layer5_id: 5 + } + } + ] + }; + + t.deepEquals(language.getParameters(undefined, res), { ids: [1, 2, 3, 5] }); + t.end(); + + }); + + test('getParameters should return empty ids array when res is undefined', (t) => { + const configBlob = { + url: 'http://localhost:1234', + timeout: 17, + retries: 19 + }; + + const language = new Language(configBlob); + + t.deepEquals(language.getParameters(undefined, undefined), { ids: [] }); + t.end(); + + }); + + test('getParameters should return empty ids array when res.data is undefined', (t) => { + const configBlob = { + url: 'http://localhost:1234', + timeout: 17, + retries: 19 + }; + + const res = { }; + + const language = new Language(configBlob); + + t.deepEquals(language.getParameters(undefined, res), { ids: [] }); + t.end(); + + }); + + test('getHeaders should return empty object', (t) => { + const configBlob = { + url: 'base url', + timeout: 17, + retries: 19 + }; + + const language = new Language(configBlob); + + t.deepEquals(language.getHeaders(), {}); + t.end(); + + }); + + test('baseUrl ending in / should not have double /\'s return by getUrl', (t) => { + const configBlob = { + url: 'http://localhost:1234/', + timeout: 17, + retries: 19 + }; + + const language = new Language(configBlob); + + t.deepEquals(language.getUrl(), 'http://localhost:1234/parser/findbyid'); + t.end(); + + }); + +}; + +module.exports.all = (tape, common) => { + function test(name, testFunction) { + return tape(`SERVICE CONFIGURATION /Language ${name}`, testFunction); + } + + for( var testCase in module.exports.tests ){ + module.exports.tests[testCase](test, common); + } +};