mirror of
https://github.com/osm-search/Nominatim.git
synced 2026-03-09 03:24:06 +00:00
generalize fixtures for cli tests
This commit is contained in:
@@ -2,6 +2,24 @@ import pytest
|
|||||||
|
|
||||||
import nominatim.cli
|
import nominatim.cli
|
||||||
|
|
||||||
|
class MockParamCapture:
|
||||||
|
""" Mock that records the parameters with which a function was called
|
||||||
|
as well as the number of calls.
|
||||||
|
"""
|
||||||
|
def __init__(self, retval=0):
|
||||||
|
self.called = 0
|
||||||
|
self.return_value = retval
|
||||||
|
self.last_args = None
|
||||||
|
self.last_kwargs = None
|
||||||
|
|
||||||
|
def __call__(self, *args, **kwargs):
|
||||||
|
self.called += 1
|
||||||
|
self.last_args = args
|
||||||
|
self.last_kwargs = kwargs
|
||||||
|
return self.return_value
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def cli_call(src_dir):
|
def cli_call(src_dir):
|
||||||
""" Call the nominatim main function with the correct paths set.
|
""" Call the nominatim main function with the correct paths set.
|
||||||
@@ -19,3 +37,20 @@ def cli_call(src_dir):
|
|||||||
|
|
||||||
return _call_nominatim
|
return _call_nominatim
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def mock_run_legacy(monkeypatch):
|
||||||
|
mock = MockParamCapture()
|
||||||
|
monkeypatch.setattr(nominatim.cli, 'run_legacy_script', mock)
|
||||||
|
return mock
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def mock_func_factory(monkeypatch):
|
||||||
|
def get_mock(module, func):
|
||||||
|
mock = MockParamCapture()
|
||||||
|
mock.func_name = func
|
||||||
|
monkeypatch.setattr(module, func, mock)
|
||||||
|
return mock
|
||||||
|
|
||||||
|
return get_mock
|
||||||
|
|||||||
@@ -24,26 +24,6 @@ import nominatim.tools.refresh
|
|||||||
import nominatim.tools.postcodes
|
import nominatim.tools.postcodes
|
||||||
import nominatim.tokenizer.factory
|
import nominatim.tokenizer.factory
|
||||||
|
|
||||||
from mocks import MockParamCapture
|
|
||||||
|
|
||||||
@pytest.fixture
|
|
||||||
def mock_run_legacy(monkeypatch):
|
|
||||||
mock = MockParamCapture()
|
|
||||||
monkeypatch.setattr(nominatim.cli, 'run_legacy_script', mock)
|
|
||||||
return mock
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
|
||||||
def mock_func_factory(monkeypatch):
|
|
||||||
def get_mock(module, func):
|
|
||||||
mock = MockParamCapture()
|
|
||||||
mock.func_name = func
|
|
||||||
monkeypatch.setattr(module, func, mock)
|
|
||||||
return mock
|
|
||||||
|
|
||||||
return get_mock
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class TestCli:
|
class TestCli:
|
||||||
|
|
||||||
|
|||||||
@@ -11,8 +11,6 @@ import nominatim.indexer.indexer
|
|||||||
import nominatim.tools.replication
|
import nominatim.tools.replication
|
||||||
from nominatim.db import status
|
from nominatim.db import status
|
||||||
|
|
||||||
from mocks import MockParamCapture
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def tokenizer_mock(monkeypatch):
|
def tokenizer_mock(monkeypatch):
|
||||||
class DummyTokenizer:
|
class DummyTokenizer:
|
||||||
@@ -35,15 +33,6 @@ def tokenizer_mock(monkeypatch):
|
|||||||
return tok
|
return tok
|
||||||
|
|
||||||
|
|
||||||
@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
|
@pytest.fixture
|
||||||
def init_status(temp_db_conn, status_table):
|
def init_status(temp_db_conn, status_table):
|
||||||
@@ -51,11 +40,8 @@ def init_status(temp_db_conn, status_table):
|
|||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def index_mock(monkeypatch, tokenizer_mock, init_status):
|
def index_mock(mock_func_factory, tokenizer_mock, init_status):
|
||||||
mock = MockParamCapture()
|
return mock_func_factory(nominatim.indexer.indexer.Indexer, 'index_full')
|
||||||
monkeypatch.setattr(nominatim.indexer.indexer.Indexer, 'index_full', mock)
|
|
||||||
|
|
||||||
return mock
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
@@ -69,6 +55,16 @@ class TestCliReplication:
|
|||||||
def setup_cli_call(self, cli_call, temp_db):
|
def setup_cli_call(self, cli_call, temp_db):
|
||||||
self.call_nominatim = lambda *args: cli_call('replication', *args)
|
self.call_nominatim = lambda *args: cli_call('replication', *args)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture(autouse=True)
|
||||||
|
def setup_update_function(self, monkeypatch):
|
||||||
|
def _mock_updates(states):
|
||||||
|
monkeypatch.setattr(nominatim.tools.replication, 'update',
|
||||||
|
lambda *args, **kwargs: states.pop())
|
||||||
|
|
||||||
|
self.update_states = _mock_updates
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize("params,func", [
|
@pytest.mark.parametrize("params,func", [
|
||||||
(('--init', '--no-update-functions'), 'init_replication'),
|
(('--init', '--no-update-functions'), 'init_replication'),
|
||||||
(('--check-for-updates',), 'check_for_updates')
|
(('--check-for-updates',), 'check_for_updates')
|
||||||
@@ -107,11 +103,9 @@ class TestCliReplication:
|
|||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize("update_interval", [60, 3600])
|
@pytest.mark.parametrize("update_interval", [60, 3600])
|
||||||
def test_replication_catchup(self, monkeypatch, index_mock, update_interval, placex_table):
|
def test_replication_catchup(self, placex_table, monkeypatch, index_mock, update_interval):
|
||||||
monkeypatch.setenv('NOMINATIM_REPLICATION_UPDATE_INTERVAL', str(update_interval))
|
monkeypatch.setenv('NOMINATIM_REPLICATION_UPDATE_INTERVAL', str(update_interval))
|
||||||
states = [nominatim.tools.replication.UpdateState.NO_CHANGES]
|
self.update_states([nominatim.tools.replication.UpdateState.NO_CHANGES])
|
||||||
monkeypatch.setattr(nominatim.tools.replication, 'update',
|
|
||||||
lambda *args, **kwargs: states.pop())
|
|
||||||
|
|
||||||
assert self.call_nominatim('--catch-up') == 0
|
assert self.call_nominatim('--catch-up') == 0
|
||||||
|
|
||||||
@@ -122,11 +116,9 @@ class TestCliReplication:
|
|||||||
assert update_mock.last_args[1]['threads'] == 4
|
assert update_mock.last_args[1]['threads'] == 4
|
||||||
|
|
||||||
|
|
||||||
def test_replication_update_continuous(self, monkeypatch, index_mock):
|
def test_replication_update_continuous(self, index_mock):
|
||||||
states = [nominatim.tools.replication.UpdateState.UP_TO_DATE,
|
self.update_states([nominatim.tools.replication.UpdateState.UP_TO_DATE,
|
||||||
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())
|
|
||||||
|
|
||||||
with pytest.raises(IndexError):
|
with pytest.raises(IndexError):
|
||||||
self.call_nominatim()
|
self.call_nominatim()
|
||||||
@@ -134,14 +126,12 @@ class TestCliReplication:
|
|||||||
assert index_mock.called == 2
|
assert index_mock.called == 2
|
||||||
|
|
||||||
|
|
||||||
def test_replication_update_continuous_no_change(self, monkeypatch, index_mock):
|
def test_replication_update_continuous_no_change(self, mock_func_factory,
|
||||||
states = [nominatim.tools.replication.UpdateState.NO_CHANGES,
|
index_mock):
|
||||||
nominatim.tools.replication.UpdateState.UP_TO_DATE]
|
self.update_states([nominatim.tools.replication.UpdateState.NO_CHANGES,
|
||||||
monkeypatch.setattr(nominatim.tools.replication, 'update',
|
nominatim.tools.replication.UpdateState.UP_TO_DATE])
|
||||||
lambda *args, **kwargs: states.pop())
|
|
||||||
|
|
||||||
sleep_mock = MockParamCapture()
|
sleep_mock = mock_func_factory(time, 'sleep')
|
||||||
monkeypatch.setattr(time, 'sleep', sleep_mock)
|
|
||||||
|
|
||||||
with pytest.raises(IndexError):
|
with pytest.raises(IndexError):
|
||||||
self.call_nominatim()
|
self.call_nominatim()
|
||||||
|
|||||||
@@ -10,23 +10,6 @@ from nominatim.db import properties
|
|||||||
# This must always point to the mock word table for the default tokenizer.
|
# This must always point to the mock word table for the default tokenizer.
|
||||||
from mock_legacy_word_table import MockLegacyWordTable as MockWordTable
|
from mock_legacy_word_table import MockLegacyWordTable as MockWordTable
|
||||||
|
|
||||||
class MockParamCapture:
|
|
||||||
""" Mock that records the parameters with which a function was called
|
|
||||||
as well as the number of calls.
|
|
||||||
"""
|
|
||||||
def __init__(self, retval=0):
|
|
||||||
self.called = 0
|
|
||||||
self.return_value = retval
|
|
||||||
self.last_args = None
|
|
||||||
self.last_kwargs = None
|
|
||||||
|
|
||||||
def __call__(self, *args, **kwargs):
|
|
||||||
self.called += 1
|
|
||||||
self.last_args = args
|
|
||||||
self.last_kwargs = kwargs
|
|
||||||
return self.return_value
|
|
||||||
|
|
||||||
|
|
||||||
class MockPlacexTable:
|
class MockPlacexTable:
|
||||||
""" A placex table for testing.
|
""" A placex table for testing.
|
||||||
"""
|
"""
|
||||||
|
|||||||
Reference in New Issue
Block a user