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
25 from mocks import MockParamCapture
27 SRC_DIR = (Path(__file__) / '..' / '..' / '..').resolve()
29 def call_nominatim(*args):
30 return nominatim.cli.nominatim(module_dir='build/module',
31 osm2pgsql_path='build/osm2pgsql/osm2pgsql',
32 phplib_dir=str(SRC_DIR / 'lib-php'),
33 data_dir=str(SRC_DIR / 'data'),
34 phpcgi_path='/usr/bin/php-cgi',
35 sqllib_dir=str(SRC_DIR / 'lib-sql'),
36 config_dir=str(SRC_DIR / 'settings'),
41 def mock_run_legacy(monkeypatch):
42 mock = MockParamCapture()
43 monkeypatch.setattr(nominatim.cli, 'run_legacy_script', mock)
48 def mock_func_factory(monkeypatch):
49 def get_mock(module, func):
50 mock = MockParamCapture()
51 monkeypatch.setattr(module, func, mock)
57 def test_cli_help(capsys):
58 """ Running nominatim tool without arguments prints help.
60 assert 1 == call_nominatim()
62 captured = capsys.readouterr()
63 assert captured.out.startswith('usage:')
66 @pytest.mark.parametrize("command,script", [
67 (('add-data', '--file', 'foo.osm'), 'update'),
68 (('export',), 'export')
70 def test_legacy_commands_simple(mock_run_legacy, command, script):
71 assert 0 == call_nominatim(*command)
73 assert mock_run_legacy.called == 1
74 assert mock_run_legacy.last_args[0] == script + '.php'
77 def test_import_missing_file(temp_db):
78 assert 1 == call_nominatim('import', '--osm-file', 'sfsafegweweggdgw.reh.erh')
81 def test_import_bad_file(temp_db):
82 assert 1 == call_nominatim('import', '--osm-file', '.')
85 def test_import_full(temp_db, mock_func_factory):
87 mock_func_factory(nominatim.tools.database_import, 'setup_database_skeleton'),
88 mock_func_factory(nominatim.tools.database_import, 'install_module'),
89 mock_func_factory(nominatim.tools.database_import, 'import_osm_data'),
90 mock_func_factory(nominatim.tools.refresh, 'import_wikipedia_articles'),
91 mock_func_factory(nominatim.tools.database_import, 'truncate_data_tables'),
92 mock_func_factory(nominatim.tools.database_import, 'load_data'),
93 mock_func_factory(nominatim.tools.database_import, 'create_tables'),
94 mock_func_factory(nominatim.tools.database_import, 'create_table_triggers'),
95 mock_func_factory(nominatim.tools.database_import, 'create_partition_tables'),
96 mock_func_factory(nominatim.tools.database_import, 'create_search_indices'),
97 mock_func_factory(nominatim.tools.database_import, 'create_country_names'),
98 mock_func_factory(nominatim.tools.refresh, 'load_address_levels_from_file'),
99 mock_func_factory(nominatim.indexer.indexer.Indexer, 'index_full'),
100 mock_func_factory(nominatim.tools.refresh, 'setup_website'),
101 mock_func_factory(nominatim.db.properties, 'set_property')
104 cf_mock = mock_func_factory(nominatim.tools.refresh, 'create_functions')
105 mock_func_factory(nominatim.clicmd.setup, 'run_legacy_script')
107 assert 0 == call_nominatim('import', '--osm-file', __file__)
109 assert cf_mock.called > 1
112 assert mock.called == 1
114 def test_freeze_command(mock_func_factory, temp_db):
115 mock_drop = mock_func_factory(nominatim.tools.freeze, 'drop_update_tables')
116 mock_flatnode = mock_func_factory(nominatim.tools.freeze, 'drop_flatnode_file')
118 assert 0 == call_nominatim('freeze')
120 assert mock_drop.called == 1
121 assert mock_flatnode.called == 1
124 @pytest.mark.parametrize("params", [('--warm', ),
125 ('--warm', '--reverse-only'),
126 ('--warm', '--search-only')])
127 def test_admin_command_legacy(mock_func_factory, params):
128 mock_run_legacy = mock_func_factory(nominatim.clicmd.admin, 'run_legacy_script')
130 assert 0 == call_nominatim('admin', *params)
132 assert mock_run_legacy.called == 1
135 @pytest.mark.parametrize("func, params", [('analyse_indexing', ('--analyse-indexing', ))])
136 def test_admin_command_tool(temp_db, mock_func_factory, func, params):
137 mock = mock_func_factory(nominatim.tools.admin, func)
139 assert 0 == call_nominatim('admin', *params)
140 assert mock.called == 1
143 def test_admin_command_check_database(mock_func_factory):
144 mock = mock_func_factory(nominatim.tools.check_database, 'check_database')
146 assert 0 == call_nominatim('admin', '--check-database')
147 assert mock.called == 1
150 @pytest.mark.parametrize("name,oid", [('file', 'foo.osm'), ('diff', 'foo.osc'),
151 ('node', 12), ('way', 8), ('relation', 32)])
152 def test_add_data_command(mock_run_legacy, name, oid):
153 assert 0 == call_nominatim('add-data', '--' + name, str(oid))
155 assert mock_run_legacy.called == 1
156 assert mock_run_legacy.last_args == ('update.php', '--import-' + name, oid)
159 @pytest.mark.parametrize("params,do_bnds,do_ranks", [
161 (['--boundaries-only'], 1, 0),
162 (['--no-boundaries'], 0, 1),
163 (['--boundaries-only', '--no-boundaries'], 0, 0)])
164 def test_index_command(mock_func_factory, temp_db_cursor, params, do_bnds, do_ranks):
165 temp_db_cursor.execute("CREATE TABLE import_status (indexed bool)")
166 bnd_mock = mock_func_factory(nominatim.indexer.indexer.Indexer, 'index_boundaries')
167 rank_mock = mock_func_factory(nominatim.indexer.indexer.Indexer, 'index_by_rank')
169 assert 0 == call_nominatim('index', *params)
171 assert bnd_mock.called == do_bnds
172 assert rank_mock.called == do_ranks
174 def test_special_phrases_command(temp_db, mock_func_factory):
175 func = mock_func_factory(nominatim.clicmd.special_phrases, 'import_from_wiki')
177 call_nominatim('import-special-phrases', '--from-wiki')
179 assert func.called == 1
181 @pytest.mark.parametrize("command,func", [
182 ('postcodes', 'update_postcodes'),
183 ('word-counts', 'recompute_word_counts'),
184 ('address-levels', 'load_address_levels_from_file'),
185 ('functions', 'create_functions'),
186 ('wiki-data', 'import_wikipedia_articles'),
187 ('importance', 'recompute_importance'),
188 ('website', 'setup_website'),
190 def test_refresh_command(mock_func_factory, temp_db, command, func):
191 func_mock = mock_func_factory(nominatim.tools.refresh, func)
193 assert 0 == call_nominatim('refresh', '--' + command)
194 assert func_mock.called == 1
197 def test_refresh_importance_computed_after_wiki_import(monkeypatch, temp_db):
199 monkeypatch.setattr(nominatim.tools.refresh, 'import_wikipedia_articles',
200 lambda *args, **kwargs: calls.append('import') or 0)
201 monkeypatch.setattr(nominatim.tools.refresh, 'recompute_importance',
202 lambda *args, **kwargs: calls.append('update'))
204 assert 0 == call_nominatim('refresh', '--importance', '--wiki-data')
206 assert calls == ['import', 'update']
209 def test_serve_command(mock_func_factory):
210 func = mock_func_factory(nominatim.cli, 'run_php_server')
212 call_nominatim('serve')
214 assert func.called == 1
216 @pytest.mark.parametrize("params", [
217 ('search', '--query', 'new'),
218 ('reverse', '--lat', '0', '--lon', '0'),
219 ('lookup', '--id', 'N1'),
220 ('details', '--node', '1'),
221 ('details', '--way', '1'),
222 ('details', '--relation', '1'),
223 ('details', '--place_id', '10001'),
226 def test_api_commands_simple(mock_func_factory, params):
227 mock_run_api = mock_func_factory(nominatim.clicmd.api, 'run_api_script')
229 assert 0 == call_nominatim(*params)
231 assert mock_run_api.called == 1
232 assert mock_run_api.last_args[0] == params[0]