From e42c1c9c7af33c1981d750bbec36b0baa36ab170 Mon Sep 17 00:00:00 2001 From: Sarah Hoffmann Date: Thu, 9 Mar 2023 19:31:21 +0100 Subject: [PATCH] bdd: new step variant 'result contains in field' This replaces the + notation for recursing into result dictionaries. --- test/bdd/api/details/params.feature | 12 ++++----- test/bdd/steps/http_responses.py | 41 ++++++++++------------------- test/bdd/steps/steps_api_queries.py | 6 ++--- 3 files changed, 23 insertions(+), 36 deletions(-) diff --git a/test/bdd/api/details/params.feature b/test/bdd/api/details/params.feature index 3bb5bf7c..3d5635de 100644 --- a/test/bdd/api/details/params.feature +++ b/test/bdd/api/details/params.feature @@ -7,9 +7,9 @@ Feature: Object details Then the result is valid json And result has attributes geometry And result has not attributes keywords,address,linked_places,parentof - And results contain - | geometry+type | - | Point | + And results contain in field geometry + | type | + | Point | Scenario: JSON Details with pretty printing When sending json details query for W297699560 @@ -75,9 +75,9 @@ Feature: Object details | 1 | Then the result is valid json And result has attributes geometry - And results contain - | geometry+type | - | | + And results contain in field geometry + | type | + | | Examples: | osmid | geometry | diff --git a/test/bdd/steps/http_responses.py b/test/bdd/steps/http_responses.py index ccf247a9..ff787546 100644 --- a/test/bdd/steps/http_responses.py +++ b/test/bdd/steps/http_responses.py @@ -87,25 +87,6 @@ class GenericResponse: r |= r.pop('geocoding') - def assert_subfield(self, idx, path, value): - assert path - - field = self.result[idx] - for p in path: - assert isinstance(field, dict) - assert p in field - field = field[p] - - if isinstance(value, float): - assert Almost(value) == float(field) - elif value.startswith("^"): - assert re.fullmatch(value, field) - elif isinstance(field, dict): - assert field, eval('{' + value + '}') - else: - assert str(field) == str(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. @@ -122,7 +103,7 @@ class GenericResponse: self.check_row_field(idx, field, value, base=address) - def match_row(self, row, context=None): + def match_row(self, row, context=None, field=None): """ Match the result fields against the given behave table row. """ if 'ID' in row.headings: @@ -131,12 +112,20 @@ class GenericResponse: todo = range(len(self.result)) for i in todo: + subdict = self.result[i] + if field is not None: + for key in field.split('.'): + self.check_row(i, key in subdict, f"Missing subfield {key}") + subdict = subdict[key] + self.check_row(i, isinstance(subdict, dict), + f"Subfield {key} not a dict") + for name, value in zip(row.headings, row.cells): if name == 'ID': pass elif name == 'osm': - self.check_row_field(i, 'osm_type', OsmType(value[0])) - self.check_row_field(i, 'osm_id', Field(value[1:])) + self.check_row_field(i, 'osm_type', OsmType(value[0]), base=subdict) + self.check_row_field(i, 'osm_id', Field(value[1:]), base=subdict) elif name == 'centroid': if ' ' in value: lon, lat = value.split(' ') @@ -144,12 +133,10 @@ class GenericResponse: lon, lat = context.osm.grid_node(int(value)) else: raise RuntimeError("Context needed when using grid coordinates") - self.check_row_field(i, 'lat', Field(float(lat))) - self.check_row_field(i, 'lon', Field(float(lon))) - elif '+' in name: - self.assert_subfield(i, name.split('+'), value) + self.check_row_field(i, 'lat', Field(float(lat)), base=subdict) + self.check_row_field(i, 'lon', Field(float(lon)), base=subdict) else: - self.check_row_field(i, name, Field(value)) + self.check_row_field(i, name, Field(value), base=subdict) def check_row(self, idx, check, msg): diff --git a/test/bdd/steps/steps_api_queries.py b/test/bdd/steps/steps_api_queries.py index b15e296f..7d8b1928 100644 --- a/test/bdd/steps/steps_api_queries.py +++ b/test/bdd/steps/steps_api_queries.py @@ -264,12 +264,12 @@ def check_header_no_attr(context, neg, attrs): 'absent' if neg else 'present') -@then(u'results contain') -def step_impl(context): +@then(u'results contain(?: in field (?P.*))?') +def step_impl(context, field): context.execute_steps("then at least 1 result is returned") for line in context.table: - context.response.match_row(line, context=context) + context.response.match_row(line, context=context, field=field) @then(u'result (?P\d+ )?has (?Pnot )?attributes (?P.*)') -- 2.39.5