2 Tests for replication command of command-line interface wrapper.
6 from pathlib import Path
11 import nominatim.indexer.indexer
12 import nominatim.tools.replication
13 from nominatim.db import status
15 from mocks import MockParamCapture
17 SRC_DIR = (Path(__file__) / '..' / '..' / '..').resolve()
19 def call_nominatim(*args):
20 return nominatim.cli.nominatim(module_dir='build/module',
21 osm2pgsql_path='build/osm2pgsql/osm2pgsql',
22 phplib_dir=str(SRC_DIR / 'lib-php'),
23 data_dir=str(SRC_DIR / 'data'),
24 phpcgi_path='/usr/bin/php-cgi',
25 sqllib_dir=str(SRC_DIR / 'lib-sql'),
26 config_dir=str(SRC_DIR / 'settings'),
27 cli_args=['replication'] + list(args))
30 def tokenizer_mock(monkeypatch):
32 def __init__(self, *args, **kwargs):
33 self.update_sql_functions_called = False
34 self.finalize_import_called = False
36 def update_sql_functions(self, *args):
37 self.update_sql_functions_called = True
39 def finalize_import(self, *args):
40 self.finalize_import_called = True
42 tok = DummyTokenizer()
43 monkeypatch.setattr(nominatim.tokenizer.factory, 'get_tokenizer_for_db' ,
45 monkeypatch.setattr(nominatim.tokenizer.factory, 'create_tokenizer' ,
52 def index_mock(monkeypatch, tokenizer_mock):
53 mock = MockParamCapture()
54 monkeypatch.setattr(nominatim.indexer.indexer.Indexer, 'index_boundaries', mock)
55 monkeypatch.setattr(nominatim.indexer.indexer.Indexer, 'index_by_rank', mock)
61 def mock_func_factory(monkeypatch):
62 def get_mock(module, func):
63 mock = MockParamCapture()
64 monkeypatch.setattr(module, func, mock)
71 def init_status(temp_db_conn, status_table):
72 status.set_status(temp_db_conn, date=dt.datetime.now(dt.timezone.utc), seq=1)
77 def update_mock(mock_func_factory, init_status, tokenizer_mock):
78 return mock_func_factory(nominatim.tools.replication, 'update')
80 @pytest.mark.parametrize("params,func", [
81 (('--init', '--no-update-functions'), 'init_replication'),
82 (('--check-for-updates',), 'check_for_updates')
84 def test_replication_command(mock_func_factory, temp_db, params, func):
85 func_mock = mock_func_factory(nominatim.tools.replication, func)
87 assert 0 == call_nominatim(*params)
88 assert func_mock.called == 1
91 def test_replication_update_bad_interval(monkeypatch, temp_db):
92 monkeypatch.setenv('NOMINATIM_REPLICATION_UPDATE_INTERVAL', 'xx')
94 assert call_nominatim() == 1
97 def test_replication_update_bad_interval_for_geofabrik(monkeypatch, temp_db):
98 monkeypatch.setenv('NOMINATIM_REPLICATION_URL',
99 'https://download.geofabrik.de/europe/ireland-and-northern-ireland-updates')
101 assert call_nominatim() == 1
104 def test_replication_update_once_no_index(update_mock):
105 assert 0 == call_nominatim('--once', '--no-index')
107 assert str(update_mock.last_args[1]['osm2pgsql']) == 'build/osm2pgsql/osm2pgsql'
110 def test_replication_update_custom_osm2pgsql(monkeypatch, update_mock):
111 monkeypatch.setenv('NOMINATIM_OSM2PGSQL_BINARY', '/secret/osm2pgsql')
112 assert 0 == call_nominatim('--once', '--no-index')
114 assert str(update_mock.last_args[1]['osm2pgsql']) == '/secret/osm2pgsql'
117 def test_replication_update_custom_threads(update_mock):
118 assert 0 == call_nominatim('--once', '--no-index', '--threads', '4')
120 assert update_mock.last_args[1]['threads'] == 4
123 def test_replication_update_continuous(monkeypatch, init_status, index_mock):
124 states = [nominatim.tools.replication.UpdateState.UP_TO_DATE,
125 nominatim.tools.replication.UpdateState.UP_TO_DATE]
126 monkeypatch.setattr(nominatim.tools.replication, 'update',
127 lambda *args, **kwargs: states.pop())
129 with pytest.raises(IndexError):
132 assert index_mock.called == 4
135 def test_replication_update_continuous_no_change(monkeypatch, init_status, index_mock):
136 states = [nominatim.tools.replication.UpdateState.NO_CHANGES,
137 nominatim.tools.replication.UpdateState.UP_TO_DATE]
138 monkeypatch.setattr(nominatim.tools.replication, 'update',
139 lambda *args, **kwargs: states.pop())
141 sleep_mock = MockParamCapture()
142 monkeypatch.setattr(time, 'sleep', sleep_mock)
144 with pytest.raises(IndexError):
147 assert index_mock.called == 2
148 assert sleep_mock.called == 1
149 assert sleep_mock.last_args[0] == 60