]> git.openstreetmap.org Git - nominatim.git/blob - test/python/test_cli_replication.py
remove country restriction from tokenizer
[nominatim.git] / test / python / test_cli_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 from mocks import MockParamCapture
15
16 @pytest.fixture
17 def tokenizer_mock(monkeypatch):
18     class DummyTokenizer:
19         def __init__(self, *args, **kwargs):
20             self.update_sql_functions_called = False
21             self.finalize_import_called = False
22
23         def update_sql_functions(self, *args):
24             self.update_sql_functions_called = True
25
26         def finalize_import(self, *args):
27             self.finalize_import_called = True
28
29     tok = DummyTokenizer()
30     monkeypatch.setattr(nominatim.tokenizer.factory, 'get_tokenizer_for_db',
31                         lambda *args: tok)
32     monkeypatch.setattr(nominatim.tokenizer.factory, 'create_tokenizer',
33                         lambda *args: tok)
34
35     return tok
36
37
38 @pytest.fixture
39 def mock_func_factory(monkeypatch):
40     def get_mock(module, func):
41         mock = MockParamCapture()
42         monkeypatch.setattr(module, func, mock)
43         return mock
44
45     return get_mock
46
47
48 @pytest.fixture
49 def init_status(temp_db_conn, status_table):
50     status.set_status(temp_db_conn, date=dt.datetime.now(dt.timezone.utc), seq=1)
51
52
53 @pytest.fixture
54 def index_mock(monkeypatch, tokenizer_mock, init_status):
55     mock = MockParamCapture()
56     monkeypatch.setattr(nominatim.indexer.indexer.Indexer, 'index_boundaries', mock)
57     monkeypatch.setattr(nominatim.indexer.indexer.Indexer, 'index_by_rank', mock)
58
59     return mock
60
61
62 @pytest.fixture
63 def update_mock(mock_func_factory, init_status, tokenizer_mock):
64     return mock_func_factory(nominatim.tools.replication, 'update')
65
66
67 class TestCliReplication:
68
69     @pytest.fixture(autouse=True)
70     def setup_cli_call(self, cli_call, temp_db):
71         self.call_nominatim = lambda *args: cli_call('replication', *args)
72
73     @pytest.mark.parametrize("params,func", [
74                              (('--init', '--no-update-functions'), 'init_replication'),
75                              (('--check-for-updates',), 'check_for_updates')
76                              ])
77     def test_replication_command(self, mock_func_factory, params, func):
78         func_mock = mock_func_factory(nominatim.tools.replication, func)
79
80         assert self.call_nominatim(*params) == 0
81         assert func_mock.called == 1
82
83
84     def test_replication_update_bad_interval(self, monkeypatch):
85         monkeypatch.setenv('NOMINATIM_REPLICATION_UPDATE_INTERVAL', 'xx')
86
87         assert self.call_nominatim() == 1
88
89
90     def test_replication_update_bad_interval_for_geofabrik(self, monkeypatch):
91         monkeypatch.setenv('NOMINATIM_REPLICATION_URL',
92                            'https://download.geofabrik.de/europe/italy-updates')
93
94         assert self.call_nominatim() == 1
95
96
97     def test_replication_update_once_no_index(self, update_mock):
98         assert self.call_nominatim('--once', '--no-index') == 0
99
100         assert str(update_mock.last_args[1]['osm2pgsql']) == 'OSM2PGSQL NOT AVAILABLE'
101
102
103     def test_replication_update_custom_osm2pgsql(self, monkeypatch, update_mock):
104         monkeypatch.setenv('NOMINATIM_OSM2PGSQL_BINARY', '/secret/osm2pgsql')
105         assert self.call_nominatim('--once', '--no-index') == 0
106
107         assert str(update_mock.last_args[1]['osm2pgsql']) == '/secret/osm2pgsql'
108
109
110     def test_replication_update_custom_threads(self, update_mock):
111         assert self.call_nominatim('--once', '--no-index', '--threads', '4') == 0
112
113         assert update_mock.last_args[1]['threads'] == 4
114
115
116     def test_replication_update_continuous(self, monkeypatch, index_mock):
117         states = [nominatim.tools.replication.UpdateState.UP_TO_DATE,
118                   nominatim.tools.replication.UpdateState.UP_TO_DATE]
119         monkeypatch.setattr(nominatim.tools.replication, 'update',
120                             lambda *args, **kwargs: states.pop())
121
122         with pytest.raises(IndexError):
123             self.call_nominatim()
124
125         assert index_mock.called == 4
126
127
128     def test_replication_update_continuous_no_change(self, monkeypatch, index_mock):
129         states = [nominatim.tools.replication.UpdateState.NO_CHANGES,
130                   nominatim.tools.replication.UpdateState.UP_TO_DATE]
131         monkeypatch.setattr(nominatim.tools.replication, 'update',
132                             lambda *args, **kwargs: states.pop())
133
134         sleep_mock = MockParamCapture()
135         monkeypatch.setattr(time, 'sleep', sleep_mock)
136
137         with pytest.raises(IndexError):
138             self.call_nominatim()
139
140         assert index_mock.called == 2
141         assert sleep_mock.called == 1
142         assert sleep_mock.last_args[0] == 60