2 Tests for replication command of command-line interface wrapper.
10 import nominatim.indexer.indexer
11 import nominatim.tools.replication
12 from nominatim.db import status
14 from mocks import MockParamCapture
17 def tokenizer_mock(monkeypatch):
19 def __init__(self, *args, **kwargs):
20 self.update_sql_functions_called = False
21 self.finalize_import_called = False
23 def update_sql_functions(self, *args):
24 self.update_sql_functions_called = True
26 def finalize_import(self, *args):
27 self.finalize_import_called = True
29 tok = DummyTokenizer()
30 monkeypatch.setattr(nominatim.tokenizer.factory, 'get_tokenizer_for_db',
32 monkeypatch.setattr(nominatim.tokenizer.factory, 'create_tokenizer',
39 def mock_func_factory(monkeypatch):
40 def get_mock(module, func):
41 mock = MockParamCapture()
42 monkeypatch.setattr(module, func, mock)
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)
54 def index_mock(monkeypatch, tokenizer_mock, init_status):
55 mock = MockParamCapture()
56 monkeypatch.setattr(nominatim.indexer.indexer.Indexer, 'index_full', mock)
62 def update_mock(mock_func_factory, init_status, tokenizer_mock):
63 return mock_func_factory(nominatim.tools.replication, 'update')
66 class TestCliReplication:
68 @pytest.fixture(autouse=True)
69 def setup_cli_call(self, cli_call, temp_db):
70 self.call_nominatim = lambda *args: cli_call('replication', *args)
72 @pytest.mark.parametrize("params,func", [
73 (('--init', '--no-update-functions'), 'init_replication'),
74 (('--check-for-updates',), 'check_for_updates')
76 def test_replication_command(self, mock_func_factory, params, func):
77 func_mock = mock_func_factory(nominatim.tools.replication, func)
79 assert self.call_nominatim(*params) == 0
80 assert func_mock.called == 1
83 def test_replication_update_bad_interval(self, monkeypatch):
84 monkeypatch.setenv('NOMINATIM_REPLICATION_UPDATE_INTERVAL', 'xx')
86 assert self.call_nominatim() == 1
89 def test_replication_update_bad_interval_for_geofabrik(self, monkeypatch):
90 monkeypatch.setenv('NOMINATIM_REPLICATION_URL',
91 'https://download.geofabrik.de/europe/italy-updates')
93 assert self.call_nominatim() == 1
96 def test_replication_update_once_no_index(self, update_mock):
97 assert self.call_nominatim('--once', '--no-index') == 0
99 assert str(update_mock.last_args[1]['osm2pgsql']) == 'OSM2PGSQL NOT AVAILABLE'
102 def test_replication_update_custom_osm2pgsql(self, monkeypatch, update_mock):
103 monkeypatch.setenv('NOMINATIM_OSM2PGSQL_BINARY', '/secret/osm2pgsql')
104 assert self.call_nominatim('--once', '--no-index') == 0
106 assert str(update_mock.last_args[1]['osm2pgsql']) == '/secret/osm2pgsql'
109 @pytest.mark.parametrize("update_interval", [60, 3600])
110 def test_replication_catchup(self, monkeypatch, index_mock, update_interval, placex_table):
111 monkeypatch.setenv('NOMINATIM_REPLICATION_UPDATE_INTERVAL', str(update_interval))
112 states = [nominatim.tools.replication.UpdateState.NO_CHANGES]
113 monkeypatch.setattr(nominatim.tools.replication, 'update',
114 lambda *args, **kwargs: states.pop())
116 assert self.call_nominatim('--catch-up') == 0
119 def test_replication_update_custom_threads(self, update_mock):
120 assert self.call_nominatim('--once', '--no-index', '--threads', '4') == 0
122 assert update_mock.last_args[1]['threads'] == 4
125 def test_replication_update_continuous(self, monkeypatch, index_mock):
126 states = [nominatim.tools.replication.UpdateState.UP_TO_DATE,
127 nominatim.tools.replication.UpdateState.UP_TO_DATE]
128 monkeypatch.setattr(nominatim.tools.replication, 'update',
129 lambda *args, **kwargs: states.pop())
131 with pytest.raises(IndexError):
132 self.call_nominatim()
134 assert index_mock.called == 2
137 def test_replication_update_continuous_no_change(self, monkeypatch, index_mock):
138 states = [nominatim.tools.replication.UpdateState.NO_CHANGES,
139 nominatim.tools.replication.UpdateState.UP_TO_DATE]
140 monkeypatch.setattr(nominatim.tools.replication, 'update',
141 lambda *args, **kwargs: states.pop())
143 sleep_mock = MockParamCapture()
144 monkeypatch.setattr(time, 'sleep', sleep_mock)
146 with pytest.raises(IndexError):
147 self.call_nominatim()
149 assert index_mock.called == 1
150 assert sleep_mock.called == 1
151 assert sleep_mock.last_args[0] == 60