From 211f4610df2006e939bb3f6e8f2cb7b87151dfba Mon Sep 17 00:00:00 2001 From: Nicolas Dandrimont Date: Thu, 16 Jul 2020 11:45:58 +0200 Subject: [PATCH] Move get_scheduler monkeypatching into an explicit pytest fixture This allows us to actually run the lister instantiation code instead of relying on the underlying structure of the lister object. In turn, this allows future listers to use the scheduler right in their __init__. --- conftest.py | 18 ++++++++++++++++++ swh/lister/core/tests/conftest.py | 4 +--- swh/lister/tests/test_cli.py | 2 +- 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/conftest.py b/conftest.py index 3ea20c3..1759d04 100644 --- a/conftest.py +++ b/conftest.py @@ -5,6 +5,24 @@ import os +import pytest + pytest_plugins = ["swh.scheduler.pytest_plugin"] os.environ["LC_ALL"] = "C.UTF-8" + + +@pytest.fixture +def mock_get_scheduler(monkeypatch, swh_scheduler): + """Override the get_scheduler function in swh.lister.core.lister_base, to + return the swh_scheduler fixture. + """ + from swh.lister.core import lister_base + + # Match the signature from swh.scheduler.get_scheduler + def get_scheduler(cls, args={}): + return swh_scheduler + + monkeypatch.setattr(lister_base, "get_scheduler", get_scheduler) + + yield monkeypatch diff --git a/swh/lister/core/tests/conftest.py b/swh/lister/core/tests/conftest.py index 21ce76f..647fd46 100644 --- a/swh/lister/core/tests/conftest.py +++ b/swh/lister/core/tests/conftest.py @@ -27,14 +27,12 @@ def lister_db_url(postgresql_proc, postgresql): @pytest.fixture -def swh_listers(request, lister_db_url, swh_scheduler): - +def swh_listers(mock_get_scheduler, lister_db_url, swh_scheduler): listers = {} # Prepare schema for all listers for lister_name in SUPPORTED_LISTERS: lister = get_lister(lister_name, db_url=lister_db_url) - lister.scheduler = swh_scheduler # inject scheduler fixture listers[lister_name] = lister initialize(create_engine(lister_db_url), drop_tables=True) diff --git a/swh/lister/tests/test_cli.py b/swh/lister/tests/test_cli.py index a333804..b3be48f 100644 --- a/swh/lister/tests/test_cli.py +++ b/swh/lister/tests/test_cli.py @@ -20,7 +20,7 @@ def test_get_lister_wrong_input(): assert "Invalid lister" in str(e.value) -def test_get_lister(): +def test_get_lister(mock_get_scheduler): """Instantiating a supported lister should be ok """