]> git.openstreetmap.org Git - nominatim.git/blob - test/python/test_cli.py
improve deadlock detection for various versions of psycopg2
[nominatim.git] / test / python / test_cli.py
1 """
2 Tests for command line interface wrapper.
3
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
6 the actual functions.
7 """
8 import datetime as dt
9 import psycopg2
10 import pytest
11 import time
12
13 import nominatim.cli
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
25
26 def call_nominatim(*args):
27     return nominatim.cli.nominatim(module_dir='build/module',
28                                    osm2pgsql_path='build/osm2pgsql/osm2pgsql',
29                                    phplib_dir='lib-php',
30                                    data_dir='.',
31                                    phpcgi_path='/usr/bin/php-cgi',
32                                    sqllib_dir='lib-sql',
33                                    config_dir='settings',
34                                    cli_args=args)
35
36 class MockParamCapture:
37     """ Mock that records the parameters with which a function was called
38         as well as the number of calls.
39     """
40     def __init__(self, retval=0):
41         self.called = 0
42         self.return_value = retval
43
44     def __call__(self, *args, **kwargs):
45         self.called += 1
46         self.last_args = args
47         self.last_kwargs = kwargs
48         return self.return_value
49
50 @pytest.fixture
51 def mock_run_legacy(monkeypatch):
52     mock = MockParamCapture()
53     monkeypatch.setattr(nominatim.cli, 'run_legacy_script', mock)
54     return mock
55
56 @pytest.fixture
57 def mock_func_factory(monkeypatch):
58     def get_mock(module, func):
59         mock = MockParamCapture()
60         monkeypatch.setattr(module, func, mock)
61         return mock
62
63     return get_mock
64
65 def test_cli_help(capsys):
66     """ Running nominatim tool without arguments prints help.
67     """
68     assert 1 == call_nominatim()
69
70     captured = capsys.readouterr()
71     assert captured.out.startswith('usage:')
72
73
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')
80                          ])
81 def test_legacy_commands_simple(mock_run_legacy, command, script):
82     assert 0 == call_nominatim(*command)
83
84     assert mock_run_legacy.called == 1
85     assert mock_run_legacy.last_args[0] == script + '.php'
86
87
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')
91
92     assert 0 == call_nominatim('freeze')
93
94     assert mock_drop.called == 1
95     assert mock_flatnode.called == 1
96
97
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')
103
104     assert 0 == call_nominatim('admin', *params)
105
106     assert mock_run_legacy.called == 1
107
108
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)
112
113     assert 0 == call_nominatim('admin', *params)
114     assert mock.called == 1
115
116
117 def test_admin_command_check_database(mock_func_factory):
118     mock = mock_func_factory(nominatim.tools.check_database, 'check_database')
119
120     assert 0 == call_nominatim('admin', '--check-database')
121     assert mock.called == 1
122
123
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))
128
129     assert mock_run_legacy.called == 1
130     assert mock_run_legacy.last_args == ('update.php', '--import-' + name, oid)
131
132
133 @pytest.mark.parametrize("params,do_bnds,do_ranks", [
134                           ([], 1, 1),
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')
142
143     assert 0 == call_nominatim('index', *params)
144
145     assert bnd_mock.called == do_bnds
146     assert rank_mock.called == do_ranks
147
148
149 @pytest.mark.parametrize("command,params", [
150                          ('wiki-data', ('setup.php', '--import-wikipedia-articles')),
151                          ('importance', ('update.php', '--recompute-importance')),
152                          ])
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')
155
156     assert 0 == call_nominatim('refresh', '--' + command)
157
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
161
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'),
168                          ])
169 def test_refresh_command(mock_func_factory, temp_db, command, func):
170     func_mock = mock_func_factory(nominatim.tools.refresh, func)
171
172     assert 0 == call_nominatim('refresh', '--' + command)
173     assert func_mock.called == 1
174
175
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')
178
179     assert 0 == call_nominatim('refresh', '--importance', '--wiki-data')
180
181     assert mock_run_legacy.called == 2
182     assert mock_run_legacy.last_args == ('update.php', '--recompute-importance')
183
184
185 @pytest.mark.parametrize("params,func", [
186                          (('--init', '--no-update-functions'), 'init_replication'),
187                          (('--check-for-updates',), 'check_for_updates')
188                          ])
189 def test_replication_command(mock_func_factory, temp_db, params, func):
190     func_mock = mock_func_factory(nominatim.tools.replication, func)
191
192     assert 0 == call_nominatim('replication', *params)
193     assert func_mock.called == 1
194
195
196 def test_replication_update_bad_interval(monkeypatch, temp_db):
197     monkeypatch.setenv('NOMINATIM_REPLICATION_UPDATE_INTERVAL', 'xx')
198
199     assert call_nominatim('replication') == 1
200
201
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')
205
206     assert call_nominatim('replication') == 1
207
208
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')
215
216     assert 0 == call_nominatim('replication', '--once', '--no-index')
217
218
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())
225
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)
229
230     with pytest.raises(IndexError):
231         call_nominatim('replication')
232
233     assert index_mock.called == 4
234
235
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())
242
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)
246
247     sleep_mock = MockParamCapture()
248     monkeypatch.setattr(time, 'sleep', sleep_mock)
249
250     with pytest.raises(IndexError):
251         call_nominatim('replication')
252
253     assert index_mock.called == 2
254     assert sleep_mock.called == 1
255     assert sleep_mock.last_args[0] == 60
256
257
258 def test_serve_command(mock_func_factory):
259     func = mock_func_factory(nominatim.cli, 'run_php_server')
260
261     call_nominatim('serve')
262
263     assert func.called == 1
264
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'),
273                          ('status',)
274                          ])
275 def test_api_commands_simple(mock_func_factory, params):
276     mock_run_api = mock_func_factory(nominatim.clicmd.api, 'run_api_script')
277
278     assert 0 == call_nominatim(*params)
279
280     assert mock_run_api.called == 1
281     assert mock_run_api.last_args[0] == params[0]