]> git.openstreetmap.org Git - nominatim.git/blob - test/python/cli/test_cmd_api.py
add event handler for default connection settings
[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) 2022 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
15 from nominatim.apicmd.status import StatusResult
16
17
18 @pytest.mark.parametrize("endpoint", (('search', 'reverse', 'lookup', 'details', 'status')))
19 def test_no_api_without_phpcgi(endpoint):
20     assert nominatim.cli.nominatim(module_dir='MODULE NOT AVAILABLE',
21                                    osm2pgsql_path='OSM2PGSQL NOT AVAILABLE',
22                                    phpcgi_path=None,
23                                    cli_args=[endpoint]) == 1
24
25
26 @pytest.mark.parametrize("params", [('search', '--query', 'new'),
27                                     ('search', '--city', 'Berlin'),
28                                     ('reverse', '--lat', '0', '--lon', '0', '--zoom', '13'),
29                                     ('lookup', '--id', 'N1'),
30                                     ('details', '--node', '1'),
31                                     ('details', '--way', '1'),
32                                     ('details', '--relation', '1'),
33                                     ('details', '--place_id', '10001')])
34 class TestCliApiCallPhp:
35
36     @pytest.fixture(autouse=True)
37     def setup_cli_call(self, params, cli_call, mock_func_factory, tmp_path):
38         self.mock_run_api = mock_func_factory(nominatim.clicmd.api, 'run_api_script')
39
40         def _run():
41             return cli_call(*params, '--project-dir', str(tmp_path))
42
43         self.run_nominatim = _run
44
45
46     def test_api_commands_simple(self, tmp_path, params):
47         (tmp_path / 'website').mkdir()
48         (tmp_path / 'website' / (params[0] + '.php')).write_text('')
49
50         assert self.run_nominatim() == 0
51
52         assert self.mock_run_api.called == 1
53         assert self.mock_run_api.last_args[0] == params[0]
54
55
56     def test_bad_project_dir(self):
57         assert self.run_nominatim() == 1
58
59
60 class TestCliStatusCall:
61
62     @pytest.fixture(autouse=True)
63     def setup_status_mock(self, monkeypatch):
64         monkeypatch.setattr(nominatim.api.NominatimAPI, 'status',
65                             lambda self: StatusResult(200, 'OK'))
66
67
68     def test_status_simple(self, cli_call, tmp_path):
69         result = cli_call('status', '--project-dir', str(tmp_path))
70
71         assert result == 0
72
73
74     def test_status_json_format(self, cli_call, tmp_path, capsys):
75         result = cli_call('status', '--project-dir', str(tmp_path),
76                           '--format', 'json')
77
78         assert result == 0
79
80         json.loads(capsys.readouterr().out)
81
82
83 QUERY_PARAMS = {
84  'search': ('--query', 'somewhere'),
85  'reverse': ('--lat', '20', '--lon', '30'),
86  'lookup': ('--id', 'R345345'),
87  'details': ('--node', '324')
88 }
89
90 @pytest.mark.parametrize("endpoint", (('search', 'reverse', 'lookup')))
91 class TestCliApiCommonParameters:
92
93     @pytest.fixture(autouse=True)
94     def setup_website_dir(self, cli_call, project_env, endpoint):
95         self.endpoint = endpoint
96         self.cli_call = cli_call
97         self.project_dir = project_env.project_dir
98         (self.project_dir / 'website').mkdir()
99
100
101     def expect_param(self, param, expected):
102         (self.project_dir / 'website' / (self.endpoint + '.php')).write_text(f"""<?php
103         exit($_GET['{param}']  == '{expected}' ? 0 : 10);
104         """)
105
106
107     def call_nominatim(self, *params):
108         return self.cli_call(self.endpoint, *QUERY_PARAMS[self.endpoint],
109                              '--project-dir', str(self.project_dir), *params)
110
111
112     def test_param_output(self):
113         self.expect_param('format', 'xml')
114         assert self.call_nominatim('--format', 'xml') == 0
115
116
117     def test_param_lang(self):
118         self.expect_param('accept-language', 'de')
119         assert self.call_nominatim('--lang', 'de') == 0
120         assert self.call_nominatim('--accept-language', 'de') == 0
121
122
123     @pytest.mark.parametrize("param", ('addressdetails', 'extratags', 'namedetails'))
124     def test_param_extradata(self, param):
125         self.expect_param(param, '1')
126
127         assert self.call_nominatim('--' + param) == 0
128
129     def test_param_polygon_output(self):
130         self.expect_param('polygon_geojson', '1')
131
132         assert self.call_nominatim('--polygon-output', 'geojson') == 0
133
134
135     def test_param_polygon_threshold(self):
136         self.expect_param('polygon_threshold', '0.3452')
137
138         assert self.call_nominatim('--polygon-threshold', '0.3452') == 0
139
140
141 def test_cli_search_param_bounded(cli_call, project_env):
142     webdir = project_env.project_dir / 'website'
143     webdir.mkdir()
144     (webdir / 'search.php').write_text(f"""<?php
145         exit($_GET['bounded']  == '1' ? 0 : 10);
146         """)
147
148     assert cli_call('search', *QUERY_PARAMS['search'], '--project-dir', str(project_env.project_dir),
149                     '--bounded') == 0
150
151
152 def test_cli_search_param_dedupe(cli_call, project_env):
153     webdir = project_env.project_dir / 'website'
154     webdir.mkdir()
155     (webdir / 'search.php').write_text(f"""<?php
156         exit($_GET['dedupe']  == '0' ? 0 : 10);
157         """)
158
159     assert cli_call('search', *QUERY_PARAMS['search'], '--project-dir', str(project_env.project_dir),
160                     '--no-dedupe') == 0
161
162
163 def test_cli_details_param_class(cli_call, project_env):
164     webdir = project_env.project_dir / 'website'
165     webdir.mkdir()
166     (webdir / 'details.php').write_text(f"""<?php
167         exit($_GET['class']  == 'highway' ? 0 : 10);
168         """)
169
170     assert cli_call('details', *QUERY_PARAMS['details'], '--project-dir', str(project_env.project_dir),
171                     '--class', 'highway') == 0
172
173
174 @pytest.mark.parametrize('param', ('lang', 'accept-language'))
175 def test_cli_details_param_lang(cli_call, project_env, param):
176     webdir = project_env.project_dir / 'website'
177     webdir.mkdir()
178     (webdir / 'details.php').write_text(f"""<?php
179         exit($_GET['accept-language']  == 'es' ? 0 : 10);
180         """)
181
182     assert cli_call('details', *QUERY_PARAMS['details'], '--project-dir', str(project_env.project_dir),
183                     '--' + param, 'es') == 0
184