]> git.openstreetmap.org Git - nominatim.git/blob - test/python/cli/test_cmd_replication.py
Merge remote-tracking branch 'upstream/master'
[nominatim.git] / test / python / cli / test_cmd_replication.py
1 """
2 Tests for replication command of command-line interface wrapper.
3 """
4 import datetime as dt
5 import time
6
7 import pytest
8
9 import nominatim.cli
10 import nominatim.indexer.indexer
11 import nominatim.tools.replication
12 from nominatim.db import status
13
14 @pytest.fixture
15 def tokenizer_mock(monkeypatch):
16     class DummyTokenizer:
17         def __init__(self, *args, **kwargs):
18             self.update_sql_functions_called = False
19             self.finalize_import_called = False
20
21         def update_sql_functions(self, *args):
22             self.update_sql_functions_called = True
23
24         def finalize_import(self, *args):
25             self.finalize_import_called = True
26
27     tok = DummyTokenizer()
28     monkeypatch.setattr(nominatim.tokenizer.factory, 'get_tokenizer_for_db',
29                         lambda *args: tok)
30     monkeypatch.setattr(nominatim.tokenizer.factory, 'create_tokenizer',
31                         lambda *args: tok)
32
33     return tok
34
35
36
37 @pytest.fixture
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)
40
41
42 @pytest.fixture
43 def index_mock(mock_func_factory, tokenizer_mock, init_status):
44     return mock_func_factory(nominatim.indexer.indexer.Indexer, 'index_full')
45
46
47 @pytest.fixture
48 def update_mock(mock_func_factory, init_status, tokenizer_mock):
49     return mock_func_factory(nominatim.tools.replication, 'update')
50
51
52 class TestCliReplication:
53
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)
57
58
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())
64
65         self.update_states = _mock_updates
66
67
68     @pytest.mark.parametrize("params,func", [
69                              (('--init',), 'init_replication'),
70                              (('--init', '--no-update-functions'), 'init_replication'),
71                              (('--check-for-updates',), 'check_for_updates')
72                              ])
73     def test_replication_command(self, mock_func_factory, params, func):
74         func_mock = mock_func_factory(nominatim.tools.replication, func)
75
76         if params == ('--init',):
77             umock = mock_func_factory(nominatim.tools.refresh, 'create_functions')
78
79         assert self.call_nominatim(*params) == 0
80         assert func_mock.called == 1
81         if params == ('--init',):
82             assert umock.called == 1
83
84
85     def test_replication_update_bad_interval(self, monkeypatch):
86         monkeypatch.setenv('NOMINATIM_REPLICATION_UPDATE_INTERVAL', 'xx')
87
88         assert self.call_nominatim() == 1
89
90
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')
94
95         assert self.call_nominatim() == 1
96
97
98     def test_replication_update_continuous_no_index(self):
99         assert self.call_nominatim('--no-index') == 1
100
101     def test_replication_update_once_no_index(self, update_mock):
102         assert self.call_nominatim('--once', '--no-index') == 0
103
104         assert str(update_mock.last_args[1]['osm2pgsql']) == 'OSM2PGSQL NOT AVAILABLE'
105
106
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
110
111         assert str(update_mock.last_args[1]['osm2pgsql']) == '/secret/osm2pgsql'
112
113
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])
118
119         assert self.call_nominatim('--catch-up') == 0
120
121
122     def test_replication_update_custom_threads(self, update_mock):
123         assert self.call_nominatim('--once', '--no-index', '--threads', '4') == 0
124
125         assert update_mock.last_args[1]['threads'] == 4
126
127
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])
131
132         with pytest.raises(IndexError):
133             self.call_nominatim()
134
135         assert index_mock.called == 2
136
137
138     def test_replication_update_continuous_no_change(self, mock_func_factory,
139                                                      index_mock):
140         self.update_states([nominatim.tools.replication.UpdateState.NO_CHANGES,
141                             nominatim.tools.replication.UpdateState.UP_TO_DATE])
142
143         sleep_mock = mock_func_factory(time, 'sleep')
144
145         with pytest.raises(IndexError):
146             self.call_nominatim()
147
148         assert index_mock.called == 1
149         assert sleep_mock.called == 1
150         assert sleep_mock.last_args[0] == 60