Skip to content

Commit 8eb07a1

Browse files
authored
Merge pull request #214 from pelias/joxit/feat/size
Add support to `size` that check the number of results
2 parents dd51381 + 1aba9fc commit 8eb07a1

File tree

5 files changed

+126
-3
lines changed

5 files changed

+126
-3
lines changed

.jshintrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"node": true,
33
"curly": true,
44
"eqeqeq": true,
5-
"esversion": 6,
5+
"esversion": 11,
66
"freeze": true,
77
"immed": true,
88
"indent": 2,

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,10 @@ properties:
100100

101101
Coordinate based tests also help to track invalid location data in the search database.
102102

103+
`size` property that will check the number of results, you can set a number or a String with a comparison sign
104+
(`<`, `<=`, `>`, `>=` or `==`). If the sign is not recognized, the test will fail. Usage example: `> 5` will ensure
105+
a number or result greater than 5.
106+
103107
+ `unexpected` is analogous to `expected`, except that you *cannot* specify a `priorityThresh` and the `properties`
104108
array does *not* support strings.
105109
+ `weights` (optional) test case specific weighting for scores of the individual expectations. See the

lib/sanitiseTestCase.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
'use strict';
2-
2+
const _ = require('lodash');
33
/**
44
* Given the properties of a test case,
55
* construct the actual expected object.
@@ -57,6 +57,10 @@ function normalizeCoordinates(coordinates) {
5757
return coordinates;
5858
}
5959

60+
function normalizeSize(size) {
61+
return _.isNumber(size) ? size.toString() : size;
62+
}
63+
6064
function sanitiseTestCase(testCase, locations) {
6165
locations = locations || {};
6266

@@ -80,6 +84,9 @@ function sanitiseTestCase(testCase, locations) {
8084
if (unmatched_placeholders.length > 0) {
8185
return 'Placeholder: no matches for ' + unmatched_placeholders.join(', ') + ' in `locations.json`.';
8286
}
87+
if (!_.isNil(testCase.expected.size)) {
88+
testCase.expected.size = normalizeSize(testCase.expected.size);
89+
}
8390
}
8491

8592
return testCase;

lib/scoreTest.js

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ var helpers = require( '../lib/scoreHelpers' );
22
var equalProperties = require( '../lib/equal_properties' );
33
var scoreCoordinates = require( '../lib/scoreCoordinates' );
44
var scoreProperties = require( '../lib/scoreProperties' );
5-
5+
const _ = require('lodash');
66
/**
77
* Calculate the score component for a response feature object's position in the returned
88
* response features. If it is later in the featuress than the threshold, no points.
@@ -127,6 +127,28 @@ function scoreOneCoordinateExpectation(expected_coords, responseFeatures, contex
127127
}).sort(sortScores)[0];
128128
}
129129

130+
function evaluateSymbol(size, length) {
131+
const exec = /^(?<symbol>[<>=]+)?\s*(?<number>[0-9]+)$/.exec(size.trim());
132+
const symbol = exec?.groups?.symbol || '==';
133+
const number = _.parseInt(exec?.groups?.number);
134+
switch(symbol) {
135+
case '<=': return length <= number;
136+
case '<': return length < number;
137+
case '>=': return length >= number;
138+
case '>': return length > number;
139+
case '==': return length === number;
140+
default: return false;
141+
}
142+
}
143+
144+
function scoreSize(size, responseFeatures) {
145+
const scored = evaluateSymbol(size, responseFeatures.length);
146+
if (!scored) {
147+
return {score: 0, max_score: 1, diff: [`size is ${size} but found ${responseFeatures.length}`]};
148+
}
149+
return {score: 1, max_score: 1, diff: []};
150+
}
151+
130152
/**
131153
* Score an entire test by combining the score for each expectation,
132154
* and the score for any unexpected properties.
@@ -148,6 +170,10 @@ function scoreTest(testCase, responseFeatures, context) {
148170
return scoreOneCoordinateExpectation(expected_coords, responseFeatures, context);
149171
}));
150172
}
173+
174+
if (_.isString(testCase.expected.size)) {
175+
scores = scores.concat(scoreSize(testCase.expected.size, responseFeatures));
176+
}
151177
}
152178

153179
if (testCase.unexpected) {

test/eval_test.js

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,92 @@ tape( 'evalTest() evaluates all edge cases correctly', function ( test ){
314314
}
315315
},
316316
expected: 'pass'
317+
},
318+
{
319+
description: 'size should match exactly - pass',
320+
priorityThresh: 1,
321+
apiResults: [{
322+
properties: {}
323+
}],
324+
testCase: {
325+
expected: {
326+
properties: {},
327+
size: 1
328+
}
329+
},
330+
expected: 'pass'
331+
},
332+
{
333+
description: 'size should match exactly - fail',
334+
priorityThresh: 1,
335+
apiResults: [{
336+
properties: {}
337+
}],
338+
testCase: {
339+
expected: {
340+
properties: {},
341+
size: 0
342+
}
343+
},
344+
expected: 'fail'
345+
},
346+
{
347+
description: 'size supports comparison signs, two results - pass',
348+
priorityThresh: 1,
349+
apiResults: [{
350+
properties: {}
351+
},{
352+
properties: {}
353+
}],
354+
testCase: {
355+
expected: {
356+
properties: {},
357+
size: '>=2'
358+
}
359+
},
360+
expected: 'pass'
361+
},
362+
{
363+
description: 'size supports comparison signs - pass',
364+
priorityThresh: 1,
365+
apiResults: [{
366+
properties: {}
367+
}],
368+
testCase: {
369+
expected: {
370+
properties: {},
371+
size: '>=1'
372+
}
373+
},
374+
expected: 'pass'
375+
},
376+
{
377+
description: 'size supports comparison signs - pass',
378+
priorityThresh: 1,
379+
apiResults: [{
380+
properties: {}
381+
}],
382+
testCase: {
383+
expected: {
384+
properties: {},
385+
size: '< 1'
386+
}
387+
},
388+
expected: 'fail'
389+
},
390+
{
391+
description: 'size wrong input - fail',
392+
priorityThresh: 1,
393+
apiResults: [{
394+
properties: {}
395+
}],
396+
testCase: {
397+
expected: {
398+
properties: {},
399+
size: 's 1'
400+
}
401+
},
402+
expected: 'fail'
317403
}
318404
];
319405

0 commit comments

Comments
 (0)