1 # SPDX-License-Identifier: GPL-2.0-only
3 # This file is part of Nominatim. (https://nominatim.org)
5 # Copyright (C) 2023 by the Nominatim developer community.
6 # For a full list of authors see the git log.
8 Tests for API access commands of command-line interface wrapper.
13 import nominatim.clicmd.api
14 import nominatim.api as napi
17 @pytest.mark.parametrize("endpoint", (('search', 'reverse', 'lookup', 'details', 'status')))
18 def test_no_api_without_phpcgi(endpoint):
19 assert nominatim.cli.nominatim(module_dir='MODULE NOT AVAILABLE',
20 osm2pgsql_path='OSM2PGSQL NOT AVAILABLE',
22 cli_args=[endpoint]) == 1
25 @pytest.mark.parametrize("params", [('search', '--query', 'new'),
26 ('search', '--city', 'Berlin'),
27 ('reverse', '--lat', '0', '--lon', '0', '--zoom', '13'),
28 ('lookup', '--id', 'N1'),
29 ('details', '--node', '1'),
30 ('details', '--way', '1'),
31 ('details', '--relation', '1'),
32 ('details', '--place_id', '10001')])
33 class TestCliApiCallPhp:
35 @pytest.fixture(autouse=True)
36 def setup_cli_call(self, params, cli_call, mock_func_factory, tmp_path):
37 self.mock_run_api = mock_func_factory(nominatim.clicmd.api, 'run_api_script')
40 return cli_call(*params, '--project-dir', str(tmp_path))
42 self.run_nominatim = _run
45 def test_api_commands_simple(self, tmp_path, params):
46 (tmp_path / 'website').mkdir()
47 (tmp_path / 'website' / (params[0] + '.php')).write_text('')
49 assert self.run_nominatim() == 0
51 assert self.mock_run_api.called == 1
52 assert self.mock_run_api.last_args[0] == params[0]
55 def test_bad_project_dir(self):
56 assert self.run_nominatim() == 1
59 class TestCliStatusCall:
61 @pytest.fixture(autouse=True)
62 def setup_status_mock(self, monkeypatch):
63 monkeypatch.setattr(napi.NominatimAPI, 'status',
64 lambda self: napi.StatusResult(200, 'OK'))
67 def test_status_simple(self, cli_call, tmp_path):
68 result = cli_call('status', '--project-dir', str(tmp_path))
73 def test_status_json_format(self, cli_call, tmp_path, capsys):
74 result = cli_call('status', '--project-dir', str(tmp_path),
79 json.loads(capsys.readouterr().out)
83 'search': ('--query', 'somewhere'),
84 'reverse': ('--lat', '20', '--lon', '30'),
85 'lookup': ('--id', 'R345345'),
86 'details': ('--node', '324')
89 @pytest.mark.parametrize("endpoint", (('search', 'reverse', 'lookup')))
90 class TestCliApiCommonParameters:
92 @pytest.fixture(autouse=True)
93 def setup_website_dir(self, cli_call, project_env, endpoint):
94 self.endpoint = endpoint
95 self.cli_call = cli_call
96 self.project_dir = project_env.project_dir
97 (self.project_dir / 'website').mkdir()
100 def expect_param(self, param, expected):
101 (self.project_dir / 'website' / (self.endpoint + '.php')).write_text(f"""<?php
102 exit($_GET['{param}'] == '{expected}' ? 0 : 10);
106 def call_nominatim(self, *params):
107 return self.cli_call(self.endpoint, *QUERY_PARAMS[self.endpoint],
108 '--project-dir', str(self.project_dir), *params)
111 def test_param_output(self):
112 self.expect_param('format', 'xml')
113 assert self.call_nominatim('--format', 'xml') == 0
116 def test_param_lang(self):
117 self.expect_param('accept-language', 'de')
118 assert self.call_nominatim('--lang', 'de') == 0
119 assert self.call_nominatim('--accept-language', 'de') == 0
122 @pytest.mark.parametrize("param", ('addressdetails', 'extratags', 'namedetails'))
123 def test_param_extradata(self, param):
124 self.expect_param(param, '1')
126 assert self.call_nominatim('--' + param) == 0
128 def test_param_polygon_output(self):
129 self.expect_param('polygon_geojson', '1')
131 assert self.call_nominatim('--polygon-output', 'geojson') == 0
134 def test_param_polygon_threshold(self):
135 self.expect_param('polygon_threshold', '0.3452')
137 assert self.call_nominatim('--polygon-threshold', '0.3452') == 0
140 def test_cli_search_param_bounded(cli_call, project_env):
141 webdir = project_env.project_dir / 'website'
143 (webdir / 'search.php').write_text(f"""<?php
144 exit($_GET['bounded'] == '1' ? 0 : 10);
147 assert cli_call('search', *QUERY_PARAMS['search'], '--project-dir', str(project_env.project_dir),
151 def test_cli_search_param_dedupe(cli_call, project_env):
152 webdir = project_env.project_dir / 'website'
154 (webdir / 'search.php').write_text(f"""<?php
155 exit($_GET['dedupe'] == '0' ? 0 : 10);
158 assert cli_call('search', *QUERY_PARAMS['search'], '--project-dir', str(project_env.project_dir),
162 def test_cli_details_param_class(cli_call, project_env):
163 webdir = project_env.project_dir / 'website'
165 (webdir / 'details.php').write_text(f"""<?php
166 exit($_GET['class'] == 'highway' ? 0 : 10);
169 assert cli_call('details', *QUERY_PARAMS['details'], '--project-dir', str(project_env.project_dir),
170 '--class', 'highway') == 0
173 @pytest.mark.parametrize('param', ('lang', 'accept-language'))
174 def test_cli_details_param_lang(cli_call, project_env, param):
175 webdir = project_env.project_dir / 'website'
177 (webdir / 'details.php').write_text(f"""<?php
178 exit($_GET['accept-language'] == 'es' ? 0 : 10);
181 assert cli_call('details', *QUERY_PARAMS['details'], '--project-dir', str(project_env.project_dir),
182 '--' + param, 'es') == 0