File utils1 (#1)
Tests / test_and_lint (push) Successful in 7s

Reviewed-on: #1
Co-authored-by: Mathias Scheider <mathias2784@web.de>
This commit was merged in pull request #1.
This commit is contained in:
2026-10-01 19:13:45 +00:00
committed by mathias2784
parent 59faa596d5
commit 9d13c6ec14
12 changed files with 452 additions and 15 deletions
Executable → Regular
+30 -13
View File
@@ -7,16 +7,16 @@ from pathlib import Path
from unittest.mock import patch
import pytest
from pydantic import ValidationError
from uvicorn.logging import DefaultFormatter
from dataflex_backend_common.logging_config import LoggingSettings
from pydantic import ValidationError
from utils.logging_config import LoggingSettings
SECONDS_PER_DAY = 24 * 60 * 60
@pytest.fixture
def restore_root_logger() -> Generator[None, None, None]:
def restore_root_logger() -> Generator[None]:
root = logging.getLogger()
old_handlers = root.handlers[:]
old_level = root.level
@@ -41,11 +41,13 @@ def test_sets_valid_log_level(restore_root_logger: None) -> None:
def test_invalid_log_level_falls_back_to_info(restore_root_logger: None) -> None:
with patch("dataflex_backend_common.logging_config.logger.error") as log_error:
with patch("utils.logging_config.logger.error") as log_error:
LoggingSettings(LOG_LEVEL="NOT_A_LEVEL", LOG_TO_FILE=False)
assert logging.getLogger().level == logging.INFO
log_error.assert_called_once_with("Invalid log level %r; falling back to INFO.", "NOT_A_LEVEL")
log_error.assert_called_once_with(
"Invalid log level %r; falling back to INFO.", "NOT_A_LEVEL"
)
def test_configures_console_handler_only(restore_root_logger: None) -> None:
@@ -66,7 +68,9 @@ def test_configures_file_handler_and_creates_parent_dir(
handlers = logging.getLogger().handlers
assert any(type(handler) is logging.StreamHandler for handler in handlers)
file_handlers = [handler for handler in handlers if isinstance(handler, logging.FileHandler)]
file_handlers = [
handler for handler in handlers if isinstance(handler, logging.FileHandler)
]
assert len(file_handlers) == 1
assert log_file.parent.exists()
assert isinstance(file_handlers[0], TimedRotatingFileHandler)
@@ -77,12 +81,16 @@ def test_configures_file_handler_and_creates_parent_dir(
assert file_handlers[0].encoding.lower() == "utf-8"
def test_configures_custom_log_backup_count(tmp_path: Path, restore_root_logger: None) -> None:
def test_configures_custom_log_backup_count(
tmp_path: Path, restore_root_logger: None
) -> None:
log_file = tmp_path / "app.log"
LoggingSettings(LOG_TO_FILE=True, LOG_FILE_PATH=str(log_file), LOG_BACKUP_COUNT=7)
file_handler = next(
handler for handler in logging.getLogger().handlers if isinstance(handler, TimedRotatingFileHandler)
handler
for handler in logging.getLogger().handlers
if isinstance(handler, TimedRotatingFileHandler)
)
assert file_handler.backupCount == 7
@@ -99,7 +107,9 @@ def test_writes_records_to_console_and_file(
logging.getLogger("test.logging_output").info("record is written")
file_handler = next(
handler for handler in logging.getLogger().handlers if isinstance(handler, TimedRotatingFileHandler)
handler
for handler in logging.getLogger().handlers
if isinstance(handler, TimedRotatingFileHandler)
)
file_handler.flush()
@@ -112,7 +122,9 @@ def test_rotates_active_log_file(tmp_path: Path, restore_root_logger: None) -> N
LoggingSettings(LOG_FILE_PATH=str(log_file))
logger = logging.getLogger("test.logging_rotation")
file_handler = next(
handler for handler in logging.getLogger().handlers if isinstance(handler, TimedRotatingFileHandler)
handler
for handler in logging.getLogger().handlers
if isinstance(handler, TimedRotatingFileHandler)
)
logger.info("before rollover")
@@ -137,7 +149,9 @@ def test_removes_old_rotated_files_after_backup_limit(
LoggingSettings(LOG_FILE_PATH=str(log_file), LOG_BACKUP_COUNT=2)
logger = logging.getLogger("test.logging_retention")
file_handler = next(
handler for handler in logging.getLogger().handlers if isinstance(handler, TimedRotatingFileHandler)
handler
for handler in logging.getLogger().handlers
if isinstance(handler, TimedRotatingFileHandler)
)
base_timestamp = int(time.time())
@@ -146,7 +160,8 @@ def test_removes_old_rotated_files_after_backup_limit(
file_handler.flush()
rollover_timestamp = base_timestamp + day * SECONDS_PER_DAY
file_handler.rolloverAt = rollover_timestamp + SECONDS_PER_DAY
monkeypatch.setattr(time, "time", lambda: rollover_timestamp)
# Bind the loop variable via a default argument to avoid late binding (B023).
monkeypatch.setattr(time, "time", lambda now=rollover_timestamp: now)
file_handler.doRollover()
assert len(list(tmp_path.glob("app.*.log"))) == 2
@@ -162,7 +177,9 @@ def test_setup_logging_is_idempotent(tmp_path: Path, restore_root_logger: None)
settings = LoggingSettings(LOG_TO_FILE=True, LOG_FILE_PATH=str(log_file))
first_file_handler = next(
handler for handler in logging.getLogger().handlers if isinstance(handler, logging.FileHandler)
handler
for handler in logging.getLogger().handlers
if isinstance(handler, logging.FileHandler)
)
first_handler_count = len(logging.getLogger().handlers)