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)
61 def tokenizer_mock(monkeypatch):
63 def __init__(self, *args, **kwargs):
64 self.update_sql_functions_called = False
65 self.finalize_import_called = False
67 def update_sql_functions(self, *args):
68 self.update_sql_functions_called = True
70 def finalize_import(self, *args):
71 self.finalize_import_called = True
73 tok = DummyTokenizer()
74 monkeypatch.setattr(nominatim.tokenizer.factory, 'get_tokenizer_for_db' ,
76 monkeypatch.setattr(nominatim.tokenizer.factory, 'create_tokenizer' ,
82 def test_cli_help(capsys):
83 """ Running nominatim tool without arguments prints help.
85 assert 1 == call_nominatim()
87 captured = capsys.readouterr()
88 assert captured.out.startswith('usage:')
91 @pytest.mark.parametrize("command,script", [
92 (('add-data', '--file', 'foo.osm'), 'update'),
93 (('export',), 'export')
95 def test_legacy_commands_simple(mock_run_legacy, command, script):
96 assert 0 == call_nominatim(*command)
98 assert mock_run_legacy.called == 1
99 assert mock_run_legacy.last_args[0] == script + '.php'
102 def test_import_missing_file(temp_db):
103 assert 1 == call_nominatim('import', '--osm-file', 'sfsafegweweggdgw.reh.erh')
106 def test_import_bad_file(temp_db):
107 assert 1 == call_nominatim('import', '--osm-file', '.')
110 def test_import_full(temp_db, mock_func_factory, tokenizer_mock):
112 mock_func_factory(nominatim.tools.database_import, 'setup_database_skeleton'),
113 mock_func_factory(nominatim.tools.database_import, 'import_osm_data'),
114 mock_func_factory(nominatim.tools.refresh, 'import_wikipedia_articles'),
115 mock_func_factory(nominatim.tools.database_import, 'truncate_data_tables'),
116 mock_func_factory(nominatim.tools.database_import, 'load_data'),
117 mock_func_factory(nominatim.tools.database_import, 'create_tables'),
118 mock_func_factory(nominatim.tools.database_import, 'create_table_triggers'),
119 mock_func_factory(nominatim.tools.database_import, 'create_partition_tables'),
120 mock_func_factory(nominatim.tools.database_import, 'create_search_indices'),
121 mock_func_factory(nominatim.tools.database_import, 'create_country_names'),
122 mock_func_factory(nominatim.tools.refresh, 'load_address_levels_from_file'),
123 mock_func_factory(nominatim.tools.postcodes, 'import_postcodes'),
124 mock_func_factory(nominatim.indexer.indexer.Indexer, 'index_full'),
125 mock_func_factory(nominatim.tools.refresh, 'setup_website'),
126 mock_func_factory(nominatim.db.properties, 'set_property')
129 cf_mock = mock_func_factory(nominatim.tools.refresh, 'create_functions')
131 assert 0 == call_nominatim('import', '--osm-file', __file__)
132 assert tokenizer_mock.finalize_import_called
134 assert cf_mock.called > 1
137 assert mock.called == 1, "Mock '{}' not called".format(mock.func_name)
140 def test_import_continue_load_data(temp_db, mock_func_factory, tokenizer_mock):
142 mock_func_factory(nominatim.tools.database_import, 'truncate_data_tables'),
143 mock_func_factory(nominatim.tools.database_import, 'load_data'),
144 mock_func_factory(nominatim.tools.database_import, 'create_search_indices'),
145 mock_func_factory(nominatim.tools.database_import, 'create_country_names'),
146 mock_func_factory(nominatim.tools.postcodes, 'import_postcodes'),
147 mock_func_factory(nominatim.indexer.indexer.Indexer, 'index_full'),
148 mock_func_factory(nominatim.tools.refresh, 'setup_website'),
149 mock_func_factory(nominatim.db.properties, 'set_property')
152 assert 0 == call_nominatim('import', '--continue', 'load-data')
153 assert tokenizer_mock.finalize_import_called
156 assert mock.called == 1, "Mock '{}' not called".format(mock.func_name)
159 def test_import_continue_indexing(temp_db, mock_func_factory, placex_table,
160 temp_db_conn, tokenizer_mock):
162 mock_func_factory(nominatim.tools.database_import, 'create_search_indices'),
163 mock_func_factory(nominatim.tools.database_import, 'create_country_names'),
164 mock_func_factory(nominatim.indexer.indexer.Indexer, 'index_full'),
165 mock_func_factory(nominatim.tools.refresh, 'setup_website'),
166 mock_func_factory(nominatim.db.properties, 'set_property')
169 assert 0 == call_nominatim('import', '--continue', 'indexing')
172 assert mock.called == 1, "Mock '{}' not called".format(mock.func_name)
174 assert temp_db_conn.index_exists('idx_placex_pendingsector')
176 # Calling it again still works for the index
177 assert 0 == call_nominatim('import', '--continue', 'indexing')
178 assert temp_db_conn.index_exists('idx_placex_pendingsector')
181 def test_import_continue_postprocess(temp_db, mock_func_factory, tokenizer_mock):
183 mock_func_factory(nominatim.tools.database_import, 'create_search_indices'),
184 mock_func_factory(nominatim.tools.database_import, 'create_country_names'),
185 mock_func_factory(nominatim.tools.refresh, 'setup_website'),
186 mock_func_factory(nominatim.db.properties, 'set_property')
189 assert 0 == call_nominatim('import', '--continue', 'db-postprocess')
191 assert tokenizer_mock.finalize_import_called
194 assert mock.called == 1, "Mock '{}' not called".format(mock.func_name)
197 def test_freeze_command(mock_func_factory, temp_db):
198 mock_drop = mock_func_factory(nominatim.tools.freeze, 'drop_update_tables')
199 mock_flatnode = mock_func_factory(nominatim.tools.freeze, 'drop_flatnode_file')
201 assert 0 == call_nominatim('freeze')
203 assert mock_drop.called == 1
204 assert mock_flatnode.called == 1
207 @pytest.mark.parametrize("params", [('--warm', ),
208 ('--warm', '--reverse-only'),
209 ('--warm', '--search-only')])
210 def test_admin_command_legacy(mock_func_factory, params):
211 mock_run_legacy = mock_func_factory(nominatim.clicmd.admin, 'run_legacy_script')
213 assert 0 == call_nominatim('admin', *params)
215 assert mock_run_legacy.called == 1
218 @pytest.mark.parametrize("func, params", [('analyse_indexing', ('--analyse-indexing', ))])
219 def test_admin_command_tool(temp_db, mock_func_factory, func, params):
220 mock = mock_func_factory(nominatim.tools.admin, func)
222 assert 0 == call_nominatim('admin', *params)
223 assert mock.called == 1
226 def test_admin_command_check_database(mock_func_factory):
227 mock = mock_func_factory(nominatim.tools.check_database, 'check_database')
229 assert 0 == call_nominatim('admin', '--check-database')
230 assert mock.called == 1
233 @pytest.mark.parametrize("name,oid", [('file', 'foo.osm'), ('diff', 'foo.osc'),
234 ('node', 12), ('way', 8), ('relation', 32)])
235 def test_add_data_command(mock_run_legacy, name, oid):
236 assert 0 == call_nominatim('add-data', '--' + name, str(oid))
238 assert mock_run_legacy.called == 1
239 assert mock_run_legacy.last_args == ('update.php', '--import-' + name, oid)
242 @pytest.mark.parametrize("params,do_bnds,do_ranks", [
244 (['--boundaries-only'], 1, 0),
245 (['--no-boundaries'], 0, 1),
246 (['--boundaries-only', '--no-boundaries'], 0, 0)])
247 def test_index_command(mock_func_factory, temp_db_cursor, tokenizer_mock,
248 params, do_bnds, do_ranks):
249 temp_db_cursor.execute("CREATE TABLE import_status (indexed bool)")
250 bnd_mock = mock_func_factory(nominatim.indexer.indexer.Indexer, 'index_boundaries')
251 rank_mock = mock_func_factory(nominatim.indexer.indexer.Indexer, 'index_by_rank')
253 assert 0 == call_nominatim('index', *params)
255 assert bnd_mock.called == do_bnds
256 assert rank_mock.called == do_ranks
258 def test_special_phrases_command(temp_db, mock_func_factory, tokenizer_mock):
259 func = mock_func_factory(nominatim.clicmd.special_phrases.SPImporter, 'import_phrases')
261 call_nominatim('special-phrases', '--import-from-wiki')
263 assert func.called == 1
265 @pytest.mark.parametrize("command,func", [
266 ('postcodes', 'update_postcodes'),
267 ('word-counts', 'recompute_word_counts'),
268 ('address-levels', 'load_address_levels_from_file'),
269 ('wiki-data', 'import_wikipedia_articles'),
270 ('importance', 'recompute_importance'),
271 ('website', 'setup_website'),
273 def test_refresh_command(mock_func_factory, temp_db, command, func):
274 func_mock = mock_func_factory(nominatim.tools.refresh, func)
276 assert 0 == call_nominatim('refresh', '--' + command)
277 assert func_mock.called == 1
280 def test_refresh_create_functions(mock_func_factory, temp_db, tokenizer_mock):
281 func_mock = mock_func_factory(nominatim.tools.refresh, 'create_functions')
283 assert 0 == call_nominatim('refresh', '--functions')
284 assert func_mock.called == 1
285 assert tokenizer_mock.update_sql_functions_called
288 def test_refresh_importance_computed_after_wiki_import(monkeypatch, temp_db):
290 monkeypatch.setattr(nominatim.tools.refresh, 'import_wikipedia_articles',
291 lambda *args, **kwargs: calls.append('import') or 0)
292 monkeypatch.setattr(nominatim.tools.refresh, 'recompute_importance',
293 lambda *args, **kwargs: calls.append('update'))
295 assert 0 == call_nominatim('refresh', '--importance', '--wiki-data')
297 assert calls == ['import', 'update']
300 def test_serve_command(mock_func_factory):
301 func = mock_func_factory(nominatim.cli, 'run_php_server')
303 call_nominatim('serve')
305 assert func.called == 1
307 @pytest.mark.parametrize("params", [
308 ('search', '--query', 'new'),
309 ('reverse', '--lat', '0', '--lon', '0'),
310 ('lookup', '--id', 'N1'),
311 ('details', '--node', '1'),
312 ('details', '--way', '1'),
313 ('details', '--relation', '1'),
314 ('details', '--place_id', '10001'),
317 def test_api_commands_simple(mock_func_factory, params):
318 mock_run_api = mock_func_factory(nominatim.clicmd.api, 'run_api_script')
320 assert 0 == call_nominatim(*params)
322 assert mock_run_api.called == 1
323 assert mock_run_api.last_args[0] == params[0]