mirror of
https://github.com/Akkudoktor-EOS/EOS.git
synced 2025-09-19 18:21:14 +00:00
Add test to PVForecast (#174)
* Add documentation to class_pv_forecast.py. Added documentation. Beware mostly generated by ChatGPT. Signed-off-by: Bobby Noelte <b0661n0e17e@gmail.com> * Add CacheFileStore, datetime and logger utilities. The `CacheFileStore` class is a singleton-based, thread-safe key-value store for managing temporary file objects, allowing the creation, retrieval, and management of cache files. The utility modules offer a flexible logging setup (`get_logger`) and utilities to handle different date-time formats (`to_datetime`, `to_timestamp`) and timezone detection (`to_timezone). - Cache files are automatically valid for the the current date unless specified otherwise. This is to mimic the current behaviour used in several classes. - The logger supports rotating log files to prevent excessive log file size. - The `to_datetime` and `to_timestamp`functions support a wide variety of input types and formats. They provide the time conversion that is e.g. used in PVForecast. Signed-off-by: Bobby Noelte <b0661n0e17e@gmail.com> * Improve testability of PVForecast Improvements for testing of PVForecast - Use common utility functions to allow for general testing at one spot. - to_datetime - CacheFileStore - Use logging instead of print to easily capture in testing. - Add validation of the json schema for Akkudoktor PV forecast data. - Allow to create an empty PVForecast instance as base instance for testing. - Make process_data() complete for filling a PVForecast instance for testing. - Normalize forecast datetime to timezone of system given in loaded data. - Do not print report but provide report for test checks. - Get rid of cache file path using the CachFileStore to automate cache file usage. - Improved module documentation. Signed-off-by: Bobby Noelte <b0661n0e17e@gmail.com> * Add test for PVForecast and newly extracted utility modules. - Add test for PVForecast - Add test for CacheFileStore in the new cachefilestore module - Add test for to_datetime, to_timestamp, to_timezone in the new datetimeutil module - Add test for get_logger in the new logutil module Signed-off-by: Bobby Noelte <b0661n0e17e@gmail.com> --------- Signed-off-by: Bobby Noelte <b0661n0e17e@gmail.com> Co-authored-by: Normann <github@koldrack.com>
This commit is contained in:
82
tests/test_logutil.py
Normal file
82
tests/test_logutil.py
Normal file
@@ -0,0 +1,82 @@
|
||||
"""Test Module for logutil Module."""
|
||||
|
||||
import logging
|
||||
import os
|
||||
from logging.handlers import RotatingFileHandler
|
||||
|
||||
import pytest
|
||||
|
||||
from akkudoktoreos.logutil 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."""
|
||||
logger = get_logger("test_logger", logging_level="INVALID")
|
||||
|
||||
# Check logger name
|
||||
assert logger.name == "test_logger"
|
||||
|
||||
# Check default logging level is DEBUG
|
||||
assert logger.level == logging.DEBUG
|
Reference in New Issue
Block a user