1 # SPDX-License-Identifier: GPL-2.0-only
3 # This file is part of Nominatim. (https://nominatim.org)
5 # Copyright (C) 2023 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.
15 """ Compares a float value with a certain jitter.
17 def __init__(self, value, offset=0.00001):
21 def __eq__(self, other):
22 return abs(other - self.value) < self.offset
25 OSM_TYPE = {'N' : 'node', 'W' : 'way', 'R' : 'relation',
26 'n' : 'node', 'w' : 'way', 'r' : 'relation',
27 'node' : 'n', 'way' : 'w', 'relation' : 'r'}
31 """ Compares an OSM type, accepting both N/R/W and node/way/relation.
34 def __init__(self, value):
38 def __eq__(self, other):
39 return other == self.value or other == OSM_TYPE[self.value]
43 return f"{self.value} or {OSM_TYPE[self.value]}"
47 """ Generic comparator for fields, which looks at the type of the
50 def __init__(self, value, **extra_args):
52 self.extra_args = extra_args
54 def __eq__(self, other):
55 if isinstance(self.value, float):
56 return math.isclose(self.value, float(other), **self.extra_args)
58 if self.value.startswith('^'):
59 return re.fullmatch(self.value, str(other))
61 if isinstance(other, dict):
62 return other == eval('{' + self.value + '}')
64 return str(self.value) == str(other)
67 return str(self.value)
71 """ Comparator for bounding boxes.
73 def __init__(self, bbox_string):
74 self.coord = [float(x) for x in bbox_string.split(',')]
76 def __contains__(self, item):
77 if isinstance(item, str):
78 item = item.split(',')
79 item = list(map(float, item))
82 return self.coord[0] <= item[0] <= self.coord[2] \
83 and self.coord[1] <= item[1] <= self.coord[3]
86 return item[0] >= self.coord[0] and item[1] <= self.coord[1] \
87 and item[2] >= self.coord[2] and item[3] <= self.coord[3]
89 raise ValueError("Not a coordinate or bbox.")
92 return str(self.coord)
96 def check_for_attributes(obj, attrs, presence='present'):
97 """ Check that the object has the given attributes. 'attrs' is a
98 string with a comma-separated list of attributes. If 'presence'
99 is set to 'absent' then the function checks that the attributes do
100 not exist for the object
103 return json.dumps(obj, sort_keys=True, indent=2, ensure_ascii=False)
105 for attr in attrs.split(','):
107 if presence == 'absent':
108 assert attr not in obj, \
109 f"Unexpected attribute {attr}. Full response:\n{_dump_json()}"
111 assert attr in obj, \
112 f"No attribute '{attr}'. Full response:\n{_dump_json()}"