2 Tests for API access commands of command-line interface wrapper.
6 import nominatim.clicmd.api
9 @pytest.mark.parametrize("endpoint", (('search', 'reverse', 'lookup', 'details', 'status')))
10 def test_no_api_without_phpcgi(src_dir, endpoint):
11 with pytest.raises(SystemExit):
12 nominatim.cli.nominatim(module_dir='MODULE NOT AVAILABLE',
13 osm2pgsql_path='OSM2PGSQL NOT AVAILABLE',
14 phplib_dir=str(src_dir / 'lib-php'),
15 data_dir=str(src_dir / 'data'),
17 sqllib_dir=str(src_dir / 'lib-sql'),
18 config_dir=str(src_dir / 'settings'),
22 @pytest.mark.parametrize("params", [('search', '--query', 'new'),
23 ('search', '--city', 'Berlin'),
24 ('reverse', '--lat', '0', '--lon', '0', '--zoom', '13'),
25 ('lookup', '--id', 'N1'),
26 ('details', '--node', '1'),
27 ('details', '--way', '1'),
28 ('details', '--relation', '1'),
29 ('details', '--place_id', '10001'),
33 @pytest.fixture(autouse=True)
34 def setup_cli_call(self, cli_call):
35 self.call_nominatim = cli_call
37 def test_api_commands_simple(self, mock_func_factory, params, tmp_path):
38 (tmp_path / 'website').mkdir()
39 (tmp_path / 'website' / (params[0] + '.php')).write_text('')
40 mock_run_api = mock_func_factory(nominatim.clicmd.api, 'run_api_script')
42 assert self.call_nominatim(*params, '--project-dir', str(tmp_path)) == 0
44 assert mock_run_api.called == 1
45 assert mock_run_api.last_args[0] == params[0]
48 def test_bad_project_idr(self, mock_func_factory, params):
49 mock_run_api = mock_func_factory(nominatim.clicmd.api, 'run_api_script')
51 assert self.call_nominatim(*params) == 1
54 'search': ('--query', 'somewhere'),
55 'reverse': ('--lat', '20', '--lon', '30'),
56 'lookup': ('--id', 'R345345'),
57 'details': ('--node', '324')
60 @pytest.mark.parametrize("endpoint", (('search', 'reverse', 'lookup')))
61 class TestCliApiCommonParameters:
63 @pytest.fixture(autouse=True)
64 def setup_website_dir(self, cli_call, project_env, endpoint):
65 self.endpoint = endpoint
66 self.cli_call = cli_call
67 self.project_dir = project_env.project_dir
68 (self.project_dir / 'website').mkdir()
71 def expect_param(self, param, expected):
72 (self.project_dir / 'website' / (self.endpoint + '.php')).write_text(f"""<?php
73 exit($_GET['{param}'] == '{expected}' ? 0 : 10);
77 def call_nominatim(self, *params):
78 return self.cli_call(self.endpoint, *QUERY_PARAMS[self.endpoint],
79 '--project-dir', str(self.project_dir), *params)
82 def test_param_output(self):
83 self.expect_param('format', 'xml')
84 assert self.call_nominatim('--format', 'xml') == 0
87 def test_param_lang(self):
88 self.expect_param('accept-language', 'de')
89 assert self.call_nominatim('--lang', 'de') == 0
90 assert self.call_nominatim('--accept-language', 'de') == 0
93 @pytest.mark.parametrize("param", ('addressdetails', 'extratags', 'namedetails'))
94 def test_param_extradata(self, param):
95 self.expect_param(param, '1')
97 assert self.call_nominatim('--' + param) == 0
99 def test_param_polygon_output(self):
100 self.expect_param('polygon_geojson', '1')
102 assert self.call_nominatim('--polygon-output', 'geojson') == 0
105 def test_param_polygon_threshold(self):
106 self.expect_param('polygon_threshold', '0.3452')
108 assert self.call_nominatim('--polygon-threshold', '0.3452') == 0
111 def test_cli_search_param_bounded(cli_call, project_env):
112 webdir = project_env.project_dir / 'website'
114 (webdir / 'search.php').write_text(f"""<?php
115 exit($_GET['bounded'] == '1' ? 0 : 10);
118 assert cli_call('search', *QUERY_PARAMS['search'], '--project-dir', str(project_env.project_dir),
122 def test_cli_search_param_dedupe(cli_call, project_env):
123 webdir = project_env.project_dir / 'website'
125 (webdir / 'search.php').write_text(f"""<?php
126 exit($_GET['dedupe'] == '0' ? 0 : 10);
129 assert cli_call('search', *QUERY_PARAMS['search'], '--project-dir', str(project_env.project_dir),
133 def test_cli_details_param_class(cli_call, project_env):
134 webdir = project_env.project_dir / 'website'
136 (webdir / 'details.php').write_text(f"""<?php
137 exit($_GET['class'] == 'highway' ? 0 : 10);
140 assert cli_call('details', *QUERY_PARAMS['details'], '--project-dir', str(project_env.project_dir),
141 '--class', 'highway') == 0
144 @pytest.mark.parametrize('param', ('lang', 'accept-language'))
145 def test_cli_details_param_lang(cli_call, project_env, param):
146 webdir = project_env.project_dir / 'website'
148 (webdir / 'details.php').write_text(f"""<?php
149 exit($_GET['accept-language'] == 'es' ? 0 : 10);
152 assert cli_call('details', *QUERY_PARAMS['details'], '--project-dir', str(project_env.project_dir),
153 '--' + param, 'es') == 0