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
12 import nominatim.db.properties
14 import nominatim.clicmd.api
15 import nominatim.clicmd.refresh
16 import nominatim.clicmd.admin
17 import nominatim.clicmd.setup
18 import nominatim.indexer.indexer
19 import nominatim.tools.admin
20 import nominatim.tools.check_database
21 import nominatim.tools.database_import
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
29 SRC_DIR = (Path(__file__) / '..' / '..' / '..').resolve()
31 def call_nominatim(*args):
32 return nominatim.cli.nominatim(module_dir='build/module',
33 osm2pgsql_path='build/osm2pgsql/osm2pgsql',
34 phplib_dir=str(SRC_DIR / 'lib-php'),
35 data_dir=str(SRC_DIR / 'data'),
36 phpcgi_path='/usr/bin/php-cgi',
37 sqllib_dir=str(SRC_DIR / 'lib-sql'),
38 config_dir=str(SRC_DIR / 'settings'),
43 def mock_run_legacy(monkeypatch):
44 mock = MockParamCapture()
45 monkeypatch.setattr(nominatim.cli, 'run_legacy_script', mock)
50 def mock_func_factory(monkeypatch):
51 def get_mock(module, func):
52 mock = MockParamCapture()
54 monkeypatch.setattr(module, func, mock)
60 def test_cli_help(capsys):
61 """ Running nominatim tool without arguments prints help.
63 assert 1 == call_nominatim()
65 captured = capsys.readouterr()
66 assert captured.out.startswith('usage:')
69 @pytest.mark.parametrize("command,script", [
70 (('add-data', '--file', 'foo.osm'), 'update'),
71 (('export',), 'export')
73 def test_legacy_commands_simple(mock_run_legacy, command, script):
74 assert 0 == call_nominatim(*command)
76 assert mock_run_legacy.called == 1
77 assert mock_run_legacy.last_args[0] == script + '.php'
80 def test_import_missing_file(temp_db):
81 assert 1 == call_nominatim('import', '--osm-file', 'sfsafegweweggdgw.reh.erh')
84 def test_import_bad_file(temp_db):
85 assert 1 == call_nominatim('import', '--osm-file', '.')
88 def test_import_full(temp_db, mock_func_factory):
90 mock_func_factory(nominatim.tools.database_import, 'setup_database_skeleton'),
91 mock_func_factory(nominatim.tools.database_import, 'import_osm_data'),
92 mock_func_factory(nominatim.tools.refresh, 'import_wikipedia_articles'),
93 mock_func_factory(nominatim.tools.database_import, 'truncate_data_tables'),
94 mock_func_factory(nominatim.tools.database_import, 'load_data'),
95 mock_func_factory(nominatim.tools.database_import, 'create_tables'),
96 mock_func_factory(nominatim.tools.database_import, 'create_table_triggers'),
97 mock_func_factory(nominatim.tools.database_import, 'create_partition_tables'),
98 mock_func_factory(nominatim.tools.database_import, 'create_search_indices'),
99 mock_func_factory(nominatim.tools.database_import, 'create_country_names'),
100 mock_func_factory(nominatim.tokenizer.factory, 'create_tokenizer'),
101 mock_func_factory(nominatim.tools.refresh, 'load_address_levels_from_file'),
102 mock_func_factory(nominatim.tools.postcodes, 'import_postcodes'),
103 mock_func_factory(nominatim.indexer.indexer.Indexer, 'index_full'),
104 mock_func_factory(nominatim.tools.refresh, 'setup_website'),
105 mock_func_factory(nominatim.db.properties, 'set_property')
108 cf_mock = mock_func_factory(nominatim.tools.refresh, 'create_functions')
110 assert 0 == call_nominatim('import', '--osm-file', __file__)
112 assert cf_mock.called > 1
115 assert mock.called == 1, "Mock '{}' not called".format(mock.func_name)
118 def test_import_continue_load_data(temp_db, mock_func_factory):
120 mock_func_factory(nominatim.tools.database_import, 'truncate_data_tables'),
121 mock_func_factory(nominatim.tools.database_import, 'load_data'),
122 mock_func_factory(nominatim.tools.database_import, 'create_search_indices'),
123 mock_func_factory(nominatim.tools.database_import, 'create_country_names'),
124 mock_func_factory(nominatim.tokenizer.factory, 'create_tokenizer'),
125 mock_func_factory(nominatim.tools.postcodes, 'import_postcodes'),
126 mock_func_factory(nominatim.indexer.indexer.Indexer, 'index_full'),
127 mock_func_factory(nominatim.tools.refresh, 'setup_website'),
128 mock_func_factory(nominatim.db.properties, 'set_property')
131 assert 0 == call_nominatim('import', '--continue', 'load-data')
134 assert mock.called == 1, "Mock '{}' not called".format(mock.func_name)
137 def test_import_continue_indexing(temp_db, mock_func_factory, placex_table, temp_db_conn):
139 mock_func_factory(nominatim.tools.database_import, 'create_search_indices'),
140 mock_func_factory(nominatim.tools.database_import, 'create_country_names'),
141 mock_func_factory(nominatim.indexer.indexer.Indexer, 'index_full'),
142 mock_func_factory(nominatim.tokenizer.factory, 'get_tokenizer_for_db'),
143 mock_func_factory(nominatim.tools.refresh, 'setup_website'),
144 mock_func_factory(nominatim.db.properties, 'set_property')
147 assert 0 == call_nominatim('import', '--continue', 'indexing')
150 assert mock.called == 1, "Mock '{}' not called".format(mock.func_name)
152 assert temp_db_conn.index_exists('idx_placex_pendingsector')
154 # Calling it again still works for the index
155 assert 0 == call_nominatim('import', '--continue', 'indexing')
156 assert temp_db_conn.index_exists('idx_placex_pendingsector')
159 def test_import_continue_postprocess(temp_db, mock_func_factory):
161 mock_func_factory(nominatim.tools.database_import, 'create_search_indices'),
162 mock_func_factory(nominatim.tools.database_import, 'create_country_names'),
163 mock_func_factory(nominatim.tools.refresh, 'setup_website'),
164 mock_func_factory(nominatim.tokenizer.factory, 'get_tokenizer_for_db'),
165 mock_func_factory(nominatim.db.properties, 'set_property')
168 assert 0 == call_nominatim('import', '--continue', 'db-postprocess')
171 assert mock.called == 1, "Mock '{}' not called".format(mock.func_name)
174 def test_freeze_command(mock_func_factory, temp_db):
175 mock_drop = mock_func_factory(nominatim.tools.freeze, 'drop_update_tables')
176 mock_flatnode = mock_func_factory(nominatim.tools.freeze, 'drop_flatnode_file')
178 assert 0 == call_nominatim('freeze')
180 assert mock_drop.called == 1
181 assert mock_flatnode.called == 1
184 @pytest.mark.parametrize("params", [('--warm', ),
185 ('--warm', '--reverse-only'),
186 ('--warm', '--search-only')])
187 def test_admin_command_legacy(mock_func_factory, params):
188 mock_run_legacy = mock_func_factory(nominatim.clicmd.admin, 'run_legacy_script')
190 assert 0 == call_nominatim('admin', *params)
192 assert mock_run_legacy.called == 1
195 @pytest.mark.parametrize("func, params", [('analyse_indexing', ('--analyse-indexing', ))])
196 def test_admin_command_tool(temp_db, mock_func_factory, func, params):
197 mock = mock_func_factory(nominatim.tools.admin, func)
199 assert 0 == call_nominatim('admin', *params)
200 assert mock.called == 1
203 def test_admin_command_check_database(mock_func_factory):
204 mock = mock_func_factory(nominatim.tools.check_database, 'check_database')
206 assert 0 == call_nominatim('admin', '--check-database')
207 assert mock.called == 1
210 @pytest.mark.parametrize("name,oid", [('file', 'foo.osm'), ('diff', 'foo.osc'),
211 ('node', 12), ('way', 8), ('relation', 32)])
212 def test_add_data_command(mock_run_legacy, name, oid):
213 assert 0 == call_nominatim('add-data', '--' + name, str(oid))
215 assert mock_run_legacy.called == 1
216 assert mock_run_legacy.last_args == ('update.php', '--import-' + name, oid)
219 @pytest.mark.parametrize("params,do_bnds,do_ranks", [
221 (['--boundaries-only'], 1, 0),
222 (['--no-boundaries'], 0, 1),
223 (['--boundaries-only', '--no-boundaries'], 0, 0)])
224 def test_index_command(mock_func_factory, temp_db_cursor, params, do_bnds, do_ranks):
225 temp_db_cursor.execute("CREATE TABLE import_status (indexed bool)")
226 bnd_mock = mock_func_factory(nominatim.indexer.indexer.Indexer, 'index_boundaries')
227 rank_mock = mock_func_factory(nominatim.indexer.indexer.Indexer, 'index_by_rank')
229 assert 0 == call_nominatim('index', *params)
231 assert bnd_mock.called == do_bnds
232 assert rank_mock.called == do_ranks
234 def test_special_phrases_command(temp_db, mock_func_factory):
235 func = mock_func_factory(nominatim.clicmd.special_phrases.SpecialPhrasesImporter, 'import_from_wiki')
237 call_nominatim('special-phrases', '--import-from-wiki')
239 assert func.called == 1
241 @pytest.mark.parametrize("command,func", [
242 ('postcodes', 'update_postcodes'),
243 ('word-counts', 'recompute_word_counts'),
244 ('address-levels', 'load_address_levels_from_file'),
245 ('wiki-data', 'import_wikipedia_articles'),
246 ('importance', 'recompute_importance'),
247 ('website', 'setup_website'),
249 def test_refresh_command(mock_func_factory, temp_db, command, func):
250 func_mock = mock_func_factory(nominatim.tools.refresh, func)
252 assert 0 == call_nominatim('refresh', '--' + command)
253 assert func_mock.called == 1
256 def test_refresh_create_functions(mock_func_factory, monkeypatch, temp_db):
257 class DummyTokenizer:
258 def update_sql_functions(self, *args):
261 func_mock = mock_func_factory(nominatim.tools.refresh, 'create_functions')
262 tok = DummyTokenizer()
263 monkeypatch.setattr(nominatim.tokenizer.factory, 'get_tokenizer_for_db' ,
267 assert 0 == call_nominatim('refresh', '--functions')
268 assert func_mock.called == 1
269 assert hasattr(tok, 'called')
272 def test_refresh_importance_computed_after_wiki_import(monkeypatch, temp_db):
274 monkeypatch.setattr(nominatim.tools.refresh, 'import_wikipedia_articles',
275 lambda *args, **kwargs: calls.append('import') or 0)
276 monkeypatch.setattr(nominatim.tools.refresh, 'recompute_importance',
277 lambda *args, **kwargs: calls.append('update'))
279 assert 0 == call_nominatim('refresh', '--importance', '--wiki-data')
281 assert calls == ['import', 'update']
284 def test_serve_command(mock_func_factory):
285 func = mock_func_factory(nominatim.cli, 'run_php_server')
287 call_nominatim('serve')
289 assert func.called == 1
291 @pytest.mark.parametrize("params", [
292 ('search', '--query', 'new'),
293 ('reverse', '--lat', '0', '--lon', '0'),
294 ('lookup', '--id', 'N1'),
295 ('details', '--node', '1'),
296 ('details', '--way', '1'),
297 ('details', '--relation', '1'),
298 ('details', '--place_id', '10001'),
301 def test_api_commands_simple(mock_func_factory, params):
302 mock_run_api = mock_func_factory(nominatim.clicmd.api, 'run_api_script')
304 assert 0 == call_nominatim(*params)
306 assert mock_run_api.called == 1
307 assert mock_run_api.last_args[0] == params[0]