]> git.openstreetmap.org Git - nominatim.git/blob - test/bdd/steps/check_functions.py
Merge remote-tracking branch 'upstream/master'
[nominatim.git] / test / bdd / steps / check_functions.py
1 # SPDX-License-Identifier: GPL-2.0-only
2 #
3 # This file is part of Nominatim. (https://nominatim.org)
4 #
5 # Copyright (C) 2025 by the Nominatim developer community.
6 # For a full list of authors see the git log.
7 """
8 Collection of assertion functions used for the steps.
9 """
10 import json
11 import math
12 import re
13
14
15 OSM_TYPE = {'N': 'node', 'W': 'way', 'R': 'relation',
16             'n': 'node', 'w': 'way', 'r': 'relation',
17             'node': 'n', 'way': 'w', 'relation': 'r'}
18
19
20 class OsmType:
21     """ Compares an OSM type, accepting both N/R/W and node/way/relation.
22     """
23
24     def __init__(self, value):
25         self.value = value
26
27     def __eq__(self, other):
28         return other == self.value or other == OSM_TYPE[self.value]
29
30     def __str__(self):
31         return f"{self.value} or {OSM_TYPE[self.value]}"
32
33
34 class Field:
35     """ Generic comparator for fields, which looks at the type of the
36         value compared.
37     """
38     def __init__(self, value, **extra_args):
39         self.value = value
40         self.extra_args = extra_args
41
42     def __eq__(self, other):
43         if isinstance(self.value, float):
44             return math.isclose(self.value, float(other), **self.extra_args)
45
46         if self.value.startswith('^'):
47             return re.fullmatch(self.value, str(other))
48
49         if isinstance(other, dict):
50             return other == eval('{' + self.value + '}')
51
52         return str(self.value) == str(other)
53
54     def __str__(self):
55         return str(self.value)
56
57
58 class Bbox:
59     """ Comparator for bounding boxes.
60     """
61     def __init__(self, bbox_string):
62         self.coord = [float(x) for x in bbox_string.split(',')]
63
64     def __contains__(self, item):
65         if isinstance(item, str):
66             item = item.split(',')
67         item = list(map(float, item))
68
69         if len(item) == 2:
70             return self.coord[0] <= item[0] <= self.coord[2] \
71                    and self.coord[1] <= item[1] <= self.coord[3]
72
73         if len(item) == 4:
74             return item[0] >= self.coord[0] and item[1] <= self.coord[1] \
75                    and item[2] >= self.coord[2] and item[3] <= self.coord[3]
76
77         raise ValueError("Not a coordinate or bbox.")
78
79     def __str__(self):
80         return str(self.coord)
81
82
83 def check_for_attributes(obj, attrs, presence='present'):
84     """ Check that the object has the given attributes. 'attrs' is a
85         string with a comma-separated list of attributes. If 'presence'
86         is set to 'absent' then the function checks that the attributes do
87         not exist for the object
88     """
89     def _dump_json():
90         return json.dumps(obj, sort_keys=True, indent=2, ensure_ascii=False)
91
92     for attr in attrs.split(','):
93         attr = attr.strip()
94         if presence == 'absent':
95             assert attr not in obj, \
96                    f"Unexpected attribute {attr}. Full response:\n{_dump_json()}"
97         else:
98             assert attr in obj, \
99                    f"No attribute '{attr}'. Full response:\n{_dump_json()}"