switch details cli command to new Python implementation

This commit is contained in:
Sarah Hoffmann
2023-02-03 10:43:54 +01:00
parent 1924beeb20
commit 104722a56a
11 changed files with 474 additions and 69 deletions

View File

@@ -0,0 +1,53 @@
# 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.
# For a full list of authors see the git log.
"""
Test functions for adapting results to the user's locale.
"""
import pytest
from nominatim.api import Locales
def test_display_name_empty_names():
l = Locales(['en', 'de'])
assert l.display_name(None) == ''
assert l.display_name({}) == ''
def test_display_name_none_localized():
l = Locales()
assert l.display_name({}) == ''
assert l.display_name({'name:de': 'DE', 'name': 'ALL'}) == 'ALL'
assert l.display_name({'ref': '34', 'name:de': 'DE'}) == '34'
def test_display_name_localized():
l = Locales(['en', 'de'])
assert l.display_name({}) == ''
assert l.display_name({'name:de': 'DE', 'name': 'ALL'}) == 'DE'
assert l.display_name({'ref': '34', 'name:de': 'DE'}) == 'DE'
def test_display_name_preference():
l = Locales(['en', 'de'])
assert l.display_name({}) == ''
assert l.display_name({'name:de': 'DE', 'name:en': 'EN'}) == 'EN'
assert l.display_name({'official_name:en': 'EN', 'name:de': 'DE'}) == 'DE'
@pytest.mark.parametrize('langstr,langlist',
[('fr', ['fr']),
('fr-FR', ['fr-FR', 'fr']),
('de,fr-FR', ['de', 'fr-FR', 'fr']),
('fr,de,fr-FR', ['fr', 'de', 'fr-FR']),
('en;q=0.5,fr', ['fr', 'en']),
('en;q=0.5,fr,en-US', ['fr', 'en-US', 'en']),
('en,fr;garbage,de', ['en', 'de'])])
def test_from_language_preferences(langstr, langlist):
assert Locales.from_accept_languages(langstr).languages == langlist

View File

@@ -32,17 +32,17 @@ def test_status_unsupported():
def test_status_format_text():
assert api_impl.format_result(StatusResult(0, 'message here'), 'text') == 'OK'
assert api_impl.format_result(StatusResult(0, 'message here'), 'text', {}) == 'OK'
def test_status_format_text():
assert api_impl.format_result(StatusResult(500, 'message here'), 'text') == 'ERROR: message here'
assert api_impl.format_result(StatusResult(500, 'message here'), 'text', {}) == 'ERROR: message here'
def test_status_format_json_minimal():
status = StatusResult(700, 'Bad format.')
result = api_impl.format_result(status, 'json')
result = api_impl.format_result(status, 'json', {})
assert result == '{"status":700,"message":"Bad format.","software_version":"%s"}' % (NOMINATIM_VERSION, )
@@ -52,6 +52,6 @@ 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 = api_impl.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, )

View File

@@ -25,11 +25,7 @@ def test_no_api_without_phpcgi(endpoint):
@pytest.mark.parametrize("params", [('search', '--query', 'new'),
('search', '--city', 'Berlin'),
('reverse', '--lat', '0', '--lon', '0', '--zoom', '13'),
('lookup', '--id', 'N1'),
('details', '--node', '1'),
('details', '--way', '1'),
('details', '--relation', '1'),
('details', '--place_id', '10001')])
('lookup', '--id', 'N1')])
class TestCliApiCallPhp:
@pytest.fixture(autouse=True)
@@ -79,6 +75,29 @@ class TestCliStatusCall:
json.loads(capsys.readouterr().out)
class TestCliDetailsCall:
@pytest.fixture(autouse=True)
def setup_status_mock(self, monkeypatch):
result = napi.SearchResult(napi.SourceTable.PLACEX, ('place', 'thing'),
(1.0, -3.0))
monkeypatch.setattr(napi.NominatimAPI, 'lookup',
lambda *args: result)
@pytest.mark.parametrize("params", [('--node', '1'),
('--way', '1'),
('--relation', '1'),
('--place_id', '10001')])
def test_status_json_format(self, cli_call, tmp_path, capsys, params):
result = cli_call('details', '--project-dir', str(tmp_path), *params)
assert result == 0
json.loads(capsys.readouterr().out)
QUERY_PARAMS = {
'search': ('--query', 'somewhere'),
'reverse': ('--lat', '20', '--lon', '30'),
@@ -157,27 +176,3 @@ def test_cli_search_param_dedupe(cli_call, project_env):
assert cli_call('search', *QUERY_PARAMS['search'], '--project-dir', str(project_env.project_dir),
'--no-dedupe') == 0
def test_cli_details_param_class(cli_call, project_env):
webdir = project_env.project_dir / 'website'
webdir.mkdir()
(webdir / 'details.php').write_text(f"""<?php
exit($_GET['class'] == 'highway' ? 0 : 10);
""")
assert cli_call('details', *QUERY_PARAMS['details'], '--project-dir', str(project_env.project_dir),
'--class', 'highway') == 0
@pytest.mark.parametrize('param', ('lang', 'accept-language'))
def test_cli_details_param_lang(cli_call, project_env, param):
webdir = project_env.project_dir / 'website'
webdir.mkdir()
(webdir / 'details.php').write_text(f"""<?php
exit($_GET['accept-language'] == 'es' ? 0 : 10);
""")
assert cli_call('details', *QUERY_PARAMS['details'], '--project-dir', str(project_env.project_dir),
'--' + param, 'es') == 0