]> git.openstreetmap.org Git - nominatim.git/blobdiff - test/bdd/steps/http_responses.py
bdd: complete coverage for API tests
[nominatim.git] / test / bdd / steps / http_responses.py
index c3cd4f5280d802c78770fc273fd0d76d213673cd..beafcd9e1ee16773294f8279d8dd26ce080a3d6e 100644 (file)
@@ -40,13 +40,17 @@ class GenericResponse:
     """ Common base class for all API responses.
     """
     def __init__(self, page, fmt, errorcode=200):
+        fmt = fmt.strip()
+        if fmt == 'jsonv2':
+            fmt = 'json'
+
         self.page = page
         self.format = fmt
         self.errorcode = errorcode
         self.result = []
         self.header = dict()
 
-        if errorcode == 200:
+        if errorcode == 200 and fmt != 'debug':
             getattr(self, '_parse_' + fmt)()
 
     def _parse_json(self):
@@ -91,6 +95,29 @@ class GenericResponse:
             assert str(self.result[idx][field]) == str(value), \
                    BadRowValueAssert(self, idx, field, value)
 
+    def assert_address_field(self, idx, field, value):
+        """ Check that result rows`idx` has a field `field` with value `value`
+            in its address. If idx is None, then all results are checked.
+        """
+        if idx is None:
+            todo = range(len(self.result))
+        else:
+            todo = [int(idx)]
+
+        for idx in todo:
+            assert 'address' in self.result[idx], \
+                   "Result row {} has no field 'address'.\nFull row: {}"\
+                       .format(idx, json.dumps(self.result[idx], indent=4))
+
+            address = self.result[idx]['address']
+            assert field in address, \
+                   "Result row {} has no field '{}' in address.\nFull address: {}"\
+                       .format(idx, field, json.dumps(address, indent=4))
+
+            assert address[field] == value, \
+                   "\nBad value for row {} field '{}' in address. Expected: {}, got: {}.\nFull address: {}"""\
+                       .format(idx, field, value, address[field], json.dumps(address, indent=4))
+
     def match_row(self, row):
         """ Match the result fields against the given behave table row.
         """