]> 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) 2022 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 pytest
11
12 import nominatim.clicmd.api
13
14
15 @pytest.mark.parametrize("endpoint", (('search', 'reverse', 'lookup', 'details', 'status')))
16 def test_no_api_without_phpcgi(src_dir, endpoint):
17     with pytest.raises(SystemExit):
18         nominatim.cli.nominatim(module_dir='MODULE NOT AVAILABLE',
19                                 osm2pgsql_path='OSM2PGSQL NOT AVAILABLE',
20                                 phplib_dir=str(src_dir / 'lib-php'),
21                                 data_dir=str(src_dir / 'data'),
22                                 phpcgi_path=None,
23                                 sqllib_dir=str(src_dir / 'lib-sql'),
24                                 config_dir=str(src_dir / 'settings'),
25                                 cli_args=[endpoint])
26
27
28 @pytest.mark.parametrize("params", [('search', '--query', 'new'),
29                                     ('search', '--city', 'Berlin'),
30                                     ('reverse', '--lat', '0', '--lon', '0', '--zoom', '13'),
31                                     ('lookup', '--id', 'N1'),
32                                     ('details', '--node', '1'),
33                                     ('details', '--way', '1'),
34                                     ('details', '--relation', '1'),
35                                     ('details', '--place_id', '10001'),
36                                     ('status',)])
37 class TestCliApiCall:
38
39     @pytest.fixture(autouse=True)
40     def setup_cli_call(self, cli_call):
41         self.call_nominatim = cli_call
42
43     def test_api_commands_simple(self, mock_func_factory, params, tmp_path):
44         (tmp_path / 'website').mkdir()
45         (tmp_path / 'website' / (params[0] + '.php')).write_text('')
46         mock_run_api = mock_func_factory(nominatim.clicmd.api, 'run_api_script')
47
48         assert self.call_nominatim(*params, '--project-dir', str(tmp_path)) == 0
49
50         assert mock_run_api.called == 1
51         assert mock_run_api.last_args[0] == params[0]
52
53
54     def test_bad_project_idr(self, mock_func_factory, params):
55         mock_run_api = mock_func_factory(nominatim.clicmd.api, 'run_api_script')
56
57         assert self.call_nominatim(*params) == 1
58
59 QUERY_PARAMS = {
60  'search': ('--query', 'somewhere'),
61  'reverse': ('--lat', '20', '--lon', '30'),
62  'lookup': ('--id', 'R345345'),
63  'details': ('--node', '324')
64 }
65
66 @pytest.mark.parametrize("endpoint", (('search', 'reverse', 'lookup')))
67 class TestCliApiCommonParameters:
68
69     @pytest.fixture(autouse=True)
70     def setup_website_dir(self, cli_call, project_env, endpoint):
71         self.endpoint = endpoint
72         self.cli_call = cli_call
73         self.project_dir = project_env.project_dir
74         (self.project_dir / 'website').mkdir()
75
76
77     def expect_param(self, param, expected):
78         (self.project_dir / 'website' / (self.endpoint + '.php')).write_text(f"""<?php
79         exit($_GET['{param}']  == '{expected}' ? 0 : 10);
80         """)
81
82
83     def call_nominatim(self, *params):
84         return self.cli_call(self.endpoint, *QUERY_PARAMS[self.endpoint],
85                              '--project-dir', str(self.project_dir), *params)
86
87
88     def test_param_output(self):
89         self.expect_param('format', 'xml')
90         assert self.call_nominatim('--format', 'xml') == 0
91
92
93     def test_param_lang(self):
94         self.expect_param('accept-language', 'de')
95         assert self.call_nominatim('--lang', 'de') == 0
96         assert self.call_nominatim('--accept-language', 'de') == 0
97
98
99     @pytest.mark.parametrize("param", ('addressdetails', 'extratags', 'namedetails'))
100     def test_param_extradata(self, param):
101         self.expect_param(param, '1')
102
103         assert self.call_nominatim('--' + param) == 0
104
105     def test_param_polygon_output(self):
106         self.expect_param('polygon_geojson', '1')
107
108         assert self.call_nominatim('--polygon-output', 'geojson') == 0
109
110
111     def test_param_polygon_threshold(self):
112         self.expect_param('polygon_threshold', '0.3452')
113
114         assert self.call_nominatim('--polygon-threshold', '0.3452') == 0
115
116
117 def test_cli_search_param_bounded(cli_call, project_env):
118     webdir = project_env.project_dir / 'website'
119     webdir.mkdir()
120     (webdir / 'search.php').write_text(f"""<?php
121         exit($_GET['bounded']  == '1' ? 0 : 10);
122         """)
123
124     assert cli_call('search', *QUERY_PARAMS['search'], '--project-dir', str(project_env.project_dir),
125                     '--bounded') == 0
126
127
128 def test_cli_search_param_dedupe(cli_call, project_env):
129     webdir = project_env.project_dir / 'website'
130     webdir.mkdir()
131     (webdir / 'search.php').write_text(f"""<?php
132         exit($_GET['dedupe']  == '0' ? 0 : 10);
133         """)
134
135     assert cli_call('search', *QUERY_PARAMS['search'], '--project-dir', str(project_env.project_dir),
136                     '--no-dedupe') == 0
137
138
139 def test_cli_details_param_class(cli_call, project_env):
140     webdir = project_env.project_dir / 'website'
141     webdir.mkdir()
142     (webdir / 'details.php').write_text(f"""<?php
143         exit($_GET['class']  == 'highway' ? 0 : 10);
144         """)
145
146     assert cli_call('details', *QUERY_PARAMS['details'], '--project-dir', str(project_env.project_dir),
147                     '--class', 'highway') == 0
148
149
150 @pytest.mark.parametrize('param', ('lang', 'accept-language'))
151 def test_cli_details_param_lang(cli_call, project_env, param):
152     webdir = project_env.project_dir / 'website'
153     webdir.mkdir()
154     (webdir / 'details.php').write_text(f"""<?php
155         exit($_GET['accept-language']  == 'es' ? 0 : 10);
156         """)
157
158     assert cli_call('details', *QUERY_PARAMS['details'], '--project-dir', str(project_env.project_dir),
159                     '--' + param, 'es') == 0
160