2 Tests for command line interface wrapper.
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
10 import nominatim.indexer.indexer
11 import nominatim.tools.add_osm_data
12 import nominatim.tools.freeze
15 def test_cli_help(cli_call, capsys):
16 """ Running nominatim tool without arguments prints help.
18 assert cli_call() == 1
20 captured = capsys.readouterr()
21 assert captured.out.startswith('usage:')
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
29 assert mock_run_legacy.called == 1
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
37 assert mock_run_legacy.called == 1
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')
44 assert cli_call('add-data', '--tiger-data', 'somewhere') == 0
46 assert mock.called == 1
49 def test_cli_serve_command(cli_call, mock_func_factory):
50 func = mock_func_factory(nominatim.cli, 'run_php_server')
52 cli_call('serve') == 0
54 assert func.called == 1
57 def test_cli_export_command(cli_call, mock_run_legacy):
58 assert cli_call('export', '--output-all-postcodes') == 0
60 assert mock_run_legacy.called == 1
61 assert mock_run_legacy.last_args[0] == 'export.php'
64 @pytest.mark.parametrize("param,value", [('output-type', 'country'),
65 ('output-format', 'street;city'),
67 ('restrict-to-country', 'us'),
68 ('restrict-to-osm-node', '536'),
69 ('restrict-to-osm-way', '727'),
70 ('restrict-to-osm-relation', '197532')
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);
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
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
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')
101 assert self.call_nominatim('freeze') == 0
103 assert mock_drop.called == 1
104 assert mock_flatnode.called == 1
107 @pytest.mark.parametrize("params,do_bnds,do_ranks", [
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')
118 assert self.call_nominatim('index', *params) == 0
120 assert bnd_mock.called == do_bnds
121 assert rank_mock.called == do_ranks
124 def test_special_phrases_wiki_command(self, mock_func_factory):
125 func = mock_func_factory(nominatim.clicmd.special_phrases.SPImporter, 'import_phrases')
127 self.call_nominatim('special-phrases', '--import-from-wiki', '--no-replace')
129 assert func.called == 1
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())
137 self.call_nominatim('special-phrases', '--import-from-csv', csv_path)
139 assert func.called == 1
142 def test_special_phrases_csv_bad_file(self, src_dir):
143 testdata = src_dir / 'something349053905.csv'
145 self.call_nominatim('special-phrases', '--import-from-csv',
146 str(testdata.resolve())) == 1