2 Tests for replication command of command-line interface wrapper.
6 from pathlib import Path
11 import nominatim.indexer.indexer
12 import nominatim.tools.replication
13 from nominatim.db import status
15 from mocks import MockParamCapture
18 def tokenizer_mock(monkeypatch):
20 def __init__(self, *args, **kwargs):
21 self.update_sql_functions_called = False
22 self.finalize_import_called = False
24 def update_sql_functions(self, *args):
25 self.update_sql_functions_called = True
27 def finalize_import(self, *args):
28 self.finalize_import_called = True
30 tok = DummyTokenizer()
31 monkeypatch.setattr(nominatim.tokenizer.factory, 'get_tokenizer_for_db' ,
33 monkeypatch.setattr(nominatim.tokenizer.factory, 'create_tokenizer' ,
40 def index_mock(monkeypatch, tokenizer_mock):
41 mock = MockParamCapture()
42 monkeypatch.setattr(nominatim.indexer.indexer.Indexer, 'index_boundaries', mock)
43 monkeypatch.setattr(nominatim.indexer.indexer.Indexer, 'index_by_rank', mock)
49 def mock_func_factory(monkeypatch):
50 def get_mock(module, func):
51 mock = MockParamCapture()
52 monkeypatch.setattr(module, func, mock)
59 def init_status(temp_db_conn, status_table):
60 status.set_status(temp_db_conn, date=dt.datetime.now(dt.timezone.utc), seq=1)
65 def update_mock(mock_func_factory, init_status, tokenizer_mock):
66 return mock_func_factory(nominatim.tools.replication, 'update')
69 class TestCliReplication:
71 @pytest.fixture(autouse=True)
72 def setup_cli_call(self, cli_call, temp_db):
73 self.call_nominatim = lambda *args: cli_call('replication', *args)
75 @pytest.mark.parametrize("params,func", [
76 (('--init', '--no-update-functions'), 'init_replication'),
77 (('--check-for-updates',), 'check_for_updates')
79 def test_replication_command(self, mock_func_factory, params, func):
80 func_mock = mock_func_factory(nominatim.tools.replication, func)
82 assert 0 == self.call_nominatim(*params)
83 assert func_mock.called == 1
86 def test_replication_update_bad_interval(self, monkeypatch):
87 monkeypatch.setenv('NOMINATIM_REPLICATION_UPDATE_INTERVAL', 'xx')
89 assert self.call_nominatim() == 1
92 def test_replication_update_bad_interval_for_geofabrik(self, monkeypatch):
93 monkeypatch.setenv('NOMINATIM_REPLICATION_URL',
94 'https://download.geofabrik.de/europe/ireland-and-northern-ireland-updates')
96 assert self.call_nominatim() == 1
99 def test_replication_update_once_no_index(self, update_mock):
100 assert 0 == self.call_nominatim('--once', '--no-index')
102 assert str(update_mock.last_args[1]['osm2pgsql']) == 'OSM2PGSQL NOT AVAILABLE'
105 def test_replication_update_custom_osm2pgsql(self, monkeypatch, update_mock):
106 monkeypatch.setenv('NOMINATIM_OSM2PGSQL_BINARY', '/secret/osm2pgsql')
107 assert 0 == self.call_nominatim('--once', '--no-index')
109 assert str(update_mock.last_args[1]['osm2pgsql']) == '/secret/osm2pgsql'
112 def test_replication_update_custom_threads(self, update_mock):
113 assert 0 == self.call_nominatim('--once', '--no-index', '--threads', '4')
115 assert update_mock.last_args[1]['threads'] == 4
118 def test_replication_update_continuous(self, monkeypatch, init_status, index_mock):
119 states = [nominatim.tools.replication.UpdateState.UP_TO_DATE,
120 nominatim.tools.replication.UpdateState.UP_TO_DATE]
121 monkeypatch.setattr(nominatim.tools.replication, 'update',
122 lambda *args, **kwargs: states.pop())
124 with pytest.raises(IndexError):
125 self.call_nominatim()
127 assert index_mock.called == 4
130 def test_replication_update_continuous_no_change(self, monkeypatch, init_status, index_mock):
131 states = [nominatim.tools.replication.UpdateState.NO_CHANGES,
132 nominatim.tools.replication.UpdateState.UP_TO_DATE]
133 monkeypatch.setattr(nominatim.tools.replication, 'update',
134 lambda *args, **kwargs: states.pop())
136 sleep_mock = MockParamCapture()
137 monkeypatch.setattr(time, 'sleep', sleep_mock)
139 with pytest.raises(IndexError):
140 self.call_nominatim()
142 assert index_mock.called == 2
143 assert sleep_mock.called == 1
144 assert sleep_mock.last_args[0] == 60