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
13 import nominatim.indexer.indexer
14 import nominatim.tools.refresh
15 import nominatim.tools.replication
17 def call_nominatim(*args):
18 return nominatim.cli.nominatim(module_dir='build/module',
19 osm2pgsql_path='build/osm2pgsql/osm2pgsql',
22 phpcgi_path='/usr/bin/php-cgi',
25 class MockParamCapture:
26 """ Mock that records the parameters with which a function was called
27 as well as the number of calls.
29 def __init__(self, retval=0):
31 self.return_value = retval
33 def __call__(self, *args, **kwargs):
36 self.last_kwargs = kwargs
37 return self.return_value
40 def mock_run_legacy(monkeypatch):
41 mock = MockParamCapture()
42 monkeypatch.setattr(nominatim.cli, 'run_legacy_script', mock)
46 def mock_run_api(monkeypatch):
47 mock = MockParamCapture()
48 monkeypatch.setattr(nominatim.cli, 'run_api_script', mock)
52 def test_cli_help(capsys):
53 """ Running nominatim tool without arguments prints help.
55 assert 1 == call_nominatim()
57 captured = capsys.readouterr()
58 assert captured.out.startswith('usage:')
61 @pytest.mark.parametrize("command,script", [
62 (('import', '--continue', 'load-data'), 'setup'),
63 (('freeze',), 'setup'),
64 (('special-phrases',), 'specialphrases'),
65 (('add-data', '--tiger-data', 'tiger'), 'setup'),
66 (('add-data', '--file', 'foo.osm'), 'update'),
67 (('check-database',), 'check_import_finished'),
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 @pytest.mark.parametrize("name,oid", [('file', 'foo.osm'), ('diff', 'foo.osc'),
79 ('node', 12), ('way', 8), ('relation', 32)])
80 def test_add_data_command(mock_run_legacy, name, oid):
81 assert 0 == call_nominatim('add-data', '--' + name, str(oid))
83 assert mock_run_legacy.called == 1
84 assert mock_run_legacy.last_args == ('update.php', '--import-' + name, oid)
87 @pytest.mark.parametrize("params,do_bnds,do_ranks", [
89 (['--boundaries-only'], 1, 0),
90 (['--no-boundaries'], 0, 1),
91 (['--boundaries-only', '--no-boundaries'], 0, 0)])
92 def test_index_command(monkeypatch, temp_db_cursor, params, do_bnds, do_ranks):
93 temp_db_cursor.execute("CREATE TABLE import_status (indexed bool)")
94 bnd_mock = MockParamCapture()
95 monkeypatch.setattr(nominatim.indexer.indexer.Indexer, 'index_boundaries', bnd_mock)
96 rank_mock = MockParamCapture()
97 monkeypatch.setattr(nominatim.indexer.indexer.Indexer, 'index_by_rank', rank_mock)
99 assert 0 == call_nominatim('index', *params)
101 assert bnd_mock.called == do_bnds
102 assert rank_mock.called == do_ranks
105 @pytest.mark.parametrize("command,params", [
106 ('wiki-data', ('setup.php', '--import-wikipedia-articles')),
107 ('importance', ('update.php', '--recompute-importance')),
108 ('website', ('setup.php', '--setup-website')),
110 def test_refresh_legacy_command(mock_run_legacy, temp_db, command, params):
111 assert 0 == call_nominatim('refresh', '--' + command)
113 assert mock_run_legacy.called == 1
114 assert len(mock_run_legacy.last_args) >= len(params)
115 assert mock_run_legacy.last_args[:len(params)] == params
117 @pytest.mark.parametrize("command,func", [
118 ('postcodes', 'update_postcodes'),
119 ('word-counts', 'recompute_word_counts'),
120 ('address-levels', 'load_address_levels_from_file'),
121 ('functions', 'create_functions'),
123 def test_refresh_command(monkeypatch, temp_db, command, func):
124 func_mock = MockParamCapture()
125 monkeypatch.setattr(nominatim.tools.refresh, func, func_mock)
127 assert 0 == call_nominatim('refresh', '--' + command)
128 assert func_mock.called == 1
131 def test_refresh_importance_computed_after_wiki_import(mock_run_legacy, temp_db):
132 assert 0 == call_nominatim('refresh', '--importance', '--wiki-data')
134 assert mock_run_legacy.called == 2
135 assert mock_run_legacy.last_args == ('update.php', '--recompute-importance')
138 @pytest.mark.parametrize("params,func", [
139 (('--init', '--no-update-functions'), 'init_replication'),
140 (('--check-for-updates',), 'check_for_updates')
142 def test_replication_command(monkeypatch, temp_db, params, func):
143 func_mock = MockParamCapture()
144 monkeypatch.setattr(nominatim.tools.replication, func, func_mock)
146 assert 0 == call_nominatim('replication', *params)
147 assert func_mock.called == 1
150 def test_replication_update_bad_interval(monkeypatch, temp_db):
151 monkeypatch.setenv('NOMINATIM_REPLICATION_UPDATE_INTERVAL', 'xx')
153 with pytest.raises(ValueError):
154 call_nominatim('replication')
157 def test_replication_update_bad_interval_for_geofabrik(monkeypatch, temp_db):
158 monkeypatch.setenv('NOMINATIM_REPLICATION_URL',
159 'https://download.geofabrik.de/europe/ireland-and-northern-ireland-updates')
161 with pytest.raises(RuntimeError, match='Invalid replication.*'):
162 call_nominatim('replication')
165 @pytest.mark.parametrize("state, retval", [
166 (nominatim.tools.replication.UpdateState.UP_TO_DATE, 0),
167 (nominatim.tools.replication.UpdateState.NO_CHANGES, 3)
169 def test_replication_update_once_no_index(monkeypatch, temp_db, status_table, state, retval):
170 func_mock = MockParamCapture(retval=state)
171 monkeypatch.setattr(nominatim.tools.replication, 'update', func_mock)
173 assert retval == call_nominatim('replication', '--once', '--no-index')
176 def test_replication_update_continuous(monkeypatch, status_table):
177 states = [nominatim.tools.replication.UpdateState.UP_TO_DATE,
178 nominatim.tools.replication.UpdateState.UP_TO_DATE]
179 monkeypatch.setattr(nominatim.tools.replication, 'update',
180 lambda *args, **kwargs: states.pop())
182 index_mock = MockParamCapture()
183 monkeypatch.setattr(nominatim.indexer.indexer.Indexer, 'index_boundaries', index_mock)
184 monkeypatch.setattr(nominatim.indexer.indexer.Indexer, 'index_by_rank', index_mock)
186 with pytest.raises(IndexError):
187 call_nominatim('replication')
189 assert index_mock.called == 4
192 def test_replication_update_continuous_no_change(monkeypatch, status_table):
193 states = [nominatim.tools.replication.UpdateState.NO_CHANGES,
194 nominatim.tools.replication.UpdateState.UP_TO_DATE]
195 monkeypatch.setattr(nominatim.tools.replication, 'update',
196 lambda *args, **kwargs: states.pop())
198 index_mock = MockParamCapture()
199 monkeypatch.setattr(nominatim.indexer.indexer.Indexer, 'index_boundaries', index_mock)
200 monkeypatch.setattr(nominatim.indexer.indexer.Indexer, 'index_by_rank', index_mock)
202 sleep_mock = MockParamCapture()
203 monkeypatch.setattr(time, 'sleep', sleep_mock)
205 with pytest.raises(IndexError):
206 call_nominatim('replication')
208 assert index_mock.called == 2
209 assert sleep_mock.called == 1
210 assert sleep_mock.last_args[0] == 60
213 @pytest.mark.parametrize("params", [
214 ('search', '--query', 'new'),
215 ('reverse', '--lat', '0', '--lon', '0'),
216 ('lookup', '--id', 'N1'),
217 ('details', '--node', '1'),
218 ('details', '--way', '1'),
219 ('details', '--relation', '1'),
220 ('details', '--place_id', '10001'),
223 def test_api_commands_simple(mock_run_api, params):
224 assert 0 == call_nominatim(*params)
226 assert mock_run_api.called == 1
227 assert mock_run_api.last_args[0] == params[0]