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
30 @pytest.fixture(autouse=True)
31 def setup_cli_call(self, cli_call):
32 self.call_nominatim = cli_call
35 def test_cli_help(self, capsys):
36 """ Running nominatim tool without arguments prints help.
38 assert self.call_nominatim() == 1
40 captured = capsys.readouterr()
41 assert captured.out.startswith('usage:')
44 @pytest.mark.parametrize("command,script", [
45 (('export',), 'export')
47 def test_legacy_commands_simple(self, mock_run_legacy, command, script):
48 assert self.call_nominatim(*command) == 0
50 assert mock_run_legacy.called == 1
51 assert mock_run_legacy.last_args[0] == script + '.php'
54 @pytest.mark.parametrize("params", [('--warm', ),
55 ('--warm', '--reverse-only'),
56 ('--warm', '--search-only')])
57 def test_admin_command_legacy(self, mock_func_factory, params):
58 mock_run_legacy = mock_func_factory(nominatim.clicmd.admin, 'run_legacy_script')
60 assert self.call_nominatim('admin', *params) == 0
62 assert mock_run_legacy.called == 1
65 def test_admin_command_check_database(self, mock_func_factory):
66 mock = mock_func_factory(nominatim.tools.check_database, 'check_database')
68 assert self.call_nominatim('admin', '--check-database') == 0
69 assert mock.called == 1
72 @pytest.mark.parametrize("name,oid", [('file', 'foo.osm'), ('diff', 'foo.osc')])
73 def test_add_data_file_command(self, mock_func_factory, name, oid):
74 mock_run_legacy = mock_func_factory(nominatim.tools.add_osm_data, 'add_data_from_file')
75 assert self.call_nominatim('add-data', '--' + name, str(oid)) == 0
77 assert mock_run_legacy.called == 1
80 @pytest.mark.parametrize("name,oid", [('node', 12), ('way', 8), ('relation', 32)])
81 def test_add_data_object_command(self, mock_func_factory, name, oid):
82 mock_run_legacy = mock_func_factory(nominatim.tools.add_osm_data, 'add_osm_object')
83 assert self.call_nominatim('add-data', '--' + name, str(oid)) == 0
85 assert mock_run_legacy.called == 1
88 def test_serve_command(self, mock_func_factory):
89 func = mock_func_factory(nominatim.cli, 'run_php_server')
91 self.call_nominatim('serve')
93 assert func.called == 1
96 @pytest.mark.parametrize("params", [('search', '--query', 'new'),
97 ('reverse', '--lat', '0', '--lon', '0'),
98 ('lookup', '--id', 'N1'),
99 ('details', '--node', '1'),
100 ('details', '--way', '1'),
101 ('details', '--relation', '1'),
102 ('details', '--place_id', '10001'),
104 class TestCliApiCall:
106 @pytest.fixture(autouse=True)
107 def setup_cli_call(self, cli_call):
108 self.call_nominatim = cli_call
110 def test_api_commands_simple(self, mock_func_factory, params, tmp_path):
111 (tmp_path / 'website').mkdir()
112 (tmp_path / 'website' / (params[0] + '.php')).write_text('')
113 mock_run_api = mock_func_factory(nominatim.clicmd.api, 'run_api_script')
115 assert self.call_nominatim(*params, '--project-dir', str(tmp_path)) == 0
117 assert mock_run_api.called == 1
118 assert mock_run_api.last_args[0] == params[0]
121 def test_bad_project_idr(self, mock_func_factory, params):
122 mock_run_api = mock_func_factory(nominatim.clicmd.api, 'run_api_script')
124 assert self.call_nominatim(*params) == 1
129 @pytest.fixture(autouse=True)
130 def setup_cli_call(self, cli_call, temp_db):
131 self.call_nominatim = cli_call
134 @pytest.fixture(autouse=True)
135 def setup_tokenizer_mock(self, monkeypatch):
136 class DummyTokenizer:
137 def __init__(self, *args, **kwargs):
138 self.update_sql_functions_called = False
139 self.finalize_import_called = False
140 self.update_statistics_called = False
142 def update_sql_functions(self, *args):
143 self.update_sql_functions_called = True
145 def finalize_import(self, *args):
146 self.finalize_import_called = True
148 def update_statistics(self):
149 self.update_statistics_called = True
152 tok = DummyTokenizer()
153 monkeypatch.setattr(nominatim.tokenizer.factory, 'get_tokenizer_for_db',
155 monkeypatch.setattr(nominatim.tokenizer.factory, 'create_tokenizer',
158 self.tokenizer_mock = tok
161 def test_import_missing_file(self):
162 assert self.call_nominatim('import', '--osm-file', 'sfsafegwedgw.reh.erh') == 1
165 def test_import_bad_file(self):
166 assert self.call_nominatim('import', '--osm-file', '.') == 1
169 def test_import_full(self, mock_func_factory):
171 mock_func_factory(nominatim.tools.database_import, 'setup_database_skeleton'),
172 mock_func_factory(nominatim.tools.country_info, 'setup_country_tables'),
173 mock_func_factory(nominatim.tools.database_import, 'import_osm_data'),
174 mock_func_factory(nominatim.tools.refresh, 'import_wikipedia_articles'),
175 mock_func_factory(nominatim.tools.database_import, 'truncate_data_tables'),
176 mock_func_factory(nominatim.tools.database_import, 'load_data'),
177 mock_func_factory(nominatim.tools.database_import, 'create_tables'),
178 mock_func_factory(nominatim.tools.database_import, 'create_table_triggers'),
179 mock_func_factory(nominatim.tools.database_import, 'create_partition_tables'),
180 mock_func_factory(nominatim.tools.database_import, 'create_search_indices'),
181 mock_func_factory(nominatim.tools.country_info, 'create_country_names'),
182 mock_func_factory(nominatim.tools.refresh, 'load_address_levels_from_config'),
183 mock_func_factory(nominatim.tools.postcodes, 'update_postcodes'),
184 mock_func_factory(nominatim.indexer.indexer.Indexer, 'index_full'),
185 mock_func_factory(nominatim.tools.refresh, 'setup_website'),
186 mock_func_factory(nominatim.db.properties, 'set_property')
189 cf_mock = mock_func_factory(nominatim.tools.refresh, 'create_functions')
191 assert self.call_nominatim('import', '--osm-file', __file__) == 0
192 assert self.tokenizer_mock.finalize_import_called
194 assert cf_mock.called > 1
197 assert mock.called == 1, "Mock '{}' not called".format(mock.func_name)
200 def test_import_continue_load_data(self, mock_func_factory):
202 mock_func_factory(nominatim.tools.database_import, 'truncate_data_tables'),
203 mock_func_factory(nominatim.tools.database_import, 'load_data'),
204 mock_func_factory(nominatim.tools.database_import, 'create_search_indices'),
205 mock_func_factory(nominatim.tools.country_info, 'create_country_names'),
206 mock_func_factory(nominatim.tools.postcodes, 'update_postcodes'),
207 mock_func_factory(nominatim.indexer.indexer.Indexer, 'index_full'),
208 mock_func_factory(nominatim.tools.refresh, 'setup_website'),
209 mock_func_factory(nominatim.db.properties, 'set_property')
212 assert self.call_nominatim('import', '--continue', 'load-data') == 0
213 assert self.tokenizer_mock.finalize_import_called
216 assert mock.called == 1, "Mock '{}' not called".format(mock.func_name)
219 def test_import_continue_indexing(self, mock_func_factory, placex_table,
222 mock_func_factory(nominatim.tools.database_import, 'create_search_indices'),
223 mock_func_factory(nominatim.tools.country_info, 'create_country_names'),
224 mock_func_factory(nominatim.indexer.indexer.Indexer, 'index_full'),
225 mock_func_factory(nominatim.tools.refresh, 'setup_website'),
226 mock_func_factory(nominatim.db.properties, 'set_property')
229 assert self.call_nominatim('import', '--continue', 'indexing') == 0
232 assert mock.called == 1, "Mock '{}' not called".format(mock.func_name)
234 assert temp_db_conn.index_exists('idx_placex_pendingsector')
236 # Calling it again still works for the index
237 assert self.call_nominatim('import', '--continue', 'indexing') == 0
238 assert temp_db_conn.index_exists('idx_placex_pendingsector')
241 def test_import_continue_postprocess(self, mock_func_factory):
243 mock_func_factory(nominatim.tools.database_import, 'create_search_indices'),
244 mock_func_factory(nominatim.tools.country_info, 'create_country_names'),
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', 'db-postprocess') == 0
251 assert self.tokenizer_mock.finalize_import_called
254 assert mock.called == 1, "Mock '{}' not called".format(mock.func_name)
257 def test_freeze_command(self, mock_func_factory):
258 mock_drop = mock_func_factory(nominatim.tools.freeze, 'drop_update_tables')
259 mock_flatnode = mock_func_factory(nominatim.tools.freeze, 'drop_flatnode_file')
261 assert self.call_nominatim('freeze') == 0
263 assert mock_drop.called == 1
264 assert mock_flatnode.called == 1
268 @pytest.mark.parametrize("func, params", [('analyse_indexing', ('--analyse-indexing', ))])
269 def test_admin_command_tool(self, mock_func_factory, func, params):
270 mock = mock_func_factory(nominatim.tools.admin, func)
272 assert self.call_nominatim('admin', *params) == 0
273 assert mock.called == 1
276 @pytest.mark.parametrize("params,do_bnds,do_ranks", [
278 (['--boundaries-only'], 1, 0),
279 (['--no-boundaries'], 0, 1),
280 (['--boundaries-only', '--no-boundaries'], 0, 0)])
281 def test_index_command(self, mock_func_factory, table_factory,
282 params, do_bnds, do_ranks):
283 table_factory('import_status', 'indexed bool')
284 bnd_mock = mock_func_factory(nominatim.indexer.indexer.Indexer, 'index_boundaries')
285 rank_mock = mock_func_factory(nominatim.indexer.indexer.Indexer, 'index_by_rank')
287 assert self.call_nominatim('index', *params) == 0
289 assert bnd_mock.called == do_bnds
290 assert rank_mock.called == do_ranks
292 @pytest.mark.parametrize("no_replace", [(True), (False)])
293 def test_special_phrases_wiki_command(self, mock_func_factory, no_replace):
294 func = mock_func_factory(nominatim.clicmd.special_phrases.SPImporter, 'import_phrases')
297 self.call_nominatim('special-phrases', '--import-from-wiki', '--no-replace')
299 self.call_nominatim('special-phrases', '--import-from-wiki')
301 assert func.called == 1
303 @pytest.mark.parametrize("no_replace", [(True), (False)])
304 def test_special_phrases_csv_command(self, src_dir, mock_func_factory, no_replace):
305 func = mock_func_factory(nominatim.clicmd.special_phrases.SPImporter, 'import_phrases')
306 testdata = src_dir / 'test' / 'testdb'
307 csv_path = str((testdata / 'full_en_phrases_test.csv').resolve())
310 self.call_nominatim('special-phrases', '--import-from-csv', csv_path, '--no-replace')
312 self.call_nominatim('special-phrases', '--import-from-csv', csv_path)
314 assert func.called == 1
316 @pytest.mark.parametrize("command,func", [
317 ('address-levels', 'load_address_levels_from_config'),
318 ('wiki-data', 'import_wikipedia_articles'),
319 ('importance', 'recompute_importance'),
320 ('website', 'setup_website'),
322 def test_refresh_command(self, mock_func_factory, command, func):
323 func_mock = mock_func_factory(nominatim.tools.refresh, func)
325 assert self.call_nominatim('refresh', '--' + command) == 0
326 assert func_mock.called == 1
329 def test_refresh_word_count(self):
330 assert self.call_nominatim('refresh', '--word-count') == 0
331 assert self.tokenizer_mock.update_statistics_called
334 def test_refresh_postcodes(self, mock_func_factory, place_table):
335 func_mock = mock_func_factory(nominatim.tools.postcodes, 'update_postcodes')
336 idx_mock = mock_func_factory(nominatim.indexer.indexer.Indexer, 'index_postcodes')
338 assert self.call_nominatim('refresh', '--postcodes') == 0
339 assert func_mock.called == 1
340 assert idx_mock.called == 1
342 def test_refresh_create_functions(self, mock_func_factory):
343 func_mock = mock_func_factory(nominatim.tools.refresh, 'create_functions')
345 assert self.call_nominatim('refresh', '--functions') == 0
346 assert func_mock.called == 1
347 assert self.tokenizer_mock.update_sql_functions_called
350 def test_refresh_importance_computed_after_wiki_import(self, monkeypatch):
352 monkeypatch.setattr(nominatim.tools.refresh, 'import_wikipedia_articles',
353 lambda *args, **kwargs: calls.append('import') or 0)
354 monkeypatch.setattr(nominatim.tools.refresh, 'recompute_importance',
355 lambda *args, **kwargs: calls.append('update'))
357 assert self.call_nominatim('refresh', '--importance', '--wiki-data') == 0
359 assert calls == ['import', 'update']