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 (('special-phrases',), 'specialphrases'),
68 (('add-data', '--file', 'foo.osm'), 'update'),
69 (('export',), 'export')
71 def test_legacy_commands_simple(mock_run_legacy, command, script):
72 assert 0 == call_nominatim(*command)
74 assert mock_run_legacy.called == 1
75 assert mock_run_legacy.last_args[0] == script + '.php'
78 def test_import_missing_file(temp_db):
79 assert 1 == call_nominatim('import', '--osm-file', 'sfsafegweweggdgw.reh.erh')
82 def test_import_bad_file(temp_db):
83 assert 1 == call_nominatim('import', '--osm-file', '.')
86 def test_import_full(temp_db, mock_func_factory):
88 mock_func_factory(nominatim.tools.database_import, 'setup_database_skeleton'),
89 mock_func_factory(nominatim.tools.database_import, 'install_module'),
90 mock_func_factory(nominatim.tools.database_import, 'import_osm_data'),
91 mock_func_factory(nominatim.tools.refresh, 'import_wikipedia_articles'),
92 mock_func_factory(nominatim.tools.database_import, 'truncate_data_tables'),
93 mock_func_factory(nominatim.tools.database_import, 'load_data'),
94 mock_func_factory(nominatim.tools.database_import, 'create_tables'),
95 mock_func_factory(nominatim.tools.database_import, 'create_table_triggers'),
96 mock_func_factory(nominatim.tools.database_import, 'create_partition_tables'),
97 mock_func_factory(nominatim.tools.database_import, 'create_search_indices'),
98 mock_func_factory(nominatim.tools.database_import, 'create_country_names'),
99 mock_func_factory(nominatim.tools.refresh, 'load_address_levels_from_file'),
100 mock_func_factory(nominatim.indexer.indexer.Indexer, 'index_full'),
101 mock_func_factory(nominatim.tools.refresh, 'setup_website'),
102 mock_func_factory(nominatim.db.properties, 'set_property')
105 cf_mock = mock_func_factory(nominatim.tools.refresh, 'create_functions')
106 mock_func_factory(nominatim.clicmd.setup, 'run_legacy_script')
108 assert 0 == call_nominatim('import', '--osm-file', __file__)
110 assert cf_mock.called > 1
113 assert mock.called == 1
115 def test_freeze_command(mock_func_factory, temp_db):
116 mock_drop = mock_func_factory(nominatim.tools.freeze, 'drop_update_tables')
117 mock_flatnode = mock_func_factory(nominatim.tools.freeze, 'drop_flatnode_file')
119 assert 0 == call_nominatim('freeze')
121 assert mock_drop.called == 1
122 assert mock_flatnode.called == 1
125 @pytest.mark.parametrize("params", [('--warm', ),
126 ('--warm', '--reverse-only'),
127 ('--warm', '--search-only')])
128 def test_admin_command_legacy(mock_func_factory, params):
129 mock_run_legacy = mock_func_factory(nominatim.clicmd.admin, 'run_legacy_script')
131 assert 0 == call_nominatim('admin', *params)
133 assert mock_run_legacy.called == 1
136 @pytest.mark.parametrize("func, params", [('analyse_indexing', ('--analyse-indexing', ))])
137 def test_admin_command_tool(temp_db, mock_func_factory, func, params):
138 mock = mock_func_factory(nominatim.tools.admin, func)
140 assert 0 == call_nominatim('admin', *params)
141 assert mock.called == 1
144 def test_admin_command_check_database(mock_func_factory):
145 mock = mock_func_factory(nominatim.tools.check_database, 'check_database')
147 assert 0 == call_nominatim('admin', '--check-database')
148 assert mock.called == 1
151 @pytest.mark.parametrize("name,oid", [('file', 'foo.osm'), ('diff', 'foo.osc'),
152 ('node', 12), ('way', 8), ('relation', 32)])
153 def test_add_data_command(mock_run_legacy, name, oid):
154 assert 0 == call_nominatim('add-data', '--' + name, str(oid))
156 assert mock_run_legacy.called == 1
157 assert mock_run_legacy.last_args == ('update.php', '--import-' + name, oid)
160 @pytest.mark.parametrize("params,do_bnds,do_ranks", [
162 (['--boundaries-only'], 1, 0),
163 (['--no-boundaries'], 0, 1),
164 (['--boundaries-only', '--no-boundaries'], 0, 0)])
165 def test_index_command(mock_func_factory, temp_db_cursor, params, do_bnds, do_ranks):
166 temp_db_cursor.execute("CREATE TABLE import_status (indexed bool)")
167 bnd_mock = mock_func_factory(nominatim.indexer.indexer.Indexer, 'index_boundaries')
168 rank_mock = mock_func_factory(nominatim.indexer.indexer.Indexer, 'index_by_rank')
170 assert 0 == call_nominatim('index', *params)
172 assert bnd_mock.called == do_bnds
173 assert rank_mock.called == do_ranks
176 @pytest.mark.parametrize("command,func", [
177 ('postcodes', 'update_postcodes'),
178 ('word-counts', 'recompute_word_counts'),
179 ('address-levels', 'load_address_levels_from_file'),
180 ('functions', 'create_functions'),
181 ('wiki-data', 'import_wikipedia_articles'),
182 ('importance', 'recompute_importance'),
183 ('website', 'setup_website'),
185 def test_refresh_command(mock_func_factory, temp_db, command, func):
186 func_mock = mock_func_factory(nominatim.tools.refresh, func)
188 assert 0 == call_nominatim('refresh', '--' + command)
189 assert func_mock.called == 1
192 def test_refresh_importance_computed_after_wiki_import(monkeypatch, temp_db):
194 monkeypatch.setattr(nominatim.tools.refresh, 'import_wikipedia_articles',
195 lambda *args, **kwargs: calls.append('import') or 0)
196 monkeypatch.setattr(nominatim.tools.refresh, 'recompute_importance',
197 lambda *args, **kwargs: calls.append('update'))
199 assert 0 == call_nominatim('refresh', '--importance', '--wiki-data')
201 assert calls == ['import', 'update']
204 def test_serve_command(mock_func_factory):
205 func = mock_func_factory(nominatim.cli, 'run_php_server')
207 call_nominatim('serve')
209 assert func.called == 1
211 @pytest.mark.parametrize("params", [
212 ('search', '--query', 'new'),
213 ('reverse', '--lat', '0', '--lon', '0'),
214 ('lookup', '--id', 'N1'),
215 ('details', '--node', '1'),
216 ('details', '--way', '1'),
217 ('details', '--relation', '1'),
218 ('details', '--place_id', '10001'),
221 def test_api_commands_simple(mock_func_factory, params):
222 mock_run_api = mock_func_factory(nominatim.clicmd.api, 'run_api_script')
224 assert 0 == call_nominatim(*params)
226 assert mock_run_api.called == 1
227 assert mock_run_api.last_args[0] == params[0]