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 ('lookup', '--id', 'N1')])
28 class TestCliApiCallPhp:
30 @pytest.fixture(autouse=True)
31 def setup_cli_call(self, params, cli_call, mock_func_factory, tmp_path):
32 self.mock_run_api = mock_func_factory(nominatim.clicmd.api, 'run_api_script')
35 return cli_call(*params, '--project-dir', str(tmp_path))
37 self.run_nominatim = _run
40 def test_api_commands_simple(self, tmp_path, params):
41 (tmp_path / 'website').mkdir()
42 (tmp_path / 'website' / (params[0] + '.php')).write_text('')
44 assert self.run_nominatim() == 0
46 assert self.mock_run_api.called == 1
47 assert self.mock_run_api.last_args[0] == params[0]
50 def test_bad_project_dir(self):
51 assert self.run_nominatim() == 1
54 class TestCliStatusCall:
56 @pytest.fixture(autouse=True)
57 def setup_status_mock(self, monkeypatch):
58 monkeypatch.setattr(napi.NominatimAPI, 'status',
59 lambda self: napi.StatusResult(200, 'OK'))
62 def test_status_simple(self, cli_call, tmp_path):
63 result = cli_call('status', '--project-dir', str(tmp_path))
68 def test_status_json_format(self, cli_call, tmp_path, capsys):
69 result = cli_call('status', '--project-dir', str(tmp_path),
74 json.loads(capsys.readouterr().out)
77 class TestCliDetailsCall:
79 @pytest.fixture(autouse=True)
80 def setup_status_mock(self, monkeypatch):
81 result = napi.DetailedResult(napi.SourceTable.PLACEX, ('place', 'thing'),
82 napi.Point(1.0, -3.0))
84 monkeypatch.setattr(napi.NominatimAPI, 'lookup',
87 @pytest.mark.parametrize("params", [('--node', '1'),
90 ('--place_id', '10001')])
92 def test_details_json_format(self, cli_call, tmp_path, capsys, params):
93 result = cli_call('details', '--project-dir', str(tmp_path), *params)
97 json.loads(capsys.readouterr().out)
100 class TestCliReverseCall:
102 @pytest.fixture(autouse=True)
103 def setup_reverse_mock(self, monkeypatch):
104 result = napi.ReverseResult(napi.SourceTable.PLACEX, ('place', 'thing'),
105 napi.Point(1.0, -3.0),
106 names={'name':'Name', 'name:fr': 'Nom'},
107 extratags={'extra':'Extra'})
109 monkeypatch.setattr(napi.NominatimAPI, 'reverse',
110 lambda *args: result)
113 def test_reverse_simple(self, cli_call, tmp_path, capsys):
114 result = cli_call('reverse', '--project-dir', str(tmp_path),
115 '--lat', '34', '--lon', '34')
119 out = json.loads(capsys.readouterr().out)
120 assert out['name'] == 'Name'
121 assert 'address' not in out
122 assert 'extratags' not in out
123 assert 'namedetails' not in out
126 @pytest.mark.parametrize('param,field', [('--addressdetails', 'address'),
127 ('--extratags', 'extratags'),
128 ('--namedetails', 'namedetails')])
129 def test_reverse_extra_stuff(self, cli_call, tmp_path, capsys, param, field):
130 result = cli_call('reverse', '--project-dir', str(tmp_path),
131 '--lat', '34', '--lon', '34', param)
135 out = json.loads(capsys.readouterr().out)
139 def test_reverse_format(self, cli_call, tmp_path, capsys):
140 result = cli_call('reverse', '--project-dir', str(tmp_path),
141 '--lat', '34', '--lon', '34', '--format', 'geojson')
145 out = json.loads(capsys.readouterr().out)
146 assert out['type'] == 'FeatureCollection'
149 def test_reverse_language(self, cli_call, tmp_path, capsys):
150 result = cli_call('reverse', '--project-dir', str(tmp_path),
151 '--lat', '34', '--lon', '34', '--lang', 'fr')
155 out = json.loads(capsys.readouterr().out)
156 assert out['name'] == 'Nom'
160 'search': ('--query', 'somewhere'),
161 'reverse': ('--lat', '20', '--lon', '30'),
162 'lookup': ('--id', 'R345345'),
163 'details': ('--node', '324')
166 @pytest.mark.parametrize("endpoint", (('search', 'lookup')))
167 class TestCliApiCommonParameters:
169 @pytest.fixture(autouse=True)
170 def setup_website_dir(self, cli_call, project_env, endpoint):
171 self.endpoint = endpoint
172 self.cli_call = cli_call
173 self.project_dir = project_env.project_dir
174 (self.project_dir / 'website').mkdir()
177 def expect_param(self, param, expected):
178 (self.project_dir / 'website' / (self.endpoint + '.php')).write_text(f"""<?php
179 exit($_GET['{param}'] == '{expected}' ? 0 : 10);
183 def call_nominatim(self, *params):
184 return self.cli_call(self.endpoint, *QUERY_PARAMS[self.endpoint],
185 '--project-dir', str(self.project_dir), *params)
188 def test_param_output(self):
189 self.expect_param('format', 'xml')
190 assert self.call_nominatim('--format', 'xml') == 0
193 def test_param_lang(self):
194 self.expect_param('accept-language', 'de')
195 assert self.call_nominatim('--lang', 'de') == 0
196 assert self.call_nominatim('--accept-language', 'de') == 0
199 @pytest.mark.parametrize("param", ('addressdetails', 'extratags', 'namedetails'))
200 def test_param_extradata(self, param):
201 self.expect_param(param, '1')
203 assert self.call_nominatim('--' + param) == 0
205 def test_param_polygon_output(self):
206 self.expect_param('polygon_geojson', '1')
208 assert self.call_nominatim('--polygon-output', 'geojson') == 0
211 def test_param_polygon_threshold(self):
212 self.expect_param('polygon_threshold', '0.3452')
214 assert self.call_nominatim('--polygon-threshold', '0.3452') == 0
217 def test_cli_search_param_bounded(cli_call, project_env):
218 webdir = project_env.project_dir / 'website'
220 (webdir / 'search.php').write_text(f"""<?php
221 exit($_GET['bounded'] == '1' ? 0 : 10);
224 assert cli_call('search', *QUERY_PARAMS['search'], '--project-dir', str(project_env.project_dir),
228 def test_cli_search_param_dedupe(cli_call, project_env):
229 webdir = project_env.project_dir / 'website'
231 (webdir / 'search.php').write_text(f"""<?php
232 exit($_GET['dedupe'] == '0' ? 0 : 10);
235 assert cli_call('search', *QUERY_PARAMS['search'], '--project-dir', str(project_env.project_dir),