]> git.openstreetmap.org Git - nominatim.git/blob - test/python/cli/test_cli.py
split cli tests by subcommand and extend coverage
[nominatim.git] / test / python / cli / test_cli.py
1 """
2 Tests for command line interface wrapper.
3
4 These tests just check that the various command line parameters route to the
5 correct functionionality. They use a lot of monkeypatching to avoid executing
6 the actual functions.
7 """
8 import pytest
9
10 import nominatim.indexer.indexer
11 import nominatim.tools.add_osm_data
12 import nominatim.tools.freeze
13
14
15 def test_cli_help(cli_call, capsys):
16     """ Running nominatim tool without arguments prints help.
17     """
18     assert cli_call() == 1
19
20     captured = capsys.readouterr()
21     assert captured.out.startswith('usage:')
22
23
24 @pytest.mark.parametrize("name,oid", [('file', 'foo.osm'), ('diff', 'foo.osc')])
25 def test_cli_add_data_file_command(cli_call, mock_func_factory, name, oid):
26     mock_run_legacy = mock_func_factory(nominatim.tools.add_osm_data, 'add_data_from_file')
27     assert cli_call('add-data', '--' + name, str(oid)) == 0
28
29     assert mock_run_legacy.called == 1
30
31
32 @pytest.mark.parametrize("name,oid", [('node', 12), ('way', 8), ('relation', 32)])
33 def test_cli_add_data_object_command(cli_call, mock_func_factory, name, oid):
34     mock_run_legacy = mock_func_factory(nominatim.tools.add_osm_data, 'add_osm_object')
35     assert cli_call('add-data', '--' + name, str(oid)) == 0
36
37     assert mock_run_legacy.called == 1
38
39
40
41 def test_cli_add_data_tiger_data(cli_call, cli_tokenizer_mock, mock_func_factory):
42     mock = mock_func_factory(nominatim.tools.tiger_data, 'add_tiger_data')
43
44     assert cli_call('add-data', '--tiger-data', 'somewhere') == 0
45
46     assert mock.called == 1
47
48
49 def test_cli_serve_command(cli_call, mock_func_factory):
50     func = mock_func_factory(nominatim.cli, 'run_php_server')
51
52     cli_call('serve') == 0
53
54     assert func.called == 1
55
56
57 def test_cli_export_command(cli_call, mock_run_legacy):
58     assert cli_call('export', '--output-all-postcodes') == 0
59
60     assert mock_run_legacy.called == 1
61     assert mock_run_legacy.last_args[0] == 'export.php'
62
63
64 @pytest.mark.parametrize("param,value", [('output-type', 'country'),
65                                          ('output-format', 'street;city'),
66                                          ('language', 'xf'),
67                                          ('restrict-to-country', 'us'),
68                                          ('restrict-to-osm-node', '536'),
69                                          ('restrict-to-osm-way', '727'),
70                                          ('restrict-to-osm-relation', '197532')
71                                         ])
72 def test_export_parameters(src_dir, tmp_path, param, value):
73     (tmp_path / 'admin').mkdir()
74     (tmp_path / 'admin' / 'export.php').write_text(f"""<?php
75         exit(strpos(implode(' ', $_SERVER['argv']), '--{param} {value}') >= 0 ? 0 : 10);
76         """)
77
78     assert nominatim.cli.nominatim(module_dir='MODULE NOT AVAILABLE',
79                                    osm2pgsql_path='OSM2PGSQL NOT AVAILABLE',
80                                    phplib_dir=str(tmp_path),
81                                    data_dir=str(src_dir / 'data'),
82                                    phpcgi_path='/usr/bin/php-cgi',
83                                    sqllib_dir=str(src_dir / 'lib-sql'),
84                                    config_dir=str(src_dir / 'settings'),
85                                    cli_args=['export', '--' + param, value]) == 0
86
87
88
89 class TestCliWithDb:
90
91     @pytest.fixture(autouse=True)
92     def setup_cli_call(self, cli_call, temp_db, cli_tokenizer_mock):
93         self.call_nominatim = cli_call
94         self.tokenizer_mock = cli_tokenizer_mock
95
96
97     def test_freeze_command(self, mock_func_factory):
98         mock_drop = mock_func_factory(nominatim.tools.freeze, 'drop_update_tables')
99         mock_flatnode = mock_func_factory(nominatim.tools.freeze, 'drop_flatnode_file')
100
101         assert self.call_nominatim('freeze') == 0
102
103         assert mock_drop.called == 1
104         assert mock_flatnode.called == 1
105
106
107     @pytest.mark.parametrize("params,do_bnds,do_ranks", [
108                               ([], 1, 1),
109                               (['--boundaries-only'], 1, 0),
110                               (['--no-boundaries'], 0, 1),
111                               (['--boundaries-only', '--no-boundaries'], 0, 0)])
112     def test_index_command(self, mock_func_factory, table_factory,
113                            params, do_bnds, do_ranks):
114         table_factory('import_status', 'indexed bool')
115         bnd_mock = mock_func_factory(nominatim.indexer.indexer.Indexer, 'index_boundaries')
116         rank_mock = mock_func_factory(nominatim.indexer.indexer.Indexer, 'index_by_rank')
117
118         assert self.call_nominatim('index', *params) == 0
119
120         assert bnd_mock.called == do_bnds
121         assert rank_mock.called == do_ranks
122
123
124     def test_special_phrases_wiki_command(self, mock_func_factory):
125         func = mock_func_factory(nominatim.clicmd.special_phrases.SPImporter, 'import_phrases')
126
127         self.call_nominatim('special-phrases', '--import-from-wiki', '--no-replace')
128
129         assert func.called == 1
130
131
132     def test_special_phrases_csv_command(self, src_dir, mock_func_factory):
133         func = mock_func_factory(nominatim.clicmd.special_phrases.SPImporter, 'import_phrases')
134         testdata = src_dir / 'test' / 'testdb'
135         csv_path = str((testdata / 'full_en_phrases_test.csv').resolve())
136
137         self.call_nominatim('special-phrases', '--import-from-csv', csv_path)
138
139         assert func.called == 1
140
141
142     def test_special_phrases_csv_bad_file(self, src_dir):
143         testdata = src_dir / 'something349053905.csv'
144
145         self.call_nominatim('special-phrases', '--import-from-csv',
146                             str(testdata.resolve())) == 1