refactor(tests): ✅ udpate tests
update tests by adding a test_utils module
This commit is contained in:
+19
-1
@@ -1,8 +1,10 @@
|
||||
# tests/conftest.py
|
||||
import os
|
||||
from pathlib import Path
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
import pytest
|
||||
from exchangelib import Message
|
||||
|
||||
from logwatcher.config import FIXTURE_PATH
|
||||
from logwatcher.logging_config import setup_logging
|
||||
@@ -52,4 +54,20 @@ def invalid_log_dir_fixture() -> Path:
|
||||
All invalid log files created for testing only.
|
||||
Used for integration tests of parsing.
|
||||
"""
|
||||
return FIXTURE_PATH / "invalid_logs"
|
||||
return FIXTURE_PATH / "invalid_logs"
|
||||
|
||||
|
||||
@pytest.fixture(scope="package")
|
||||
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
|
||||
+1
-1
@@ -57,7 +57,7 @@ def test_cli_help():
|
||||
assert results.exit_code == 0
|
||||
assert "--verbose" in results.output
|
||||
assert "--version" in results.output
|
||||
assert "from-mail" in results.output
|
||||
assert "from-mails" in results.output
|
||||
assert "from-files" in results.output
|
||||
|
||||
|
||||
|
||||
@@ -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())
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
from logwatcher.utils import get_or_create_folder
|
||||
|
||||
|
||||
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.utils.Folder", fake_folder_cls)
|
||||
result = get_or_create_folder(account, "Analyzed")
|
||||
|
||||
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.utils.Folder", fake_folder_cls)
|
||||
result = get_or_create_folder(account, "Analyzed")
|
||||
|
||||
assert result is fake_folder_instance
|
||||
fake_folder_instance.save.assert_called_once()
|
||||
Reference in New Issue
Block a user