1 # SPDX-License-Identifier: GPL-2.0-only
3 # This file is part of Nominatim. (https://nominatim.org)
5 # Copyright (C) 2022 by the Nominatim developer community.
6 # For a full list of authors see the git log.
8 Tests for import command of the command-line interface wrapper.
12 import nominatim.tools.database_import
13 import nominatim.tools.country_info
14 import nominatim.tools.refresh
15 import nominatim.tools.postcodes
16 import nominatim.indexer.indexer
17 import nominatim.db.properties
20 class TestCliImportWithDb:
22 @pytest.fixture(autouse=True)
23 def setup_cli_call(self, cli_call, temp_db, cli_tokenizer_mock):
24 self.call_nominatim = cli_call
25 self.tokenizer_mock = cli_tokenizer_mock
28 def test_import_missing_file(self):
29 assert self.call_nominatim('import', '--osm-file', 'sfsafegwedgw.reh.erh') == 1
32 def test_import_bad_file(self):
33 assert self.call_nominatim('import', '--osm-file', '.') == 1
36 @pytest.mark.parametrize('with_updates', [True, False])
37 def test_import_full(self, mock_func_factory, with_updates, place_table, property_table):
39 mock_func_factory(nominatim.tools.database_import, 'setup_database_skeleton'),
40 mock_func_factory(nominatim.tools.country_info, 'setup_country_tables'),
41 mock_func_factory(nominatim.tools.database_import, 'import_osm_data'),
42 mock_func_factory(nominatim.tools.refresh, 'import_wikipedia_articles'),
43 mock_func_factory(nominatim.tools.database_import, 'truncate_data_tables'),
44 mock_func_factory(nominatim.tools.database_import, 'load_data'),
45 mock_func_factory(nominatim.tools.database_import, 'create_tables'),
46 mock_func_factory(nominatim.tools.database_import, 'create_table_triggers'),
47 mock_func_factory(nominatim.tools.database_import, 'create_partition_tables'),
48 mock_func_factory(nominatim.tools.database_import, 'create_search_indices'),
49 mock_func_factory(nominatim.tools.country_info, 'create_country_names'),
50 mock_func_factory(nominatim.tools.refresh, 'load_address_levels_from_config'),
51 mock_func_factory(nominatim.tools.postcodes, 'update_postcodes'),
52 mock_func_factory(nominatim.indexer.indexer.Indexer, 'index_full'),
53 mock_func_factory(nominatim.tools.refresh, 'setup_website'),
56 params = ['import', '--osm-file', __file__]
59 mocks.append(mock_func_factory(nominatim.tools.freeze, 'drop_update_tables'))
60 params.append('--no-updates')
62 cf_mock = mock_func_factory(nominatim.tools.refresh, 'create_functions')
65 assert self.call_nominatim(*params) == 0
66 assert self.tokenizer_mock.finalize_import_called
68 assert cf_mock.called > 1
71 assert mock.called == 1, "Mock '{}' not called".format(mock.func_name)
74 def test_import_continue_load_data(self, mock_func_factory):
76 mock_func_factory(nominatim.tools.database_import, 'truncate_data_tables'),
77 mock_func_factory(nominatim.tools.database_import, 'load_data'),
78 mock_func_factory(nominatim.tools.database_import, 'create_search_indices'),
79 mock_func_factory(nominatim.tools.country_info, 'create_country_names'),
80 mock_func_factory(nominatim.tools.postcodes, 'update_postcodes'),
81 mock_func_factory(nominatim.indexer.indexer.Indexer, 'index_full'),
82 mock_func_factory(nominatim.tools.refresh, 'setup_website'),
83 mock_func_factory(nominatim.db.properties, 'set_property')
86 assert self.call_nominatim('import', '--continue', 'load-data') == 0
87 assert self.tokenizer_mock.finalize_import_called
90 assert mock.called == 1, "Mock '{}' not called".format(mock.func_name)
93 def test_import_continue_indexing(self, mock_func_factory, placex_table,
96 mock_func_factory(nominatim.tools.database_import, 'create_search_indices'),
97 mock_func_factory(nominatim.tools.country_info, 'create_country_names'),
98 mock_func_factory(nominatim.indexer.indexer.Indexer, 'index_full'),
99 mock_func_factory(nominatim.tools.refresh, 'setup_website'),
100 mock_func_factory(nominatim.db.properties, 'set_property')
103 assert self.call_nominatim('import', '--continue', 'indexing') == 0
106 assert mock.called == 1, "Mock '{}' not called".format(mock.func_name)
108 assert temp_db_conn.index_exists('idx_placex_pendingsector')
110 # Calling it again still works for the index
111 assert self.call_nominatim('import', '--continue', 'indexing') == 0
112 assert temp_db_conn.index_exists('idx_placex_pendingsector')
115 def test_import_continue_postprocess(self, mock_func_factory):
117 mock_func_factory(nominatim.tools.database_import, 'create_search_indices'),
118 mock_func_factory(nominatim.tools.country_info, 'create_country_names'),
119 mock_func_factory(nominatim.tools.refresh, 'setup_website'),
120 mock_func_factory(nominatim.db.properties, 'set_property')
123 assert self.call_nominatim('import', '--continue', 'db-postprocess') == 0
125 assert self.tokenizer_mock.finalize_import_called
128 assert mock.called == 1, "Mock '{}' not called".format(mock.func_name)