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
26 from mocks import MockParamCapture
28 SRC_DIR = (Path(__file__) / '..' / '..' / '..').resolve()
30 def call_nominatim(*args):
31 return nominatim.cli.nominatim(module_dir='build/module',
32 osm2pgsql_path='build/osm2pgsql/osm2pgsql',
33 phplib_dir=str(SRC_DIR / 'lib-php'),
34 data_dir=str(SRC_DIR / 'data'),
35 phpcgi_path='/usr/bin/php-cgi',
36 sqllib_dir=str(SRC_DIR / 'lib-sql'),
37 config_dir=str(SRC_DIR / 'settings'),
42 def mock_run_legacy(monkeypatch):
43 mock = MockParamCapture()
44 monkeypatch.setattr(nominatim.cli, 'run_legacy_script', mock)
49 def mock_func_factory(monkeypatch):
50 def get_mock(module, func):
51 mock = MockParamCapture()
53 monkeypatch.setattr(module, func, mock)
59 def test_cli_help(capsys):
60 """ Running nominatim tool without arguments prints help.
62 assert 1 == call_nominatim()
64 captured = capsys.readouterr()
65 assert captured.out.startswith('usage:')
68 @pytest.mark.parametrize("command,script", [
69 (('add-data', '--file', 'foo.osm'), 'update'),
70 (('export',), 'export')
72 def test_legacy_commands_simple(mock_run_legacy, command, script):
73 assert 0 == call_nominatim(*command)
75 assert mock_run_legacy.called == 1
76 assert mock_run_legacy.last_args[0] == script + '.php'
79 def test_import_missing_file(temp_db):
80 assert 1 == call_nominatim('import', '--osm-file', 'sfsafegweweggdgw.reh.erh')
83 def test_import_bad_file(temp_db):
84 assert 1 == call_nominatim('import', '--osm-file', '.')
87 def test_import_full(temp_db, mock_func_factory):
89 mock_func_factory(nominatim.tools.database_import, 'setup_database_skeleton'),
90 mock_func_factory(nominatim.tools.database_import, 'install_module'),
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.tools.refresh, 'load_address_levels_from_file'),
101 mock_func_factory(nominatim.tools.postcodes, 'import_postcodes'),
102 mock_func_factory(nominatim.indexer.indexer.Indexer, 'index_full'),
103 mock_func_factory(nominatim.tools.refresh, 'setup_website'),
104 mock_func_factory(nominatim.db.properties, 'set_property')
107 cf_mock = mock_func_factory(nominatim.tools.refresh, 'create_functions')
109 assert 0 == call_nominatim('import', '--osm-file', __file__)
111 assert cf_mock.called > 1
114 assert mock.called == 1, "Mock '{}' not called".format(mock.func_name)
117 def test_import_continue_load_data(temp_db, mock_func_factory):
119 mock_func_factory(nominatim.tools.database_import, 'truncate_data_tables'),
120 mock_func_factory(nominatim.tools.database_import, 'load_data'),
121 mock_func_factory(nominatim.tools.database_import, 'create_search_indices'),
122 mock_func_factory(nominatim.tools.database_import, 'create_country_names'),
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 assert 0 == call_nominatim('import', '--continue', 'load-data')
132 assert mock.called == 1, "Mock '{}' not called".format(mock.func_name)
135 def test_import_continue_indexing(temp_db, mock_func_factory, placex_table, temp_db_conn):
137 mock_func_factory(nominatim.tools.database_import, 'create_search_indices'),
138 mock_func_factory(nominatim.tools.database_import, 'create_country_names'),
139 mock_func_factory(nominatim.indexer.indexer.Indexer, 'index_full'),
140 mock_func_factory(nominatim.tools.refresh, 'setup_website'),
141 mock_func_factory(nominatim.db.properties, 'set_property')
144 assert 0 == call_nominatim('import', '--continue', 'indexing')
147 assert mock.called == 1, "Mock '{}' not called".format(mock.func_name)
149 assert temp_db_conn.index_exists('idx_placex_pendingsector')
151 # Calling it again still works for the index
152 assert 0 == call_nominatim('import', '--continue', 'indexing')
153 assert temp_db_conn.index_exists('idx_placex_pendingsector')
156 def test_import_continue_postprocess(temp_db, mock_func_factory):
158 mock_func_factory(nominatim.tools.database_import, 'create_search_indices'),
159 mock_func_factory(nominatim.tools.database_import, 'create_country_names'),
160 mock_func_factory(nominatim.tools.refresh, 'setup_website'),
161 mock_func_factory(nominatim.db.properties, 'set_property')
164 assert 0 == call_nominatim('import', '--continue', 'db-postprocess')
167 assert mock.called == 1, "Mock '{}' not called".format(mock.func_name)
170 def test_freeze_command(mock_func_factory, temp_db):
171 mock_drop = mock_func_factory(nominatim.tools.freeze, 'drop_update_tables')
172 mock_flatnode = mock_func_factory(nominatim.tools.freeze, 'drop_flatnode_file')
174 assert 0 == call_nominatim('freeze')
176 assert mock_drop.called == 1
177 assert mock_flatnode.called == 1
180 @pytest.mark.parametrize("params", [('--warm', ),
181 ('--warm', '--reverse-only'),
182 ('--warm', '--search-only')])
183 def test_admin_command_legacy(mock_func_factory, params):
184 mock_run_legacy = mock_func_factory(nominatim.clicmd.admin, 'run_legacy_script')
186 assert 0 == call_nominatim('admin', *params)
188 assert mock_run_legacy.called == 1
191 @pytest.mark.parametrize("func, params", [('analyse_indexing', ('--analyse-indexing', ))])
192 def test_admin_command_tool(temp_db, mock_func_factory, func, params):
193 mock = mock_func_factory(nominatim.tools.admin, func)
195 assert 0 == call_nominatim('admin', *params)
196 assert mock.called == 1
199 def test_admin_command_check_database(mock_func_factory):
200 mock = mock_func_factory(nominatim.tools.check_database, 'check_database')
202 assert 0 == call_nominatim('admin', '--check-database')
203 assert mock.called == 1
206 @pytest.mark.parametrize("name,oid", [('file', 'foo.osm'), ('diff', 'foo.osc'),
207 ('node', 12), ('way', 8), ('relation', 32)])
208 def test_add_data_command(mock_run_legacy, name, oid):
209 assert 0 == call_nominatim('add-data', '--' + name, str(oid))
211 assert mock_run_legacy.called == 1
212 assert mock_run_legacy.last_args == ('update.php', '--import-' + name, oid)
215 @pytest.mark.parametrize("params,do_bnds,do_ranks", [
217 (['--boundaries-only'], 1, 0),
218 (['--no-boundaries'], 0, 1),
219 (['--boundaries-only', '--no-boundaries'], 0, 0)])
220 def test_index_command(mock_func_factory, temp_db_cursor, params, do_bnds, do_ranks):
221 temp_db_cursor.execute("CREATE TABLE import_status (indexed bool)")
222 bnd_mock = mock_func_factory(nominatim.indexer.indexer.Indexer, 'index_boundaries')
223 rank_mock = mock_func_factory(nominatim.indexer.indexer.Indexer, 'index_by_rank')
225 assert 0 == call_nominatim('index', *params)
227 assert bnd_mock.called == do_bnds
228 assert rank_mock.called == do_ranks
230 def test_special_phrases_command(temp_db, mock_func_factory):
231 func = mock_func_factory(nominatim.clicmd.special_phrases.SpecialPhrasesImporter, 'import_from_wiki')
233 call_nominatim('special-phrases', '--import-from-wiki')
235 assert func.called == 1
237 @pytest.mark.parametrize("command,func", [
238 ('postcodes', 'update_postcodes'),
239 ('word-counts', 'recompute_word_counts'),
240 ('address-levels', 'load_address_levels_from_file'),
241 ('functions', 'create_functions'),
242 ('wiki-data', 'import_wikipedia_articles'),
243 ('importance', 'recompute_importance'),
244 ('website', 'setup_website'),
246 def test_refresh_command(mock_func_factory, temp_db, command, func):
247 func_mock = mock_func_factory(nominatim.tools.refresh, func)
249 assert 0 == call_nominatim('refresh', '--' + command)
250 assert func_mock.called == 1
253 def test_refresh_importance_computed_after_wiki_import(monkeypatch, temp_db):
255 monkeypatch.setattr(nominatim.tools.refresh, 'import_wikipedia_articles',
256 lambda *args, **kwargs: calls.append('import') or 0)
257 monkeypatch.setattr(nominatim.tools.refresh, 'recompute_importance',
258 lambda *args, **kwargs: calls.append('update'))
260 assert 0 == call_nominatim('refresh', '--importance', '--wiki-data')
262 assert calls == ['import', 'update']
265 def test_serve_command(mock_func_factory):
266 func = mock_func_factory(nominatim.cli, 'run_php_server')
268 call_nominatim('serve')
270 assert func.called == 1
272 @pytest.mark.parametrize("params", [
273 ('search', '--query', 'new'),
274 ('reverse', '--lat', '0', '--lon', '0'),
275 ('lookup', '--id', 'N1'),
276 ('details', '--node', '1'),
277 ('details', '--way', '1'),
278 ('details', '--relation', '1'),
279 ('details', '--place_id', '10001'),
282 def test_api_commands_simple(mock_func_factory, params):
283 mock_run_api = mock_func_factory(nominatim.clicmd.api, 'run_api_script')
285 assert 0 == call_nominatim(*params)
287 assert mock_run_api.called == 1
288 assert mock_run_api.last_args[0] == params[0]