]> git.openstreetmap.org Git - nominatim.git/blob - test/python/cli/test_cmd_api.py
switch API parameters to keyword arguments
[nominatim.git] / test / python / cli / test_cmd_api.py
1 # SPDX-License-Identifier: GPL-2.0-only
2 #
3 # This file is part of Nominatim. (https://nominatim.org)
4 #
5 # Copyright (C) 2023 by the Nominatim developer community.
6 # For a full list of authors see the git log.
7 """
8 Tests for API access commands of command-line interface wrapper.
9 """
10 import json
11 import pytest
12
13 import nominatim.clicmd.api
14 import nominatim.api as napi
15
16
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',
21                                    phpcgi_path=None,
22                                    cli_args=[endpoint]) == 1
23
24
25 @pytest.mark.parametrize("params", [('search', '--query', 'new'),
26                                     ('search', '--city', 'Berlin')])
27 class TestCliApiCallPhp:
28
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')
32
33         def _run():
34             return cli_call(*params, '--project-dir', str(tmp_path))
35
36         self.run_nominatim = _run
37
38
39     def test_api_commands_simple(self, tmp_path, params):
40         (tmp_path / 'website').mkdir()
41         (tmp_path / 'website' / (params[0] + '.php')).write_text('')
42
43         assert self.run_nominatim() == 0
44
45         assert self.mock_run_api.called == 1
46         assert self.mock_run_api.last_args[0] == params[0]
47
48
49     def test_bad_project_dir(self):
50         assert self.run_nominatim() == 1
51
52
53 class TestCliStatusCall:
54
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'))
59
60
61     def test_status_simple(self, cli_call, tmp_path):
62         result = cli_call('status', '--project-dir', str(tmp_path))
63
64         assert result == 0
65
66
67     def test_status_json_format(self, cli_call, tmp_path, capsys):
68         result = cli_call('status', '--project-dir', str(tmp_path),
69                           '--format', 'json')
70
71         assert result == 0
72
73         json.loads(capsys.readouterr().out)
74
75
76 class TestCliDetailsCall:
77
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))
82
83         monkeypatch.setattr(napi.NominatimAPI, 'details',
84                             lambda *args, **kwargs: result)
85
86     @pytest.mark.parametrize("params", [('--node', '1'),
87                                         ('--way', '1'),
88                                         ('--relation', '1'),
89                                         ('--place_id', '10001')])
90
91     def test_details_json_format(self, cli_call, tmp_path, capsys, params):
92         result = cli_call('details', '--project-dir', str(tmp_path), *params)
93
94         assert result == 0
95
96         json.loads(capsys.readouterr().out)
97
98
99 class TestCliReverseCall:
100
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'})
107
108         monkeypatch.setattr(napi.NominatimAPI, 'reverse',
109                             lambda *args, **kwargs: result)
110
111
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')
115
116         assert result == 0
117
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
123
124
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)
131
132         assert result == 0
133
134         out = json.loads(capsys.readouterr().out)
135         assert field in out
136
137
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')
141
142         assert result == 0
143
144         out = json.loads(capsys.readouterr().out)
145         assert out['type'] == 'FeatureCollection'
146
147
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')
151
152         assert result == 0
153
154         out = json.loads(capsys.readouterr().out)
155         assert out['name'] == 'Nom'
156
157
158 class TestCliLookupCall:
159
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'})
166
167         monkeypatch.setattr(napi.NominatimAPI, 'lookup',
168                             lambda *args, **kwargs: napi.SearchResults([result]))
169
170     def test_lookup_simple(self, cli_call, tmp_path, capsys):
171         result = cli_call('lookup', '--project-dir', str(tmp_path),
172                           '--id', 'N34')
173
174         assert result == 0
175
176         out = json.loads(capsys.readouterr().out)
177         assert len(out) == 1
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]
182
183
184 class TestCliApiCommonParameters:
185
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()
191
192
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);
196         """)
197
198
199     def call_nominatim(self, *params):
200         return self.cli_call('search', '--query', 'somewhere',
201                              '--project-dir', str(self.project_dir), *params)
202
203
204     def test_param_output(self):
205         self.expect_param('format', 'xml')
206         assert self.call_nominatim('--format', 'xml') == 0
207
208
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
213
214
215     @pytest.mark.parametrize("param", ('addressdetails', 'extratags', 'namedetails'))
216     def test_param_extradata(self, param):
217         self.expect_param(param, '1')
218
219         assert self.call_nominatim('--' + param) == 0
220
221     def test_param_polygon_output(self):
222         self.expect_param('polygon_geojson', '1')
223
224         assert self.call_nominatim('--polygon-output', 'geojson') == 0
225
226
227     def test_param_polygon_threshold(self):
228         self.expect_param('polygon_threshold', '0.3452')
229
230         assert self.call_nominatim('--polygon-threshold', '0.3452') == 0
231
232
233 def test_cli_search_param_bounded(cli_call, project_env):
234     webdir = project_env.project_dir / 'website'
235     webdir.mkdir()
236     (webdir / 'search.php').write_text(f"""<?php
237         exit($_GET['bounded']  == '1' ? 0 : 10);
238         """)
239
240     assert cli_call('search', '--query', 'somewhere', '--project-dir', str(project_env.project_dir),
241                     '--bounded') == 0
242
243
244 def test_cli_search_param_dedupe(cli_call, project_env):
245     webdir = project_env.project_dir / 'website'
246     webdir.mkdir()
247     (webdir / 'search.php').write_text(f"""<?php
248         exit($_GET['dedupe']  == '0' ? 0 : 10);
249         """)
250
251     assert cli_call('search', '--query', 'somewhere', '--project-dir', str(project_env.project_dir),
252                     '--no-dedupe') == 0