1 # SPDX-License-Identifier: GPL-2.0-only
3 # This file is part of Nominatim. (https://nominatim.org)
5 # Copyright (C) 2023 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
14 import nominatim.api as napi
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',
22 cli_args=[endpoint]) == 1
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:
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')
36 return cli_call(*params, '--project-dir', str(tmp_path))
38 self.run_nominatim = _run
41 def test_api_commands_simple(self, tmp_path, params):
42 (tmp_path / 'website').mkdir()
43 (tmp_path / 'website' / (params[0] + '.php')).write_text('')
45 assert self.run_nominatim() == 0
47 assert self.mock_run_api.called == 1
48 assert self.mock_run_api.last_args[0] == params[0]
51 def test_bad_project_dir(self):
52 assert self.run_nominatim() == 1
55 class TestCliStatusCall:
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'))
63 def test_status_simple(self, cli_call, tmp_path):
64 result = cli_call('status', '--project-dir', str(tmp_path))
69 def test_status_json_format(self, cli_call, tmp_path, capsys):
70 result = cli_call('status', '--project-dir', str(tmp_path),
75 json.loads(capsys.readouterr().out)
78 class TestCliDetailsCall:
80 @pytest.fixture(autouse=True)
81 def setup_status_mock(self, monkeypatch):
82 result = napi.SearchResult(napi.SourceTable.PLACEX, ('place', 'thing'),
85 monkeypatch.setattr(napi.NominatimAPI, 'lookup',
88 @pytest.mark.parametrize("params", [('--node', '1'),
91 ('--place_id', '10001')])
93 def test_status_json_format(self, cli_call, tmp_path, capsys, params):
94 result = cli_call('details', '--project-dir', str(tmp_path), *params)
98 json.loads(capsys.readouterr().out)
102 'search': ('--query', 'somewhere'),
103 'reverse': ('--lat', '20', '--lon', '30'),
104 'lookup': ('--id', 'R345345'),
105 'details': ('--node', '324')
108 @pytest.mark.parametrize("endpoint", (('search', 'reverse', 'lookup')))
109 class TestCliApiCommonParameters:
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()
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);
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)
130 def test_param_output(self):
131 self.expect_param('format', 'xml')
132 assert self.call_nominatim('--format', 'xml') == 0
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
141 @pytest.mark.parametrize("param", ('addressdetails', 'extratags', 'namedetails'))
142 def test_param_extradata(self, param):
143 self.expect_param(param, '1')
145 assert self.call_nominatim('--' + param) == 0
147 def test_param_polygon_output(self):
148 self.expect_param('polygon_geojson', '1')
150 assert self.call_nominatim('--polygon-output', 'geojson') == 0
153 def test_param_polygon_threshold(self):
154 self.expect_param('polygon_threshold', '0.3452')
156 assert self.call_nominatim('--polygon-threshold', '0.3452') == 0
159 def test_cli_search_param_bounded(cli_call, project_env):
160 webdir = project_env.project_dir / 'website'
162 (webdir / 'search.php').write_text(f"""<?php
163 exit($_GET['bounded'] == '1' ? 0 : 10);
166 assert cli_call('search', *QUERY_PARAMS['search'], '--project-dir', str(project_env.project_dir),
170 def test_cli_search_param_dedupe(cli_call, project_env):
171 webdir = project_env.project_dir / 'website'
173 (webdir / 'search.php').write_text(f"""<?php
174 exit($_GET['dedupe'] == '0' ? 0 : 10);
177 assert cli_call('search', *QUERY_PARAMS['search'], '--project-dir', str(project_env.project_dir),