]> git.openstreetmap.org Git - nominatim.git/blob - test/python/cli/test_cmd_replication.py
extend API unit tests
[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', '--no-update-functions'), 'init_replication'),
70                              (('--check-for-updates',), 'check_for_updates')
71                              ])
72     def test_replication_command(self, mock_func_factory, params, func):
73         func_mock = mock_func_factory(nominatim.tools.replication, func)
74
75         assert self.call_nominatim(*params) == 0
76         assert func_mock.called == 1
77
78
79     def test_replication_update_bad_interval(self, monkeypatch):
80         monkeypatch.setenv('NOMINATIM_REPLICATION_UPDATE_INTERVAL', 'xx')
81
82         assert self.call_nominatim() == 1
83
84
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')
88
89         assert self.call_nominatim() == 1
90
91
92     def test_replication_update_once_no_index(self, update_mock):
93         assert self.call_nominatim('--once', '--no-index') == 0
94
95         assert str(update_mock.last_args[1]['osm2pgsql']) == 'OSM2PGSQL NOT AVAILABLE'
96
97
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
101
102         assert str(update_mock.last_args[1]['osm2pgsql']) == '/secret/osm2pgsql'
103
104
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])
109
110         assert self.call_nominatim('--catch-up') == 0
111
112
113     def test_replication_update_custom_threads(self, update_mock):
114         assert self.call_nominatim('--once', '--no-index', '--threads', '4') == 0
115
116         assert update_mock.last_args[1]['threads'] == 4
117
118
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])
122
123         with pytest.raises(IndexError):
124             self.call_nominatim()
125
126         assert index_mock.called == 2
127
128
129     def test_replication_update_continuous_no_change(self, mock_func_factory,
130                                                      index_mock):
131         self.update_states([nominatim.tools.replication.UpdateState.NO_CHANGES,
132                             nominatim.tools.replication.UpdateState.UP_TO_DATE])
133
134         sleep_mock = mock_func_factory(time, 'sleep')
135
136         with pytest.raises(IndexError):
137             self.call_nominatim()
138
139         assert index_mock.called == 1
140         assert sleep_mock.called == 1
141         assert sleep_mock.last_args[0] == 60