forked from hans/Nominatim
bdd: reorganise field comparisons
Move comparision on Field values from assert_field() into a comparator class. Replace BadRowValueAssert with a simpler check_row() function.
This commit is contained in:
@@ -2,12 +2,14 @@
|
||||
#
|
||||
# This file is part of Nominatim. (https://nominatim.org)
|
||||
#
|
||||
# Copyright (C) 2022 by the Nominatim developer community.
|
||||
# Copyright (C) 2023 by the Nominatim developer community.
|
||||
# For a full list of authors see the git log.
|
||||
"""
|
||||
Collection of assertion functions used for the steps.
|
||||
"""
|
||||
import json
|
||||
import math
|
||||
import re
|
||||
|
||||
class Almost:
|
||||
""" Compares a float value with a certain jitter.
|
||||
@@ -19,6 +21,51 @@ class Almost:
|
||||
def __eq__(self, other):
|
||||
return abs(other - self.value) < self.offset
|
||||
|
||||
|
||||
OSM_TYPE = {'N' : 'node', 'W' : 'way', 'R' : 'relation',
|
||||
'n' : 'node', 'w' : 'way', 'r' : 'relation',
|
||||
'node' : 'n', 'way' : 'w', 'relation' : 'r'}
|
||||
|
||||
|
||||
class OsmType:
|
||||
""" Compares an OSM type, accepting both N/R/W and node/way/relation.
|
||||
"""
|
||||
|
||||
def __init__(self, value):
|
||||
self.value = value
|
||||
|
||||
|
||||
def __eq__(self, other):
|
||||
return other == self.value or other == OSM_TYPE[self.value]
|
||||
|
||||
|
||||
def __str__(self):
|
||||
return f"{self.value} or {OSM_TYPE[self.value]}"
|
||||
|
||||
|
||||
class Field:
|
||||
""" Generic comparator for fields, which looks at the type of the
|
||||
value compared.
|
||||
"""
|
||||
def __init__(self, value):
|
||||
self.value = value
|
||||
|
||||
def __eq__(self, other):
|
||||
if isinstance(self.value, float):
|
||||
return math.isclose(self.value, float(other))
|
||||
|
||||
if self.value.startswith('^'):
|
||||
return re.fullmatch(self.value, other)
|
||||
|
||||
if isinstance(other, dict):
|
||||
return other == eval('{' + self.value + '}')
|
||||
|
||||
return str(self.value) == str(other)
|
||||
|
||||
def __str__(self):
|
||||
return str(self.value)
|
||||
|
||||
|
||||
class Bbox:
|
||||
""" Comparator for bounding boxes.
|
||||
"""
|
||||
|
||||
Reference in New Issue
Block a user