EOS/tests/test_logging.py
Bobby Noelte d4e31d556a
Add Documentation 2 (#334)
Add documentation that covers:

- configuration
- prediction

Add Python scripts that support automatic documentation generation for
configuration data defined with pydantic.

Adapt EOS configuration to provide more methods for REST API and
automatic documentation generation.

Adapt REST API to allow for EOS configuration file load and save.
Sort REST API on generation of openapi markdown for docs.

Move logutil to core/logging to allow configuration of logging by standard config.

Make Akkudoktor predictions always start extraction of prediction data at start of day.
Previously extraction started at actual hour. This is to support the code that assumes
prediction data to start at start of day.

Signed-off-by: Bobby Noelte <b0661n0e17e@gmail.com>
2025-01-05 14:41:07 +01:00

78 lines
2.2 KiB
Python

"""Test Module for logging Module."""
import logging
import os
from logging.handlers import RotatingFileHandler
import pytest
from akkudoktoreos.core.logging import get_logger
# -----------------------------
# get_logger
# -----------------------------
@pytest.fixture
def clean_up_log_file():
"""Fixture to clean up log files after tests."""
log_file = "test.log"
yield log_file
if os.path.exists(log_file):
os.remove(log_file)
def test_get_logger_console_logging(clean_up_log_file):
"""Test logger creation with console logging."""
logger = get_logger("test_logger", logging_level="DEBUG")
# Check logger name
assert logger.name == "test_logger"
# Check logger level
assert logger.level == logging.DEBUG
# Check console handler is present
assert len(logger.handlers) == 1
assert isinstance(logger.handlers[0], logging.StreamHandler)
def test_get_logger_file_logging(clean_up_log_file):
"""Test logger creation with file logging."""
logger = get_logger("test_logger", log_file="test.log", logging_level="WARNING")
# Check logger name
assert logger.name == "test_logger"
# Check logger level
assert logger.level == logging.WARNING
# Check console handler is present
assert len(logger.handlers) == 2 # One for console and one for file
assert isinstance(logger.handlers[0], logging.StreamHandler)
assert isinstance(logger.handlers[1], RotatingFileHandler)
# Check file existence
assert os.path.exists("test.log")
def test_get_logger_no_file_logging(clean_up_log_file):
"""Test logger creation without file logging."""
logger = get_logger("test_logger")
# Check logger name
assert logger.name == "test_logger"
# Check logger level
assert logger.level == logging.INFO
# Check no file handler is present
assert len(logger.handlers) >= 1 # First is console handler (maybe be pytest handler)
assert isinstance(logger.handlers[0], logging.StreamHandler)
def test_get_logger_with_invalid_level(clean_up_log_file):
"""Test logger creation with an invalid logging level."""
with pytest.raises(ValueError, match="Unknown loggin level: INVALID"):
logger = get_logger("test_logger", logging_level="INVALID")