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', '--tiger-data', 'tiger'), 'setup'),
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.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
175 @pytest.mark.parametrize("command,func", [
176 ('postcodes', 'update_postcodes'),
177 ('word-counts', 'recompute_word_counts'),
178 ('address-levels', 'load_address_levels_from_file'),
179 ('functions', 'create_functions'),
180 ('wiki-data', 'import_wikipedia_articles'),
181 ('importance', 'recompute_importance'),
182 ('website', 'setup_website'),
184 def test_refresh_command(mock_func_factory, temp_db, command, func):
185 func_mock = mock_func_factory(nominatim.tools.refresh, func)
187 assert 0 == call_nominatim('refresh', '--' + command)
188 assert func_mock.called == 1
191 def test_refresh_importance_computed_after_wiki_import(monkeypatch, temp_db):
193 monkeypatch.setattr(nominatim.tools.refresh, 'import_wikipedia_articles',
194 lambda *args, **kwargs: calls.append('import') or 0)
195 monkeypatch.setattr(nominatim.tools.refresh, 'recompute_importance',
196 lambda *args, **kwargs: calls.append('update'))
198 assert 0 == call_nominatim('refresh', '--importance', '--wiki-data')
200 assert calls == ['import', 'update']
203 def test_serve_command(mock_func_factory):
204 func = mock_func_factory(nominatim.cli, 'run_php_server')
206 call_nominatim('serve')
208 assert func.called == 1
210 @pytest.mark.parametrize("params", [
211 ('search', '--query', 'new'),
212 ('reverse', '--lat', '0', '--lon', '0'),
213 ('lookup', '--id', 'N1'),
214 ('details', '--node', '1'),
215 ('details', '--way', '1'),
216 ('details', '--relation', '1'),
217 ('details', '--place_id', '10001'),
220 def test_api_commands_simple(mock_func_factory, params):
221 mock_run_api = mock_func_factory(nominatim.clicmd.api, 'run_api_script')
223 assert 0 == call_nominatim(*params)
225 assert mock_run_api.called == 1
226 assert mock_run_api.last_args[0] == params[0]