]> git.openstreetmap.org Git - nominatim.git/blobdiff - test/python/api/test_result_formatting_v1.py
use custom result formatters in CLI commands
[nominatim.git] / test / python / api / test_result_formatting_v1.py
index 0c54667ead80b86180ff8e1d654fec0ae17bc42e..aaecab4539995121c0b5204b8eec1b6acb22dfec 100644 (file)
@@ -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)
 #
 #
 # 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 formatting results for the V1 API.
 # For a full list of authors see the git log.
 """
 Tests for formatting results for the V1 API.
@@ -15,41 +15,41 @@ import json
 
 import pytest
 
 
 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():
 
 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):
 
 
 @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():
 
 
 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():
 
 
 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():
 
 
 def test_status_format_text():
-    assert api_impl.format_result(napi.StatusResult(500, 'message here'), 'text', {}) == 'ERROR: message here'
+    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.')
 
 
 
 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 result == \
+           f'{{"status":700,"message":"Bad format.","software_version":"{napi.__version__}"}}'
 
 
 def test_status_format_json_full():
 
 
 def test_status_format_json_full():
@@ -57,9 +57,10 @@ 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'
 
     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 result == \
+           f'{{"status":0,"message":"OK","data_updated":"2010-02-07T20:20:03+00:00","software_version":"{napi.__version__}","database_version":"5.6"}}'
 
 
 # DetailedResult
 
 
 # DetailedResult
@@ -69,7 +70,7 @@ def test_search_details_minimal():
                                  ('place', 'thing'),
                                  napi.Point(1.0, 2.0))
 
                                  ('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',
 
     assert json.loads(result) == \
            {'category': 'place',
@@ -77,7 +78,7 @@ def test_search_details_minimal():
             'admin_level': 15,
             'names': {},
             'localname': '',
             'admin_level': 15,
             'names': {},
             'localname': '',
-            'calculated_importance': pytest.approx(0.0000001),
+            'calculated_importance': pytest.approx(0.00001),
             'rank_address': 30,
             'rank_search': 30,
             'isarea': False,
             'rank_address': 30,
             'rank_search': 30,
             'isarea': False,
@@ -113,7 +114,7 @@ def test_search_details_full():
                   )
     search.localize(napi.Locales())
 
                   )
     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,
 
     assert json.loads(result) == \
            {'place_id': 37563,
@@ -152,7 +153,7 @@ def test_search_details_no_geometry(gtype, isarea):
                                napi.Point(1.0, 2.0),
                                geometry={'type': gtype})
 
                                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]}
     js = json.loads(result)
 
     assert js['geometry'] == {'type': 'Point', 'coordinates': [1.0, 2.0]}
@@ -165,7 +166,7 @@ def test_search_details_with_geometry():
                                  napi.Point(1.0, 2.0),
                                  geometry={'geojson': '{"type":"Point","coordinates":[56.947,-87.44]}'})
 
                                  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]}
     js = json.loads(result)
 
     assert js['geometry'] == {'type': 'Point', 'coordinates': [56.947, -87.44]}
@@ -177,7 +178,7 @@ def test_search_details_with_icon_available():
                                  ('amenity', 'restaurant'),
                                  napi.Point(1.0, 2.0))
 
                                  ('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'
     js = json.loads(result)
 
     assert js['icon'] == 'foo/food_restaurant.p.20.png'
@@ -188,7 +189,7 @@ def test_search_details_with_icon_not_available():
                                  ('amenity', 'tree'),
                                  napi.Point(1.0, 2.0))
 
                                  ('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
     js = json.loads(result)
 
     assert 'icon' not in js
@@ -211,7 +212,7 @@ def test_search_details_with_address_minimal():
                                                     distance=0.0)
                                  ])
 
                                                     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': '',
     js = json.loads(result)
 
     assert js['address'] == [{'localname': '',
@@ -244,7 +245,7 @@ def test_search_details_with_further_infos(field, outfield):
                                              distance=0.034)
                             ])
 
                                              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',
     js = json.loads(result)
 
     assert js[outfield] == [{'localname': 'Trespass',
@@ -278,7 +279,7 @@ def test_search_details_grouped_hierarchy():
                                              distance=0.034)
                                      ])
 
                                              distance=0.034)
                                      ])
 
-    result = api_impl.format_result(search, 'json', {'group_hierarchy': True})
+    result = v1_format.format_result(search, 'json', {'group_hierarchy': True})
     js = json.loads(result)
 
     assert js['hierarchy'] == {'note': [{'localname': 'Trespass',
     js = json.loads(result)
 
     assert js['hierarchy'] == {'note': [{'localname': 'Trespass',
@@ -302,7 +303,7 @@ def test_search_details_keywords_name():
                                      napi.WordInfo(23, 'foo', 'mefoo'),
                                      napi.WordInfo(24, 'foo', 'bafoo')])
 
                                      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'},
     js = json.loads(result)
 
     assert js['keywords'] == {'name': [{'id': 23, 'token': 'foo'},
@@ -318,7 +319,7 @@ def test_search_details_keywords_address():
                                      napi.WordInfo(23, 'foo', 'mefoo'),
                                      napi.WordInfo(24, 'foo', 'bafoo')])
 
                                      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'},
     js = json.loads(result)
 
     assert js['keywords'] == {'address': [{'id': 23, 'token': 'foo'},