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
15 def tokenizer_mock(monkeypatch):
17 def __init__(self, *args, **kwargs):
18 self.update_sql_functions_called = False
19 self.finalize_import_called = False
21 def update_sql_functions(self, *args):
22 self.update_sql_functions_called = True
24 def finalize_import(self, *args):
25 self.finalize_import_called = True
27 tok = DummyTokenizer()
28 monkeypatch.setattr(nominatim.tokenizer.factory, 'get_tokenizer_for_db',
30 monkeypatch.setattr(nominatim.tokenizer.factory, 'create_tokenizer',
38 def init_status(temp_db_conn, status_table):
39 status.set_status(temp_db_conn, date=dt.datetime.now(dt.timezone.utc), seq=1)
43 def index_mock(mock_func_factory, tokenizer_mock, init_status):
44 return mock_func_factory(nominatim.indexer.indexer.Indexer, 'index_full')
48 def update_mock(mock_func_factory, init_status, tokenizer_mock):
49 return mock_func_factory(nominatim.tools.replication, 'update')
52 class TestCliReplication:
54 @pytest.fixture(autouse=True)
55 def setup_cli_call(self, cli_call, temp_db):
56 self.call_nominatim = lambda *args: cli_call('replication', *args)
59 @pytest.fixture(autouse=True)
60 def setup_update_function(self, monkeypatch):
61 def _mock_updates(states):
62 monkeypatch.setattr(nominatim.tools.replication, 'update',
63 lambda *args, **kwargs: states.pop())
65 self.update_states = _mock_updates
68 @pytest.mark.parametrize("params,func", [
69 (('--init',), 'init_replication'),
70 (('--init', '--no-update-functions'), 'init_replication'),
71 (('--check-for-updates',), 'check_for_updates')
73 def test_replication_command(self, mock_func_factory, params, func):
74 func_mock = mock_func_factory(nominatim.tools.replication, func)
76 if params == ('--init',):
77 umock = mock_func_factory(nominatim.tools.refresh, 'create_functions')
79 assert self.call_nominatim(*params) == 0
80 assert func_mock.called == 1
81 if params == ('--init',):
82 assert umock.called == 1
85 def test_replication_update_bad_interval(self, monkeypatch):
86 monkeypatch.setenv('NOMINATIM_REPLICATION_UPDATE_INTERVAL', 'xx')
88 assert self.call_nominatim() == 1
91 def test_replication_update_bad_interval_for_geofabrik(self, monkeypatch):
92 monkeypatch.setenv('NOMINATIM_REPLICATION_URL',
93 'https://download.geofabrik.de/europe/italy-updates')
95 assert self.call_nominatim() == 1
98 def test_replication_update_continuous_no_index(self):
99 assert self.call_nominatim('--no-index') == 1
101 def test_replication_update_once_no_index(self, update_mock):
102 assert self.call_nominatim('--once', '--no-index') == 0
104 assert str(update_mock.last_args[1]['osm2pgsql']) == 'OSM2PGSQL NOT AVAILABLE'
107 def test_replication_update_custom_osm2pgsql(self, monkeypatch, update_mock):
108 monkeypatch.setenv('NOMINATIM_OSM2PGSQL_BINARY', '/secret/osm2pgsql')
109 assert self.call_nominatim('--once', '--no-index') == 0
111 assert str(update_mock.last_args[1]['osm2pgsql']) == '/secret/osm2pgsql'
114 @pytest.mark.parametrize("update_interval", [60, 3600])
115 def test_replication_catchup(self, placex_table, monkeypatch, index_mock, update_interval):
116 monkeypatch.setenv('NOMINATIM_REPLICATION_UPDATE_INTERVAL', str(update_interval))
117 self.update_states([nominatim.tools.replication.UpdateState.NO_CHANGES])
119 assert self.call_nominatim('--catch-up') == 0
122 def test_replication_update_custom_threads(self, update_mock):
123 assert self.call_nominatim('--once', '--no-index', '--threads', '4') == 0
125 assert update_mock.last_args[1]['threads'] == 4
128 def test_replication_update_continuous(self, index_mock):
129 self.update_states([nominatim.tools.replication.UpdateState.UP_TO_DATE,
130 nominatim.tools.replication.UpdateState.UP_TO_DATE])
132 with pytest.raises(IndexError):
133 self.call_nominatim()
135 assert index_mock.called == 2
138 def test_replication_update_continuous_no_change(self, mock_func_factory,
140 self.update_states([nominatim.tools.replication.UpdateState.NO_CHANGES,
141 nominatim.tools.replication.UpdateState.UP_TO_DATE])
143 sleep_mock = mock_func_factory(time, 'sleep')
145 with pytest.raises(IndexError):
146 self.call_nominatim()
148 assert index_mock.called == 1
149 assert sleep_mock.called == 1
150 assert sleep_mock.last_args[0] == 60