]> git.openstreetmap.org Git - nominatim.git/blob - test/python/test_cli_replication.py
simplify token precomputation
[nominatim.git] / test / python / test_cli_replication.py
1 """
2 Tests for replication command of command-line interface wrapper.
3 """
4 import datetime as dt
5 import time
6 from pathlib import Path
7
8 import pytest
9
10 import nominatim.cli
11 import nominatim.indexer.indexer
12 import nominatim.tools.replication
13 from nominatim.db import status
14
15 from mocks import MockParamCapture
16
17 SRC_DIR = (Path(__file__) / '..' / '..' / '..').resolve()
18
19 def call_nominatim(*args):
20     return nominatim.cli.nominatim(module_dir='build/module',
21                                    osm2pgsql_path='build/osm2pgsql/osm2pgsql',
22                                    phplib_dir=str(SRC_DIR / 'lib-php'),
23                                    data_dir=str(SRC_DIR / 'data'),
24                                    phpcgi_path='/usr/bin/php-cgi',
25                                    sqllib_dir=str(SRC_DIR / 'lib-sql'),
26                                    config_dir=str(SRC_DIR / 'settings'),
27                                    cli_args=['replication'] + list(args))
28
29 @pytest.fixture
30 def index_mock(monkeypatch):
31     mock = MockParamCapture()
32     monkeypatch.setattr(nominatim.indexer.indexer.Indexer, 'index_boundaries', mock)
33     monkeypatch.setattr(nominatim.indexer.indexer.Indexer, 'index_by_rank', mock)
34
35     return mock
36
37
38 @pytest.fixture
39 def mock_func_factory(monkeypatch):
40     def get_mock(module, func):
41         mock = MockParamCapture()
42         monkeypatch.setattr(module, func, mock)
43         return mock
44
45     return get_mock
46
47
48 @pytest.fixture
49 def init_status(temp_db_conn, status_table):
50     status.set_status(temp_db_conn, date=dt.datetime.now(dt.timezone.utc), seq=1)
51     return 1
52
53
54 @pytest.fixture
55 def update_mock(mock_func_factory, init_status):
56     return mock_func_factory(nominatim.tools.replication, 'update')
57
58 @pytest.mark.parametrize("params,func", [
59                          (('--init', '--no-update-functions'), 'init_replication'),
60                          (('--check-for-updates',), 'check_for_updates')
61                          ])
62 def test_replication_command(mock_func_factory, temp_db, params, func):
63     func_mock = mock_func_factory(nominatim.tools.replication, func)
64
65     assert 0 == call_nominatim(*params)
66     assert func_mock.called == 1
67
68
69 def test_replication_update_bad_interval(monkeypatch, temp_db):
70     monkeypatch.setenv('NOMINATIM_REPLICATION_UPDATE_INTERVAL', 'xx')
71
72     assert call_nominatim() == 1
73
74
75 def test_replication_update_bad_interval_for_geofabrik(monkeypatch, temp_db):
76     monkeypatch.setenv('NOMINATIM_REPLICATION_URL',
77                        'https://download.geofabrik.de/europe/ireland-and-northern-ireland-updates')
78
79     assert call_nominatim() == 1
80
81
82 def test_replication_update_once_no_index(update_mock):
83     assert 0 == call_nominatim('--once', '--no-index')
84
85     assert str(update_mock.last_args[1]['osm2pgsql']) == 'build/osm2pgsql/osm2pgsql'
86
87
88 def test_replication_update_custom_osm2pgsql(monkeypatch, update_mock):
89     monkeypatch.setenv('NOMINATIM_OSM2PGSQL_BINARY', '/secret/osm2pgsql')
90     assert 0 == call_nominatim('--once', '--no-index')
91
92     assert str(update_mock.last_args[1]['osm2pgsql']) == '/secret/osm2pgsql'
93
94
95 def test_replication_update_custom_threads(update_mock):
96     assert 0 == call_nominatim('--once', '--no-index', '--threads', '4')
97
98     assert update_mock.last_args[1]['threads'] == 4
99
100
101 def test_replication_update_continuous(monkeypatch, init_status, index_mock):
102     states = [nominatim.tools.replication.UpdateState.UP_TO_DATE,
103               nominatim.tools.replication.UpdateState.UP_TO_DATE]
104     monkeypatch.setattr(nominatim.tools.replication, 'update',
105                         lambda *args, **kwargs: states.pop())
106
107     with pytest.raises(IndexError):
108         call_nominatim()
109
110     assert index_mock.called == 4
111
112
113 def test_replication_update_continuous_no_change(monkeypatch, init_status, index_mock):
114     states = [nominatim.tools.replication.UpdateState.NO_CHANGES,
115               nominatim.tools.replication.UpdateState.UP_TO_DATE]
116     monkeypatch.setattr(nominatim.tools.replication, 'update',
117                         lambda *args, **kwargs: states.pop())
118
119     sleep_mock = MockParamCapture()
120     monkeypatch.setattr(time, 'sleep', sleep_mock)
121
122     with pytest.raises(IndexError):
123         call_nominatim()
124
125     assert index_mock.called == 2
126     assert sleep_mock.called == 1
127     assert sleep_mock.last_args[0] == 60