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