mirror of https://github.com/pelias/api.git
Stephen K Hess
7 years ago
committed by
GitHub
21 changed files with 1689 additions and 1433 deletions
@ -1,18 +0,0 @@
|
||||
const _ = require('lodash'); |
||||
|
||||
// return true if any setup parameter is a key of request.clean.parsed_text
|
||||
// "arguments" is only available in long-form function declarations, cannot be shortened to fat arrow syntax
|
||||
// potential improvement: inject set operator to allow for any/all functionality
|
||||
module.exports = function() { |
||||
// save off requested properties since arguments can't be referenced later
|
||||
const properties = _.values(arguments); |
||||
|
||||
// return true if any of the supplied properties are in clean.parsed_text
|
||||
return (request, response) => !_.isEmpty( |
||||
_.intersection( |
||||
properties, |
||||
_.keys(_.get(request, ['clean', 'parsed_text'], {})) |
||||
) |
||||
); |
||||
|
||||
}; |
@ -0,0 +1,33 @@
|
||||
const _ = require('lodash'); |
||||
|
||||
// "arguments" is only available in long-form function declarations, cannot be shortened to fat arrow syntax
|
||||
// potential improvement: inject set operator to allow for any/all functionality
|
||||
module.exports = { |
||||
all: function() { |
||||
// save off property names for future reference
|
||||
const properties = _.values(arguments); |
||||
|
||||
// return true if ALL of the supplied properties are in clean.parsed_text
|
||||
return request => _.isEmpty( |
||||
_.difference( |
||||
_.values(properties), |
||||
_.keys(_.get(request, ['clean', 'parsed_text'], {})) |
||||
) |
||||
); |
||||
|
||||
}, |
||||
any: function() { |
||||
// save off property names for future reference
|
||||
const properties = _.values(arguments); |
||||
|
||||
// return true if ANY of the supplied properties are in clean.parsed_text
|
||||
return request => !_.isEmpty( |
||||
_.intersection( |
||||
_.values(properties), |
||||
_.keys(_.get(request, ['clean', 'parsed_text'], {})) |
||||
) |
||||
); |
||||
|
||||
} |
||||
|
||||
}; |
@ -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; |
@ -0,0 +1,34 @@
|
||||
'use strict'; |
||||
|
||||
const url = require('url'); |
||||
|
||||
const _ = require('lodash'); |
||||
|
||||
const ServiceConfiguration = require('pelias-microservice-wrapper').ServiceConfiguration; |
||||
|
||||
class Language extends ServiceConfiguration { |
||||
constructor(o) { |
||||
super('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 { |
||||
// arrays will be nested, so flatten first, then uniqify, and finally join elements with comma
|
||||
ids: _.uniq(_.flattenDeep(ids)).join(',') |
||||
}; |
||||
|
||||
} |
||||
|
||||
getUrl(req) { |
||||
return url.resolve(this.baseUrl, 'parser/findbyid'); |
||||
} |
||||
|
||||
} |
||||
|
||||
module.exports = Language; |
@ -1,118 +0,0 @@
|
||||
|
||||
var logger = require( 'pelias-logger' ).get( 'api' ), |
||||
request = require( 'superagent' ), |
||||
peliasConfig = require( 'pelias-config' ); |
||||
|
||||
/** |
||||
|
||||
street address interpolation service client |
||||
|
||||
this file provides several different 'transports' which can be used to access the interpolation |
||||
service, either directly from disk or via a network connnection. |
||||
|
||||
the exported method for this module checks pelias-config for a configuration block such as: |
||||
|
||||
"interpolation": { |
||||
"client": { |
||||
"adapter": "http", |
||||
"host": "http://localhost:4444" |
||||
} |
||||
} |
||||
|
||||
for more info on running the service see: https://github.com/pelias/interpolation
|
||||
|
||||
**/ |
||||
|
||||
/** |
||||
NullTransport |
||||
|
||||
disables the service completely |
||||
**/ |
||||
function NullTransport(){} |
||||
NullTransport.prototype.query = function( coord, number, street, cb ){ |
||||
cb(); // no-op
|
||||
}; |
||||
|
||||
/** |
||||
RequireTransport |
||||
|
||||
allows the api to be used by simply requiring the module |
||||
**/ |
||||
function RequireTransport( addressDbPath, streetDbPath ){ |
||||
try { |
||||
var lib = require('pelias-interpolation'); // lazy load dependency
|
||||
this.query = lib.api.search( addressDbPath, streetDbPath ); |
||||
} catch( e ){ |
||||
logger.error( 'RequireTransport: failed to connect to interpolation service' ); |
||||
} |
||||
} |
||||
RequireTransport.prototype.query = function( coord, number, street, cb ){ |
||||
throw new Error( 'interpolation: transport not connected' ); |
||||
}; |
||||
|
||||
/** |
||||
HttpTransport |
||||
|
||||
allows the api to be used via a remote web service |
||||
**/ |
||||
function HttpTransport( host, settings ){ |
||||
this.query = function( coord, number, street, cb ){ |
||||
request |
||||
.get( host + '/search/geojson' ) |
||||
.set( 'Accept', 'application/json' ) |
||||
.query({ lat: coord.lat, lon: coord.lon, number: number, street: street }) |
||||
.timeout( settings && settings.timeout || 1000 ) |
||||
.end( function( err, res ){ |
||||
if( err || !res ){ return cb( err ); } |
||||
if( 200 !== res.status ){ return cb( 'non 200 status' ); } |
||||
return cb( null, res.body ); |
||||
}); |
||||
}; |
||||
} |
||||
HttpTransport.prototype.query = function( coord, number, street, cb ){ |
||||
throw new Error( 'interpolation: transport not connected' ); |
||||
}; |
||||
|
||||
/** |
||||
Setup |
||||
|
||||
allows instantiation of transport depending on configuration and preference |
||||
**/ |
||||
module.exports.search = function setup(){ |
||||
|
||||
// user config
|
||||
var config = peliasConfig.generate(); |
||||
|
||||
// ensure config variables set correctly
|
||||
if( !config.hasOwnProperty('interpolation') || !config.interpolation.hasOwnProperty('client') ){ |
||||
logger.warn( 'interpolation: configuration not found' ); |
||||
} |
||||
|
||||
// valid configuration found
|
||||
else { |
||||
|
||||
// get adapter settings from config
|
||||
var settings = config.interpolation.client; |
||||
|
||||
// http adapter
|
||||
if( 'http' === settings.adapter && settings.hasOwnProperty('host') ){ |
||||
logger.info( 'interpolation: using http transport:', settings.host ); |
||||
if( settings.hasOwnProperty('timeout') ){ |
||||
return new HttpTransport( settings.host, { timeout: parseInt( settings.timeout, 10 ) } ); |
||||
} |
||||
return new HttpTransport( settings.host ); |
||||
} |
||||
|
||||
// require adapter
|
||||
else if( 'require' === settings.adapter ){ |
||||
if( settings.hasOwnProperty('streetdb') && settings.hasOwnProperty('addressdb') ){ |
||||
logger.info( 'interpolation: using require transport' ); |
||||
return new RequireTransport( settings.addressdb, settings.streetdb ); |
||||
} |
||||
} |
||||
} |
||||
|
||||
// default adapter
|
||||
logger.info( 'interpolation: using null transport' ); |
||||
return new NullTransport(); |
||||
}; |
@ -1,93 +0,0 @@
|
||||
|
||||
var logger = require( 'pelias-logger' ).get( 'api' ), |
||||
request = require( 'superagent' ), |
||||
peliasConfig = require( 'pelias-config' ); |
||||
|
||||
/** |
||||
|
||||
language subsitution service client |
||||
|
||||
this file provides a 'transport' which can be used to access the language |
||||
service via a network connnection. |
||||
|
||||
the exported method for this module checks pelias-config for a configuration block such as: |
||||
|
||||
"language": { |
||||
"client": { |
||||
"adapter": "http", |
||||
"host": "http://localhost:6100" |
||||
} |
||||
} |
||||
|
||||
for more info on running the service see: https://github.com/pelias/placeholder
|
||||
|
||||
**/ |
||||
|
||||
/** |
||||
NullTransport |
||||
|
||||
disables the service completely |
||||
**/ |
||||
function NullTransport(){} |
||||
NullTransport.prototype.query = function( ids, cb ){ |
||||
cb(); // no-op
|
||||
}; |
||||
|
||||
/** |
||||
HttpTransport |
||||
|
||||
allows the api to be used via a remote web service |
||||
**/ |
||||
function HttpTransport( host, settings ){ |
||||
this.query = function( ids, cb ){ |
||||
request |
||||
.get( host + '/parser/findbyid' ) |
||||
.set( 'Accept', 'application/json' ) |
||||
.query({ ids: Array.isArray( ids ) ? ids.join(',') : '' }) |
||||
.timeout( settings && settings.timeout || 1000 ) |
||||
.end( function( err, res ){ |
||||
if( err || !res ){ return cb( err ); } |
||||
if( 200 !== res.status ){ return cb( 'non 200 status' ); } |
||||
return cb( null, res.body ); |
||||
}); |
||||
}; |
||||
} |
||||
HttpTransport.prototype.query = function( coord, number, street, cb ){ |
||||
throw new Error( 'language: transport not connected' ); |
||||
}; |
||||
|
||||
/** |
||||
Setup |
||||
|
||||
allows instantiation of transport depending on configuration and preference |
||||
**/ |
||||
module.exports.findById = function setup(){ |
||||
|
||||
// user config
|
||||
var config = peliasConfig.generate(); |
||||
|
||||
// ensure config variables set correctly
|
||||
if( !config.hasOwnProperty('language') || !config.language.hasOwnProperty('client') ){ |
||||
logger.warn( 'language: configuration not found' ); |
||||
} |
||||
|
||||
// valid configuration found
|
||||
else { |
||||
|
||||
// get adapter settings from config
|
||||
var settings = config.language.client; |
||||
|
||||
// http adapter
|
||||
if( 'http' === settings.adapter && settings.hasOwnProperty('host') ){ |
||||
logger.info( 'language: using http transport:', settings.host ); |
||||
if( settings.hasOwnProperty('timeout') ){ |
||||
return new HttpTransport( settings.host, { timeout: parseInt( settings.timeout, 10 ) } ); |
||||
} |
||||
return new HttpTransport( settings.host ); |
||||
} |
||||
} |
||||
|
||||
// default adapter
|
||||
logger.info( 'language: using null transport' ); |
||||
return new NullTransport(); |
||||
}; |
@ -1,94 +0,0 @@
|
||||
'use strict'; |
||||
|
||||
const _ = require('lodash'); |
||||
const has_any_parsed_text_property = require('../../../../controller/predicates/has_any_parsed_text_property'); |
||||
|
||||
module.exports.tests = {}; |
||||
|
||||
module.exports.tests.interface = (test, common) => { |
||||
test('valid interface', (t) => { |
||||
t.ok(_.isFunction(has_any_parsed_text_property), 'has_any_parsed_text_property is a function'); |
||||
t.end(); |
||||
}); |
||||
}; |
||||
|
||||
module.exports.tests.true_conditions = (test, common) => { |
||||
test('defined request.clean.parsed_text.property should return true', (t) => { |
||||
const req = { |
||||
clean: { |
||||
parsed_text: { |
||||
property: 'value' |
||||
} |
||||
} |
||||
}; |
||||
|
||||
t.ok(has_any_parsed_text_property('property')(req)); |
||||
t.end(); |
||||
|
||||
}); |
||||
|
||||
test('clean.parsed_text with any property should return true ', (t) => { |
||||
const req = { |
||||
clean: { |
||||
parsed_text: { |
||||
property2: 'value2', |
||||
property3: 'value3' |
||||
} |
||||
} |
||||
}; |
||||
|
||||
t.ok(has_any_parsed_text_property('property1', 'property3')(req)); |
||||
t.end(); |
||||
|
||||
}); |
||||
|
||||
}; |
||||
|
||||
module.exports.tests.false_conditions = (test, common) => { |
||||
test('undefined request should return false', (t) => { |
||||
t.notOk(has_any_parsed_text_property('property')()); |
||||
t.end(); |
||||
|
||||
}); |
||||
|
||||
test('undefined request.clean should return false', (t) => { |
||||
const req = {}; |
||||
|
||||
t.notOk(has_any_parsed_text_property('property')(req)); |
||||
t.end(); |
||||
|
||||
}); |
||||
|
||||
test('undefined request.clean.parsed_text should return false', (t) => { |
||||
const req = { |
||||
clean: {} |
||||
}; |
||||
|
||||
t.notOk(has_any_parsed_text_property('property')(req)); |
||||
t.end(); |
||||
|
||||
}); |
||||
|
||||
test('request.clean.parsed_text with none of the supplied properties should return false', (t) => { |
||||
const req = { |
||||
clean: { |
||||
parsed_text: {} |
||||
} |
||||
}; |
||||
|
||||
t.notOk(has_any_parsed_text_property('property1', 'property2')(req)); |
||||
t.end(); |
||||
|
||||
}); |
||||
|
||||
}; |
||||
|
||||
module.exports.all = (tape, common) => { |
||||
function test(name, testFunction) { |
||||
return tape(`GET /has_any_parsed_text_property ${name}`, testFunction); |
||||
} |
||||
|
||||
for( const testCase in module.exports.tests ){ |
||||
module.exports.tests[testCase](test, common); |
||||
} |
||||
}; |
@ -0,0 +1,163 @@
|
||||
'use strict'; |
||||
|
||||
const _ = require('lodash'); |
||||
const has_parsed_text_properties = require('../../../../controller/predicates/has_parsed_text_properties'); |
||||
|
||||
module.exports.tests = {}; |
||||
|
||||
module.exports.tests.interface = (test, common) => { |
||||
test('valid interface', (t) => { |
||||
t.ok(_.isFunction(has_parsed_text_properties.all), 'has_parsed_text_properties.all is a function'); |
||||
t.ok(_.isFunction(has_parsed_text_properties.any), 'has_parsed_text_properties.any is a function'); |
||||
t.end(); |
||||
}); |
||||
|
||||
}; |
||||
|
||||
module.exports.tests.true_conditions = (test, common) => { |
||||
test('all: defined request.clean.parsed_text.property should return true', (t) => { |
||||
const req = { |
||||
clean: { |
||||
parsed_text: { |
||||
property: 'value' |
||||
} |
||||
} |
||||
}; |
||||
|
||||
t.ok(has_parsed_text_properties.all('property')(req)); |
||||
t.end(); |
||||
|
||||
}); |
||||
|
||||
test('all: clean.parsed_text with any property should return true ', (t) => { |
||||
const req = { |
||||
clean: { |
||||
parsed_text: { |
||||
property1: 'value1', |
||||
property2: 'value2' |
||||
} |
||||
} |
||||
}; |
||||
|
||||
t.ok(has_parsed_text_properties.all('property2', 'property1')(req)); |
||||
t.end(); |
||||
|
||||
}); |
||||
|
||||
test('any: defined request.clean.parsed_text.property should return true', (t) => { |
||||
const req = { |
||||
clean: { |
||||
parsed_text: { |
||||
property: 'value' |
||||
} |
||||
} |
||||
}; |
||||
|
||||
t.ok(has_parsed_text_properties.any('property')(req)); |
||||
t.end(); |
||||
|
||||
}); |
||||
|
||||
test('any: clean.parsed_text with any property should return true ', (t) => { |
||||
const req = { |
||||
clean: { |
||||
parsed_text: { |
||||
property2: 'value2', |
||||
property3: 'value3' |
||||
} |
||||
} |
||||
}; |
||||
|
||||
t.ok(has_parsed_text_properties.any('property1', 'property3')(req)); |
||||
t.end(); |
||||
|
||||
}); |
||||
|
||||
}; |
||||
|
||||
module.exports.tests.false_conditions = (test, common) => { |
||||
test('all: undefined request should return false', (t) => { |
||||
t.notOk(has_parsed_text_properties.all('property')()); |
||||
t.end(); |
||||
|
||||
}); |
||||
|
||||
test('all: undefined request.clean should return false', (t) => { |
||||
const req = {}; |
||||
|
||||
t.notOk(has_parsed_text_properties.all('property')(req)); |
||||
t.end(); |
||||
|
||||
}); |
||||
|
||||
test('all: undefined request.clean.parsed_text should return false', (t) => { |
||||
const req = { |
||||
clean: {} |
||||
}; |
||||
|
||||
t.notOk(has_parsed_text_properties.all('property')(req)); |
||||
t.end(); |
||||
|
||||
}); |
||||
|
||||
test('all: request.clean.parsed_text with none of the supplied properties should return false', (t) => { |
||||
const req = { |
||||
clean: { |
||||
parsed_text: { |
||||
property1: 'value1' |
||||
} |
||||
} |
||||
}; |
||||
|
||||
t.notOk(has_parsed_text_properties.all('property1', 'property2')(req)); |
||||
t.end(); |
||||
|
||||
}); |
||||
|
||||
test('any: undefined request should return false', (t) => { |
||||
t.notOk(has_parsed_text_properties.any('property')()); |
||||
t.end(); |
||||
|
||||
}); |
||||
|
||||
test('any: undefined request.clean should return false', (t) => { |
||||
const req = {}; |
||||
|
||||
t.notOk(has_parsed_text_properties.any('property')(req)); |
||||
t.end(); |
||||
|
||||
}); |
||||
|
||||
test('any: undefined request.clean.parsed_text should return false', (t) => { |
||||
const req = { |
||||
clean: {} |
||||
}; |
||||
|
||||
t.notOk(has_parsed_text_properties.any('property')(req)); |
||||
t.end(); |
||||
|
||||
}); |
||||
|
||||
test('any: request.clean.parsed_text with none of the supplied properties should return false', (t) => { |
||||
const req = { |
||||
clean: { |
||||
parsed_text: {} |
||||
} |
||||
}; |
||||
|
||||
t.notOk(has_parsed_text_properties.any('property1', 'property2')(req)); |
||||
t.end(); |
||||
|
||||
}); |
||||
|
||||
}; |
||||
|
||||
module.exports.all = (tape, common) => { |
||||
function test(name, testFunction) { |
||||
return tape(`GET /has_parsed_text_properties ${name}`, testFunction); |
||||
} |
||||
|
||||
for( const testCase in module.exports.tests ){ |
||||
module.exports.tests[testCase](test, common); |
||||
} |
||||
}; |
@ -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); |
||||
} |
||||
}; |
@ -0,0 +1,158 @@
|
||||
module.exports.tests = {}; |
||||
|
||||
const Language = require('../../../../service/configurations/Language'); |
||||
|
||||
module.exports.tests.all = (test, common) => { |
||||
test('getName should return \'language\'', (t) => { |
||||
const configBlob = { |
||||
url: 'http://localhost:1234', |
||||
timeout: 17, |
||||
retries: 19 |
||||
}; |
||||
|
||||
const language = new Language(configBlob); |
||||
|
||||
t.equals(language.getName(), '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: { |
||||
// this is a duplicate id
|
||||
layer1_name: 'layer1 name', |
||||
layer1_id: [1], |
||||
// two ids, both should be added
|
||||
layer5_name: 'layer5 name', |
||||
layer5_id: [5, 6] |
||||
} |
||||
} |
||||
] |
||||
}; |
||||
|
||||
t.deepEquals(language.getParameters(undefined, res), { ids: '1,2,3,5,6' }); |
||||
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); |
||||
} |
||||
}; |
@ -1,128 +0,0 @@
|
||||
|
||||
var fs = require('fs'), |
||||
tmp = require('tmp'), |
||||
setup = require('../../../service/interpolation').search; |
||||
|
||||
module.exports.tests = {}; |
||||
|
||||
module.exports.tests.interface = function(test, common) { |
||||
test('valid interface', function(t) { |
||||
t.equal(typeof setup, 'function', 'setup is a function'); |
||||
t.end(); |
||||
}); |
||||
}; |
||||
|
||||
// adapter factory
|
||||
module.exports.tests.factory = function(test, common) { |
||||
|
||||
test('http adapter', function(t) { |
||||
var config = { interpolation: { client: { |
||||
adapter: 'http', |
||||
host: 'http://example.com' |
||||
}}}; |
||||
|
||||
// adapter is driven by config
|
||||
var tmpfile = tmp.tmpNameSync({ postfix: '.json' }); |
||||
fs.writeFileSync( tmpfile, JSON.stringify( config ), { encoding: 'utf8' } ); |
||||
process.env.PELIAS_CONFIG = tmpfile; |
||||
var adapter = setup(); |
||||
delete process.env.PELIAS_CONFIG; |
||||
|
||||
t.equal(adapter.constructor.name, 'HttpTransport', 'HttpTransport'); |
||||
t.equal(typeof adapter, 'object', 'adapter is an object'); |
||||
t.equal(typeof adapter.query, 'function', 'query is a function'); |
||||
t.equal(adapter.query.length, 4, 'query function signature'); |
||||
t.end(); |
||||
}); |
||||
|
||||
test('require adapter', function(t) { |
||||
var config = { interpolation: { client: { |
||||
adapter: 'require', |
||||
addressdb: '/tmp/address.db', |
||||
streetdb: '/tmp/street.db' |
||||
}}}; |
||||
|
||||
// adapter is driven by config
|
||||
var tmpfile = tmp.tmpNameSync({ postfix: '.json' }); |
||||
fs.writeFileSync( tmpfile, JSON.stringify( config ), { encoding: 'utf8' } ); |
||||
process.env.PELIAS_CONFIG = tmpfile; |
||||
var adapter = setup(); |
||||
delete process.env.PELIAS_CONFIG; |
||||
|
||||
t.equal(adapter.constructor.name, 'RequireTransport', 'RequireTransport'); |
||||
t.equal(typeof adapter, 'object', 'adapter is an object'); |
||||
t.equal(typeof adapter.query, 'function', 'query is a function'); |
||||
t.equal(adapter.query.length, 4, 'query function signature'); |
||||
t.end(); |
||||
}); |
||||
|
||||
test('null adapter', function(t) { |
||||
var config = { interpolation: { client: { |
||||
adapter: 'null' |
||||
}}}; |
||||
|
||||
// adapter is driven by config
|
||||
var tmpfile = tmp.tmpNameSync({ postfix: '.json' }); |
||||
fs.writeFileSync( tmpfile, JSON.stringify( config ), { encoding: 'utf8' } ); |
||||
process.env.PELIAS_CONFIG = tmpfile; |
||||
var adapter = setup(); |
||||
delete process.env.PELIAS_CONFIG; |
||||
|
||||
t.equal(adapter.constructor.name, 'NullTransport', 'NullTransport'); |
||||
t.equal(typeof adapter, 'object', 'adapter is an object'); |
||||
t.equal(typeof adapter.query, 'function', 'query is a function'); |
||||
t.equal(adapter.query.length, 4, 'query function signature'); |
||||
t.end(); |
||||
}); |
||||
|
||||
test('default adapter', function(t) { |
||||
var config = {}; |
||||
|
||||
// adapter is driven by config
|
||||
var tmpfile = tmp.tmpNameSync({ postfix: '.json' }); |
||||
fs.writeFileSync( tmpfile, JSON.stringify( config ), { encoding: 'utf8' } ); |
||||
process.env.PELIAS_CONFIG = tmpfile; |
||||
var adapter = setup(); |
||||
delete process.env.PELIAS_CONFIG; |
||||
|
||||
t.equal(adapter.constructor.name, 'NullTransport', 'NullTransport'); |
||||
t.equal(typeof adapter, 'object', 'adapter is an object'); |
||||
t.equal(typeof adapter.query, 'function', 'query is a function'); |
||||
t.equal(adapter.query.length, 4, 'query function signature'); |
||||
t.end(); |
||||
}); |
||||
|
||||
}; |
||||
|
||||
// null transport
|
||||
module.exports.tests.NullTransport = function(test, common) { |
||||
|
||||
test('null transport', function(t) { |
||||
|
||||
// adapter is driven by config
|
||||
var tmpfile = tmp.tmpNameSync({ postfix: '.json' }); |
||||
fs.writeFileSync( tmpfile, '{}', { encoding: 'utf8' } ); |
||||
process.env.PELIAS_CONFIG = tmpfile; |
||||
var adapter = setup(); |
||||
delete process.env.PELIAS_CONFIG; |
||||
|
||||
// test null transport performs a no-op
|
||||
adapter.query( null, null, null, function( err, res ){ |
||||
t.equal(err, undefined, 'no-op'); |
||||
t.equal(res, undefined, 'no-op'); |
||||
t.end(); |
||||
}); |
||||
}); |
||||
|
||||
}; |
||||
|
||||
module.exports.all = function (tape, common) { |
||||
|
||||
function test(name, testFunction) { |
||||
return tape('SERVICE interpolation', testFunction); |
||||
} |
||||
|
||||
for( var testCase in module.exports.tests ){ |
||||
module.exports.tests[testCase](test, common); |
||||
} |
||||
}; |
@ -1,107 +0,0 @@
|
||||
|
||||
var fs = require('fs'), |
||||
tmp = require('tmp'), |
||||
setup = require('../../../service/language').findById; |
||||
|
||||
module.exports.tests = {}; |
||||
|
||||
module.exports.tests.interface = function(test, common) { |
||||
test('valid interface', function(t) { |
||||
t.equal(typeof setup, 'function', 'setup is a function'); |
||||
t.end(); |
||||
}); |
||||
}; |
||||
|
||||
// adapter factory
|
||||
module.exports.tests.factory = function(test, common) { |
||||
|
||||
test('http adapter', function(t) { |
||||
var config = { language: { client: { |
||||
adapter: 'http', |
||||
host: 'http://example.com' |
||||
}}}; |
||||
|
||||
// adapter is driven by config
|
||||
var tmpfile = tmp.tmpNameSync({ postfix: '.json' }); |
||||
fs.writeFileSync( tmpfile, JSON.stringify( config ), { encoding: 'utf8' } ); |
||||
process.env.PELIAS_CONFIG = tmpfile; |
||||
var adapter = setup(); |
||||
delete process.env.PELIAS_CONFIG; |
||||
|
||||
t.equal(adapter.constructor.name, 'HttpTransport', 'HttpTransport'); |
||||
t.equal(typeof adapter, 'object', 'adapter is an object'); |
||||
t.equal(typeof adapter.query, 'function', 'query is a function'); |
||||
t.equal(adapter.query.length, 2, 'query function signature'); |
||||
t.end(); |
||||
}); |
||||
|
||||
test('null adapter', function(t) { |
||||
var config = { language: { client: { |
||||
adapter: 'null' |
||||
}}}; |
||||
|
||||
// adapter is driven by config
|
||||
var tmpfile = tmp.tmpNameSync({ postfix: '.json' }); |
||||
fs.writeFileSync( tmpfile, JSON.stringify( config ), { encoding: 'utf8' } ); |
||||
process.env.PELIAS_CONFIG = tmpfile; |
||||
var adapter = setup(); |
||||
delete process.env.PELIAS_CONFIG; |
||||
|
||||
t.equal(adapter.constructor.name, 'NullTransport', 'NullTransport'); |
||||
t.equal(typeof adapter, 'object', 'adapter is an object'); |
||||
t.equal(typeof adapter.query, 'function', 'query is a function'); |
||||
t.equal(adapter.query.length, 2, 'query function signature'); |
||||
t.end(); |
||||
}); |
||||
|
||||
test('default adapter', function(t) { |
||||
var config = {}; |
||||
|
||||
// adapter is driven by config
|
||||
var tmpfile = tmp.tmpNameSync({ postfix: '.json' }); |
||||
fs.writeFileSync( tmpfile, JSON.stringify( config ), { encoding: 'utf8' } ); |
||||
process.env.PELIAS_CONFIG = tmpfile; |
||||
var adapter = setup(); |
||||
delete process.env.PELIAS_CONFIG; |
||||
|
||||
t.equal(adapter.constructor.name, 'NullTransport', 'NullTransport'); |
||||
t.equal(typeof adapter, 'object', 'adapter is an object'); |
||||
t.equal(typeof adapter.query, 'function', 'query is a function'); |
||||
t.equal(adapter.query.length, 2, 'query function signature'); |
||||
t.end(); |
||||
}); |
||||
|
||||
}; |
||||
|
||||
// null transport
|
||||
module.exports.tests.NullTransport = function(test, common) { |
||||
|
||||
test('null transport', function(t) { |
||||
|
||||
// adapter is driven by config
|
||||
var tmpfile = tmp.tmpNameSync({ postfix: '.json' }); |
||||
fs.writeFileSync( tmpfile, '{}', { encoding: 'utf8' } ); |
||||
process.env.PELIAS_CONFIG = tmpfile; |
||||
var adapter = setup(); |
||||
delete process.env.PELIAS_CONFIG; |
||||
|
||||
// test null transport performs a no-op
|
||||
adapter.query( null, function( err, res ){ |
||||
t.equal(err, undefined, 'no-op'); |
||||
t.equal(res, undefined, 'no-op'); |
||||
t.end(); |
||||
}); |
||||
}); |
||||
|
||||
}; |
||||
|
||||
module.exports.all = function (tape, common) { |
||||
|
||||
function test(name, testFunction) { |
||||
return tape('SERVICE language', testFunction); |
||||
} |
||||
|
||||
for( var testCase in module.exports.tests ){ |
||||
module.exports.tests[testCase](test, common); |
||||
} |
||||
}; |
Loading…
Reference in new issue