]> git.openstreetmap.org Git - nominatim.git/blob - test/python/cli/test_cmd_api.py
Merge pull request #2542 from lonvia/update-phpunit
[nominatim.git] / test / python / cli / test_cmd_api.py
1 """
2 Tests for API access commands of command-line interface wrapper.
3 """
4 import pytest
5
6 import nominatim.clicmd.api
7
8
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'),
16                                 phpcgi_path=None,
17                                 sqllib_dir=str(src_dir / 'lib-sql'),
18                                 config_dir=str(src_dir / 'settings'),
19                                 cli_args=[endpoint])
20
21
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'),
30                                     ('status',)])
31 class TestCliApiCall:
32
33     @pytest.fixture(autouse=True)
34     def setup_cli_call(self, cli_call):
35         self.call_nominatim = cli_call
36
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')
41
42         assert self.call_nominatim(*params, '--project-dir', str(tmp_path)) == 0
43
44         assert mock_run_api.called == 1
45         assert mock_run_api.last_args[0] == params[0]
46
47
48     def test_bad_project_idr(self, mock_func_factory, params):
49         mock_run_api = mock_func_factory(nominatim.clicmd.api, 'run_api_script')
50
51         assert self.call_nominatim(*params) == 1
52
53 QUERY_PARAMS = {
54  'search': ('--query', 'somewhere'),
55  'reverse': ('--lat', '20', '--lon', '30'),
56  'lookup': ('--id', 'R345345'),
57  'details': ('--node', '324')
58 }
59
60 @pytest.mark.parametrize("endpoint", (('search', 'reverse', 'lookup')))
61 class TestCliApiCommonParameters:
62
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()
69
70
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);
74         """)
75
76
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)
80
81
82     def test_param_output(self):
83         self.expect_param('format', 'xml')
84         assert self.call_nominatim('--format', 'xml') == 0
85
86
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
91
92
93     @pytest.mark.parametrize("param", ('addressdetails', 'extratags', 'namedetails'))
94     def test_param_extradata(self, param):
95         self.expect_param(param, '1')
96
97         assert self.call_nominatim('--' + param) == 0
98
99     def test_param_polygon_output(self):
100         self.expect_param('polygon_geojson', '1')
101
102         assert self.call_nominatim('--polygon-output', 'geojson') == 0
103
104
105     def test_param_polygon_threshold(self):
106         self.expect_param('polygon_threshold', '0.3452')
107
108         assert self.call_nominatim('--polygon-threshold', '0.3452') == 0
109
110
111 def test_cli_search_param_bounded(cli_call, project_env):
112     webdir = project_env.project_dir / 'website'
113     webdir.mkdir()
114     (webdir / 'search.php').write_text(f"""<?php
115         exit($_GET['bounded']  == '1' ? 0 : 10);
116         """)
117
118     assert cli_call('search', *QUERY_PARAMS['search'], '--project-dir', str(project_env.project_dir),
119                     '--bounded') == 0
120
121
122 def test_cli_search_param_dedupe(cli_call, project_env):
123     webdir = project_env.project_dir / 'website'
124     webdir.mkdir()
125     (webdir / 'search.php').write_text(f"""<?php
126         exit($_GET['dedupe']  == '0' ? 0 : 10);
127         """)
128
129     assert cli_call('search', *QUERY_PARAMS['search'], '--project-dir', str(project_env.project_dir),
130                     '--no-dedupe') == 0
131
132
133 def test_cli_details_param_class(cli_call, project_env):
134     webdir = project_env.project_dir / 'website'
135     webdir.mkdir()
136     (webdir / 'details.php').write_text(f"""<?php
137         exit($_GET['class']  == 'highway' ? 0 : 10);
138         """)
139
140     assert cli_call('details', *QUERY_PARAMS['details'], '--project-dir', str(project_env.project_dir),
141                     '--class', 'highway') == 0
142
143
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'
147     webdir.mkdir()
148     (webdir / 'details.php').write_text(f"""<?php
149         exit($_GET['accept-language']  == 'es' ? 0 : 10);
150         """)
151
152     assert cli_call('details', *QUERY_PARAMS['details'], '--project-dir', str(project_env.project_dir),
153                     '--' + param, 'es') == 0
154