Files
logwatcher/tests/test_utils.py
T
maurane 02e85ddbfd feat(cleaner): ✨ add command prune
this new command will be called by cron-like process to clean purge old logs and old mails. All files were formatted and mail_utils was renamed in utils since it now defines utility function not only for mail functions.
2026-09-25 16:30:45 +02:00

63 lines
2.0 KiB
Python

from pathlib import Path
from unittest.mock import MagicMock
from logwatcher.utils import get_or_create_folder, is_log_file
def test_is_log_file(tmp_path: Path):
"""Assert only .log, .txt or file without extensions are valid.
Args:
tmp_path: Temporary root path for testing results
"""
# set up files
Path(tmp_path / "a.log").touch()
Path(tmp_path / "a.txt").touch()
Path(tmp_path / "a.csv").touch()
Path(tmp_path / "a.py").touch()
Path(tmp_path / "a").touch()
Path(tmp_path / "a.pdf").touch()
# test
assert is_log_file(Path(tmp_path / "a.log"))
assert is_log_file(Path(tmp_path / "a.txt"))
assert is_log_file(Path(tmp_path / "a")) # without extensions
assert not is_log_file(Path(tmp_path / "a.csv"))
assert not is_log_file(Path(tmp_path / "a.py"))
assert not is_log_file(Path(tmp_path / "a.pdf"))
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()