1 # SPDX-License-Identifier: GPL-2.0-only
3 # This file is part of Nominatim. (https://nominatim.org)
5 # Copyright (C) 2022 by the Nominatim developer community.
6 # For a full list of authors see the git log.
8 Tests for API access commands of command-line interface wrapper.
13 import nominatim.clicmd.api
15 from nominatim.apicmd.status import StatusResult
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',
23 cli_args=[endpoint]) == 1
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:
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')
41 return cli_call(*params, '--project-dir', str(tmp_path))
43 self.run_nominatim = _run
46 def test_api_commands_simple(self, tmp_path, params):
47 (tmp_path / 'website').mkdir()
48 (tmp_path / 'website' / (params[0] + '.php')).write_text('')
50 assert self.run_nominatim() == 0
52 assert self.mock_run_api.called == 1
53 assert self.mock_run_api.last_args[0] == params[0]
56 def test_bad_project_dir(self):
57 assert self.run_nominatim() == 1
60 class TestCliStatusCall:
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'))
68 def test_status_simple(self, cli_call, tmp_path):
69 result = cli_call('status', '--project-dir', str(tmp_path))
74 def test_status_json_format(self, cli_call, tmp_path, capsys):
75 result = cli_call('status', '--project-dir', str(tmp_path),
80 json.loads(capsys.readouterr().out)
84 'search': ('--query', 'somewhere'),
85 'reverse': ('--lat', '20', '--lon', '30'),
86 'lookup': ('--id', 'R345345'),
87 'details': ('--node', '324')
90 @pytest.mark.parametrize("endpoint", (('search', 'reverse', 'lookup')))
91 class TestCliApiCommonParameters:
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()
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);
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)
112 def test_param_output(self):
113 self.expect_param('format', 'xml')
114 assert self.call_nominatim('--format', 'xml') == 0
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
123 @pytest.mark.parametrize("param", ('addressdetails', 'extratags', 'namedetails'))
124 def test_param_extradata(self, param):
125 self.expect_param(param, '1')
127 assert self.call_nominatim('--' + param) == 0
129 def test_param_polygon_output(self):
130 self.expect_param('polygon_geojson', '1')
132 assert self.call_nominatim('--polygon-output', 'geojson') == 0
135 def test_param_polygon_threshold(self):
136 self.expect_param('polygon_threshold', '0.3452')
138 assert self.call_nominatim('--polygon-threshold', '0.3452') == 0
141 def test_cli_search_param_bounded(cli_call, project_env):
142 webdir = project_env.project_dir / 'website'
144 (webdir / 'search.php').write_text(f"""<?php
145 exit($_GET['bounded'] == '1' ? 0 : 10);
148 assert cli_call('search', *QUERY_PARAMS['search'], '--project-dir', str(project_env.project_dir),
152 def test_cli_search_param_dedupe(cli_call, project_env):
153 webdir = project_env.project_dir / 'website'
155 (webdir / 'search.php').write_text(f"""<?php
156 exit($_GET['dedupe'] == '0' ? 0 : 10);
159 assert cli_call('search', *QUERY_PARAMS['search'], '--project-dir', str(project_env.project_dir),
163 def test_cli_details_param_class(cli_call, project_env):
164 webdir = project_env.project_dir / 'website'
166 (webdir / 'details.php').write_text(f"""<?php
167 exit($_GET['class'] == 'highway' ? 0 : 10);
170 assert cli_call('details', *QUERY_PARAMS['details'], '--project-dir', str(project_env.project_dir),
171 '--class', 'highway') == 0
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'
178 (webdir / 'details.php').write_text(f"""<?php
179 exit($_GET['accept-language'] == 'es' ? 0 : 10);
182 assert cli_call('details', *QUERY_PARAMS['details'], '--project-dir', str(project_env.project_dir),
183 '--' + param, 'es') == 0