refactor(tests): ✅ udpate tests

update tests by adding a test_utils module
This commit is contained in:
2026-09-15 13:20:20 +02:00
parent 555af86884
commit 20ad43d694
7 changed files with 111 additions and 96 deletions
-51
View File
@@ -13,7 +13,6 @@ from logwatcher.mail_reader import (
_find_log_attachment,
_get_attachment_content,
_get_mail_body,
_get_or_create_analyzed_folder,
_has_attachment_marker,
connect_to_mailbox,
extract_log_lines,
@@ -72,22 +71,6 @@ def get_log_mail_with_body() -> Message:
)
@pytest.fixture
def make_mock_account():
"""A fake Account with a msg_folder_root returning a fake Logs folder."""
def _make_mock_account(
messages: list[Message] | None = None, analyzed_folder: MagicMock | None = None
):
account = MagicMock()
logs_folder = account.msg_folder_root.__truediv__.return_value
logs_folder.all.return_value = messages or []
logs_folder.glob.return_value = [analyzed_folder] if analyzed_folder else []
return account
return _make_mock_account
@pytest.fixture
def set_env(monkeypatch):
"""Set the mailbox environment variables."""
@@ -526,40 +509,6 @@ def test_extract_logs_from_mails_no_mails():
assert content_list == []
def test_get_or_create_analyzed_folder_exists(make_mock_account, monkeypatch):
"""
Returns the existing 'Analyzed' folder without creating it.
Args:
make_mock_account: Fake account owning the Logs folder.
monkeypatch: MonkeyPatch to generate test context
"""
existing_folder = MagicMock()
account = make_mock_account(analyzed_folder=existing_folder)
fake_folder_cls = MagicMock()
with monkeypatch.context() as m:
m.setattr("logwatcher.mail_reader.Folder", fake_folder_cls)
result = _get_or_create_analyzed_folder(account)
assert result is existing_folder
fake_folder_cls.assert_not_called()
def test_get_or_create_analyzed_folder_creates(make_mock_account, monkeypatch):
"""Creates the 'Analyzed' folder when it doesn't exist."""
account = make_mock_account(analyzed_folder=None)
fake_folder_instance = MagicMock()
fake_folder_cls = MagicMock(return_value=fake_folder_instance)
with monkeypatch.context() as m:
m.setattr("logwatcher.mail_reader.Folder", fake_folder_cls)
result = _get_or_create_analyzed_folder(account)
assert result is fake_folder_instance
fake_folder_instance.save.assert_called_once()
def test_move_analyzed_mails_moves_all(make_mock_account):
"""Calls bulk_move once with every (id, changekey) pair."""
account = make_mock_account(analyzed_folder=MagicMock())