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 class TestCliApiCallPhp:
29 @pytest.fixture(autouse=True)
30 def setup_cli_call(self, params, cli_call, mock_func_factory, tmp_path):
31 self.mock_run_api = mock_func_factory(nominatim.clicmd.api, 'run_api_script')
34 return cli_call(*params, '--project-dir', str(tmp_path))
36 self.run_nominatim = _run
39 def test_api_commands_simple(self, tmp_path, params):
40 (tmp_path / 'website').mkdir()
41 (tmp_path / 'website' / (params[0] + '.php')).write_text('')
43 assert self.run_nominatim() == 0
45 assert self.mock_run_api.called == 1
46 assert self.mock_run_api.last_args[0] == params[0]
49 def test_bad_project_dir(self):
50 assert self.run_nominatim() == 1
53 class TestCliStatusCall:
55 @pytest.fixture(autouse=True)
56 def setup_status_mock(self, monkeypatch):
57 monkeypatch.setattr(napi.NominatimAPI, 'status',
58 lambda self: napi.StatusResult(200, 'OK'))
61 def test_status_simple(self, cli_call, tmp_path):
62 result = cli_call('status', '--project-dir', str(tmp_path))
67 def test_status_json_format(self, cli_call, tmp_path, capsys):
68 result = cli_call('status', '--project-dir', str(tmp_path),
73 json.loads(capsys.readouterr().out)
76 class TestCliDetailsCall:
78 @pytest.fixture(autouse=True)
79 def setup_status_mock(self, monkeypatch):
80 result = napi.DetailedResult(napi.SourceTable.PLACEX, ('place', 'thing'),
81 napi.Point(1.0, -3.0))
83 monkeypatch.setattr(napi.NominatimAPI, 'details',
84 lambda *args, **kwargs: result)
86 @pytest.mark.parametrize("params", [('--node', '1'),
89 ('--place_id', '10001')])
91 def test_details_json_format(self, cli_call, tmp_path, capsys, params):
92 result = cli_call('details', '--project-dir', str(tmp_path), *params)
96 json.loads(capsys.readouterr().out)
99 class TestCliReverseCall:
101 @pytest.fixture(autouse=True)
102 def setup_reverse_mock(self, monkeypatch):
103 result = napi.ReverseResult(napi.SourceTable.PLACEX, ('place', 'thing'),
104 napi.Point(1.0, -3.0),
105 names={'name':'Name', 'name:fr': 'Nom'},
106 extratags={'extra':'Extra'})
108 monkeypatch.setattr(napi.NominatimAPI, 'reverse',
109 lambda *args, **kwargs: result)
112 def test_reverse_simple(self, cli_call, tmp_path, capsys):
113 result = cli_call('reverse', '--project-dir', str(tmp_path),
114 '--lat', '34', '--lon', '34')
118 out = json.loads(capsys.readouterr().out)
119 assert out['name'] == 'Name'
120 assert 'address' not in out
121 assert 'extratags' not in out
122 assert 'namedetails' not in out
125 @pytest.mark.parametrize('param,field', [('--addressdetails', 'address'),
126 ('--extratags', 'extratags'),
127 ('--namedetails', 'namedetails')])
128 def test_reverse_extra_stuff(self, cli_call, tmp_path, capsys, param, field):
129 result = cli_call('reverse', '--project-dir', str(tmp_path),
130 '--lat', '34', '--lon', '34', param)
134 out = json.loads(capsys.readouterr().out)
138 def test_reverse_format(self, cli_call, tmp_path, capsys):
139 result = cli_call('reverse', '--project-dir', str(tmp_path),
140 '--lat', '34', '--lon', '34', '--format', 'geojson')
144 out = json.loads(capsys.readouterr().out)
145 assert out['type'] == 'FeatureCollection'
148 def test_reverse_language(self, cli_call, tmp_path, capsys):
149 result = cli_call('reverse', '--project-dir', str(tmp_path),
150 '--lat', '34', '--lon', '34', '--lang', 'fr')
154 out = json.loads(capsys.readouterr().out)
155 assert out['name'] == 'Nom'
158 class TestCliLookupCall:
160 @pytest.fixture(autouse=True)
161 def setup_lookup_mock(self, monkeypatch):
162 result = napi.SearchResult(napi.SourceTable.PLACEX, ('place', 'thing'),
163 napi.Point(1.0, -3.0),
164 names={'name':'Name', 'name:fr': 'Nom'},
165 extratags={'extra':'Extra'})
167 monkeypatch.setattr(napi.NominatimAPI, 'lookup',
168 lambda *args, **kwargs: napi.SearchResults([result]))
170 def test_lookup_simple(self, cli_call, tmp_path, capsys):
171 result = cli_call('lookup', '--project-dir', str(tmp_path),
176 out = json.loads(capsys.readouterr().out)
178 assert out[0]['name'] == 'Name'
179 assert 'address' not in out[0]
180 assert 'extratags' not in out[0]
181 assert 'namedetails' not in out[0]
184 class TestCliApiCommonParameters:
186 @pytest.fixture(autouse=True)
187 def setup_website_dir(self, cli_call, project_env):
188 self.cli_call = cli_call
189 self.project_dir = project_env.project_dir
190 (self.project_dir / 'website').mkdir()
193 def expect_param(self, param, expected):
194 (self.project_dir / 'website' / ('search.php')).write_text(f"""<?php
195 exit($_GET['{param}'] == '{expected}' ? 0 : 10);
199 def call_nominatim(self, *params):
200 return self.cli_call('search', '--query', 'somewhere',
201 '--project-dir', str(self.project_dir), *params)
204 def test_param_output(self):
205 self.expect_param('format', 'xml')
206 assert self.call_nominatim('--format', 'xml') == 0
209 def test_param_lang(self):
210 self.expect_param('accept-language', 'de')
211 assert self.call_nominatim('--lang', 'de') == 0
212 assert self.call_nominatim('--accept-language', 'de') == 0
215 @pytest.mark.parametrize("param", ('addressdetails', 'extratags', 'namedetails'))
216 def test_param_extradata(self, param):
217 self.expect_param(param, '1')
219 assert self.call_nominatim('--' + param) == 0
221 def test_param_polygon_output(self):
222 self.expect_param('polygon_geojson', '1')
224 assert self.call_nominatim('--polygon-output', 'geojson') == 0
227 def test_param_polygon_threshold(self):
228 self.expect_param('polygon_threshold', '0.3452')
230 assert self.call_nominatim('--polygon-threshold', '0.3452') == 0
233 def test_cli_search_param_bounded(cli_call, project_env):
234 webdir = project_env.project_dir / 'website'
236 (webdir / 'search.php').write_text(f"""<?php
237 exit($_GET['bounded'] == '1' ? 0 : 10);
240 assert cli_call('search', '--query', 'somewhere', '--project-dir', str(project_env.project_dir),
244 def test_cli_search_param_dedupe(cli_call, project_env):
245 webdir = project_env.project_dir / 'website'
247 (webdir / 'search.php').write_text(f"""<?php
248 exit($_GET['dedupe'] == '0' ? 0 : 10);
251 assert cli_call('search', '--query', 'somewhere', '--project-dir', str(project_env.project_dir),