1 # SPDX-License-Identifier: GPL-2.0-only
3 # This file is part of Nominatim. (https://nominatim.org)
5 # Copyright (C) 2022 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 functionionality. They use a lot of monkeypatching to avoid executing
16 import nominatim.indexer.indexer
17 import nominatim.tools.add_osm_data
18 import nominatim.tools.freeze
21 def test_cli_help(cli_call, capsys):
22 """ Running nominatim tool without arguments prints help.
24 assert cli_call() == 1
26 captured = capsys.readouterr()
27 assert captured.out.startswith('usage:')
29 def test_cli_version(cli_call, capsys):
30 """ Running nominatim tool --version prints a version string.
32 assert cli_call('--version') == 0
34 captured = capsys.readouterr()
35 assert captured.out.startswith('Nominatim version')
37 @pytest.mark.parametrize("name,oid", [('file', 'foo.osm'), ('diff', 'foo.osc')])
38 def test_cli_add_data_file_command(cli_call, mock_func_factory, name, oid):
39 mock_run_legacy = mock_func_factory(nominatim.tools.add_osm_data, 'add_data_from_file')
40 assert cli_call('add-data', '--' + name, str(oid)) == 0
42 assert mock_run_legacy.called == 1
45 @pytest.mark.parametrize("name,oid", [('node', 12), ('way', 8), ('relation', 32)])
46 def test_cli_add_data_object_command(cli_call, mock_func_factory, name, oid):
47 mock_run_legacy = mock_func_factory(nominatim.tools.add_osm_data, 'add_osm_object')
48 assert cli_call('add-data', '--' + name, str(oid)) == 0
50 assert mock_run_legacy.called == 1
54 def test_cli_add_data_tiger_data(cli_call, cli_tokenizer_mock, mock_func_factory):
55 mock = mock_func_factory(nominatim.tools.tiger_data, 'add_tiger_data')
57 assert cli_call('add-data', '--tiger-data', 'somewhere') == 0
59 assert mock.called == 1
62 def test_cli_serve_command(cli_call, mock_func_factory):
63 func = mock_func_factory(nominatim.cli, 'run_php_server')
65 cli_call('serve') == 0
67 assert func.called == 1
70 def test_cli_export_command(cli_call, mock_run_legacy):
71 assert cli_call('export', '--output-all-postcodes') == 0
73 assert mock_run_legacy.called == 1
74 assert mock_run_legacy.last_args[0] == 'export.php'
77 @pytest.mark.parametrize("param,value", [('output-type', 'country'),
78 ('output-format', 'street;city'),
80 ('restrict-to-country', 'us'),
81 ('restrict-to-osm-node', '536'),
82 ('restrict-to-osm-way', '727'),
83 ('restrict-to-osm-relation', '197532')
85 def test_export_parameters(src_dir, tmp_path, param, value):
86 (tmp_path / 'admin').mkdir()
87 (tmp_path / 'admin' / 'export.php').write_text(f"""<?php
88 exit(strpos(implode(' ', $_SERVER['argv']), '--{param} {value}') >= 0 ? 0 : 10);
91 assert nominatim.cli.nominatim(module_dir='MODULE NOT AVAILABLE',
92 osm2pgsql_path='OSM2PGSQL NOT AVAILABLE',
93 phplib_dir=str(tmp_path),
94 data_dir=str(src_dir / 'data'),
95 phpcgi_path='/usr/bin/php-cgi',
96 sqllib_dir=str(src_dir / 'lib-sql'),
97 config_dir=str(src_dir / 'settings'),
98 cli_args=['export', '--' + param, value]) == 0
104 @pytest.fixture(autouse=True)
105 def setup_cli_call(self, cli_call, temp_db, cli_tokenizer_mock):
106 self.call_nominatim = cli_call
107 self.tokenizer_mock = cli_tokenizer_mock
110 def test_freeze_command(self, mock_func_factory):
111 mock_drop = mock_func_factory(nominatim.tools.freeze, 'drop_update_tables')
112 mock_flatnode = mock_func_factory(nominatim.tools.freeze, 'drop_flatnode_file')
114 assert self.call_nominatim('freeze') == 0
116 assert mock_drop.called == 1
117 assert mock_flatnode.called == 1
120 @pytest.mark.parametrize("params,do_bnds,do_ranks", [
122 (['--boundaries-only'], 1, 0),
123 (['--no-boundaries'], 0, 1),
124 (['--boundaries-only', '--no-boundaries'], 0, 0)])
125 def test_index_command(self, mock_func_factory, table_factory,
126 params, do_bnds, do_ranks):
127 table_factory('import_status', 'indexed bool')
128 bnd_mock = mock_func_factory(nominatim.indexer.indexer.Indexer, 'index_boundaries')
129 rank_mock = mock_func_factory(nominatim.indexer.indexer.Indexer, 'index_by_rank')
131 assert self.call_nominatim('index', *params) == 0
133 assert bnd_mock.called == do_bnds
134 assert rank_mock.called == do_ranks
137 def test_special_phrases_wiki_command(self, mock_func_factory):
138 func = mock_func_factory(nominatim.clicmd.special_phrases.SPImporter, 'import_phrases')
140 self.call_nominatim('special-phrases', '--import-from-wiki', '--no-replace')
142 assert func.called == 1
145 def test_special_phrases_csv_command(self, src_dir, mock_func_factory):
146 func = mock_func_factory(nominatim.clicmd.special_phrases.SPImporter, 'import_phrases')
147 testdata = src_dir / 'test' / 'testdb'
148 csv_path = str((testdata / 'full_en_phrases_test.csv').resolve())
150 self.call_nominatim('special-phrases', '--import-from-csv', csv_path)
152 assert func.called == 1
155 def test_special_phrases_csv_bad_file(self, src_dir):
156 testdata = src_dir / 'something349053905.csv'
158 self.call_nominatim('special-phrases', '--import-from-csv',
159 str(testdata.resolve())) == 1