mirror of https://github.com/pelias/api.git
Stephen K Hess
8 years ago
committed by
GitHub
6 changed files with 157 additions and 1 deletions
@ -0,0 +1,57 @@
|
||||
/** |
||||
* |
||||
* Accuracy level should be set for each item in the results. |
||||
* The level can be any of the following: |
||||
* - point |
||||
* - interpolated |
||||
* - centroid |
||||
*/ |
||||
|
||||
var check = require('check-types'); |
||||
|
||||
var accuracyLevelPoint = 'point'; |
||||
var accuracyLevelInterpolated = 'interpolated'; |
||||
var accuracyLevelCentroid = 'centroid'; |
||||
|
||||
|
||||
function setup() { |
||||
return computeAccuracy; |
||||
} |
||||
|
||||
function computeAccuracy(req, res, next) { |
||||
// do nothing if no result data set
|
||||
if (check.undefined(res) || check.undefined(res.data)) { |
||||
return next(); |
||||
} |
||||
|
||||
// loop through data items and determine accuracy levels
|
||||
res.data = res.data.map(computeAccuracyLevelForResult); |
||||
|
||||
next(); |
||||
} |
||||
|
||||
/** |
||||
* Determine accuracy level based on the type of result being returned. |
||||
* |
||||
* @param {object} hit |
||||
* @returns {object} |
||||
*/ |
||||
function computeAccuracyLevelForResult(hit) { |
||||
|
||||
// TODO: add a check for interpolated addresses when that feature lands
|
||||
|
||||
switch (hit.layer) { |
||||
case 'venue': |
||||
case 'address': |
||||
hit.accuracy = accuracyLevelPoint; |
||||
break; |
||||
// this means it's a street or admin area
|
||||
default: |
||||
hit.accuracy = accuracyLevelCentroid; |
||||
break; |
||||
} |
||||
|
||||
return hit; |
||||
} |
||||
|
||||
module.exports = setup; |
@ -0,0 +1,90 @@
|
||||
var accuracy = require('../../../middleware/accuracy')(); |
||||
|
||||
module.exports.tests = {}; |
||||
|
||||
module.exports.tests.accuracy = function(test, common) { |
||||
|
||||
test('empty res and req should not throw exception', function(t) { |
||||
function testIt() { |
||||
accuracy({}, {}, function() {}); |
||||
} |
||||
|
||||
t.doesNotThrow(testIt, 'an exception should not have been thrown'); |
||||
t.end(); |
||||
}); |
||||
|
||||
test('res.results without parsed_text should not throw exception', function(t) { |
||||
var res = { |
||||
data: [{ |
||||
layer: 'venue' |
||||
}] |
||||
}; |
||||
|
||||
accuracy({}, res, function() { |
||||
t.equal(res.data[0].accuracy, 'point', 'accuracy was set'); |
||||
t.end(); |
||||
}); |
||||
}); |
||||
|
||||
test('venue should have accuracy set to point', function(t) { |
||||
var res = { |
||||
data: [{ |
||||
layer: 'venue' |
||||
}] |
||||
}; |
||||
|
||||
accuracy({}, res, function() { |
||||
t.equal(res.data[0].accuracy, 'point', 'accuracy was set'); |
||||
t.end(); |
||||
}); |
||||
}); |
||||
|
||||
test('address should have accuracy set to point', function(t) { |
||||
var res = { |
||||
data: [{ |
||||
layer: 'address' |
||||
}] |
||||
}; |
||||
|
||||
accuracy({}, res, function() { |
||||
t.equal(res.data[0].accuracy, 'point', 'accuracy was set'); |
||||
t.end(); |
||||
}); |
||||
}); |
||||
|
||||
test('region should have accuracy set to centroid', function(t) { |
||||
var res = { |
||||
data: [{ |
||||
layer: 'region' |
||||
}] |
||||
}; |
||||
|
||||
accuracy({}, res, function() { |
||||
t.equal(res.data[0].accuracy, 'centroid', 'accuracy was set'); |
||||
t.end(); |
||||
}); |
||||
}); |
||||
|
||||
test('street should have accuracy set to centroid', function(t) { |
||||
var res = { |
||||
data: [{ |
||||
layer: 'street' |
||||
}] |
||||
}; |
||||
|
||||
accuracy({}, res, function() { |
||||
t.equal(res.data[0].accuracy, 'centroid', 'accuracy was set'); |
||||
t.end(); |
||||
}); |
||||
}); |
||||
}; |
||||
|
||||
module.exports.all = function (tape, common) { |
||||
function test(name, testFunction) { |
||||
return tape('[middleware] confidenceScore: ' + name, testFunction); |
||||
} |
||||
|
||||
for( var testCase in module.exports.tests ){ |
||||
module.exports.tests[testCase](test, common); |
||||
} |
||||
}; |
Loading…
Reference in new issue