Skip to content

Commit

Permalink
Merge pull request #194 from pelias/improve-distance-error-message
Browse files Browse the repository at this point in the history
feat: Improve distance threshold error message
  • Loading branch information
orangejulius authored May 5, 2020
2 parents 0efc66e + 1d0bc55 commit 5626fbc
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
10 changes: 7 additions & 3 deletions lib/scoreCoordinates.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
var haversine = require( 'haversine' ); // distance measure for angle coords

function failMessage(actual, distance, threshold) {
const name = actual.properties.label || actual.properties.name;

return `'${name}' is not close enough: distance is ${distance}m but should be under ${threshold}m`;
}

/**
* Calculate the score of an api coordinate result against
* a single entry of expected coordinates.
Expand All @@ -18,9 +24,7 @@ function scoreCoordinates(expected, actual, threshold, weight) {
return {
score: pass ? weight : 0,
max_score: weight,
diff: pass ? '' : actual.properties.name +
',' + actual.properties.locality +
' is not close enough, distance=' + dist + ' m'
diff: pass ? '' : failMessage(actual, dist, threshold)
};
}

Expand Down
7 changes: 4 additions & 3 deletions test/scoreTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ tape( 'scoreUnexpected basics', function ( test ){
var result = scoreTest.scoreTest(testCase, features, context);
t.equal(result.score, 2, 'score is 2');
t.equal(result.max_score, 3, 'max score is 3');
t.deepEquals(result.diff,[ 'test,place is not close enough, distance=7140266 m' ],
t.deepEquals(result.diff,[ '\'test\' is not close enough: distance is 7140266m but should be under 1000m' ],
'the diff shows the distance from the expected coordinate');
t.end();
});
Expand Down Expand Up @@ -125,7 +125,8 @@ tape( 'scoreUnexpected basics', function ( test ){
{
properties: {
name: 'test',
locality: 'place'
locality: 'place',
label: 'test, place, country'
},
geometry: {
coordinates: [ 50.0, 50.0]
Expand All @@ -136,7 +137,7 @@ tape( 'scoreUnexpected basics', function ( test ){
var result = scoreTest.scoreTest(testCase, features, context);
t.equal(result.score, 0, 'score is 0');
t.equal(result.max_score, 1, 'max score is 1');
t.deepEquals(result.diff,[ 'test,place is not close enough, distance=7140266 m' ],
t.deepEquals(result.diff,[ '\'test, place, country\' is not close enough: distance is 7140266m but should be under 1000m' ],
'the diff shows the distance from the expected coordinate');
t.end();
});
Expand Down

0 comments on commit 5626fbc

Please sign in to comment.