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', '--no-update-functions'), 'init_replication'),
70 (('--check-for-updates',), 'check_for_updates')
72 def test_replication_command(self, mock_func_factory, params, func):
73 func_mock = mock_func_factory(nominatim.tools.replication, func)
75 assert self.call_nominatim(*params) == 0
76 assert func_mock.called == 1
79 def test_replication_update_bad_interval(self, monkeypatch):
80 monkeypatch.setenv('NOMINATIM_REPLICATION_UPDATE_INTERVAL', 'xx')
82 assert self.call_nominatim() == 1
85 def test_replication_update_bad_interval_for_geofabrik(self, monkeypatch):
86 monkeypatch.setenv('NOMINATIM_REPLICATION_URL',
87 'https://download.geofabrik.de/europe/italy-updates')
89 assert self.call_nominatim() == 1
92 def test_replication_update_once_no_index(self, update_mock):
93 assert self.call_nominatim('--once', '--no-index') == 0
95 assert str(update_mock.last_args[1]['osm2pgsql']) == 'OSM2PGSQL NOT AVAILABLE'
98 def test_replication_update_custom_osm2pgsql(self, monkeypatch, update_mock):
99 monkeypatch.setenv('NOMINATIM_OSM2PGSQL_BINARY', '/secret/osm2pgsql')
100 assert self.call_nominatim('--once', '--no-index') == 0
102 assert str(update_mock.last_args[1]['osm2pgsql']) == '/secret/osm2pgsql'
105 @pytest.mark.parametrize("update_interval", [60, 3600])
106 def test_replication_catchup(self, placex_table, monkeypatch, index_mock, update_interval):
107 monkeypatch.setenv('NOMINATIM_REPLICATION_UPDATE_INTERVAL', str(update_interval))
108 self.update_states([nominatim.tools.replication.UpdateState.NO_CHANGES])
110 assert self.call_nominatim('--catch-up') == 0
113 def test_replication_update_custom_threads(self, update_mock):
114 assert self.call_nominatim('--once', '--no-index', '--threads', '4') == 0
116 assert update_mock.last_args[1]['threads'] == 4
119 def test_replication_update_continuous(self, index_mock):
120 self.update_states([nominatim.tools.replication.UpdateState.UP_TO_DATE,
121 nominatim.tools.replication.UpdateState.UP_TO_DATE])
123 with pytest.raises(IndexError):
124 self.call_nominatim()
126 assert index_mock.called == 2
129 def test_replication_update_continuous_no_change(self, mock_func_factory,
131 self.update_states([nominatim.tools.replication.UpdateState.NO_CHANGES,
132 nominatim.tools.replication.UpdateState.UP_TO_DATE])
134 sleep_mock = mock_func_factory(time, 'sleep')
136 with pytest.raises(IndexError):
137 self.call_nominatim()
139 assert index_mock.called == 1
140 assert sleep_mock.called == 1
141 assert sleep_mock.last_args[0] == 60