101 lines
2.5 KiB
Python
101 lines
2.5 KiB
Python
"""Tests for logging system."""
|
|
|
|
import logging
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
|
|
from src.core.exceptions import ConfigurationError
|
|
from src.logging import get_logger, reset_logging_config
|
|
|
|
|
|
def test_get_logger_with_name():
|
|
"""Test getting logger with name."""
|
|
logger = get_logger("test_module")
|
|
assert isinstance(logger, logging.Logger)
|
|
assert logger.name == "test_module"
|
|
|
|
|
|
def test_get_logger_root():
|
|
"""Test getting root logger."""
|
|
logger = get_logger()
|
|
assert isinstance(logger, logging.Logger)
|
|
|
|
|
|
def test_logger_logs_message(caplog):
|
|
"""Test that logger actually logs messages."""
|
|
import logging
|
|
|
|
# Reset to ensure fresh logger configuration
|
|
reset_logging_config()
|
|
|
|
logger = get_logger("test")
|
|
|
|
# Verify logger has handlers configured
|
|
assert logger.hasHandlers() or logger.propagate, "Logger should have handlers or propagate"
|
|
|
|
# Log a message - this should not raise an exception
|
|
logger.info("Test message")
|
|
|
|
# Verify the logger level allows INFO messages
|
|
assert logger.isEnabledFor(logging.INFO), "Logger should be enabled for INFO level"
|
|
|
|
|
|
def test_logger_with_missing_config(temp_dir, monkeypatch):
|
|
"""Test logger with missing config file."""
|
|
# Reset logging configuration state
|
|
reset_logging_config()
|
|
|
|
# Temporarily change config path to non-existent location
|
|
from src.core import constants
|
|
original_path = constants.PATHS["config"]
|
|
constants.PATHS["config"] = temp_dir / "nonexistent"
|
|
|
|
try:
|
|
with pytest.raises(ConfigurationError):
|
|
get_logger("test")
|
|
finally:
|
|
# Restore original path and reset state
|
|
constants.PATHS["config"] = original_path
|
|
reset_logging_config()
|
|
|
|
|
|
def test_logger_creates_directories(temp_dir, monkeypatch):
|
|
"""Test that logger creates log directories."""
|
|
from src.core import constants
|
|
original_path = constants.PATHS["logs"]
|
|
constants.PATHS["logs"] = temp_dir / "logs"
|
|
|
|
# Create minimal config
|
|
config_dir = temp_dir / "config"
|
|
config_dir.mkdir()
|
|
logging_config = config_dir / "logging.yaml"
|
|
logging_config.write_text(
|
|
"""
|
|
version: 1
|
|
disable_existing_loggers: false
|
|
|
|
formatters:
|
|
detailed:
|
|
format: '%(message)s'
|
|
|
|
handlers:
|
|
console:
|
|
class: logging.StreamHandler
|
|
level: INFO
|
|
formatter: detailed
|
|
|
|
root:
|
|
level: INFO
|
|
handlers:
|
|
- console
|
|
"""
|
|
)
|
|
|
|
logger = get_logger("test")
|
|
assert isinstance(logger, logging.Logger)
|
|
|
|
# Restore original path
|
|
constants.PATHS["logs"] = original_path
|
|
|