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