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