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
8 from pathlib import Path
13 import nominatim.clicmd.api
14 import nominatim.clicmd.refresh
15 import nominatim.clicmd.admin
16 import nominatim.clicmd.setup
17 import nominatim.indexer.indexer
18 import nominatim.tools.admin
19 import nominatim.tools.check_database
20 import nominatim.tools.database_import
21 import nominatim.tools.freeze
22 import nominatim.tools.refresh
24 from mocks import MockParamCapture
26 SRC_DIR = (Path(__file__) / '..' / '..' / '..').resolve()
28 def call_nominatim(*args):
29 return nominatim.cli.nominatim(module_dir='build/module',
30 osm2pgsql_path='build/osm2pgsql/osm2pgsql',
31 phplib_dir=str(SRC_DIR / 'lib-php'),
32 data_dir=str(SRC_DIR / 'data'),
33 phpcgi_path='/usr/bin/php-cgi',
34 sqllib_dir=str(SRC_DIR / 'lib-sql'),
35 config_dir=str(SRC_DIR / 'settings'),
40 def mock_run_legacy(monkeypatch):
41 mock = MockParamCapture()
42 monkeypatch.setattr(nominatim.cli, 'run_legacy_script', mock)
47 def mock_func_factory(monkeypatch):
48 def get_mock(module, func):
49 mock = MockParamCapture()
50 monkeypatch.setattr(module, func, mock)
56 def test_cli_help(capsys):
57 """ Running nominatim tool without arguments prints help.
59 assert 1 == call_nominatim()
61 captured = capsys.readouterr()
62 assert captured.out.startswith('usage:')
65 @pytest.mark.parametrize("command,script", [
66 (('special-phrases',), 'specialphrases'),
67 (('add-data', '--tiger-data', 'tiger'), 'setup'),
68 (('add-data', '--file', 'foo.osm'), 'update'),
69 (('export',), 'export')
71 def test_legacy_commands_simple(mock_run_legacy, command, script):
72 assert 0 == call_nominatim(*command)
74 assert mock_run_legacy.called == 1
75 assert mock_run_legacy.last_args[0] == script + '.php'
78 def test_import_missing_file(temp_db):
79 assert 1 == call_nominatim('import', '--osm-file', 'sfsafegweweggdgw.reh.erh')
82 def test_import_bad_file(temp_db):
83 assert 1 == call_nominatim('import', '--osm-file', '.')
86 def test_import_full(temp_db, mock_func_factory):
88 mock_func_factory(nominatim.tools.database_import, 'setup_database_skeleton'),
89 mock_func_factory(nominatim.tools.database_import, 'install_module'),
90 mock_func_factory(nominatim.tools.database_import, 'import_osm_data'),
91 mock_func_factory(nominatim.tools.refresh, 'import_wikipedia_articles'),
92 mock_func_factory(nominatim.tools.database_import, 'truncate_data_tables'),
93 mock_func_factory(nominatim.tools.database_import, 'load_data'),
94 mock_func_factory(nominatim.indexer.indexer.Indexer, 'index_full'),
95 mock_func_factory(nominatim.tools.refresh, 'setup_website'),
98 cf_mock = mock_func_factory(nominatim.tools.refresh, 'create_functions')
99 mock_func_factory(nominatim.clicmd.setup, 'run_legacy_script')
101 assert 0 == call_nominatim('import', '--osm-file', __file__)
103 assert cf_mock.called > 1
106 assert mock.called == 1
108 def test_freeze_command(mock_func_factory, temp_db):
109 mock_drop = mock_func_factory(nominatim.tools.freeze, 'drop_update_tables')
110 mock_flatnode = mock_func_factory(nominatim.tools.freeze, 'drop_flatnode_file')
112 assert 0 == call_nominatim('freeze')
114 assert mock_drop.called == 1
115 assert mock_flatnode.called == 1
118 @pytest.mark.parametrize("params", [('--warm', ),
119 ('--warm', '--reverse-only'),
120 ('--warm', '--search-only')])
121 def test_admin_command_legacy(mock_func_factory, params):
122 mock_run_legacy = mock_func_factory(nominatim.clicmd.admin, 'run_legacy_script')
124 assert 0 == call_nominatim('admin', *params)
126 assert mock_run_legacy.called == 1
129 @pytest.mark.parametrize("func, params", [('analyse_indexing', ('--analyse-indexing', ))])
130 def test_admin_command_tool(temp_db, mock_func_factory, func, params):
131 mock = mock_func_factory(nominatim.tools.admin, func)
133 assert 0 == call_nominatim('admin', *params)
134 assert mock.called == 1
137 def test_admin_command_check_database(mock_func_factory):
138 mock = mock_func_factory(nominatim.tools.check_database, 'check_database')
140 assert 0 == call_nominatim('admin', '--check-database')
141 assert mock.called == 1
144 @pytest.mark.parametrize("name,oid", [('file', 'foo.osm'), ('diff', 'foo.osc'),
145 ('node', 12), ('way', 8), ('relation', 32)])
146 def test_add_data_command(mock_run_legacy, name, oid):
147 assert 0 == call_nominatim('add-data', '--' + name, str(oid))
149 assert mock_run_legacy.called == 1
150 assert mock_run_legacy.last_args == ('update.php', '--import-' + name, oid)
153 @pytest.mark.parametrize("params,do_bnds,do_ranks", [
155 (['--boundaries-only'], 1, 0),
156 (['--no-boundaries'], 0, 1),
157 (['--boundaries-only', '--no-boundaries'], 0, 0)])
158 def test_index_command(mock_func_factory, temp_db_cursor, params, do_bnds, do_ranks):
159 temp_db_cursor.execute("CREATE TABLE import_status (indexed bool)")
160 bnd_mock = mock_func_factory(nominatim.indexer.indexer.Indexer, 'index_boundaries')
161 rank_mock = mock_func_factory(nominatim.indexer.indexer.Indexer, 'index_by_rank')
163 assert 0 == call_nominatim('index', *params)
165 assert bnd_mock.called == do_bnds
166 assert rank_mock.called == do_ranks
169 @pytest.mark.parametrize("command,func", [
170 ('postcodes', 'update_postcodes'),
171 ('word-counts', 'recompute_word_counts'),
172 ('address-levels', 'load_address_levels_from_file'),
173 ('functions', 'create_functions'),
174 ('wiki-data', 'import_wikipedia_articles'),
175 ('importance', 'recompute_importance'),
176 ('website', 'setup_website'),
178 def test_refresh_command(mock_func_factory, temp_db, command, func):
179 func_mock = mock_func_factory(nominatim.tools.refresh, func)
181 assert 0 == call_nominatim('refresh', '--' + command)
182 assert func_mock.called == 1
185 def test_refresh_importance_computed_after_wiki_import(monkeypatch, temp_db):
187 monkeypatch.setattr(nominatim.tools.refresh, 'import_wikipedia_articles',
188 lambda *args, **kwargs: calls.append('import') or 0)
189 monkeypatch.setattr(nominatim.tools.refresh, 'recompute_importance',
190 lambda *args, **kwargs: calls.append('update'))
192 assert 0 == call_nominatim('refresh', '--importance', '--wiki-data')
194 assert calls == ['import', 'update']
197 def test_serve_command(mock_func_factory):
198 func = mock_func_factory(nominatim.cli, 'run_php_server')
200 call_nominatim('serve')
202 assert func.called == 1
204 @pytest.mark.parametrize("params", [
205 ('search', '--query', 'new'),
206 ('reverse', '--lat', '0', '--lon', '0'),
207 ('lookup', '--id', 'N1'),
208 ('details', '--node', '1'),
209 ('details', '--way', '1'),
210 ('details', '--relation', '1'),
211 ('details', '--place_id', '10001'),
214 def test_api_commands_simple(mock_func_factory, params):
215 mock_run_api = mock_func_factory(nominatim.clicmd.api, 'run_api_script')
217 assert 0 == call_nominatim(*params)
219 assert mock_run_api.called == 1
220 assert mock_run_api.last_args[0] == params[0]