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