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 index_mock(monkeypatch, tokenizer_mock):
 
  31     mock = MockParamCapture()
 
  32     monkeypatch.setattr(nominatim.indexer.indexer.Indexer, 'index_boundaries', mock)
 
  33     monkeypatch.setattr(nominatim.indexer.indexer.Indexer, 'index_by_rank', mock)
 
  39 def mock_func_factory(monkeypatch):
 
  40     def get_mock(module, func):
 
  41         mock = MockParamCapture()
 
  42         monkeypatch.setattr(module, func, mock)
 
  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)
 
  55 def update_mock(mock_func_factory, init_status, tokenizer_mock):
 
  56     return mock_func_factory(nominatim.tools.replication, 'update')
 
  58 @pytest.mark.parametrize("params,func", [
 
  59                          (('--init', '--no-update-functions'), 'init_replication'),
 
  60                          (('--check-for-updates',), 'check_for_updates')
 
  62 def test_replication_command(mock_func_factory, temp_db, params, func):
 
  63     func_mock = mock_func_factory(nominatim.tools.replication, func)
 
  65     assert 0 == call_nominatim(*params)
 
  66     assert func_mock.called == 1
 
  69 def test_replication_update_bad_interval(monkeypatch, temp_db):
 
  70     monkeypatch.setenv('NOMINATIM_REPLICATION_UPDATE_INTERVAL', 'xx')
 
  72     assert call_nominatim() == 1
 
  75 def test_replication_update_bad_interval_for_geofabrik(monkeypatch, temp_db):
 
  76     monkeypatch.setenv('NOMINATIM_REPLICATION_URL',
 
  77                        'https://download.geofabrik.de/europe/ireland-and-northern-ireland-updates')
 
  79     assert call_nominatim() == 1
 
  82 def test_replication_update_once_no_index(update_mock):
 
  83     assert 0 == call_nominatim('--once', '--no-index')
 
  85     assert str(update_mock.last_args[1]['osm2pgsql']) == 'build/osm2pgsql/osm2pgsql'
 
  88 def test_replication_update_custom_osm2pgsql(monkeypatch, update_mock):
 
  89     monkeypatch.setenv('NOMINATIM_OSM2PGSQL_BINARY', '/secret/osm2pgsql')
 
  90     assert 0 == call_nominatim('--once', '--no-index')
 
  92     assert str(update_mock.last_args[1]['osm2pgsql']) == '/secret/osm2pgsql'
 
  95 def test_replication_update_custom_threads(update_mock):
 
  96     assert 0 == call_nominatim('--once', '--no-index', '--threads', '4')
 
  98     assert update_mock.last_args[1]['threads'] == 4
 
 101 def test_replication_update_continuous(monkeypatch, init_status, index_mock):
 
 102     states = [nominatim.tools.replication.UpdateState.UP_TO_DATE,
 
 103               nominatim.tools.replication.UpdateState.UP_TO_DATE]
 
 104     monkeypatch.setattr(nominatim.tools.replication, 'update',
 
 105                         lambda *args, **kwargs: states.pop())
 
 107     with pytest.raises(IndexError):
 
 110     assert index_mock.called == 4
 
 113 def test_replication_update_continuous_no_change(monkeypatch, init_status, index_mock):
 
 114     states = [nominatim.tools.replication.UpdateState.NO_CHANGES,
 
 115               nominatim.tools.replication.UpdateState.UP_TO_DATE]
 
 116     monkeypatch.setattr(nominatim.tools.replication, 'update',
 
 117                         lambda *args, **kwargs: states.pop())
 
 119     sleep_mock = MockParamCapture()
 
 120     monkeypatch.setattr(time, 'sleep', sleep_mock)
 
 122     with pytest.raises(IndexError):
 
 125     assert index_mock.called == 2
 
 126     assert sleep_mock.called == 1
 
 127     assert sleep_mock.last_args[0] == 60