2021-02-24 12:38:19 +03:00
|
|
|
"""
|
|
|
|
Tests for replication command of command-line interface wrapper.
|
|
|
|
"""
|
|
|
|
import datetime as dt
|
|
|
|
import time
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
import nominatim.cli
|
|
|
|
import nominatim.indexer.indexer
|
|
|
|
import nominatim.tools.replication
|
|
|
|
from nominatim.db import status
|
|
|
|
|
|
|
|
from mocks import MockParamCapture
|
|
|
|
|
2021-05-01 11:50:39 +03:00
|
|
|
@pytest.fixture
|
|
|
|
def tokenizer_mock(monkeypatch):
|
|
|
|
class DummyTokenizer:
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
self.update_sql_functions_called = False
|
|
|
|
self.finalize_import_called = False
|
|
|
|
|
|
|
|
def update_sql_functions(self, *args):
|
|
|
|
self.update_sql_functions_called = True
|
|
|
|
|
|
|
|
def finalize_import(self, *args):
|
|
|
|
self.finalize_import_called = True
|
|
|
|
|
|
|
|
tok = DummyTokenizer()
|
2021-05-20 00:07:39 +03:00
|
|
|
monkeypatch.setattr(nominatim.tokenizer.factory, 'get_tokenizer_for_db',
|
2021-05-01 11:50:39 +03:00
|
|
|
lambda *args: tok)
|
2021-05-20 00:07:39 +03:00
|
|
|
monkeypatch.setattr(nominatim.tokenizer.factory, 'create_tokenizer',
|
2021-05-01 11:50:39 +03:00
|
|
|
lambda *args: tok)
|
|
|
|
|
|
|
|
return tok
|
|
|
|
|
|
|
|
|
2021-02-24 12:38:19 +03:00
|
|
|
@pytest.fixture
|
|
|
|
def mock_func_factory(monkeypatch):
|
|
|
|
def get_mock(module, func):
|
|
|
|
mock = MockParamCapture()
|
|
|
|
monkeypatch.setattr(module, func, mock)
|
|
|
|
return mock
|
|
|
|
|
|
|
|
return get_mock
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def init_status(temp_db_conn, status_table):
|
|
|
|
status.set_status(temp_db_conn, date=dt.datetime.now(dt.timezone.utc), seq=1)
|
2021-05-20 00:07:39 +03:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def index_mock(monkeypatch, tokenizer_mock, init_status):
|
|
|
|
mock = MockParamCapture()
|
|
|
|
monkeypatch.setattr(nominatim.indexer.indexer.Indexer, 'index_boundaries', mock)
|
|
|
|
monkeypatch.setattr(nominatim.indexer.indexer.Indexer, 'index_by_rank', mock)
|
|
|
|
|
|
|
|
return mock
|
2021-02-24 12:38:19 +03:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
2021-04-24 12:25:47 +03:00
|
|
|
def update_mock(mock_func_factory, init_status, tokenizer_mock):
|
2021-02-24 12:38:19 +03:00
|
|
|
return mock_func_factory(nominatim.tools.replication, 'update')
|
|
|
|
|
|
|
|
|
2021-05-19 17:03:54 +03:00
|
|
|
class TestCliReplication:
|
|
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
|
|
def setup_cli_call(self, cli_call, temp_db):
|
|
|
|
self.call_nominatim = lambda *args: cli_call('replication', *args)
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("params,func", [
|
|
|
|
(('--init', '--no-update-functions'), 'init_replication'),
|
|
|
|
(('--check-for-updates',), 'check_for_updates')
|
|
|
|
])
|
|
|
|
def test_replication_command(self, mock_func_factory, params, func):
|
|
|
|
func_mock = mock_func_factory(nominatim.tools.replication, func)
|
|
|
|
|
2021-05-20 00:07:39 +03:00
|
|
|
assert self.call_nominatim(*params) == 0
|
2021-05-19 17:03:54 +03:00
|
|
|
assert func_mock.called == 1
|
2021-02-24 12:38:19 +03:00
|
|
|
|
|
|
|
|
2021-05-19 17:03:54 +03:00
|
|
|
def test_replication_update_bad_interval(self, monkeypatch):
|
|
|
|
monkeypatch.setenv('NOMINATIM_REPLICATION_UPDATE_INTERVAL', 'xx')
|
2021-02-24 12:38:19 +03:00
|
|
|
|
2021-05-19 17:03:54 +03:00
|
|
|
assert self.call_nominatim() == 1
|
2021-02-24 12:38:19 +03:00
|
|
|
|
|
|
|
|
2021-05-19 17:03:54 +03:00
|
|
|
def test_replication_update_bad_interval_for_geofabrik(self, monkeypatch):
|
|
|
|
monkeypatch.setenv('NOMINATIM_REPLICATION_URL',
|
2021-05-20 00:07:39 +03:00
|
|
|
'https://download.geofabrik.de/europe/italy-updates')
|
2021-02-24 12:38:19 +03:00
|
|
|
|
2021-05-19 17:03:54 +03:00
|
|
|
assert self.call_nominatim() == 1
|
2021-02-24 12:38:19 +03:00
|
|
|
|
|
|
|
|
2021-05-19 17:03:54 +03:00
|
|
|
def test_replication_update_once_no_index(self, update_mock):
|
2021-05-20 00:07:39 +03:00
|
|
|
assert self.call_nominatim('--once', '--no-index') == 0
|
2021-02-24 12:38:19 +03:00
|
|
|
|
2021-05-19 17:03:54 +03:00
|
|
|
assert str(update_mock.last_args[1]['osm2pgsql']) == 'OSM2PGSQL NOT AVAILABLE'
|
2021-02-24 12:38:19 +03:00
|
|
|
|
|
|
|
|
2021-05-19 17:03:54 +03:00
|
|
|
def test_replication_update_custom_osm2pgsql(self, monkeypatch, update_mock):
|
|
|
|
monkeypatch.setenv('NOMINATIM_OSM2PGSQL_BINARY', '/secret/osm2pgsql')
|
2021-05-20 00:07:39 +03:00
|
|
|
assert self.call_nominatim('--once', '--no-index') == 0
|
2021-02-24 12:38:19 +03:00
|
|
|
|
2021-05-19 17:03:54 +03:00
|
|
|
assert str(update_mock.last_args[1]['osm2pgsql']) == '/secret/osm2pgsql'
|
2021-02-24 12:38:19 +03:00
|
|
|
|
|
|
|
|
2021-05-19 17:03:54 +03:00
|
|
|
def test_replication_update_custom_threads(self, update_mock):
|
2021-05-20 00:07:39 +03:00
|
|
|
assert self.call_nominatim('--once', '--no-index', '--threads', '4') == 0
|
2021-02-24 12:38:19 +03:00
|
|
|
|
2021-05-19 17:03:54 +03:00
|
|
|
assert update_mock.last_args[1]['threads'] == 4
|
2021-02-24 12:38:19 +03:00
|
|
|
|
|
|
|
|
2021-05-20 00:07:39 +03:00
|
|
|
def test_replication_update_continuous(self, monkeypatch, index_mock):
|
2021-05-19 17:03:54 +03:00
|
|
|
states = [nominatim.tools.replication.UpdateState.UP_TO_DATE,
|
|
|
|
nominatim.tools.replication.UpdateState.UP_TO_DATE]
|
|
|
|
monkeypatch.setattr(nominatim.tools.replication, 'update',
|
|
|
|
lambda *args, **kwargs: states.pop())
|
2021-02-24 12:38:19 +03:00
|
|
|
|
2021-05-19 17:03:54 +03:00
|
|
|
with pytest.raises(IndexError):
|
|
|
|
self.call_nominatim()
|
2021-02-24 12:38:19 +03:00
|
|
|
|
2021-05-19 17:03:54 +03:00
|
|
|
assert index_mock.called == 4
|
2021-02-24 12:38:19 +03:00
|
|
|
|
|
|
|
|
2021-05-20 00:07:39 +03:00
|
|
|
def test_replication_update_continuous_no_change(self, monkeypatch, index_mock):
|
2021-05-19 17:03:54 +03:00
|
|
|
states = [nominatim.tools.replication.UpdateState.NO_CHANGES,
|
|
|
|
nominatim.tools.replication.UpdateState.UP_TO_DATE]
|
|
|
|
monkeypatch.setattr(nominatim.tools.replication, 'update',
|
|
|
|
lambda *args, **kwargs: states.pop())
|
2021-02-24 12:38:19 +03:00
|
|
|
|
2021-05-19 17:03:54 +03:00
|
|
|
sleep_mock = MockParamCapture()
|
|
|
|
monkeypatch.setattr(time, 'sleep', sleep_mock)
|
2021-02-24 12:38:19 +03:00
|
|
|
|
2021-05-19 17:03:54 +03:00
|
|
|
with pytest.raises(IndexError):
|
|
|
|
self.call_nominatim()
|
2021-02-24 12:38:19 +03:00
|
|
|
|
2021-05-19 17:03:54 +03:00
|
|
|
assert index_mock.called == 2
|
|
|
|
assert sleep_mock.called == 1
|
|
|
|
assert sleep_mock.last_args[0] == 60
|