]> git.openstreetmap.org Git - nominatim.git/blob - test/python/cli/test_cmd_api.py
Merge pull request #3020 from lonvia/reverse-api
[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                                     ('lookup', '--id', 'N1')])
28 class TestCliApiCallPhp:
29
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')
33
34         def _run():
35             return cli_call(*params, '--project-dir', str(tmp_path))
36
37         self.run_nominatim = _run
38
39
40     def test_api_commands_simple(self, tmp_path, params):
41         (tmp_path / 'website').mkdir()
42         (tmp_path / 'website' / (params[0] + '.php')).write_text('')
43
44         assert self.run_nominatim() == 0
45
46         assert self.mock_run_api.called == 1
47         assert self.mock_run_api.last_args[0] == params[0]
48
49
50     def test_bad_project_dir(self):
51         assert self.run_nominatim() == 1
52
53
54 class TestCliStatusCall:
55
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'))
60
61
62     def test_status_simple(self, cli_call, tmp_path):
63         result = cli_call('status', '--project-dir', str(tmp_path))
64
65         assert result == 0
66
67
68     def test_status_json_format(self, cli_call, tmp_path, capsys):
69         result = cli_call('status', '--project-dir', str(tmp_path),
70                           '--format', 'json')
71
72         assert result == 0
73
74         json.loads(capsys.readouterr().out)
75
76
77 class TestCliDetailsCall:
78
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))
83
84         monkeypatch.setattr(napi.NominatimAPI, 'lookup',
85                             lambda *args: result)
86
87     @pytest.mark.parametrize("params", [('--node', '1'),
88                                         ('--way', '1'),
89                                         ('--relation', '1'),
90                                         ('--place_id', '10001')])
91
92     def test_details_json_format(self, cli_call, tmp_path, capsys, params):
93         result = cli_call('details', '--project-dir', str(tmp_path), *params)
94
95         assert result == 0
96
97         json.loads(capsys.readouterr().out)
98
99
100 class TestCliReverseCall:
101
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'})
108
109         monkeypatch.setattr(napi.NominatimAPI, 'reverse',
110                             lambda *args: result)
111
112
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')
116
117         assert result == 0
118
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
124
125
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)
132
133         assert result == 0
134
135         out = json.loads(capsys.readouterr().out)
136         assert field in out
137
138
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')
142
143         assert result == 0
144
145         out = json.loads(capsys.readouterr().out)
146         assert out['type'] == 'FeatureCollection'
147
148
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')
152
153         assert result == 0
154
155         out = json.loads(capsys.readouterr().out)
156         assert out['name'] == 'Nom'
157
158
159 QUERY_PARAMS = {
160  'search': ('--query', 'somewhere'),
161  'reverse': ('--lat', '20', '--lon', '30'),
162  'lookup': ('--id', 'R345345'),
163  'details': ('--node', '324')
164 }
165
166 @pytest.mark.parametrize("endpoint", (('search', 'lookup')))
167 class TestCliApiCommonParameters:
168
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()
175
176
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);
180         """)
181
182
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)
186
187
188     def test_param_output(self):
189         self.expect_param('format', 'xml')
190         assert self.call_nominatim('--format', 'xml') == 0
191
192
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
197
198
199     @pytest.mark.parametrize("param", ('addressdetails', 'extratags', 'namedetails'))
200     def test_param_extradata(self, param):
201         self.expect_param(param, '1')
202
203         assert self.call_nominatim('--' + param) == 0
204
205     def test_param_polygon_output(self):
206         self.expect_param('polygon_geojson', '1')
207
208         assert self.call_nominatim('--polygon-output', 'geojson') == 0
209
210
211     def test_param_polygon_threshold(self):
212         self.expect_param('polygon_threshold', '0.3452')
213
214         assert self.call_nominatim('--polygon-threshold', '0.3452') == 0
215
216
217 def test_cli_search_param_bounded(cli_call, project_env):
218     webdir = project_env.project_dir / 'website'
219     webdir.mkdir()
220     (webdir / 'search.php').write_text(f"""<?php
221         exit($_GET['bounded']  == '1' ? 0 : 10);
222         """)
223
224     assert cli_call('search', *QUERY_PARAMS['search'], '--project-dir', str(project_env.project_dir),
225                     '--bounded') == 0
226
227
228 def test_cli_search_param_dedupe(cli_call, project_env):
229     webdir = project_env.project_dir / 'website'
230     webdir.mkdir()
231     (webdir / 'search.php').write_text(f"""<?php
232         exit($_GET['dedupe']  == '0' ? 0 : 10);
233         """)
234
235     assert cli_call('search', *QUERY_PARAMS['search'], '--project-dir', str(project_env.project_dir),
236                     '--no-dedupe') == 0