]> 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 # SPDX-License-Identifier: GPL-3.0-or-later
2 #
3 # This file is part of Nominatim. (https://nominatim.org)
4 #
5 # Copyright (C) 2025 by the Nominatim developer community.
6 # For a full list of authors see the git log.
7 """
8 Tests for replication command of command-line interface wrapper.
9 """
10 import datetime as dt
11 import time
12
13 import pytest
14
15 import nominatim_db.cli
16 import nominatim_db.indexer.indexer
17 import nominatim_db.tools.replication
18 import nominatim_db.tools.refresh
19 from nominatim_db.db import status
20
21
22 @pytest.fixture
23 def tokenizer_mock(monkeypatch):
24     class DummyTokenizer:
25         def __init__(self, *args, **kwargs):
26             self.update_sql_functions_called = False
27             self.finalize_import_called = False
28
29         def update_sql_functions(self, *args):
30             self.update_sql_functions_called = True
31
32         def finalize_import(self, *args):
33             self.finalize_import_called = True
34
35     tok = DummyTokenizer()
36     monkeypatch.setattr(nominatim_db.tokenizer.factory, 'get_tokenizer_for_db',
37                         lambda *args: tok)
38     monkeypatch.setattr(nominatim_db.tokenizer.factory, 'create_tokenizer',
39                         lambda *args: tok)
40
41     return tok
42
43
44 @pytest.fixture
45 def init_status(temp_db_conn, status_table):
46     status.set_status(temp_db_conn, date=dt.datetime.now(dt.timezone.utc), seq=1)
47
48
49 @pytest.fixture
50 def index_mock(async_mock_func_factory, tokenizer_mock, init_status):
51     return async_mock_func_factory(nominatim_db.indexer.indexer.Indexer, 'index_full')
52
53
54 @pytest.fixture
55 def update_mock(mock_func_factory, init_status, tokenizer_mock):
56     return mock_func_factory(nominatim_db.tools.replication, 'update')
57
58
59 class TestCliReplication:
60
61     @pytest.fixture(autouse=True)
62     def setup_cli_call(self, cli_call, temp_db):
63         self.call_nominatim = lambda *args: cli_call('replication', *args)
64
65     @pytest.fixture(autouse=True)
66     def setup_update_function(self, monkeypatch):
67         def _mock_updates(states):
68             monkeypatch.setattr(nominatim_db.tools.replication, 'update',
69                                 lambda *args, **kwargs: states.pop())
70
71         self.update_states = _mock_updates
72
73     @pytest.mark.parametrize("params,func", [
74                              (('--init',), 'init_replication'),
75                              (('--init', '--no-update-functions'), 'init_replication'),
76                              (('--check-for-updates',), 'check_for_updates')
77                              ])
78     def test_replication_command(self, mock_func_factory, params, func):
79         func_mock = mock_func_factory(nominatim_db.tools.replication, func)
80
81         if params == ('--init',):
82             umock = mock_func_factory(nominatim_db.tools.refresh, 'create_functions')
83
84         assert self.call_nominatim(*params) == 0
85         assert func_mock.called == 1
86         if params == ('--init',):
87             assert umock.called == 1
88
89     def test_replication_update_bad_interval(self, monkeypatch):
90         monkeypatch.setenv('NOMINATIM_REPLICATION_UPDATE_INTERVAL', 'xx')
91
92         assert self.call_nominatim() == 1
93
94     def test_replication_update_bad_interval_for_geofabrik(self, monkeypatch):
95         monkeypatch.setenv('NOMINATIM_REPLICATION_URL',
96                            'https://download.geofabrik.de/europe/italy-updates')
97
98         assert self.call_nominatim() == 1
99
100     def test_replication_update_continuous_no_index(self):
101         assert self.call_nominatim('--no-index') == 1
102
103     def test_replication_update_once_no_index(self, update_mock, monkeypatch):
104         monkeypatch.setenv('NOMINATIM_OSM2PGSQL_BINARY', 'OSM2PGSQL NOT AVAILABLE')
105         assert self.call_nominatim('--once', '--no-index') == 0
106
107         assert str(update_mock.last_args[1]['osm2pgsql']).endswith('OSM2PGSQL NOT AVAILABLE')
108
109     def test_replication_update_custom_osm2pgsql(self, monkeypatch, update_mock):
110         monkeypatch.setenv('NOMINATIM_OSM2PGSQL_BINARY', '/secret/osm2pgsql')
111         assert self.call_nominatim('--once', '--no-index') == 0
112
113         assert str(update_mock.last_args[1]['osm2pgsql']) == '/secret/osm2pgsql'
114
115     @pytest.mark.parametrize("update_interval", [60, 3600])
116     def test_replication_catchup(self, placex_table, monkeypatch, index_mock, update_interval):
117         monkeypatch.setenv('NOMINATIM_REPLICATION_UPDATE_INTERVAL', str(update_interval))
118         self.update_states([nominatim_db.tools.replication.UpdateState.NO_CHANGES])
119
120         assert self.call_nominatim('--catch-up') == 0
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     def test_replication_update_continuous(self, index_mock):
128         self.update_states([nominatim_db.tools.replication.UpdateState.UP_TO_DATE,
129                             nominatim_db.tools.replication.UpdateState.UP_TO_DATE])
130
131         with pytest.raises(IndexError):
132             self.call_nominatim()
133
134         assert index_mock.called == 2
135
136     def test_replication_update_continuous_no_change(self, mock_func_factory,
137                                                      index_mock):
138         self.update_states([nominatim_db.tools.replication.UpdateState.NO_CHANGES,
139                             nominatim_db.tools.replication.UpdateState.UP_TO_DATE])
140
141         sleep_mock = mock_func_factory(time, 'sleep')
142
143         with pytest.raises(IndexError):
144             self.call_nominatim()
145
146         assert index_mock.called == 1
147         assert sleep_mock.called == 1
148         assert sleep_mock.last_args[0] == 60