From baca42f06ecc2d2186d60262aa514f020a96c1c3 Mon Sep 17 00:00:00 2001 From: Athanasius Date: Wed, 24 Mar 2021 16:41:56 +0000 Subject: [PATCH] JournalLock: Fix update_lock() with an extra mock * update_lock() wants to, potentially, invoke JournalLock.JournalAlreadyLocked() instance for a tkinter pop-up. We do *not* want to mock all of that, so monkeypatch a mock so that in the 'test PASSED' case this just works. --- tests/journal_lock.py/test_journal_lock.py | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/tests/journal_lock.py/test_journal_lock.py b/tests/journal_lock.py/test_journal_lock.py index cf4e5e42..4fe03671 100644 --- a/tests/journal_lock.py/test_journal_lock.py +++ b/tests/journal_lock.py/test_journal_lock.py @@ -120,7 +120,7 @@ class TestJournalLock: def get_str(key: str, *, default: str = None) -> str: """Mock config.*Config get_str to provide fake journaldir.""" if key == 'journaldir': - return tmpdir_factory.mktemp() + return tmpdir_factory.mktemp("changing") print('Other key, calling up ...') return config.get_str(key) # Call the non-mocked @@ -129,6 +129,17 @@ class TestJournalLock: m.setattr(config, "get_str", get_str) yield tmpdir_factory + @pytest.fixture + def mock_journalalreadylocked(self, monkeypatch: _pytest_monkeypatch) -> JournalLock: + """Fixture to mock JournalAlreadyLocked in JournalLock instance.""" + class MockJournalAlreadyLocked: + def __init__(self, parent, callback) -> None: + pass + + with monkeypatch.context() as m: + m.setattr(JournalLock, "JournalAlreadyLocked", MockJournalAlreadyLocked) + yield + ########################################################################### # Tests against JournalLock.__init__() def test_journal_lock_init(self, mock_journaldir: py_path_local_LocalPath): @@ -295,7 +306,10 @@ class TestJournalLock: ########################################################################### # Tests against JournalLock.update_lock() - def test_update_lock(self, mock_journaldir_changing: py_path_local_LocalPath): + def test_update_lock( + self, + mock_journaldir_changing: py_path_local_LocalPath, + mock_journalalreadylocked): """ Test JournalLock.update_lock(). @@ -312,6 +326,6 @@ class TestJournalLock: # Now store the 'current' journaldir for reference and attempt # to update to a new one. old_journaldir = jlock.journal_dir - jlock.update_lock() - assert jlock.journaldir != old_journaldir + jlock.update_lock(None) + assert jlock.journal_dir != old_journaldir assert jlock.locked is True