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
14 import nominatim.clicmd.api
15 import nominatim.clicmd.refresh
16 import nominatim.clicmd.admin
17 import nominatim.indexer.indexer
18 import nominatim.tools.admin
19 import nominatim.tools.check_database
20 import nominatim.tools.freeze
21 import nominatim.tools.refresh
22 import nominatim.tools.replication
23 from nominatim.errors import UsageError
24 from nominatim.db import status
26 def call_nominatim(*args):
27 return nominatim.cli.nominatim(module_dir='build/module',
28 osm2pgsql_path='build/osm2pgsql/osm2pgsql',
31 phpcgi_path='/usr/bin/php-cgi',
33 config_dir='settings',
36 class MockParamCapture:
37 """ Mock that records the parameters with which a function was called
38 as well as the number of calls.
40 def __init__(self, retval=0):
42 self.return_value = retval
44 def __call__(self, *args, **kwargs):
47 self.last_kwargs = kwargs
48 return self.return_value
51 def mock_run_legacy(monkeypatch):
52 mock = MockParamCapture()
53 monkeypatch.setattr(nominatim.cli, 'run_legacy_script', mock)
57 def mock_func_factory(monkeypatch):
58 def get_mock(module, func):
59 mock = MockParamCapture()
60 monkeypatch.setattr(module, func, mock)
65 def test_cli_help(capsys):
66 """ Running nominatim tool without arguments prints help.
68 assert 1 == call_nominatim()
70 captured = capsys.readouterr()
71 assert captured.out.startswith('usage:')
74 @pytest.mark.parametrize("command,script", [
75 (('import', '--continue', 'load-data'), 'setup'),
76 (('special-phrases',), 'specialphrases'),
77 (('add-data', '--tiger-data', 'tiger'), 'setup'),
78 (('add-data', '--file', 'foo.osm'), 'update'),
79 (('export',), 'export')
81 def test_legacy_commands_simple(mock_run_legacy, command, script):
82 assert 0 == call_nominatim(*command)
84 assert mock_run_legacy.called == 1
85 assert mock_run_legacy.last_args[0] == script + '.php'
88 def test_freeze_command(mock_func_factory, temp_db):
89 mock_drop = mock_func_factory(nominatim.tools.freeze, 'drop_update_tables')
90 mock_flatnode = mock_func_factory(nominatim.tools.freeze, 'drop_flatnode_file')
92 assert 0 == call_nominatim('freeze')
94 assert mock_drop.called == 1
95 assert mock_flatnode.called == 1
98 @pytest.mark.parametrize("params", [('--warm', ),
99 ('--warm', '--reverse-only'),
100 ('--warm', '--search-only')])
101 def test_admin_command_legacy(mock_func_factory, params):
102 mock_run_legacy = mock_func_factory(nominatim.clicmd.admin, 'run_legacy_script')
104 assert 0 == call_nominatim('admin', *params)
106 assert mock_run_legacy.called == 1
109 @pytest.mark.parametrize("func, params", [('analyse_indexing', ('--analyse-indexing', ))])
110 def test_admin_command_tool(temp_db, mock_func_factory, func, params):
111 mock = mock_func_factory(nominatim.tools.admin, func)
113 assert 0 == call_nominatim('admin', *params)
114 assert mock.called == 1
117 def test_admin_command_check_database(mock_func_factory):
118 mock = mock_func_factory(nominatim.tools.check_database, 'check_database')
120 assert 0 == call_nominatim('admin', '--check-database')
121 assert mock.called == 1
124 @pytest.mark.parametrize("name,oid", [('file', 'foo.osm'), ('diff', 'foo.osc'),
125 ('node', 12), ('way', 8), ('relation', 32)])
126 def test_add_data_command(mock_run_legacy, name, oid):
127 assert 0 == call_nominatim('add-data', '--' + name, str(oid))
129 assert mock_run_legacy.called == 1
130 assert mock_run_legacy.last_args == ('update.php', '--import-' + name, oid)
133 @pytest.mark.parametrize("params,do_bnds,do_ranks", [
135 (['--boundaries-only'], 1, 0),
136 (['--no-boundaries'], 0, 1),
137 (['--boundaries-only', '--no-boundaries'], 0, 0)])
138 def test_index_command(mock_func_factory, temp_db_cursor, params, do_bnds, do_ranks):
139 temp_db_cursor.execute("CREATE TABLE import_status (indexed bool)")
140 bnd_mock = mock_func_factory(nominatim.indexer.indexer.Indexer, 'index_boundaries')
141 rank_mock = mock_func_factory(nominatim.indexer.indexer.Indexer, 'index_by_rank')
143 assert 0 == call_nominatim('index', *params)
145 assert bnd_mock.called == do_bnds
146 assert rank_mock.called == do_ranks
149 @pytest.mark.parametrize("command,params", [
150 ('wiki-data', ('setup.php', '--import-wikipedia-articles')),
151 ('importance', ('update.php', '--recompute-importance')),
153 def test_refresh_legacy_command(mock_func_factory, temp_db, command, params):
154 mock_run_legacy = mock_func_factory(nominatim.clicmd.refresh, 'run_legacy_script')
156 assert 0 == call_nominatim('refresh', '--' + command)
158 assert mock_run_legacy.called == 1
159 assert len(mock_run_legacy.last_args) >= len(params)
160 assert mock_run_legacy.last_args[:len(params)] == params
162 @pytest.mark.parametrize("command,func", [
163 ('postcodes', 'update_postcodes'),
164 ('word-counts', 'recompute_word_counts'),
165 ('address-levels', 'load_address_levels_from_file'),
166 ('functions', 'create_functions'),
167 ('website', 'setup_website'),
169 def test_refresh_command(mock_func_factory, temp_db, command, func):
170 func_mock = mock_func_factory(nominatim.tools.refresh, func)
172 assert 0 == call_nominatim('refresh', '--' + command)
173 assert func_mock.called == 1
176 def test_refresh_importance_computed_after_wiki_import(mock_func_factory, temp_db):
177 mock_run_legacy = mock_func_factory(nominatim.clicmd.refresh, 'run_legacy_script')
179 assert 0 == call_nominatim('refresh', '--importance', '--wiki-data')
181 assert mock_run_legacy.called == 2
182 assert mock_run_legacy.last_args == ('update.php', '--recompute-importance')
185 @pytest.mark.parametrize("params,func", [
186 (('--init', '--no-update-functions'), 'init_replication'),
187 (('--check-for-updates',), 'check_for_updates')
189 def test_replication_command(mock_func_factory, temp_db, params, func):
190 func_mock = mock_func_factory(nominatim.tools.replication, func)
192 assert 0 == call_nominatim('replication', *params)
193 assert func_mock.called == 1
196 def test_replication_update_bad_interval(monkeypatch, temp_db):
197 monkeypatch.setenv('NOMINATIM_REPLICATION_UPDATE_INTERVAL', 'xx')
199 assert call_nominatim('replication') == 1
202 def test_replication_update_bad_interval_for_geofabrik(monkeypatch, temp_db):
203 monkeypatch.setenv('NOMINATIM_REPLICATION_URL',
204 'https://download.geofabrik.de/europe/ireland-and-northern-ireland-updates')
206 assert call_nominatim('replication') == 1
209 @pytest.mark.parametrize("state", [nominatim.tools.replication.UpdateState.UP_TO_DATE,
210 nominatim.tools.replication.UpdateState.NO_CHANGES])
211 def test_replication_update_once_no_index(mock_func_factory, temp_db, temp_db_conn,
212 status_table, state):
213 status.set_status(temp_db_conn, date=dt.datetime.now(dt.timezone.utc), seq=1)
214 func_mock = mock_func_factory(nominatim.tools.replication, 'update')
216 assert 0 == call_nominatim('replication', '--once', '--no-index')
219 def test_replication_update_continuous(monkeypatch, temp_db_conn, status_table):
220 status.set_status(temp_db_conn, date=dt.datetime.now(dt.timezone.utc), seq=1)
221 states = [nominatim.tools.replication.UpdateState.UP_TO_DATE,
222 nominatim.tools.replication.UpdateState.UP_TO_DATE]
223 monkeypatch.setattr(nominatim.tools.replication, 'update',
224 lambda *args, **kwargs: states.pop())
226 index_mock = MockParamCapture()
227 monkeypatch.setattr(nominatim.indexer.indexer.Indexer, 'index_boundaries', index_mock)
228 monkeypatch.setattr(nominatim.indexer.indexer.Indexer, 'index_by_rank', index_mock)
230 with pytest.raises(IndexError):
231 call_nominatim('replication')
233 assert index_mock.called == 4
236 def test_replication_update_continuous_no_change(monkeypatch, temp_db_conn, status_table):
237 status.set_status(temp_db_conn, date=dt.datetime.now(dt.timezone.utc), seq=1)
238 states = [nominatim.tools.replication.UpdateState.NO_CHANGES,
239 nominatim.tools.replication.UpdateState.UP_TO_DATE]
240 monkeypatch.setattr(nominatim.tools.replication, 'update',
241 lambda *args, **kwargs: states.pop())
243 index_mock = MockParamCapture()
244 monkeypatch.setattr(nominatim.indexer.indexer.Indexer, 'index_boundaries', index_mock)
245 monkeypatch.setattr(nominatim.indexer.indexer.Indexer, 'index_by_rank', index_mock)
247 sleep_mock = MockParamCapture()
248 monkeypatch.setattr(time, 'sleep', sleep_mock)
250 with pytest.raises(IndexError):
251 call_nominatim('replication')
253 assert index_mock.called == 2
254 assert sleep_mock.called == 1
255 assert sleep_mock.last_args[0] == 60
258 def test_serve_command(mock_func_factory):
259 func = mock_func_factory(nominatim.cli, 'run_php_server')
261 call_nominatim('serve')
263 assert func.called == 1
265 @pytest.mark.parametrize("params", [
266 ('search', '--query', 'new'),
267 ('reverse', '--lat', '0', '--lon', '0'),
268 ('lookup', '--id', 'N1'),
269 ('details', '--node', '1'),
270 ('details', '--way', '1'),
271 ('details', '--relation', '1'),
272 ('details', '--place_id', '10001'),
275 def test_api_commands_simple(mock_func_factory, params):
276 mock_run_api = mock_func_factory(nominatim.clicmd.api, 'run_api_script')
278 assert 0 == call_nominatim(*params)
280 assert mock_run_api.called == 1
281 assert mock_run_api.last_args[0] == params[0]