Browse Source

service: interpolation: tests

pull/769/head
Peter Johnson 8 years ago committed by missinglink
parent
commit
69be6e062a
  1. 22
      middleware/interpolate.js
  2. 200
      test/unit/middleware/interpolate.js
  3. 1
      test/unit/run.js

22
middleware/interpolate.js

@ -1,7 +1,7 @@
var async = require('async');
var service = require('../service/interpolation')();
var logger = require( 'pelias-logger' ).get( 'api' );
var service = require('../service/interpolation');
/**
example response from interpolation web service:
@ -22,7 +22,9 @@ example response from interpolation web service:
**/
function setup() {
return function middleware(req, res, next) {
var transport = service.search();
var middleware = function(req, res, next) {
// no-op, user did not request an address
if( !isAddressQuery( req ) ){
@ -30,7 +32,7 @@ function setup() {
}
// bind parsed_text variables to function call
var bound = interpolate.bind( null, req.clean.parsed_text );
var bound = interpolate.bind( transport, req.clean.parsed_text );
// perform interpolations asynchronously for all relevant hits
var timer = (new Date()).getTime();
@ -46,6 +48,9 @@ function setup() {
next();
});
};
middleware.transport = transport;
return middleware;
}
function interpolate( parsed_text, hit, cb ){
@ -62,7 +67,7 @@ function interpolate( parsed_text, hit, cb ){
var street = hit.address_parts.street || parsed_text.street;
// query interpolation service
service.query( coord, number, street, function( err, data ){
this.query( coord, number, street, function( err, data ){
// an error occurred
// note: leave this hit unmodified
@ -94,12 +99,15 @@ function interpolate( parsed_text, hit, cb ){
// -- source --
var source = 'mixed';
if( data.properties.source === 'osm' ){ source = 'openstreetmap'; }
else if( data.properties.source === 'oa' ){ source = 'openaddresses'; }
if( data.properties.source === 'OSM' ){ source = 'openstreetmap'; }
else if( data.properties.source === 'OA' ){ source = 'openaddresses'; }
hit.source = source;
// -- source_id --
hit.source_id = 'derived:'+ hit.source_id;
// note: interpolated values have no source_id
if( hit.hasOwnProperty( 'source_id' ) ){
hit.source_id = hit.source_id;
}
// -- address_parts --
hit.address_parts.number = data.properties.number;

200
test/unit/middleware/interpolate.js

@ -0,0 +1,200 @@
var fs = require('fs'),
tmp = require('tmp'),
setup = require('../../../middleware/interpolate');
// load middleware using the default pelias config
var load = function(){
// adapter is driven by config
var tmpfile = tmp.tmpNameSync({ postfix: '.json' });
fs.writeFileSync( tmpfile, '{}', { encoding: 'utf8' } );
process.env.PELIAS_CONFIG = tmpfile;
var middleware = setup();
delete process.env.PELIAS_CONFIG;
return middleware;
};
module.exports.tests = {};
module.exports.tests.interface = function(test, common) {
test('valid interface', function(t) {
var middleware = load();
t.equal(typeof middleware, 'function', 'middleware is a function');
t.equal(middleware.length, 3, 'middleware is a function');
t.end();
});
};
module.exports.tests.isAddressQuery = function(test, common) {
test('invalid address query - no parsed text', function(t) {
var req = { clean: {} };
var middleware = load();
middleware(req, null, t.end);
});
test('invalid address query - no number', function(t) {
var req = { clean: {
parsed_text: {
street: 'sesame st'
}}
};
var middleware = load();
middleware(req, null, t.end);
});
test('invalid address query - no street', function(t) {
var req = { clean: {
parsed_text: {
number: '1',
}}
};
var middleware = load();
middleware(req, null, t.end);
});
};
// test results are correctly mapped to the transport
module.exports.tests.map = function(test, common) {
test('documents mapped to transport: no hits', function(t) {
var req = { clean: {
parsed_text: {
number: '1',
street: 'sesame st'
}}
};
var res = { data: [] };
var middleware = load();
middleware(req, res, function(){
t.deepEqual( res, { data: [] } );
t.end();
});
});
test('documents mapped to transport: no street layer hits', function(t) {
var req = { clean: {
parsed_text: {
number: '1',
street: 'sesame st'
}}
};
var res = { data: [{ layer: 'foo' }] };
var middleware = load();
middleware(req, res, function(){
t.deepEqual( res, { data: [{ layer: 'foo' }] } );
t.end();
});
});
};
// check the service is called and response mapped correctly
module.exports.tests.miss = function(test, common) {
test('miss', 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: 'example' }
}
]};
var middleware = load();
// mock out the transport
middleware.transport.query = function mock( coord, number, street, cb ){
t.deepEqual( coord, res.data[0].center_point );
t.deepEqual( number, req.clean.parsed_text.number );
t.deepEqual( street, res.data[0].address_parts.street );
t.equal( typeof cb, 'function' );
cb( 'error' );
};
middleware(req, res, function(){
t.deepEqual( res, { data: [
{
layer: 'street',
center_point: { lat: 1, lon: 1 },
address_parts: { street: 'sesame rd' },
name: { default: 'example' }
}
]});
t.end();
});
});
};
// 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' }
}
]};
var middleware = load();
// mock out the transport
middleware.transport.query = function mock( coord, number, street, cb ){
t.deepEqual( coord, res.data[0].center_point );
t.deepEqual( number, req.clean.parsed_text.number );
t.deepEqual( street, res.data[0].address_parts.street );
t.equal( typeof cb, 'function' );
cb( null, {
properties: {
number: '100A',
source: 'OSM',
source_id: 'way:111111',
lat: 22.2,
lon: -33.3,
}
});
};
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'
}
]});
t.end();
});
});
};
module.exports.all = function (tape, common) {
function test(name, testFunction) {
return tape('[middleware] interpolate: ' + name, testFunction);
}
for( var testCase in module.exports.tests ){
module.exports.tests[testCase](test, common);
}
};

1
test/unit/run.js

@ -25,6 +25,7 @@ var tests = [
require('./middleware/confidenceScoreFallback'),
require('./middleware/confidenceScoreReverse'),
require('./middleware/distance'),
require('./middleware/interpolate'),
require('./middleware/localNamingConventions'),
require('./middleware/dedupe'),
require('./middleware/parseBBox'),

Loading…
Cancel
Save