From 2b7eb4906a40cdc6733f41bb3e03c83e78797e2a Mon Sep 17 00:00:00 2001 From: Sarah Hoffmann Date: Thu, 9 Mar 2023 20:10:51 +0100 Subject: [PATCH] bdd: add tests for valid debug output --- nominatim/api/logging.py | 2 +- test/bdd/api/details/simple.feature | 13 ++++++++++++ test/bdd/api/reverse/simple.feature | 6 ++++-- test/bdd/steps/steps_api_queries.py | 31 +++++++++++++++++++---------- 4 files changed, 39 insertions(+), 13 deletions(-) diff --git a/nominatim/api/logging.py b/nominatim/api/logging.py index e9c88470..3759ba1b 100644 --- a/nominatim/api/logging.py +++ b/nominatim/api/logging.py @@ -96,7 +96,7 @@ class HTMLLogger(BaseLogger): .compile(conn.sync_engine, compile_kwargs={"literal_binds": True})) if CODE_HIGHLIGHT: sqlstr = highlight(sqlstr, PostgresLexer(), - HtmlFormatter(nowrap=True, lineseparator='
')) + HtmlFormatter(nowrap=True, lineseparator='
')) self._write(f'
{sqlstr}
') else: self._write(f'{sqlstr}') diff --git a/test/bdd/api/details/simple.feature b/test/bdd/api/details/simple.feature index 58e5e59e..eed95a73 100644 --- a/test/bdd/api/details/simple.feature +++ b/test/bdd/api/details/simple.feature @@ -103,3 +103,16 @@ Feature: Object details | category | type | admin_level | | place | postcode | 15 | And result has not attributes osm_type,osm_id + + + @v1-api-python-only + Scenario Outline: Details debug output returns no errors + When sending debug details query for + Then the result is valid html + + Examples: + | feature | + | N5484325405 | + | W1 | + | 112820 | + | 112871 | diff --git a/test/bdd/api/reverse/simple.feature b/test/bdd/api/reverse/simple.feature index 4da311e7..a65ca0ac 100644 --- a/test/bdd/api/reverse/simple.feature +++ b/test/bdd/api/reverse/simple.feature @@ -132,6 +132,8 @@ Feature: Simple Reverse Tests | Nan | 8.448 | | 48.966 | Nan | - Scenario: Reverse Debug output returns no errors + + @v1-api-python-only + Scenario: Reverse Debug output returns no errors When sending debug reverse coordinates 47.11,9.57 - Then a HTTP 200 is returned + Then the result is valid html diff --git a/test/bdd/steps/steps_api_queries.py b/test/bdd/steps/steps_api_queries.py index 9f865ab7..78967ad1 100644 --- a/test/bdd/steps/steps_api_queries.py +++ b/test/bdd/steps/steps_api_queries.py @@ -15,6 +15,7 @@ import os import re import logging import asyncio +import xml.etree.ElementTree as ET from urllib.parse import urlencode from utils import run_script @@ -73,8 +74,12 @@ def compare(operator, op1, op2): def send_api_query(endpoint, params, fmt, context): - if fmt is not None and fmt.strip() != 'debug': - params['format'] = fmt.strip() + if fmt is not None: + if fmt.strip() == 'debug': + params['debug'] = '1' + else: + params['format'] = fmt.strip() + if context.table: if context.table.headings[0] == 'param': for line in context.table: @@ -154,8 +159,6 @@ def website_search_request(context, fmt, query, addr): params['q'] = query if addr is not None: params['addressdetails'] = '1' - if fmt and fmt.strip() == 'debug': - params['debug'] = '1' outp, status = send_api_query('search', params, fmt, context) @@ -168,8 +171,6 @@ def website_reverse_request(context, fmt, lat, lon): params['lat'] = lat if lon is not None: params['lon'] = lon - if fmt and fmt.strip() == 'debug': - params['debug'] = '1' outp, status = send_api_query('reverse', params, fmt, context) @@ -178,8 +179,6 @@ def website_reverse_request(context, fmt, lat, lon): @when(u'sending (?P\S+ )?reverse point (?P.+)') def website_reverse_request(context, fmt, nodeid): params = {} - if fmt and fmt.strip() == 'debug': - params['debug'] = '1' params['lon'], params['lat'] = (f'{c:f}' for c in context.osm.grid_node(int(nodeid))) @@ -220,7 +219,7 @@ def website_status_request(context, fmt): @step(u'(?Pless than|more than|exactly|at least|at most) (?P\d+) results? (?:is|are) returned') def validate_result_number(context, operator, number): - assert context.response.errorcode == 200 + context.execute_steps("Then a HTTP 200 is returned") numres = len(context.response.result) assert compare(operator, numres, int(number)), \ f"Bad number of results: expected {operator} {number}, got {numres}." @@ -237,7 +236,19 @@ def check_page_content_equals(context, text): @then(u'the result is valid (?P\w+)') def step_impl(context, fmt): context.execute_steps("Then a HTTP 200 is returned") - assert context.response.format == fmt + if fmt.strip() == 'html': + try: + tree = ET.fromstring(context.response.page) + except Exception as ex: + assert False, f"Could not parse page:\n{context.response.page}" + + assert tree.tag == 'html' + body = tree.find('./body') + assert body is not None + assert body.find('.//script') is None + else: + assert context.response.format == fmt + @then(u'a (?P\w+) user error is returned') def check_page_error(context, fmt): -- 2.39.5