1 # SPDX-License-Identifier: GPL-2.0-only
3 # This file is part of Nominatim. (https://nominatim.org)
5 # Copyright (C) 2022 by the Nominatim developer community.
6 # For a full list of authors see the git log.
8 Collection of assertion functions used for the steps.
13 """ Compares a float value with a certain jitter.
15 def __init__(self, value, offset=0.00001):
19 def __eq__(self, other):
20 return abs(other - self.value) < self.offset
23 """ Comparator for bounding boxes.
25 def __init__(self, bbox_string):
26 self.coord = [float(x) for x in bbox_string.split(',')]
28 def __contains__(self, item):
29 if isinstance(item, str):
30 item = item.split(',')
31 item = list(map(float, item))
34 return self.coord[0] <= item[0] <= self.coord[2] \
35 and self.coord[1] <= item[1] <= self.coord[3]
38 return item[0] >= self.coord[0] and item[1] <= self.coord[1] \
39 and item[2] >= self.coord[2] and item[3] <= self.coord[3]
41 raise ValueError("Not a coordinate or bbox.")
44 return str(self.coord)
48 def check_for_attributes(obj, attrs, presence='present'):
49 """ Check that the object has the given attributes. 'attrs' is a
50 string with a comma-separated list of attributes. If 'presence'
51 is set to 'absent' then the function checks that the attributes do
52 not exist for the object
55 return json.dumps(obj, sort_keys=True, indent=2, ensure_ascii=False)
57 for attr in attrs.split(','):
59 if presence == 'absent':
60 assert attr not in obj, \
61 f"Unexpected attribute {attr}. Full response:\n{_dump_json()}"
64 f"No attribute '{attr}'. Full response:\n{_dump_json()}"