X-Git-Url: https://git.openstreetmap.org./nominatim.git/blobdiff_plain/c6d40d4bf44821e9d451ccf43213f9a9caf245b6..c70dfccacac90a90bfc193f49262abe7505d7d75:/test/python/api/test_result_formatting_v1.py diff --git a/test/python/api/test_result_formatting_v1.py b/test/python/api/test_result_formatting_v1.py index 0ff834a4..406c7654 100644 --- a/test/python/api/test_result_formatting_v1.py +++ b/test/python/api/test_result_formatting_v1.py @@ -1,8 +1,8 @@ -# SPDX-License-Identifier: GPL-2.0-only +# SPDX-License-Identifier: GPL-3.0-or-later # # This file is part of Nominatim. (https://nominatim.org) # -# Copyright (C) 2023 by the Nominatim developer community. +# Copyright (C) 2025 by the Nominatim developer community. # For a full list of authors see the git log. """ Tests for formatting results for the V1 API. @@ -15,41 +15,45 @@ import json import pytest -import nominatim.api.v1 as api_impl -import nominatim.api as napi -from nominatim.version import NOMINATIM_VERSION +from nominatim_api.v1.format import dispatch as v1_format +import nominatim_api as napi STATUS_FORMATS = {'text', 'json'} # StatusResult + def test_status_format_list(): - assert set(api_impl.list_formats(napi.StatusResult)) == STATUS_FORMATS + assert set(v1_format.list_formats(napi.StatusResult)) == STATUS_FORMATS @pytest.mark.parametrize('fmt', list(STATUS_FORMATS)) def test_status_supported(fmt): - assert api_impl.supports_format(napi.StatusResult, fmt) + assert v1_format.supports_format(napi.StatusResult, fmt) def test_status_unsupported(): - assert not api_impl.supports_format(napi.StatusResult, 'gagaga') + assert not v1_format.supports_format(napi.StatusResult, 'gagaga') def test_status_format_text(): - assert api_impl.format_result(napi.StatusResult(0, 'message here'), 'text', {}) == 'OK' + assert v1_format.format_result(napi.StatusResult(0, 'message here'), 'text', {}) \ + == 'OK' -def test_status_format_text(): - assert api_impl.format_result(napi.StatusResult(500, 'message here'), 'text', {}) == 'ERROR: message here' +def test_status_format_error_text(): + assert v1_format.format_result(napi.StatusResult(500, 'message here'), 'text', {}) \ + == 'ERROR: message here' def test_status_format_json_minimal(): status = napi.StatusResult(700, 'Bad format.') - result = api_impl.format_result(status, 'json', {}) + result = v1_format.format_result(status, 'json', {}) - assert result == '{"status":700,"message":"Bad format.","software_version":"%s"}' % (NOMINATIM_VERSION, ) + assert json.loads(result) == {'status': 700, + 'message': 'Bad format.', + 'software_version': napi.__version__} def test_status_format_json_full(): @@ -57,9 +61,13 @@ def test_status_format_json_full(): status.data_updated = dt.datetime(2010, 2, 7, 20, 20, 3, 0, tzinfo=dt.timezone.utc) status.database_version = '5.6' - result = api_impl.format_result(status, 'json', {}) + result = v1_format.format_result(status, 'json', {}) - assert result == '{"status":0,"message":"OK","data_updated":"2010-02-07T20:20:03+00:00","software_version":"%s","database_version":"5.6"}' % (NOMINATIM_VERSION, ) + assert json.loads(result) == {'status': 0, + 'message': 'OK', + 'data_updated': '2010-02-07T20:20:03+00:00', + 'software_version': napi.__version__, + 'database_version': '5.6'} # DetailedResult @@ -69,7 +77,7 @@ def test_search_details_minimal(): ('place', 'thing'), napi.Point(1.0, 2.0)) - result = api_impl.format_result(search, 'json', {}) + result = v1_format.format_result(search, 'json', {}) assert json.loads(result) == \ {'category': 'place', @@ -85,7 +93,7 @@ def test_search_details_minimal(): 'extratags': {}, 'centroid': {'type': 'Point', 'coordinates': [1.0, 2.0]}, 'geometry': {'type': 'Point', 'coordinates': [1.0, 2.0]}, - } + } def test_search_details_full(): @@ -109,11 +117,11 @@ def test_search_details_full(): rank_search=28, importance=0.0443, country_code='ll', - indexed_date = import_date + indexed_date=import_date ) search.localize(napi.Locales()) - result = api_impl.format_result(search, 'json', {}) + result = v1_format.format_result(search, 'json', {}) assert json.loads(result) == \ {'place_id': 37563, @@ -139,7 +147,7 @@ def test_search_details_full(): 'isarea': False, 'centroid': {'type': 'Point', 'coordinates': [56.947, -87.44]}, 'geometry': {'type': 'Point', 'coordinates': [56.947, -87.44]}, - } + } @pytest.mark.parametrize('gtype,isarea', [('ST_Point', False), @@ -148,11 +156,11 @@ def test_search_details_full(): ('ST_MultiPolygon', True)]) def test_search_details_no_geometry(gtype, isarea): search = napi.DetailedResult(napi.SourceTable.PLACEX, - ('place', 'thing'), - napi.Point(1.0, 2.0), - geometry={'type': gtype}) + ('place', 'thing'), + napi.Point(1.0, 2.0), + geometry={'type': gtype}) - result = api_impl.format_result(search, 'json', {}) + result = v1_format.format_result(search, 'json', {}) js = json.loads(result) assert js['geometry'] == {'type': 'Point', 'coordinates': [1.0, 2.0]} @@ -160,16 +168,17 @@ def test_search_details_no_geometry(gtype, isarea): def test_search_details_with_geometry(): - search = napi.DetailedResult(napi.SourceTable.PLACEX, - ('place', 'thing'), - napi.Point(1.0, 2.0), - geometry={'geojson': '{"type":"Point","coordinates":[56.947,-87.44]}'}) + search = napi.DetailedResult( + napi.SourceTable.PLACEX, + ('place', 'thing'), + napi.Point(1.0, 2.0), + geometry={'geojson': '{"type":"Point","coordinates":[56.947,-87.44]}'}) - result = api_impl.format_result(search, 'json', {}) + result = v1_format.format_result(search, 'json', {}) js = json.loads(result) assert js['geometry'] == {'type': 'Point', 'coordinates': [56.947, -87.44]} - assert js['isarea'] == False + assert js['isarea'] is False def test_search_details_with_icon_available(): @@ -177,7 +186,7 @@ def test_search_details_with_icon_available(): ('amenity', 'restaurant'), napi.Point(1.0, 2.0)) - result = api_impl.format_result(search, 'json', {'icon_base_url': 'foo'}) + result = v1_format.format_result(search, 'json', {'icon_base_url': 'foo'}) js = json.loads(result) assert js['icon'] == 'foo/food_restaurant.p.20.png' @@ -188,7 +197,7 @@ def test_search_details_with_icon_not_available(): ('amenity', 'tree'), napi.Point(1.0, 2.0)) - result = api_impl.format_result(search, 'json', {'icon_base_url': 'foo'}) + result = v1_format.format_result(search, 'json', {'icon_base_url': 'foo'}) js = json.loads(result) assert 'icon' not in js @@ -211,7 +220,7 @@ def test_search_details_with_address_minimal(): distance=0.0) ]) - result = api_impl.format_result(search, 'json', {}) + result = v1_format.format_result(search, 'json', {}) js = json.loads(result) assert js['address'] == [{'localname': '', @@ -225,7 +234,7 @@ def test_search_details_with_address_minimal(): @pytest.mark.parametrize('field,outfield', [('address_rows', 'address'), ('linked_rows', 'linked_places'), ('parented_rows', 'hierarchy') - ]) + ]) def test_search_details_with_further_infos(field, outfield): search = napi.DetailedResult(napi.SourceTable.PLACEX, ('place', 'thing'), @@ -244,54 +253,53 @@ def test_search_details_with_further_infos(field, outfield): distance=0.034) ]) - result = api_impl.format_result(search, 'json', {}) + result = v1_format.format_result(search, 'json', {}) js = json.loads(result) assert js[outfield] == [{'localname': 'Trespass', - 'place_id': 3498, - 'osm_id': 442, - 'osm_type': 'R', - 'place_type': 'spec', - 'class': 'bnd', - 'type': 'note', - 'admin_level': 4, - 'rank_address': 10, - 'distance': 0.034, - 'isaddress': True}] + 'place_id': 3498, + 'osm_id': 442, + 'osm_type': 'R', + 'place_type': 'spec', + 'class': 'bnd', + 'type': 'note', + 'admin_level': 4, + 'rank_address': 10, + 'distance': 0.034, + 'isaddress': True}] def test_search_details_grouped_hierarchy(): search = napi.DetailedResult(napi.SourceTable.PLACEX, ('place', 'thing'), napi.Point(1.0, 2.0), - parented_rows = - [napi.AddressLine(place_id=3498, - osm_object=('R', 442), - category=('bnd', 'note'), - names={'name': 'Trespass'}, - extratags={'access': 'no', - 'place_type': 'spec'}, - admin_level=4, - fromarea=True, - isaddress=True, - rank_address=10, - distance=0.034) - ]) - - result = api_impl.format_result(search, 'json', {'group_hierarchy': True}) + parented_rows=[napi.AddressLine( + place_id=3498, + osm_object=('R', 442), + category=('bnd', 'note'), + names={'name': 'Trespass'}, + extratags={'access': 'no', + 'place_type': 'spec'}, + admin_level=4, + fromarea=True, + isaddress=True, + rank_address=10, + distance=0.034)]) + + result = v1_format.format_result(search, 'json', {'group_hierarchy': True}) js = json.loads(result) assert js['hierarchy'] == {'note': [{'localname': 'Trespass', - 'place_id': 3498, - 'osm_id': 442, - 'osm_type': 'R', - 'place_type': 'spec', - 'class': 'bnd', - 'type': 'note', - 'admin_level': 4, - 'rank_address': 10, - 'distance': 0.034, - 'isaddress': True}]} + 'place_id': 3498, + 'osm_id': 442, + 'osm_type': 'R', + 'place_type': 'spec', + 'class': 'bnd', + 'type': 'note', + 'admin_level': 4, + 'rank_address': 10, + 'distance': 0.034, + 'isaddress': True}]} def test_search_details_keywords_name(): @@ -302,11 +310,11 @@ def test_search_details_keywords_name(): napi.WordInfo(23, 'foo', 'mefoo'), napi.WordInfo(24, 'foo', 'bafoo')]) - result = api_impl.format_result(search, 'json', {'keywords': True}) + result = v1_format.format_result(search, 'json', {'keywords': True}) js = json.loads(result) assert js['keywords'] == {'name': [{'id': 23, 'token': 'foo'}, - {'id': 24, 'token': 'foo'}], + {'id': 24, 'token': 'foo'}], 'address': []} @@ -318,10 +326,9 @@ def test_search_details_keywords_address(): napi.WordInfo(23, 'foo', 'mefoo'), napi.WordInfo(24, 'foo', 'bafoo')]) - result = api_impl.format_result(search, 'json', {'keywords': True}) + result = v1_format.format_result(search, 'json', {'keywords': True}) js = json.loads(result) assert js['keywords'] == {'address': [{'id': 23, 'token': 'foo'}, - {'id': 24, 'token': 'foo'}], + {'id': 24, 'token': 'foo'}], 'name': []} -