EOS/tests/test_eosdashconfig.py
Bobby Noelte 0bda5ba4cc
EOSdash: Improve PV forecast configuration. (#500)
* Allow to configure planes and configuration values of planes separatedly.

Make single configuration values for planes explicitly available for configuration.
Still allows to also configure a plane by a whole plane value struct.

* Enhance admin page by file import and export of the EOS configuration

The actual EOS configuration can now be exported to the EOSdash server.
From there it can be also imported. For security reasons only import and export
from/ to a predefined directory on the EOSdash server is possible.

* Improve handling of nested value pathes in pydantic models.

Added separate methods for nested path access (get_nested_value, set_nested_value).
On value setting the missing fields along the nested path are now added automatically
and initialized with default values. Nested path access was before restricted to the
EOS configuration and is now part of the pydantic base model.

* Makefile

Add new target to run rests as CI does on Github. Improve target docs.

* Datetimeutil tests

Prolong acceptable time difference for comparison of approximately equal times in tests.

Signed-off-by: Bobby Noelte <b0661n0e17e@gmail.com>
2025-04-05 13:08:12 +02:00

106 lines
4.3 KiB
Python

"""Test suite for the EOS Dash configuration module.
This module contains tests for utility functions related to retrieving and processing
configuration data using Pydantic models.
"""
import json
from pathlib import Path
from typing import Union
import pytest
from pydantic.fields import FieldInfo
from akkudoktoreos.core.pydantic import PydanticBaseModel
from akkudoktoreos.prediction.pvforecast import PVForecastPlaneSetting
from akkudoktoreos.server.dash.configuration import (
configuration,
get_default_value,
get_nested_value,
resolve_nested_types,
)
DIR_TESTDATA = Path(__file__).absolute().parent.joinpath("testdata")
FILE_TESTDATA_EOSSERVER_CONFIG_1 = DIR_TESTDATA.joinpath("eosserver_config_1.json")
class SampleModel(PydanticBaseModel):
field1: str = "default_value"
field2: int = 10
class TestEOSdashConfig:
"""Test case for EOS Dash configuration utility functions.
This class tests functions for retrieving nested values, extracting default values,
resolving nested types, and generating configuration details from Pydantic models.
"""
def test_get_nested_value_from_dict(self):
"""Test retrieving a nested value from a dictionary using a sequence of keys."""
data = {"a": {"b": {"c": 42}}}
assert get_nested_value(data, ["a", "b", "c"]) == 42
assert get_nested_value(data, ["a", "x"], default="not found") == "not found"
with pytest.raises(TypeError):
get_nested_value("not_a_dict", ["a"]) # type: ignore
def test_get_nested_value_from_list(self):
"""Test retrieving a nested value from a list using a sequence of keys."""
data = {"a": {"b": {"c": [42]}}}
assert get_nested_value(data, ["a", "b", "c", 0]) == 42
assert get_nested_value(data, ["a", "b", "c", "0"]) == 42
def test_get_default_value(self):
"""Test retrieving the default value of a field based on FieldInfo metadata."""
field_info = FieldInfo(default="test_value")
assert get_default_value(field_info, True) == "test_value"
field_info_no_default = FieldInfo()
assert get_default_value(field_info_no_default, True) == ""
assert get_default_value(field_info, False) == "N/A"
def test_resolve_nested_types(self):
"""Test resolving nested types within a field, ensuring correct type extraction."""
nested_types = resolve_nested_types(Union[int, str], [])
assert (int, []) in nested_types
assert (str, []) in nested_types
def test_configuration(self):
"""Test extracting configuration details from a Pydantic model based on provided values."""
values = {"field1": "custom_value", "field2": 20}
config = configuration(SampleModel, values)
assert any(
item["name"] == "field1" and item["value"] == '"custom_value"' for item in config
)
assert any(item["name"] == "field2" and item["value"] == "20" for item in config)
def test_configuration_eos(self, config_eos):
"""Test extracting EOS configuration details from EOS config based on provided values."""
with FILE_TESTDATA_EOSSERVER_CONFIG_1.open("r", encoding="utf-8", newline=None) as fd:
values = json.load(fd)
config = configuration(config_eos, values)
assert any(
item["name"] == "server.eosdash_port" and item["value"] == "8504" for item in config
)
assert any(
item["name"] == "server.eosdash_host" and item["value"] == '"0.0.0.0"'
for item in config
)
def test_configuration_pvforecast_plane_settings(self):
"""Test extracting EOS PV forecast plane configuration details from EOS config based on provided values."""
with FILE_TESTDATA_EOSSERVER_CONFIG_1.open("r", encoding="utf-8", newline=None) as fd:
values = json.load(fd)
config = configuration(
PVForecastPlaneSetting(), values, values_prefix=["pvforecast", "planes", "0"]
)
assert any(
item["name"] == "pvforecast.planes.0.surface_azimuth" and item["value"] == "-10"
for item in config
)
assert any(
item["name"] == "pvforecast.planes.0.userhorizon"
and item["value"] == "[20, 27, 22, 20]"
for item in config
)