]> git.openstreetmap.org Git - nominatim.git/blob - test/python/test_cli_replication.py
test: use src_dir fixture instead of self-computed paths
[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 from pathlib import Path
7
8 import pytest
9
10 import nominatim.cli
11 import nominatim.indexer.indexer
12 import nominatim.tools.replication
13 from nominatim.db import status
14
15 from mocks import MockParamCapture
16
17 @pytest.fixture
18 def tokenizer_mock(monkeypatch):
19     class DummyTokenizer:
20         def __init__(self, *args, **kwargs):
21             self.update_sql_functions_called = False
22             self.finalize_import_called = False
23
24         def update_sql_functions(self, *args):
25             self.update_sql_functions_called = True
26
27         def finalize_import(self, *args):
28             self.finalize_import_called = True
29
30     tok = DummyTokenizer()
31     monkeypatch.setattr(nominatim.tokenizer.factory, 'get_tokenizer_for_db' ,
32                         lambda *args: tok)
33     monkeypatch.setattr(nominatim.tokenizer.factory, 'create_tokenizer' ,
34                         lambda *args: tok)
35
36     return tok
37
38
39 @pytest.fixture
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)
44
45     return mock
46
47
48 @pytest.fixture
49 def mock_func_factory(monkeypatch):
50     def get_mock(module, func):
51         mock = MockParamCapture()
52         monkeypatch.setattr(module, func, mock)
53         return mock
54
55     return get_mock
56
57
58 @pytest.fixture
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)
61     return 1
62
63
64 @pytest.fixture
65 def update_mock(mock_func_factory, init_status, tokenizer_mock):
66     return mock_func_factory(nominatim.tools.replication, 'update')
67
68
69 class TestCliReplication:
70
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)
74
75     @pytest.mark.parametrize("params,func", [
76                              (('--init', '--no-update-functions'), 'init_replication'),
77                              (('--check-for-updates',), 'check_for_updates')
78                              ])
79     def test_replication_command(self, mock_func_factory, params, func):
80         func_mock = mock_func_factory(nominatim.tools.replication, func)
81
82         assert 0 == self.call_nominatim(*params)
83         assert func_mock.called == 1
84
85
86     def test_replication_update_bad_interval(self, monkeypatch):
87         monkeypatch.setenv('NOMINATIM_REPLICATION_UPDATE_INTERVAL', 'xx')
88
89         assert self.call_nominatim() == 1
90
91
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')
95
96         assert self.call_nominatim() == 1
97
98
99     def test_replication_update_once_no_index(self, update_mock):
100         assert 0 == self.call_nominatim('--once', '--no-index')
101
102         assert str(update_mock.last_args[1]['osm2pgsql']) == 'OSM2PGSQL NOT AVAILABLE'
103
104
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')
108
109         assert str(update_mock.last_args[1]['osm2pgsql']) == '/secret/osm2pgsql'
110
111
112     def test_replication_update_custom_threads(self, update_mock):
113         assert 0 == self.call_nominatim('--once', '--no-index', '--threads', '4')
114
115         assert update_mock.last_args[1]['threads'] == 4
116
117
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())
123
124         with pytest.raises(IndexError):
125             self.call_nominatim()
126
127         assert index_mock.called == 4
128
129
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())
135
136         sleep_mock = MockParamCapture()
137         monkeypatch.setattr(time, 'sleep', sleep_mock)
138
139         with pytest.raises(IndexError):
140             self.call_nominatim()
141
142         assert index_mock.called == 2
143         assert sleep_mock.called == 1
144         assert sleep_mock.last_args[0] == 60