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.db.properties
12 import nominatim.clicmd.api
13 import nominatim.clicmd.refresh
14 import nominatim.clicmd.admin
15 import nominatim.clicmd.setup
16 import nominatim.indexer.indexer
17 import nominatim.tools.admin
18 import nominatim.tools.add_osm_data
19 import nominatim.tools.check_database
20 import nominatim.tools.database_import
21 import nominatim.tools.country_info
22 import nominatim.tools.freeze
23 import nominatim.tools.refresh
24 import nominatim.tools.postcodes
25 import nominatim.tokenizer.factory
27 from mocks import MockParamCapture
30 def mock_run_legacy(monkeypatch):
31 mock = MockParamCapture()
32 monkeypatch.setattr(nominatim.cli, 'run_legacy_script', mock)
37 def mock_func_factory(monkeypatch):
38 def get_mock(module, func):
39 mock = MockParamCapture()
41 monkeypatch.setattr(module, func, mock)
50 @pytest.fixture(autouse=True)
51 def setup_cli_call(self, cli_call):
52 self.call_nominatim = cli_call
55 def test_cli_help(self, capsys):
56 """ Running nominatim tool without arguments prints help.
58 assert self.call_nominatim() == 1
60 captured = capsys.readouterr()
61 assert captured.out.startswith('usage:')
64 @pytest.mark.parametrize("command,script", [
65 (('export',), 'export')
67 def test_legacy_commands_simple(self, mock_run_legacy, command, script):
68 assert self.call_nominatim(*command) == 0
70 assert mock_run_legacy.called == 1
71 assert mock_run_legacy.last_args[0] == script + '.php'
74 @pytest.mark.parametrize("params", [('--warm', ),
75 ('--warm', '--reverse-only'),
76 ('--warm', '--search-only')])
77 def test_admin_command_legacy(self, mock_func_factory, params):
78 mock_run_legacy = mock_func_factory(nominatim.clicmd.admin, 'run_legacy_script')
80 assert self.call_nominatim('admin', *params) == 0
82 assert mock_run_legacy.called == 1
85 def test_admin_command_check_database(self, mock_func_factory):
86 mock = mock_func_factory(nominatim.tools.check_database, 'check_database')
88 assert self.call_nominatim('admin', '--check-database') == 0
89 assert mock.called == 1
92 @pytest.mark.parametrize("name,oid", [('file', 'foo.osm'), ('diff', 'foo.osc')])
93 def test_add_data_file_command(self, mock_func_factory, name, oid):
94 mock_run_legacy = mock_func_factory(nominatim.tools.add_osm_data, 'add_data_from_file')
95 assert self.call_nominatim('add-data', '--' + name, str(oid)) == 0
97 assert mock_run_legacy.called == 1
100 @pytest.mark.parametrize("name,oid", [('node', 12), ('way', 8), ('relation', 32)])
101 def test_add_data_object_command(self, mock_func_factory, name, oid):
102 mock_run_legacy = mock_func_factory(nominatim.tools.add_osm_data, 'add_osm_object')
103 assert self.call_nominatim('add-data', '--' + name, str(oid)) == 0
105 assert mock_run_legacy.called == 1
108 def test_serve_command(self, mock_func_factory):
109 func = mock_func_factory(nominatim.cli, 'run_php_server')
111 self.call_nominatim('serve')
113 assert func.called == 1
116 @pytest.mark.parametrize("params", [('search', '--query', 'new'),
117 ('reverse', '--lat', '0', '--lon', '0'),
118 ('lookup', '--id', 'N1'),
119 ('details', '--node', '1'),
120 ('details', '--way', '1'),
121 ('details', '--relation', '1'),
122 ('details', '--place_id', '10001'),
124 class TestCliApiCall:
126 @pytest.fixture(autouse=True)
127 def setup_cli_call(self, cli_call):
128 self.call_nominatim = cli_call
130 def test_api_commands_simple(self, mock_func_factory, params, tmp_path):
131 (tmp_path / 'website').mkdir()
132 (tmp_path / 'website' / (params[0] + '.php')).write_text('')
133 mock_run_api = mock_func_factory(nominatim.clicmd.api, 'run_api_script')
135 assert self.call_nominatim(*params, '--project-dir', str(tmp_path)) == 0
137 assert mock_run_api.called == 1
138 assert mock_run_api.last_args[0] == params[0]
141 def test_bad_project_idr(self, mock_func_factory, params):
142 mock_run_api = mock_func_factory(nominatim.clicmd.api, 'run_api_script')
144 assert self.call_nominatim(*params) == 1
149 @pytest.fixture(autouse=True)
150 def setup_cli_call(self, cli_call, temp_db):
151 self.call_nominatim = cli_call
154 @pytest.fixture(autouse=True)
155 def setup_tokenizer_mock(self, monkeypatch):
156 class DummyTokenizer:
157 def __init__(self, *args, **kwargs):
158 self.update_sql_functions_called = False
159 self.finalize_import_called = False
160 self.update_statistics_called = False
162 def update_sql_functions(self, *args):
163 self.update_sql_functions_called = True
165 def finalize_import(self, *args):
166 self.finalize_import_called = True
168 def update_statistics(self):
169 self.update_statistics_called = True
172 tok = DummyTokenizer()
173 monkeypatch.setattr(nominatim.tokenizer.factory, 'get_tokenizer_for_db',
175 monkeypatch.setattr(nominatim.tokenizer.factory, 'create_tokenizer',
178 self.tokenizer_mock = tok
181 def test_import_missing_file(self):
182 assert self.call_nominatim('import', '--osm-file', 'sfsafegwedgw.reh.erh') == 1
185 def test_import_bad_file(self):
186 assert self.call_nominatim('import', '--osm-file', '.') == 1
189 def test_import_full(self, mock_func_factory):
191 mock_func_factory(nominatim.tools.database_import, 'setup_database_skeleton'),
192 mock_func_factory(nominatim.tools.country_info, 'setup_country_tables'),
193 mock_func_factory(nominatim.tools.database_import, 'import_osm_data'),
194 mock_func_factory(nominatim.tools.refresh, 'import_wikipedia_articles'),
195 mock_func_factory(nominatim.tools.database_import, 'truncate_data_tables'),
196 mock_func_factory(nominatim.tools.database_import, 'load_data'),
197 mock_func_factory(nominatim.tools.database_import, 'create_tables'),
198 mock_func_factory(nominatim.tools.database_import, 'create_table_triggers'),
199 mock_func_factory(nominatim.tools.database_import, 'create_partition_tables'),
200 mock_func_factory(nominatim.tools.database_import, 'create_search_indices'),
201 mock_func_factory(nominatim.tools.country_info, 'create_country_names'),
202 mock_func_factory(nominatim.tools.refresh, 'load_address_levels_from_config'),
203 mock_func_factory(nominatim.tools.postcodes, 'update_postcodes'),
204 mock_func_factory(nominatim.indexer.indexer.Indexer, 'index_full'),
205 mock_func_factory(nominatim.tools.refresh, 'setup_website'),
206 mock_func_factory(nominatim.db.properties, 'set_property')
209 cf_mock = mock_func_factory(nominatim.tools.refresh, 'create_functions')
211 assert self.call_nominatim('import', '--osm-file', __file__) == 0
212 assert self.tokenizer_mock.finalize_import_called
214 assert cf_mock.called > 1
217 assert mock.called == 1, "Mock '{}' not called".format(mock.func_name)
220 def test_import_continue_load_data(self, mock_func_factory):
222 mock_func_factory(nominatim.tools.database_import, 'truncate_data_tables'),
223 mock_func_factory(nominatim.tools.database_import, 'load_data'),
224 mock_func_factory(nominatim.tools.database_import, 'create_search_indices'),
225 mock_func_factory(nominatim.tools.country_info, 'create_country_names'),
226 mock_func_factory(nominatim.tools.postcodes, 'update_postcodes'),
227 mock_func_factory(nominatim.indexer.indexer.Indexer, 'index_full'),
228 mock_func_factory(nominatim.tools.refresh, 'setup_website'),
229 mock_func_factory(nominatim.db.properties, 'set_property')
232 assert self.call_nominatim('import', '--continue', 'load-data') == 0
233 assert self.tokenizer_mock.finalize_import_called
236 assert mock.called == 1, "Mock '{}' not called".format(mock.func_name)
239 def test_import_continue_indexing(self, mock_func_factory, placex_table,
242 mock_func_factory(nominatim.tools.database_import, 'create_search_indices'),
243 mock_func_factory(nominatim.tools.country_info, 'create_country_names'),
244 mock_func_factory(nominatim.indexer.indexer.Indexer, 'index_full'),
245 mock_func_factory(nominatim.tools.refresh, 'setup_website'),
246 mock_func_factory(nominatim.db.properties, 'set_property')
249 assert self.call_nominatim('import', '--continue', 'indexing') == 0
252 assert mock.called == 1, "Mock '{}' not called".format(mock.func_name)
254 assert temp_db_conn.index_exists('idx_placex_pendingsector')
256 # Calling it again still works for the index
257 assert self.call_nominatim('import', '--continue', 'indexing') == 0
258 assert temp_db_conn.index_exists('idx_placex_pendingsector')
261 def test_import_continue_postprocess(self, mock_func_factory):
263 mock_func_factory(nominatim.tools.database_import, 'create_search_indices'),
264 mock_func_factory(nominatim.tools.country_info, 'create_country_names'),
265 mock_func_factory(nominatim.tools.refresh, 'setup_website'),
266 mock_func_factory(nominatim.db.properties, 'set_property')
269 assert self.call_nominatim('import', '--continue', 'db-postprocess') == 0
271 assert self.tokenizer_mock.finalize_import_called
274 assert mock.called == 1, "Mock '{}' not called".format(mock.func_name)
277 def test_freeze_command(self, mock_func_factory):
278 mock_drop = mock_func_factory(nominatim.tools.freeze, 'drop_update_tables')
279 mock_flatnode = mock_func_factory(nominatim.tools.freeze, 'drop_flatnode_file')
281 assert self.call_nominatim('freeze') == 0
283 assert mock_drop.called == 1
284 assert mock_flatnode.called == 1
288 @pytest.mark.parametrize("func, params", [('analyse_indexing', ('--analyse-indexing', ))])
289 def test_admin_command_tool(self, mock_func_factory, func, params):
290 mock = mock_func_factory(nominatim.tools.admin, func)
292 assert self.call_nominatim('admin', *params) == 0
293 assert mock.called == 1
296 @pytest.mark.parametrize("params,do_bnds,do_ranks", [
298 (['--boundaries-only'], 1, 0),
299 (['--no-boundaries'], 0, 1),
300 (['--boundaries-only', '--no-boundaries'], 0, 0)])
301 def test_index_command(self, mock_func_factory, table_factory,
302 params, do_bnds, do_ranks):
303 table_factory('import_status', 'indexed bool')
304 bnd_mock = mock_func_factory(nominatim.indexer.indexer.Indexer, 'index_boundaries')
305 rank_mock = mock_func_factory(nominatim.indexer.indexer.Indexer, 'index_by_rank')
307 assert self.call_nominatim('index', *params) == 0
309 assert bnd_mock.called == do_bnds
310 assert rank_mock.called == do_ranks
312 @pytest.mark.parametrize("no_replace", [(True), (False)])
313 def test_special_phrases_wiki_command(self, mock_func_factory, no_replace):
314 func = mock_func_factory(nominatim.clicmd.special_phrases.SPImporter, 'import_phrases')
317 self.call_nominatim('special-phrases', '--import-from-wiki', '--no-replace')
319 self.call_nominatim('special-phrases', '--import-from-wiki')
321 assert func.called == 1
323 @pytest.mark.parametrize("no_replace", [(True), (False)])
324 def test_special_phrases_csv_command(self, src_dir, mock_func_factory, no_replace):
325 func = mock_func_factory(nominatim.clicmd.special_phrases.SPImporter, 'import_phrases')
326 testdata = src_dir / 'test' / 'testdb'
327 csv_path = str((testdata / 'full_en_phrases_test.csv').resolve())
330 self.call_nominatim('special-phrases', '--import-from-csv', csv_path, '--no-replace')
332 self.call_nominatim('special-phrases', '--import-from-csv', csv_path)
334 assert func.called == 1
336 @pytest.mark.parametrize("command,func", [
337 ('address-levels', 'load_address_levels_from_config'),
338 ('wiki-data', 'import_wikipedia_articles'),
339 ('importance', 'recompute_importance'),
340 ('website', 'setup_website'),
342 def test_refresh_command(self, mock_func_factory, command, func):
343 func_mock = mock_func_factory(nominatim.tools.refresh, func)
345 assert self.call_nominatim('refresh', '--' + command) == 0
346 assert func_mock.called == 1
349 def test_refresh_word_count(self):
350 assert self.call_nominatim('refresh', '--word-count') == 0
351 assert self.tokenizer_mock.update_statistics_called
354 def test_refresh_postcodes(self, mock_func_factory, place_table):
355 func_mock = mock_func_factory(nominatim.tools.postcodes, 'update_postcodes')
356 idx_mock = mock_func_factory(nominatim.indexer.indexer.Indexer, 'index_postcodes')
358 assert self.call_nominatim('refresh', '--postcodes') == 0
359 assert func_mock.called == 1
360 assert idx_mock.called == 1
362 def test_refresh_create_functions(self, mock_func_factory):
363 func_mock = mock_func_factory(nominatim.tools.refresh, 'create_functions')
365 assert self.call_nominatim('refresh', '--functions') == 0
366 assert func_mock.called == 1
367 assert self.tokenizer_mock.update_sql_functions_called
370 def test_refresh_importance_computed_after_wiki_import(self, monkeypatch):
372 monkeypatch.setattr(nominatim.tools.refresh, 'import_wikipedia_articles',
373 lambda *args, **kwargs: calls.append('import') or 0)
374 monkeypatch.setattr(nominatim.tools.refresh, 'recompute_importance',
375 lambda *args, **kwargs: calls.append('update'))
377 assert self.call_nominatim('refresh', '--importance', '--wiki-data') == 0
379 assert calls == ['import', 'update']