X-Git-Url: https://git.openstreetmap.org./nominatim.git/blobdiff_plain/8a93f8ed94d04fed67824366b1691aa62b2c8a1d..c4f22a42eba499ed1854c5967b2e3932fe9b3896:/test/bdd/steps/http_responses.py?ds=inline diff --git a/test/bdd/steps/http_responses.py b/test/bdd/steps/http_responses.py index c3cd4f52..fa6ab7fb 100644 --- a/test/bdd/steps/http_responses.py +++ b/test/bdd/steps/http_responses.py @@ -1,3 +1,9 @@ +# SPDX-License-Identifier: GPL-2.0-only +# +# This file is part of Nominatim. (https://nominatim.org) +# +# Copyright (C) 2022 by the Nominatim developer community. +# For a full list of authors see the git log. """ Classes wrapping HTTP responses from the Nominatim API. """ @@ -8,6 +14,10 @@ import xml.etree.ElementTree as ET from check_functions import Almost +OSM_TYPE = {'N' : 'node', 'W' : 'way', 'R' : 'relation', + 'n' : 'node', 'w' : 'way', 'r' : 'relation', + 'node' : 'n', 'way' : 'w', 'relation' : 'r'} + def _geojson_result_to_json_result(geojson_result): result = geojson_result['properties'] result['geojson'] = geojson_result['geometry'] @@ -40,13 +50,17 @@ class GenericResponse: """ Common base class for all API responses. """ def __init__(self, page, fmt, errorcode=200): + fmt = fmt.strip() + if fmt == 'jsonv2': + fmt = 'json' + self.page = page self.format = fmt self.errorcode = errorcode self.result = [] self.header = dict() - if errorcode == 200: + if errorcode == 200 and fmt != 'debug': getattr(self, '_parse_' + fmt)() def _parse_json(self): @@ -91,6 +105,29 @@ class GenericResponse: assert str(self.result[idx][field]) == str(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): """ Match the result fields against the given behave table row. """ @@ -104,8 +141,15 @@ class GenericResponse: if name == 'ID': pass elif name == 'osm': - self.assert_field(i, 'osm_type', value[0]) + assert 'osm_type' in self.result[i], \ + "Result row {} has no field 'osm_type'.\nFull row: {}"\ + .format(i, json.dumps(self.result[i], indent=4)) + assert self.result[i]['osm_type'] in (OSM_TYPE[value[0]], value[0]), \ + BadRowValueAssert(self, i, 'osm_type', value) self.assert_field(i, 'osm_id', value[1:]) + elif name == 'osm_type': + assert self.result[i]['osm_type'] in (OSM_TYPE[value[0]], value[0]), \ + BadRowValueAssert(self, i, 'osm_type', value) elif name == 'centroid': lon, lat = value.split(' ') self.assert_field(i, 'lat', float(lat))