✅test(classifier): add tests for moving folders functions
This commit is contained in:
@@ -13,11 +13,13 @@ 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,
|
||||
extract_logs_from_mails,
|
||||
fetch_log_messages,
|
||||
move_analyzed_mails,
|
||||
)
|
||||
|
||||
|
||||
@@ -74,10 +76,13 @@ def get_log_mail_with_body() -> Message:
|
||||
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):
|
||||
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
|
||||
@@ -519,3 +524,61 @@ def test_extract_logs_from_mails_no_mails():
|
||||
"""Returns [] when no mails in folder."""
|
||||
content_list = extract_logs_from_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())
|
||||
|
||||
mail_one = Message(subject="mail1", text_body="text1")
|
||||
mail_two = Message(subject="mail2", text_body="text2")
|
||||
mail_one.id, mail_one.changekey = "id1", "ck1"
|
||||
mail_two.id, mail_two.changekey = "id2", "ck2"
|
||||
|
||||
move_analyzed_mails(account, [mail_one, mail_two])
|
||||
|
||||
account.bulk_move.assert_called_once()
|
||||
assert account.bulk_move.call_args.kwargs["ids"] == [("id1", "ck1"), ("id2", "ck2")]
|
||||
|
||||
|
||||
def test_move_analyzed_mails_empty_list(make_mock_account):
|
||||
"""Does nothing when there is no mail to move."""
|
||||
account = make_mock_account(analyzed_folder=MagicMock())
|
||||
|
||||
move_analyzed_mails(account, [])
|
||||
|
||||
account.bulk_move.assert_not_called()
|
||||
|
||||
Reference in New Issue
Block a user