]> git.openstreetmap.org Git - nominatim.git/blob - test/bdd/steps/check_functions.py
58d6c1f2a481ddf61453df633d90e725ef457036
[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) 2023 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 class Almost:
15     """ Compares a float value with a certain jitter.
16     """
17     def __init__(self, value, offset=0.00001):
18         self.value = value
19         self.offset = offset
20
21     def __eq__(self, other):
22         return abs(other - self.value) < self.offset
23
24
25 OSM_TYPE = {'N' : 'node', 'W' : 'way', 'R' : 'relation',
26             'n' : 'node', 'w' : 'way', 'r' : 'relation',
27             'node' : 'n', 'way' : 'w', 'relation' : 'r'}
28
29
30 class OsmType:
31     """ Compares an OSM type, accepting both N/R/W and node/way/relation.
32     """
33
34     def __init__(self, value):
35         self.value = value
36
37
38     def __eq__(self, other):
39         return other == self.value or other == OSM_TYPE[self.value]
40
41
42     def __str__(self):
43         return f"{self.value} or {OSM_TYPE[self.value]}"
44
45
46 class Field:
47     """ Generic comparator for fields, which looks at the type of the
48         value compared.
49     """
50     def __init__(self, value):
51         self.value = value
52
53     def __eq__(self, other):
54         if isinstance(self.value, float):
55             return math.isclose(self.value, float(other))
56
57         if self.value.startswith('^'):
58             return re.fullmatch(self.value, other)
59
60         if isinstance(other, dict):
61             return other == eval('{' + self.value + '}')
62
63         return str(self.value) == str(other)
64
65     def __str__(self):
66         return str(self.value)
67
68
69 class Bbox:
70     """ Comparator for bounding boxes.
71     """
72     def __init__(self, bbox_string):
73         self.coord = [float(x) for x in bbox_string.split(',')]
74
75     def __contains__(self, item):
76         if isinstance(item, str):
77             item = item.split(',')
78         item = list(map(float, item))
79
80         if len(item) == 2:
81             return self.coord[0] <= item[0] <= self.coord[2] \
82                    and self.coord[1] <= item[1] <= self.coord[3]
83
84         if len(item) == 4:
85             return item[0] >= self.coord[0] and item[1] <= self.coord[1] \
86                    and item[2] >= self.coord[2] and item[3] <= self.coord[3]
87
88         raise ValueError("Not a coordinate or bbox.")
89
90     def __str__(self):
91         return str(self.coord)
92
93
94
95 def check_for_attributes(obj, attrs, presence='present'):
96     """ Check that the object has the given attributes. 'attrs' is a
97         string with a comma-separated list of attributes. If 'presence'
98         is set to 'absent' then the function checks that the attributes do
99         not exist for the object
100     """
101     def _dump_json():
102         return json.dumps(obj, sort_keys=True, indent=2, ensure_ascii=False)
103
104     for attr in attrs.split(','):
105         attr = attr.strip()
106         if presence == 'absent':
107             assert attr not in obj, \
108                    f"Unexpected attribute {attr}. Full response:\n{_dump_json()}"
109         else:
110             assert attr in obj, \
111                    f"No attribute '{attr}'. Full response:\n{_dump_json()}"
112