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 def test_api_commands_simple(self, mock_func_factory, params):
125 mock_run_api = mock_func_factory(nominatim.clicmd.api, 'run_api_script')
127 assert self.call_nominatim(*params) == 0
129 assert mock_run_api.called == 1
130 assert mock_run_api.last_args[0] == params[0]
136 @pytest.fixture(autouse=True)
137 def setup_cli_call(self, cli_call, temp_db):
138 self.call_nominatim = cli_call
141 @pytest.fixture(autouse=True)
142 def setup_tokenizer_mock(self, monkeypatch):
143 class DummyTokenizer:
144 def __init__(self, *args, **kwargs):
145 self.update_sql_functions_called = False
146 self.finalize_import_called = False
147 self.update_statistics_called = False
149 def update_sql_functions(self, *args):
150 self.update_sql_functions_called = True
152 def finalize_import(self, *args):
153 self.finalize_import_called = True
155 def update_statistics(self):
156 self.update_statistics_called = True
159 tok = DummyTokenizer()
160 monkeypatch.setattr(nominatim.tokenizer.factory, 'get_tokenizer_for_db',
162 monkeypatch.setattr(nominatim.tokenizer.factory, 'create_tokenizer',
165 self.tokenizer_mock = tok
168 def test_import_missing_file(self):
169 assert self.call_nominatim('import', '--osm-file', 'sfsafegwedgw.reh.erh') == 1
172 def test_import_bad_file(self):
173 assert self.call_nominatim('import', '--osm-file', '.') == 1
176 def test_import_full(self, mock_func_factory):
178 mock_func_factory(nominatim.tools.database_import, 'setup_database_skeleton'),
179 mock_func_factory(nominatim.tools.country_info, 'setup_country_tables'),
180 mock_func_factory(nominatim.tools.database_import, 'import_osm_data'),
181 mock_func_factory(nominatim.tools.refresh, 'import_wikipedia_articles'),
182 mock_func_factory(nominatim.tools.database_import, 'truncate_data_tables'),
183 mock_func_factory(nominatim.tools.database_import, 'load_data'),
184 mock_func_factory(nominatim.tools.database_import, 'create_tables'),
185 mock_func_factory(nominatim.tools.database_import, 'create_table_triggers'),
186 mock_func_factory(nominatim.tools.database_import, 'create_partition_tables'),
187 mock_func_factory(nominatim.tools.database_import, 'create_search_indices'),
188 mock_func_factory(nominatim.tools.country_info, 'create_country_names'),
189 mock_func_factory(nominatim.tools.refresh, 'load_address_levels_from_config'),
190 mock_func_factory(nominatim.tools.postcodes, 'update_postcodes'),
191 mock_func_factory(nominatim.indexer.indexer.Indexer, 'index_full'),
192 mock_func_factory(nominatim.tools.refresh, 'setup_website'),
193 mock_func_factory(nominatim.db.properties, 'set_property')
196 cf_mock = mock_func_factory(nominatim.tools.refresh, 'create_functions')
198 assert self.call_nominatim('import', '--osm-file', __file__) == 0
199 assert self.tokenizer_mock.finalize_import_called
201 assert cf_mock.called > 1
204 assert mock.called == 1, "Mock '{}' not called".format(mock.func_name)
207 def test_import_continue_load_data(self, mock_func_factory):
209 mock_func_factory(nominatim.tools.database_import, 'truncate_data_tables'),
210 mock_func_factory(nominatim.tools.database_import, 'load_data'),
211 mock_func_factory(nominatim.tools.database_import, 'create_search_indices'),
212 mock_func_factory(nominatim.tools.country_info, 'create_country_names'),
213 mock_func_factory(nominatim.tools.postcodes, 'update_postcodes'),
214 mock_func_factory(nominatim.indexer.indexer.Indexer, 'index_full'),
215 mock_func_factory(nominatim.tools.refresh, 'setup_website'),
216 mock_func_factory(nominatim.db.properties, 'set_property')
219 assert self.call_nominatim('import', '--continue', 'load-data') == 0
220 assert self.tokenizer_mock.finalize_import_called
223 assert mock.called == 1, "Mock '{}' not called".format(mock.func_name)
226 def test_import_continue_indexing(self, mock_func_factory, placex_table,
229 mock_func_factory(nominatim.tools.database_import, 'create_search_indices'),
230 mock_func_factory(nominatim.tools.country_info, 'create_country_names'),
231 mock_func_factory(nominatim.indexer.indexer.Indexer, 'index_full'),
232 mock_func_factory(nominatim.tools.refresh, 'setup_website'),
233 mock_func_factory(nominatim.db.properties, 'set_property')
236 assert self.call_nominatim('import', '--continue', 'indexing') == 0
239 assert mock.called == 1, "Mock '{}' not called".format(mock.func_name)
241 assert temp_db_conn.index_exists('idx_placex_pendingsector')
243 # Calling it again still works for the index
244 assert self.call_nominatim('import', '--continue', 'indexing') == 0
245 assert temp_db_conn.index_exists('idx_placex_pendingsector')
248 def test_import_continue_postprocess(self, mock_func_factory):
250 mock_func_factory(nominatim.tools.database_import, 'create_search_indices'),
251 mock_func_factory(nominatim.tools.country_info, 'create_country_names'),
252 mock_func_factory(nominatim.tools.refresh, 'setup_website'),
253 mock_func_factory(nominatim.db.properties, 'set_property')
256 assert self.call_nominatim('import', '--continue', 'db-postprocess') == 0
258 assert self.tokenizer_mock.finalize_import_called
261 assert mock.called == 1, "Mock '{}' not called".format(mock.func_name)
264 def test_freeze_command(self, mock_func_factory):
265 mock_drop = mock_func_factory(nominatim.tools.freeze, 'drop_update_tables')
266 mock_flatnode = mock_func_factory(nominatim.tools.freeze, 'drop_flatnode_file')
268 assert self.call_nominatim('freeze') == 0
270 assert mock_drop.called == 1
271 assert mock_flatnode.called == 1
275 @pytest.mark.parametrize("func, params", [('analyse_indexing', ('--analyse-indexing', ))])
276 def test_admin_command_tool(self, mock_func_factory, func, params):
277 mock = mock_func_factory(nominatim.tools.admin, func)
279 assert self.call_nominatim('admin', *params) == 0
280 assert mock.called == 1
283 @pytest.mark.parametrize("params,do_bnds,do_ranks", [
285 (['--boundaries-only'], 1, 0),
286 (['--no-boundaries'], 0, 1),
287 (['--boundaries-only', '--no-boundaries'], 0, 0)])
288 def test_index_command(self, mock_func_factory, table_factory,
289 params, do_bnds, do_ranks):
290 table_factory('import_status', 'indexed bool')
291 bnd_mock = mock_func_factory(nominatim.indexer.indexer.Indexer, 'index_boundaries')
292 rank_mock = mock_func_factory(nominatim.indexer.indexer.Indexer, 'index_by_rank')
294 assert self.call_nominatim('index', *params) == 0
296 assert bnd_mock.called == do_bnds
297 assert rank_mock.called == do_ranks
299 @pytest.mark.parametrize("no_replace", [(True), (False)])
300 def test_special_phrases_wiki_command(self, mock_func_factory, no_replace):
301 func = mock_func_factory(nominatim.clicmd.special_phrases.SPImporter, 'import_phrases')
304 self.call_nominatim('special-phrases', '--import-from-wiki', '--no-replace')
306 self.call_nominatim('special-phrases', '--import-from-wiki')
308 assert func.called == 1
310 @pytest.mark.parametrize("no_replace", [(True), (False)])
311 def test_special_phrases_csv_command(self, src_dir, mock_func_factory, no_replace):
312 func = mock_func_factory(nominatim.clicmd.special_phrases.SPImporter, 'import_phrases')
313 testdata = src_dir / 'test' / 'testdb'
314 csv_path = str((testdata / 'full_en_phrases_test.csv').resolve())
317 self.call_nominatim('special-phrases', '--import-from-csv', csv_path, '--no-replace')
319 self.call_nominatim('special-phrases', '--import-from-csv', csv_path)
321 assert func.called == 1
323 @pytest.mark.parametrize("command,func", [
324 ('address-levels', 'load_address_levels_from_config'),
325 ('wiki-data', 'import_wikipedia_articles'),
326 ('importance', 'recompute_importance'),
327 ('website', 'setup_website'),
329 def test_refresh_command(self, mock_func_factory, command, func):
330 func_mock = mock_func_factory(nominatim.tools.refresh, func)
332 assert self.call_nominatim('refresh', '--' + command) == 0
333 assert func_mock.called == 1
336 def test_refresh_word_count(self):
337 assert self.call_nominatim('refresh', '--word-count') == 0
338 assert self.tokenizer_mock.update_statistics_called
341 def test_refresh_postcodes(self, mock_func_factory, place_table):
342 func_mock = mock_func_factory(nominatim.tools.postcodes, 'update_postcodes')
343 idx_mock = mock_func_factory(nominatim.indexer.indexer.Indexer, 'index_postcodes')
345 assert self.call_nominatim('refresh', '--postcodes') == 0
346 assert func_mock.called == 1
347 assert idx_mock.called == 1
349 def test_refresh_create_functions(self, mock_func_factory):
350 func_mock = mock_func_factory(nominatim.tools.refresh, 'create_functions')
352 assert self.call_nominatim('refresh', '--functions') == 0
353 assert func_mock.called == 1
354 assert self.tokenizer_mock.update_sql_functions_called
357 def test_refresh_importance_computed_after_wiki_import(self, monkeypatch):
359 monkeypatch.setattr(nominatim.tools.refresh, 'import_wikipedia_articles',
360 lambda *args, **kwargs: calls.append('import') or 0)
361 monkeypatch.setattr(nominatim.tools.refresh, 'recompute_importance',
362 lambda *args, **kwargs: calls.append('update'))
364 assert self.call_nominatim('refresh', '--importance', '--wiki-data') == 0
366 assert calls == ['import', 'update']