X-Git-Url: https://git.openstreetmap.org./nominatim.git/blobdiff_plain/8c7140d92b7a679ae5ef5bb3655c69bd39b7dfeb..7f11de0db9ecbb2a8f7551815285d7553747a96e:/test/python/api/test_server_glue_v1.py diff --git a/test/python/api/test_server_glue_v1.py b/test/python/api/test_server_glue_v1.py index 5a7430f4..6ea790c0 100644 --- a/test/python/api/test_server_glue_v1.py +++ b/test/python/api/test_server_glue_v1.py @@ -2,22 +2,21 @@ # # This file is part of Nominatim. (https://nominatim.org) # -# Copyright (C) 2023 by the Nominatim developer community. +# Copyright (C) 2024 by the Nominatim developer community. # For a full list of authors see the git log. """ Tests for the Python web frameworks adaptor, v1 API. """ import json import xml.etree.ElementTree as ET -from pathlib import Path import pytest from fake_adaptor import FakeAdaptor, FakeError, FakeResponse -import nominatim.api.v1.server_glue as glue -import nominatim.api as napi -import nominatim.api.logging as loglib +import nominatim_api.v1.server_glue as glue +import nominatim_api as napi +import nominatim_api.logging as loglib # ASGIAdaptor.get_int/bool() @@ -59,14 +58,14 @@ def test_adaptor_get_bool_falsish(): def test_adaptor_parse_format_use_default(): adaptor = FakeAdaptor() - assert adaptor.parse_format(napi.StatusResult, 'text') == 'text' + assert glue.parse_format(adaptor, napi.StatusResult, 'text') == 'text' assert adaptor.content_type == 'text/plain; charset=utf-8' def test_adaptor_parse_format_use_configured(): adaptor = FakeAdaptor(params={'format': 'json'}) - assert adaptor.parse_format(napi.StatusResult, 'text') == 'json' + assert glue.parse_format(adaptor, napi.StatusResult, 'text') == 'json' assert adaptor.content_type == 'application/json; charset=utf-8' @@ -74,37 +73,37 @@ def test_adaptor_parse_format_invalid_value(): adaptor = FakeAdaptor(params={'format': '@!#'}) with pytest.raises(FakeError, match='^400 -- .*must be one of'): - adaptor.parse_format(napi.StatusResult, 'text') + glue.parse_format(adaptor, napi.StatusResult, 'text') # ASGIAdaptor.get_accepted_languages() def test_accepted_languages_from_param(): a = FakeAdaptor(params={'accept-language': 'de'}) - assert a.get_accepted_languages() == 'de' + assert glue.get_accepted_languages(a) == 'de' def test_accepted_languages_from_header(): a = FakeAdaptor(headers={'accept-language': 'de'}) - assert a.get_accepted_languages() == 'de' + assert glue.get_accepted_languages(a) == 'de' def test_accepted_languages_from_default(monkeypatch): monkeypatch.setenv('NOMINATIM_DEFAULT_LANGUAGE', 'de') a = FakeAdaptor() - assert a.get_accepted_languages() == 'de' + assert glue.get_accepted_languages(a) == 'de' def test_accepted_languages_param_over_header(): a = FakeAdaptor(params={'accept-language': 'de'}, headers={'accept-language': 'en'}) - assert a.get_accepted_languages() == 'de' + assert glue.get_accepted_languages(a) == 'de' def test_accepted_languages_header_over_default(monkeypatch): monkeypatch.setenv('NOMINATIM_DEFAULT_LANGUAGE', 'en') a = FakeAdaptor(headers={'accept-language': 'de'}) - assert a.get_accepted_languages() == 'de' + assert glue.get_accepted_languages(a) == 'de' # ASGIAdaptor.raise_error() @@ -114,7 +113,7 @@ class TestAdaptorRaiseError: @pytest.fixture(autouse=True) def init_adaptor(self): self.adaptor = FakeAdaptor() - self.adaptor.setup_debugging() + glue.setup_debugging(self.adaptor) def run_raise_error(self, msg, status): with pytest.raises(FakeError) as excinfo: @@ -127,7 +126,7 @@ class TestAdaptorRaiseError: err = self.run_raise_error('TEST', 404) assert self.adaptor.content_type == 'text/plain; charset=utf-8' - assert err.msg == 'TEST' + assert err.msg == 'ERROR 404: TEST' assert err.status == 404 @@ -155,7 +154,7 @@ class TestAdaptorRaiseError: def test_raise_error_during_debug(): a = FakeAdaptor(params={'debug': '1'}) - a.setup_debugging() + glue.setup_debugging(a) loglib.log().section('Ongoing') with pytest.raises(FakeError) as excinfo: @@ -172,7 +171,7 @@ def test_raise_error_during_debug(): # ASGIAdaptor.build_response def test_build_response_without_content_type(): - resp = FakeAdaptor().build_response('attention') + resp = glue.build_response(FakeAdaptor(), 'attention') assert isinstance(resp, FakeResponse) assert resp.status == 200 @@ -182,9 +181,9 @@ def test_build_response_without_content_type(): def test_build_response_with_status(): a = FakeAdaptor(params={'format': 'json'}) - a.parse_format(napi.StatusResult, 'text') + glue.parse_format(a, napi.StatusResult, 'text') - resp = a.build_response('stuff\nmore stuff', status=404) + resp = glue.build_response(a, 'stuff\nmore stuff', status=404) assert isinstance(resp, FakeResponse) assert resp.status == 404 @@ -194,9 +193,9 @@ def test_build_response_with_status(): def test_build_response_jsonp_with_json(): a = FakeAdaptor(params={'format': 'json', 'json_callback': 'test.func'}) - a.parse_format(napi.StatusResult, 'text') + glue.parse_format(a, napi.StatusResult, 'text') - resp = a.build_response('{}') + resp = glue.build_response(a, '{}') assert isinstance(resp, FakeResponse) assert resp.status == 200 @@ -206,9 +205,9 @@ def test_build_response_jsonp_with_json(): def test_build_response_jsonp_without_json(): a = FakeAdaptor(params={'format': 'text', 'json_callback': 'test.func'}) - a.parse_format(napi.StatusResult, 'text') + glue.parse_format(a, napi.StatusResult, 'text') - resp = a.build_response('{}') + resp = glue.build_response(a, '{}') assert isinstance(resp, FakeResponse) assert resp.status == 200 @@ -219,10 +218,10 @@ def test_build_response_jsonp_without_json(): @pytest.mark.parametrize('param', ['alert(); func', '\\n', '', 'a b']) def test_build_response_jsonp_bad_format(param): a = FakeAdaptor(params={'format': 'json', 'json_callback': param}) - a.parse_format(napi.StatusResult, 'text') + glue.parse_format(a, napi.StatusResult, 'text') with pytest.raises(FakeError, match='^400 -- .*Invalid'): - a.build_response('{}') + glue.build_response(a, '{}') # status_endpoint() @@ -242,7 +241,7 @@ class TestStatusEndpoint: a = FakeAdaptor() self.status = napi.StatusResult(0, 'foo') - resp = await glue.status_endpoint(napi.NominatimAPIAsync(Path('/invalid')), a) + resp = await glue.status_endpoint(napi.NominatimAPIAsync(), a) assert isinstance(resp, FakeResponse) assert resp.status == 200 @@ -254,7 +253,7 @@ class TestStatusEndpoint: a = FakeAdaptor() self.status = napi.StatusResult(405, 'foo') - resp = await glue.status_endpoint(napi.NominatimAPIAsync(Path('/invalid')), a) + resp = await glue.status_endpoint(napi.NominatimAPIAsync(), a) assert isinstance(resp, FakeResponse) assert resp.status == 500 @@ -266,7 +265,7 @@ class TestStatusEndpoint: a = FakeAdaptor(params={'format': 'json'}) self.status = napi.StatusResult(405, 'foo') - resp = await glue.status_endpoint(napi.NominatimAPIAsync(Path('/invalid')), a) + resp = await glue.status_endpoint(napi.NominatimAPIAsync(), a) assert isinstance(resp, FakeResponse) assert resp.status == 200 @@ -279,7 +278,7 @@ class TestStatusEndpoint: self.status = napi.StatusResult(0, 'foo') with pytest.raises(FakeError): - await glue.status_endpoint(napi.NominatimAPIAsync(Path('/invalid')), a) + await glue.status_endpoint(napi.NominatimAPIAsync(), a) # details_endpoint() @@ -305,14 +304,14 @@ class TestDetailsEndpoint: a = FakeAdaptor() with pytest.raises(FakeError, match='^400 -- .*Missing'): - await glue.details_endpoint(napi.NominatimAPIAsync(Path('/invalid')), a) + await glue.details_endpoint(napi.NominatimAPIAsync(), a) @pytest.mark.asyncio async def test_details_by_place_id(self): a = FakeAdaptor(params={'place_id': '4573'}) - await glue.details_endpoint(napi.NominatimAPIAsync(Path('/invalid')), a) + await glue.details_endpoint(napi.NominatimAPIAsync(), a) assert self.lookup_args[0].place_id == 4573 @@ -321,7 +320,7 @@ class TestDetailsEndpoint: async def test_details_by_osm_id(self): a = FakeAdaptor(params={'osmtype': 'N', 'osmid': '45'}) - await glue.details_endpoint(napi.NominatimAPIAsync(Path('/invalid')), a) + await glue.details_endpoint(napi.NominatimAPIAsync(), a) assert self.lookup_args[0].osm_type == 'N' assert self.lookup_args[0].osm_id == 45 @@ -332,7 +331,7 @@ class TestDetailsEndpoint: async def test_details_with_debugging(self): a = FakeAdaptor(params={'osmtype': 'N', 'osmid': '45', 'debug': '1'}) - resp = await glue.details_endpoint(napi.NominatimAPIAsync(Path('/invalid')), a) + resp = await glue.details_endpoint(napi.NominatimAPIAsync(), a) content = ET.fromstring(resp.output) assert resp.content_type == 'text/html; charset=utf-8' @@ -345,7 +344,7 @@ class TestDetailsEndpoint: self.result = None with pytest.raises(FakeError, match='^404 -- .*found'): - await glue.details_endpoint(napi.NominatimAPIAsync(Path('/invalid')), a) + await glue.details_endpoint(napi.NominatimAPIAsync(), a) # reverse_endpoint() @@ -370,7 +369,7 @@ class TestReverseEndPoint: a.params['format'] = 'xml' with pytest.raises(FakeError, match='^400 -- (?s:.*)missing'): - await glue.reverse_endpoint(napi.NominatimAPIAsync(Path('/invalid')), a) + await glue.reverse_endpoint(napi.NominatimAPIAsync(), a) @pytest.mark.asyncio @@ -380,7 +379,7 @@ class TestReverseEndPoint: a.params = params a.params['format'] = 'json' - res = await glue.reverse_endpoint(napi.NominatimAPIAsync(Path('/invalid')), a) + res = await glue.reverse_endpoint(napi.NominatimAPIAsync(), a) assert res == '' @@ -391,7 +390,7 @@ class TestReverseEndPoint: a.params['lat'] = '56.3' a.params['lon'] = '6.8' - assert await glue.reverse_endpoint(napi.NominatimAPIAsync(Path('/invalid')), a) + assert await glue.reverse_endpoint(napi.NominatimAPIAsync(), a) @pytest.mark.asyncio @@ -400,7 +399,7 @@ class TestReverseEndPoint: a.params['q'] = '34.6 2.56' a.params['format'] = 'json' - res = await glue.search_endpoint(napi.NominatimAPIAsync(Path('/invalid')), a) + res = await glue.search_endpoint(napi.NominatimAPIAsync(), a) assert len(json.loads(res.output)) == 1 @@ -425,7 +424,7 @@ class TestLookupEndpoint: a = FakeAdaptor() a.params['format'] = 'json' - res = await glue.lookup_endpoint(napi.NominatimAPIAsync(Path('/invalid')), a) + res = await glue.lookup_endpoint(napi.NominatimAPIAsync(), a) assert res.output == '[]' @@ -437,7 +436,7 @@ class TestLookupEndpoint: a.params['format'] = 'json' a.params['osm_ids'] = f'W34,{param},N33333' - res = await glue.lookup_endpoint(napi.NominatimAPIAsync(Path('/invalid')), a) + res = await glue.lookup_endpoint(napi.NominatimAPIAsync(), a) assert len(json.loads(res.output)) == 1 @@ -449,7 +448,7 @@ class TestLookupEndpoint: a.params['format'] = 'json' a.params['osm_ids'] = f'W34,{param},N33333' - res = await glue.lookup_endpoint(napi.NominatimAPIAsync(Path('/invalid')), a) + res = await glue.lookup_endpoint(napi.NominatimAPIAsync(), a) assert len(json.loads(res.output)) == 1 @@ -460,7 +459,7 @@ class TestLookupEndpoint: a.params['format'] = 'json' a.params['osm_ids'] = 'N23,W34' - res = await glue.lookup_endpoint(napi.NominatimAPIAsync(Path('/invalid')), a) + res = await glue.lookup_endpoint(napi.NominatimAPIAsync(), a) assert len(json.loads(res.output)) == 1 @@ -485,7 +484,7 @@ class TestSearchEndPointSearch: a = FakeAdaptor() a.params['q'] = 'something' - res = await glue.search_endpoint(napi.NominatimAPIAsync(Path('/invalid')), a) + res = await glue.search_endpoint(napi.NominatimAPIAsync(), a) assert len(json.loads(res.output)) == 1 @@ -496,7 +495,7 @@ class TestSearchEndPointSearch: a.params['q'] = 'something' a.params['format'] = 'xml' - res = await glue.search_endpoint(napi.NominatimAPIAsync(Path('/invalid')), a) + res = await glue.search_endpoint(napi.NominatimAPIAsync(), a) assert res.status == 200 assert res.output.index('something') > 0 @@ -509,7 +508,7 @@ class TestSearchEndPointSearch: a.params['city'] = 'ignored' with pytest.raises(FakeError, match='^400 -- .*cannot be used together'): - res = await glue.search_endpoint(napi.NominatimAPIAsync(Path('/invalid')), a) + res = await glue.search_endpoint(napi.NominatimAPIAsync(), a) @pytest.mark.asyncio @@ -521,7 +520,7 @@ class TestSearchEndPointSearch: if not dedupe: a.params['dedupe'] = '0' - res = await glue.search_endpoint(napi.NominatimAPIAsync(Path('/invalid')), a) + res = await glue.search_endpoint(napi.NominatimAPIAsync(), a) assert len(json.loads(res.output)) == numres @@ -544,7 +543,7 @@ class TestSearchEndPointSearchAddress: a = FakeAdaptor() a.params['street'] = 'something' - res = await glue.search_endpoint(napi.NominatimAPIAsync(Path('/invalid')), a) + res = await glue.search_endpoint(napi.NominatimAPIAsync(), a) assert len(json.loads(res.output)) == 1 @@ -567,6 +566,6 @@ class TestSearchEndPointSearchCategory: a = FakeAdaptor() a.params['q'] = '[shop=fog]' - res = await glue.search_endpoint(napi.NominatimAPIAsync(Path('/invalid')), a) + res = await glue.search_endpoint(napi.NominatimAPIAsync(), a) assert len(json.loads(res.output)) == 1