]> git.openstreetmap.org Git - nominatim.git/blob - test/python/cli/test_cli.py
Merge pull request #2937 from lonvia/python-server-stub
[nominatim.git] / test / python / cli / test_cli.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 command line interface wrapper.
9
10 These tests just check that the various command line parameters route to the
11 correct functionionality. They use a lot of monkeypatching to avoid executing
12 the actual functions.
13 """
14 import importlib
15 import pytest
16
17 import nominatim.indexer.indexer
18 import nominatim.tools.add_osm_data
19 import nominatim.tools.freeze
20
21
22 def test_cli_help(cli_call, capsys):
23     """ Running nominatim tool without arguments prints help.
24     """
25     assert cli_call() == 1
26
27     captured = capsys.readouterr()
28     assert captured.out.startswith('usage:')
29
30 def test_cli_version(cli_call, capsys):
31     """ Running nominatim tool --version prints a version string.
32     """
33     assert cli_call('--version') == 0
34
35     captured = capsys.readouterr()
36     assert captured.out.startswith('Nominatim version')
37
38 @pytest.mark.parametrize("name,oid", [('file', 'foo.osm'), ('diff', 'foo.osc')])
39 def test_cli_add_data_file_command(cli_call, mock_func_factory, name, oid):
40     mock_run_legacy = mock_func_factory(nominatim.tools.add_osm_data, 'add_data_from_file')
41     assert cli_call('add-data', '--' + name, str(oid)) == 0
42
43     assert mock_run_legacy.called == 1
44
45
46 @pytest.mark.parametrize("name,oid", [('node', 12), ('way', 8), ('relation', 32)])
47 def test_cli_add_data_object_command(cli_call, mock_func_factory, name, oid):
48     mock_run_legacy = mock_func_factory(nominatim.tools.add_osm_data, 'add_osm_object')
49     assert cli_call('add-data', '--' + name, str(oid)) == 0
50
51     assert mock_run_legacy.called == 1
52
53
54
55 def test_cli_add_data_tiger_data(cli_call, cli_tokenizer_mock, mock_func_factory):
56     mock = mock_func_factory(nominatim.tools.tiger_data, 'add_tiger_data')
57
58     assert cli_call('add-data', '--tiger-data', 'somewhere') == 0
59
60     assert mock.called == 1
61
62
63 def test_cli_serve_php(cli_call, mock_func_factory):
64     func = mock_func_factory(nominatim.cli, 'run_php_server')
65
66     cli_call('serve') == 0
67
68     assert func.called == 1
69
70
71 def test_cli_serve_sanic(cli_call, mock_func_factory):
72     mod = pytest.importorskip("sanic")
73     func = mock_func_factory(mod.Sanic, "run")
74
75     cli_call('serve', '--engine', 'sanic') == 0
76
77     assert func.called == 1
78
79
80 def test_cli_serve_starlette_custom_server(cli_call, mock_func_factory):
81     pytest.importorskip("starlette")
82     mod = pytest.importorskip("uvicorn")
83     func = mock_func_factory(mod, "run")
84
85     cli_call('serve', '--engine', 'starlette', '--server', 'foobar:4545') == 0
86
87     assert func.called == 1
88     assert func.last_kwargs['host'] == 'foobar'
89     assert func.last_kwargs['port'] == 4545
90
91
92 def test_cli_serve_starlette_custom_server_bad_port(cli_call, mock_func_factory):
93     pytest.importorskip("starlette")
94     mod = pytest.importorskip("uvicorn")
95     func = mock_func_factory(mod, "run")
96
97     cli_call('serve', '--engine', 'starlette', '--server', 'foobar:45:45') == 1
98
99
100 @pytest.mark.parametrize("engine", ['falcon', 'starlette'])
101 def test_cli_serve_uvicorn_based(cli_call, engine, mock_func_factory):
102     pytest.importorskip(engine)
103     mod = pytest.importorskip("uvicorn")
104     func = mock_func_factory(mod, "run")
105
106     cli_call('serve', '--engine', engine) == 0
107
108     assert func.called == 1
109     assert func.last_kwargs['host'] == '127.0.0.1'
110     assert func.last_kwargs['port'] == 8088
111
112 def test_cli_export_command(cli_call, mock_run_legacy):
113     assert cli_call('export', '--output-all-postcodes') == 0
114
115     assert mock_run_legacy.called == 1
116     assert mock_run_legacy.last_args[0] == 'export.php'
117
118
119 @pytest.mark.parametrize("param,value", [('output-type', 'country'),
120                                          ('output-format', 'street;city'),
121                                          ('language', 'xf'),
122                                          ('restrict-to-country', 'us'),
123                                          ('restrict-to-osm-node', '536'),
124                                          ('restrict-to-osm-way', '727'),
125                                          ('restrict-to-osm-relation', '197532')
126                                         ])
127 def test_export_parameters(src_dir, tmp_path, param, value, monkeypatch):
128     (tmp_path / 'admin').mkdir()
129     (tmp_path / 'admin' / 'export.php').write_text(f"""<?php
130         exit(strpos(implode(' ', $_SERVER['argv']), '--{param} {value}') >= 0 ? 0 : 10);
131         """)
132
133     monkeypatch.setattr(nominatim.paths, 'PHPLIB_DIR', tmp_path)
134
135     assert nominatim.cli.nominatim(module_dir='MODULE NOT AVAILABLE',
136                                    osm2pgsql_path='OSM2PGSQL NOT AVAILABLE',
137                                    phpcgi_path='/usr/bin/php-cgi',
138                                    cli_args=['export', '--' + param, value]) == 0
139
140
141
142 class TestCliWithDb:
143
144     @pytest.fixture(autouse=True)
145     def setup_cli_call(self, cli_call, temp_db, cli_tokenizer_mock):
146         self.call_nominatim = cli_call
147         self.tokenizer_mock = cli_tokenizer_mock
148
149
150     def test_freeze_command(self, mock_func_factory):
151         mock_drop = mock_func_factory(nominatim.tools.freeze, 'drop_update_tables')
152         mock_flatnode = mock_func_factory(nominatim.tools.freeze, 'drop_flatnode_file')
153
154         assert self.call_nominatim('freeze') == 0
155
156         assert mock_drop.called == 1
157         assert mock_flatnode.called == 1
158
159
160     @pytest.mark.parametrize("params,do_bnds,do_ranks", [
161                               ([], 1, 1),
162                               (['--boundaries-only'], 1, 0),
163                               (['--no-boundaries'], 0, 1),
164                               (['--boundaries-only', '--no-boundaries'], 0, 0)])
165     def test_index_command(self, mock_func_factory, table_factory,
166                            params, do_bnds, do_ranks):
167         table_factory('import_status', 'indexed bool')
168         bnd_mock = mock_func_factory(nominatim.indexer.indexer.Indexer, 'index_boundaries')
169         rank_mock = mock_func_factory(nominatim.indexer.indexer.Indexer, 'index_by_rank')
170
171         assert self.call_nominatim('index', *params) == 0
172
173         assert bnd_mock.called == do_bnds
174         assert rank_mock.called == do_ranks
175
176
177     def test_special_phrases_wiki_command(self, mock_func_factory):
178         func = mock_func_factory(nominatim.clicmd.special_phrases.SPImporter, 'import_phrases')
179
180         self.call_nominatim('special-phrases', '--import-from-wiki', '--no-replace')
181
182         assert func.called == 1
183
184
185     def test_special_phrases_csv_command(self, src_dir, mock_func_factory):
186         func = mock_func_factory(nominatim.clicmd.special_phrases.SPImporter, 'import_phrases')
187         testdata = src_dir / 'test' / 'testdb'
188         csv_path = str((testdata / 'full_en_phrases_test.csv').resolve())
189
190         self.call_nominatim('special-phrases', '--import-from-csv', csv_path)
191
192         assert func.called == 1
193
194
195     def test_special_phrases_csv_bad_file(self, src_dir):
196         testdata = src_dir / 'something349053905.csv'
197
198         self.call_nominatim('special-phrases', '--import-from-csv',
199                             str(testdata.resolve())) == 1