1 # SPDX-License-Identifier: GPL-3.0-or-later
3 # This file is part of Nominatim. (https://nominatim.org)
5 # Copyright (C) 2024 by the Nominatim developer community.
6 # For a full list of authors see the git log.
8 Tests for command line interface wrapper.
10 These tests just check that the various command line parameters route to the
11 correct functionality. They use a lot of monkeypatching to avoid executing
17 import nominatim_db.indexer.indexer
18 import nominatim_db.tools.add_osm_data
19 import nominatim_db.tools.freeze
20 import nominatim_db.tools.tiger_data
23 def test_cli_help(cli_call, capsys):
24 """ Running nominatim tool without arguments prints help.
26 assert cli_call() == 1
28 captured = capsys.readouterr()
29 assert captured.out.startswith('usage:')
31 def test_cli_version(cli_call, capsys):
32 """ Running nominatim tool --version prints a version string.
34 assert cli_call('--version') == 0
36 captured = capsys.readouterr()
37 assert captured.out.startswith('Nominatim version')
40 def test_cli_serve_php(cli_call, mock_func_factory):
41 func = mock_func_factory(nominatim_db.cli, 'run_php_server')
43 cli_call('serve', '--engine', 'php') == 0
45 assert func.called == 1
51 @pytest.fixture(autouse=True)
52 def setup_cli_call(self, cli_call, temp_db, cli_tokenizer_mock, table_factory):
53 self.call_nominatim = cli_call
54 self.tokenizer_mock = cli_tokenizer_mock
55 # Make sure tools.freeze.is_frozen doesn't report database as frozen. Monkeypatching failed
56 table_factory('place')
59 @pytest.mark.parametrize("name,oid", [('file', 'foo.osm'), ('diff', 'foo.osc')])
60 def test_cli_add_data_file_command(self, cli_call, mock_func_factory, name, oid):
61 mock_run_legacy = mock_func_factory(nominatim_db.tools.add_osm_data, 'add_data_from_file')
62 assert cli_call('add-data', '--' + name, str(oid)) == 0
64 assert mock_run_legacy.called == 1
67 @pytest.mark.parametrize("name,oid", [('node', 12), ('way', 8), ('relation', 32)])
68 def test_cli_add_data_object_command(self, cli_call, mock_func_factory, name, oid):
69 mock_run_legacy = mock_func_factory(nominatim_db.tools.add_osm_data, 'add_osm_object')
70 assert cli_call('add-data', '--' + name, str(oid)) == 0
72 assert mock_run_legacy.called == 1
76 def test_cli_add_data_tiger_data(self, cli_call, cli_tokenizer_mock, async_mock_func_factory):
77 mock = async_mock_func_factory(nominatim_db.tools.tiger_data, 'add_tiger_data')
79 assert cli_call('add-data', '--tiger-data', 'somewhere') == 0
81 assert mock.called == 1
83 def test_freeze_command(self, mock_func_factory):
84 mock_drop = mock_func_factory(nominatim_db.tools.freeze, 'drop_update_tables')
85 mock_flatnode = mock_func_factory(nominatim_db.tools.freeze, 'drop_flatnode_file')
87 assert self.call_nominatim('freeze') == 0
89 assert mock_drop.called == 1
90 assert mock_flatnode.called == 1
93 @pytest.mark.parametrize("params,do_bnds,do_ranks", [
95 (['--boundaries-only'], 2, 0),
96 (['--no-boundaries'], 0, 2),
97 (['--boundaries-only', '--no-boundaries'], 0, 0)])
98 def test_index_command(self, monkeypatch, async_mock_func_factory, table_factory,
99 params, do_bnds, do_ranks):
100 table_factory('import_status', 'indexed bool')
101 bnd_mock = async_mock_func_factory(nominatim_db.indexer.indexer.Indexer, 'index_boundaries')
102 rank_mock = async_mock_func_factory(nominatim_db.indexer.indexer.Indexer, 'index_by_rank')
103 postcode_mock = async_mock_func_factory(nominatim_db.indexer.indexer.Indexer, 'index_postcodes')
105 monkeypatch.setattr(nominatim_db.indexer.indexer.Indexer, 'has_pending',
108 assert self.call_nominatim('index', *params) == 0
110 assert bnd_mock.called == do_bnds
111 assert rank_mock.called == do_ranks
112 assert postcode_mock.called == do_ranks
115 def test_special_phrases_wiki_command(self, mock_func_factory):
116 func = mock_func_factory(nominatim_db.clicmd.special_phrases.SPImporter, 'import_phrases')
118 self.call_nominatim('special-phrases', '--import-from-wiki', '--no-replace')
120 assert func.called == 1
123 def test_special_phrases_csv_command(self, src_dir, mock_func_factory):
124 func = mock_func_factory(nominatim_db.clicmd.special_phrases.SPImporter, 'import_phrases')
125 testdata = src_dir / 'test' / 'testdb'
126 csv_path = str((testdata / 'full_en_phrases_test.csv').resolve())
128 self.call_nominatim('special-phrases', '--import-from-csv', csv_path)
130 assert func.called == 1
133 def test_special_phrases_csv_bad_file(self, src_dir):
134 testdata = src_dir / 'something349053905.csv'
136 self.call_nominatim('special-phrases', '--import-from-csv',
137 str(testdata.resolve())) == 1