mirror of
https://github.com/osm-search/Nominatim.git
synced 2026-02-26 11:08:13 +00:00
bdd: improve assert output for API query checks
Adds wrapper function for checking address parts and more explanation strings to asserts.
This commit is contained in:
@@ -3,10 +3,35 @@ Collection of assertion functions used for the steps.
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
class Almost:
|
class Almost:
|
||||||
|
""" Compares a float value with a certain jitter.
|
||||||
|
"""
|
||||||
def __init__(self, value, offset=0.00001):
|
def __init__(self, value, offset=0.00001):
|
||||||
self.value = value
|
self.value = value
|
||||||
self.offset = offset
|
self.offset = offset
|
||||||
|
|
||||||
def __eq__(self, other):
|
def __eq__(self, other):
|
||||||
return abs(other - self.value) < self.offset
|
return abs(other - self.value) < self.offset
|
||||||
|
|
||||||
|
class Bbox:
|
||||||
|
""" Comparator for bounding boxes.
|
||||||
|
"""
|
||||||
|
def __init__(self, bbox_string):
|
||||||
|
self.coord = [float(x) for x in bbox_string.split(',')]
|
||||||
|
|
||||||
|
def __contains__(self, item):
|
||||||
|
if isinstance(item, str):
|
||||||
|
item = item.split(',')
|
||||||
|
item = list(map(float, item))
|
||||||
|
|
||||||
|
if len(item) == 2:
|
||||||
|
return self.coord[0] <= item[0] <= self.coord[2] \
|
||||||
|
and self.coord[1] <= item[1] <= self.coord[3]
|
||||||
|
|
||||||
|
if len(item) == 4:
|
||||||
|
return item[0] >= self.coord[0] and item[1] <= self.coord[1] \
|
||||||
|
and item[2] >= self.coord[2] and item[3] <= self.coord[3]
|
||||||
|
|
||||||
|
raise ValueError("Not a coordinate or bbox.")
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return str(self.coord)
|
||||||
|
|||||||
@@ -95,6 +95,29 @@ class GenericResponse:
|
|||||||
assert str(self.result[idx][field]) == str(value), \
|
assert str(self.result[idx][field]) == str(value), \
|
||||||
BadRowValueAssert(self, idx, field, value)
|
BadRowValueAssert(self, idx, field, value)
|
||||||
|
|
||||||
|
def assert_address_field(self, idx, field, value):
|
||||||
|
""" Check that result rows`idx` has a field `field` with value `value`
|
||||||
|
in its address. If idx is None, then all results are checked.
|
||||||
|
"""
|
||||||
|
if idx is None:
|
||||||
|
todo = range(len(self.result))
|
||||||
|
else:
|
||||||
|
todo = [int(idx)]
|
||||||
|
|
||||||
|
for idx in todo:
|
||||||
|
assert 'address' in self.result[idx], \
|
||||||
|
"Result row {} has no field 'address'.\nFull row: {}"\
|
||||||
|
.format(idx, json.dumps(self.result[idx], indent=4))
|
||||||
|
|
||||||
|
address = self.result[idx]['address']
|
||||||
|
assert field in address, \
|
||||||
|
"Result row {} has no field '{}' in address.\nFull address: {}"\
|
||||||
|
.format(idx, field, json.dumps(address, indent=4))
|
||||||
|
|
||||||
|
assert address[field] == value, \
|
||||||
|
"\nBad value for row {} field '{}' in address. Expected: {}, got: {}.\nFull address: {}"""\
|
||||||
|
.format(idx, field, value, address[field], json.dumps(address, indent=4))
|
||||||
|
|
||||||
def match_row(self, row):
|
def match_row(self, row):
|
||||||
""" Match the result fields against the given behave table row.
|
""" Match the result fields against the given behave table row.
|
||||||
"""
|
"""
|
||||||
|
|||||||
@@ -167,8 +167,11 @@ class NominatimEnvironment:
|
|||||||
self.test_env['NOMINATIM_WIKIPEDIA_DATA_PATH'] = str(testdata.resolve())
|
self.test_env['NOMINATIM_WIKIPEDIA_DATA_PATH'] = str(testdata.resolve())
|
||||||
|
|
||||||
try:
|
try:
|
||||||
self.run_setup_script('all', 'import-tiger-data',
|
self.run_setup_script('all', osm_file=self.api_test_file)
|
||||||
osm_file=self.api_test_file)
|
self.run_setup_script('import-tiger-data')
|
||||||
|
|
||||||
|
phrase_file = str((testdata / 'specialphrases_testdb.sql').resolve())
|
||||||
|
run_script(['psql', '-d', self.api_test_db, '-f', phrase_file])
|
||||||
except:
|
except:
|
||||||
self.db_drop_database(self.api_test_db)
|
self.db_drop_database(self.api_test_db)
|
||||||
raise
|
raise
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ from urllib.parse import urlencode
|
|||||||
|
|
||||||
from utils import run_script
|
from utils import run_script
|
||||||
from http_responses import GenericResponse, SearchResponse, ReverseResponse, StatusResponse
|
from http_responses import GenericResponse, SearchResponse, ReverseResponse, StatusResponse
|
||||||
|
from check_functions import Bbox
|
||||||
|
|
||||||
LOG = logging.getLogger(__name__)
|
LOG = logging.getLogger(__name__)
|
||||||
|
|
||||||
@@ -193,7 +194,7 @@ def validate_result_number(context, operator, number):
|
|||||||
assert context.response.errorcode == 200
|
assert context.response.errorcode == 200
|
||||||
numres = len(context.response.result)
|
numres = len(context.response.result)
|
||||||
assert compare(operator, numres, int(number)), \
|
assert compare(operator, numres, int(number)), \
|
||||||
"Bad number of results: expected %s %s, got %d." % (operator, number, numres)
|
"Bad number of results: expected {} {}, got {}.".format(operator, number, numres)
|
||||||
|
|
||||||
@then(u'a HTTP (?P<status>\d+) is returned')
|
@then(u'a HTTP (?P<status>\d+) is returned')
|
||||||
def check_http_return_status(context, status):
|
def check_http_return_status(context, status):
|
||||||
@@ -261,18 +262,12 @@ def validate_attributes(context, lid, neg, attrs):
|
|||||||
def step_impl(context):
|
def step_impl(context):
|
||||||
context.execute_steps("then at least 1 result is returned")
|
context.execute_steps("then at least 1 result is returned")
|
||||||
|
|
||||||
if 'ID' not in context.table.headings:
|
|
||||||
addr_parts = context.response.property_list('address')
|
|
||||||
|
|
||||||
for line in context.table:
|
for line in context.table:
|
||||||
if 'ID' in context.table.headings:
|
idx = int(line['ID']) if 'ID' in line.headings else None
|
||||||
addr_parts = [dict(context.response.result[int(line['ID'])]['address'])]
|
|
||||||
|
|
||||||
for h in context.table.headings:
|
for name, value in zip(line.headings, line.cells):
|
||||||
if h != 'ID':
|
if name != 'ID':
|
||||||
for p in addr_parts:
|
context.response.assert_address_field(idx, name, value)
|
||||||
assert h in p
|
|
||||||
assert p[h] == line[h], "Bad address value for %s" % h
|
|
||||||
|
|
||||||
@then(u'address of result (?P<lid>\d+) has(?P<neg> no)? types (?P<attrs>.*)')
|
@then(u'address of result (?P<lid>\d+) has(?P<neg> no)? types (?P<attrs>.*)')
|
||||||
def check_address(context, lid, neg, attrs):
|
def check_address(context, lid, neg, attrs):
|
||||||
@@ -290,12 +285,11 @@ def check_address(context, lid, neg, attrs):
|
|||||||
def check_address(context, lid, complete):
|
def check_address(context, lid, complete):
|
||||||
context.execute_steps("then more than %s results are returned" % lid)
|
context.execute_steps("then more than %s results are returned" % lid)
|
||||||
|
|
||||||
addr_parts = dict(context.response.result[int(lid)]['address'])
|
lid = int(lid)
|
||||||
|
addr_parts = dict(context.response.result[lid]['address'])
|
||||||
|
|
||||||
for line in context.table:
|
for line in context.table:
|
||||||
assert line['type'] in addr_parts
|
context.response.assert_address_field(lid, line['type'], line['value'])
|
||||||
assert addr_parts[line['type']] == line['value'], \
|
|
||||||
"Bad address value for %s" % line['type']
|
|
||||||
del addr_parts[line['type']]
|
del addr_parts[line['type']]
|
||||||
|
|
||||||
if complete == 'is':
|
if complete == 'is':
|
||||||
@@ -307,41 +301,30 @@ def step_impl(context, lid, coords):
|
|||||||
context.execute_steps("then at least 1 result is returned")
|
context.execute_steps("then at least 1 result is returned")
|
||||||
bboxes = context.response.property_list('boundingbox')
|
bboxes = context.response.property_list('boundingbox')
|
||||||
else:
|
else:
|
||||||
context.execute_steps("then more than %sresults are returned" % lid)
|
context.execute_steps("then more than {}results are returned".format(lid))
|
||||||
bboxes = [ context.response.result[int(lid)]['boundingbox']]
|
bboxes = [context.response.result[int(lid)]['boundingbox']]
|
||||||
|
|
||||||
coord = [ float(x) for x in coords.split(',') ]
|
expected = Bbox(coords)
|
||||||
|
|
||||||
for bbox in bboxes:
|
for bbox in bboxes:
|
||||||
if isinstance(bbox, str):
|
assert bbox in expected, "Bbox {} is not contained in {}.".format(bbox, expected)
|
||||||
bbox = bbox.split(',')
|
|
||||||
bbox = [ float(x) for x in bbox ]
|
|
||||||
|
|
||||||
assert bbox[0] >= coord[0]
|
|
||||||
assert bbox[1] <= coord[1]
|
|
||||||
assert bbox[2] >= coord[2]
|
|
||||||
assert bbox[3] <= coord[3]
|
|
||||||
|
|
||||||
@then(u'result (?P<lid>\d+ )?has centroid in (?P<coords>[\d,.-]+)')
|
@then(u'result (?P<lid>\d+ )?has centroid in (?P<coords>[\d,.-]+)')
|
||||||
def step_impl(context, lid, coords):
|
def step_impl(context, lid, coords):
|
||||||
if lid is None:
|
if lid is None:
|
||||||
context.execute_steps("then at least 1 result is returned")
|
context.execute_steps("then at least 1 result is returned")
|
||||||
bboxes = zip(context.response.property_list('lat'),
|
centroids = zip(context.response.property_list('lon'),
|
||||||
context.response.property_list('lon'))
|
context.response.property_list('lat'))
|
||||||
else:
|
else:
|
||||||
context.execute_steps("then more than %sresults are returned" % lid)
|
context.execute_steps("then more than %sresults are returned".format(lid))
|
||||||
res = context.response.result[int(lid)]
|
res = context.response.result[int(lid)]
|
||||||
bboxes = [ (res['lat'], res['lon']) ]
|
centroids = [(res['lon'], res['lat'])]
|
||||||
|
|
||||||
coord = [ float(x) for x in coords.split(',') ]
|
expected = Bbox(coords)
|
||||||
|
|
||||||
for lat, lon in bboxes:
|
for centroid in centroids:
|
||||||
lat = float(lat)
|
assert centroid in expected,\
|
||||||
lon = float(lon)
|
"Centroid {} is not inside {}.".format(centroid, expected)
|
||||||
assert lat >= coord[0]
|
|
||||||
assert lat <= coord[1]
|
|
||||||
assert lon >= coord[2]
|
|
||||||
assert lon <= coord[3]
|
|
||||||
|
|
||||||
@then(u'there are(?P<neg> no)? duplicates')
|
@then(u'there are(?P<neg> no)? duplicates')
|
||||||
def check_for_duplicates(context, neg):
|
def check_for_duplicates(context, neg):
|
||||||
|
|||||||
Reference in New Issue
Block a user