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, 'install_module'),
92 mock_func_factory(nominatim.tools.database_import, 'import_osm_data'),
93 mock_func_factory(nominatim.tools.refresh, 'import_wikipedia_articles'),
94 mock_func_factory(nominatim.tools.database_import, 'truncate_data_tables'),
95 mock_func_factory(nominatim.tools.database_import, 'load_data'),
96 mock_func_factory(nominatim.tools.database_import, 'create_tables'),
97 mock_func_factory(nominatim.tools.database_import, 'create_table_triggers'),
98 mock_func_factory(nominatim.tools.database_import, 'create_partition_tables'),
99 mock_func_factory(nominatim.tools.database_import, 'create_search_indices'),
100 mock_func_factory(nominatim.tools.database_import, 'create_country_names'),
101 mock_func_factory(nominatim.tokenizer.factory, 'create_tokenizer'),
102 mock_func_factory(nominatim.tools.refresh, 'load_address_levels_from_file'),
103 mock_func_factory(nominatim.tools.postcodes, 'import_postcodes'),
104 mock_func_factory(nominatim.indexer.indexer.Indexer, 'index_full'),
105 mock_func_factory(nominatim.tools.refresh, 'setup_website'),
106 mock_func_factory(nominatim.db.properties, 'set_property')
109 cf_mock = mock_func_factory(nominatim.tools.refresh, 'create_functions')
111 assert 0 == call_nominatim('import', '--osm-file', __file__)
113 assert cf_mock.called > 1
116 assert mock.called == 1, "Mock '{}' not called".format(mock.func_name)
119 def test_import_continue_load_data(temp_db, mock_func_factory):
121 mock_func_factory(nominatim.tools.database_import, 'truncate_data_tables'),
122 mock_func_factory(nominatim.tools.database_import, 'load_data'),
123 mock_func_factory(nominatim.tools.database_import, 'create_search_indices'),
124 mock_func_factory(nominatim.tools.database_import, 'create_country_names'),
125 mock_func_factory(nominatim.tokenizer.factory, 'create_tokenizer'),
126 mock_func_factory(nominatim.tools.postcodes, 'import_postcodes'),
127 mock_func_factory(nominatim.indexer.indexer.Indexer, 'index_full'),
128 mock_func_factory(nominatim.tools.refresh, 'setup_website'),
129 mock_func_factory(nominatim.db.properties, 'set_property')
132 assert 0 == call_nominatim('import', '--continue', 'load-data')
135 assert mock.called == 1, "Mock '{}' not called".format(mock.func_name)
138 def test_import_continue_indexing(temp_db, mock_func_factory, placex_table, temp_db_conn):
140 mock_func_factory(nominatim.tools.database_import, 'create_search_indices'),
141 mock_func_factory(nominatim.tools.database_import, 'create_country_names'),
142 mock_func_factory(nominatim.indexer.indexer.Indexer, 'index_full'),
143 mock_func_factory(nominatim.tokenizer.factory, 'create_tokenizer'),
144 mock_func_factory(nominatim.tools.refresh, 'setup_website'),
145 mock_func_factory(nominatim.db.properties, 'set_property')
148 assert 0 == call_nominatim('import', '--continue', 'indexing')
151 assert mock.called == 1, "Mock '{}' not called".format(mock.func_name)
153 assert temp_db_conn.index_exists('idx_placex_pendingsector')
155 # Calling it again still works for the index
156 assert 0 == call_nominatim('import', '--continue', 'indexing')
157 assert temp_db_conn.index_exists('idx_placex_pendingsector')
160 def test_import_continue_postprocess(temp_db, mock_func_factory):
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.tools.refresh, 'setup_website'),
165 mock_func_factory(nominatim.tokenizer.factory, 'create_tokenizer'),
166 mock_func_factory(nominatim.db.properties, 'set_property')
169 assert 0 == call_nominatim('import', '--continue', 'db-postprocess')
172 assert mock.called == 1, "Mock '{}' not called".format(mock.func_name)
175 def test_freeze_command(mock_func_factory, temp_db):
176 mock_drop = mock_func_factory(nominatim.tools.freeze, 'drop_update_tables')
177 mock_flatnode = mock_func_factory(nominatim.tools.freeze, 'drop_flatnode_file')
179 assert 0 == call_nominatim('freeze')
181 assert mock_drop.called == 1
182 assert mock_flatnode.called == 1
185 @pytest.mark.parametrize("params", [('--warm', ),
186 ('--warm', '--reverse-only'),
187 ('--warm', '--search-only')])
188 def test_admin_command_legacy(mock_func_factory, params):
189 mock_run_legacy = mock_func_factory(nominatim.clicmd.admin, 'run_legacy_script')
191 assert 0 == call_nominatim('admin', *params)
193 assert mock_run_legacy.called == 1
196 @pytest.mark.parametrize("func, params", [('analyse_indexing', ('--analyse-indexing', ))])
197 def test_admin_command_tool(temp_db, mock_func_factory, func, params):
198 mock = mock_func_factory(nominatim.tools.admin, func)
200 assert 0 == call_nominatim('admin', *params)
201 assert mock.called == 1
204 def test_admin_command_check_database(mock_func_factory):
205 mock = mock_func_factory(nominatim.tools.check_database, 'check_database')
207 assert 0 == call_nominatim('admin', '--check-database')
208 assert mock.called == 1
211 @pytest.mark.parametrize("name,oid", [('file', 'foo.osm'), ('diff', 'foo.osc'),
212 ('node', 12), ('way', 8), ('relation', 32)])
213 def test_add_data_command(mock_run_legacy, name, oid):
214 assert 0 == call_nominatim('add-data', '--' + name, str(oid))
216 assert mock_run_legacy.called == 1
217 assert mock_run_legacy.last_args == ('update.php', '--import-' + name, oid)
220 @pytest.mark.parametrize("params,do_bnds,do_ranks", [
222 (['--boundaries-only'], 1, 0),
223 (['--no-boundaries'], 0, 1),
224 (['--boundaries-only', '--no-boundaries'], 0, 0)])
225 def test_index_command(mock_func_factory, temp_db_cursor, params, do_bnds, do_ranks):
226 temp_db_cursor.execute("CREATE TABLE import_status (indexed bool)")
227 bnd_mock = mock_func_factory(nominatim.indexer.indexer.Indexer, 'index_boundaries')
228 rank_mock = mock_func_factory(nominatim.indexer.indexer.Indexer, 'index_by_rank')
230 assert 0 == call_nominatim('index', *params)
232 assert bnd_mock.called == do_bnds
233 assert rank_mock.called == do_ranks
235 def test_special_phrases_command(temp_db, mock_func_factory):
236 func = mock_func_factory(nominatim.clicmd.special_phrases.SpecialPhrasesImporter, 'import_from_wiki')
238 call_nominatim('special-phrases', '--import-from-wiki')
240 assert func.called == 1
242 @pytest.mark.parametrize("command,func", [
243 ('postcodes', 'update_postcodes'),
244 ('word-counts', 'recompute_word_counts'),
245 ('address-levels', 'load_address_levels_from_file'),
246 ('functions', 'create_functions'),
247 ('wiki-data', 'import_wikipedia_articles'),
248 ('importance', 'recompute_importance'),
249 ('website', 'setup_website'),
251 def test_refresh_command(mock_func_factory, temp_db, command, func):
252 func_mock = mock_func_factory(nominatim.tools.refresh, func)
254 assert 0 == call_nominatim('refresh', '--' + command)
255 assert func_mock.called == 1
258 def test_refresh_importance_computed_after_wiki_import(monkeypatch, temp_db):
260 monkeypatch.setattr(nominatim.tools.refresh, 'import_wikipedia_articles',
261 lambda *args, **kwargs: calls.append('import') or 0)
262 monkeypatch.setattr(nominatim.tools.refresh, 'recompute_importance',
263 lambda *args, **kwargs: calls.append('update'))
265 assert 0 == call_nominatim('refresh', '--importance', '--wiki-data')
267 assert calls == ['import', 'update']
270 def test_serve_command(mock_func_factory):
271 func = mock_func_factory(nominatim.cli, 'run_php_server')
273 call_nominatim('serve')
275 assert func.called == 1
277 @pytest.mark.parametrize("params", [
278 ('search', '--query', 'new'),
279 ('reverse', '--lat', '0', '--lon', '0'),
280 ('lookup', '--id', 'N1'),
281 ('details', '--node', '1'),
282 ('details', '--way', '1'),
283 ('details', '--relation', '1'),
284 ('details', '--place_id', '10001'),
287 def test_api_commands_simple(mock_func_factory, params):
288 mock_run_api = mock_func_factory(nominatim.clicmd.api, 'run_api_script')
290 assert 0 == call_nominatim(*params)
292 assert mock_run_api.called == 1
293 assert mock_run_api.last_args[0] == params[0]