Skip to content

Commit

Permalink
Merge pull request geocoders#7 from amatissart/maxMatches
Browse files Browse the repository at this point in the history
[WIP] Implement "max_matches" param to fail on duplicated results
  • Loading branch information
antoine-de authored Dec 7, 2017
2 parents a6b64c0 + 137cc88 commit 91d1bae
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 6 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,15 @@ of the form `lat,lon,tolerated deviation in meters`, e.g. `51.0,10.3,700`.

Optional columns:
* `limit`: decide how many results you want to look at for finding your result
(defaul: 1)
(default: 1)
* `lat`, `lon`: if you want to add a center for the search
* `comment`: if you want to take control of the ouput of the test in the
command line
* `lang`: language
* `skip`: add a `skip` message if you want a test to be always skipped (feature
not supported yet for example)
* `max_matches`: maximum number of results that should match expected value.
Should be used such as `limit` > `max_matches`. (default: no limit)

### YAML

Expand Down
10 changes: 9 additions & 1 deletion conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,13 @@ def __init__(self, name, parent, **kwargs):
self.comment = kwargs.get('comment')
self.skip = kwargs.get('skip')
self.mark = kwargs.get('mark', [])

self.max_matches = kwargs.get('max_matches')
if self.max_matches:
self.max_matches = int(self.max_matches)
else:
self.max_matches = None

for mark in self.mark:
self.add_marker(mark)

Expand All @@ -143,7 +150,8 @@ def runtest(self):
'query': self.query,
'expected': self.expected,
'lang': self.lang,
'comment': self.comment
'comment': self.comment,
'max_matches': self.max_matches
}
if self.lat and self.lon:
kwargs['center'] = [self.lat, self.lon]
Expand Down
23 changes: 19 additions & 4 deletions geocoder_tester/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,8 @@ def compare_values(get, expected):


def assert_search(query, expected, limit=1,
comment=None, lang=None, center=None):
comment=None, lang=None, center=None,
max_matches=None):
params = {"q": query, "limit": limit}
if lang:
params['lang'] = lang
Expand All @@ -133,7 +134,7 @@ def assert_search(query, expected, limit=1,
results = search(**params)

def assert_expected(expected):
found = False
nb_found = 0
for r in results['features']:
found = True
properties = None
Expand All @@ -156,14 +157,28 @@ def assert_expected(expected):
if int(deviation.meters) <= int(max_deviation):
continue # Continue to other properties
found = False

if found:
break
if not found:
nb_found += 1
if max_matches is None:
break

if nb_found == 0:
raise SearchException(
params=params,
expected=expected,
results=results
)
elif max_matches is not None and nb_found > max_matches:
message = 'Got {} matching results. Expected at most {}.'.format(
nb_found, max_matches
)
raise SearchException(
params=params,
expected=expected,
results=results,
message=message
)

if not isinstance(expected, list):
expected = [expected]
Expand Down

0 comments on commit 91d1bae

Please sign in to comment.