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>
This commit is contained in:
Bobby Noelte
2025-04-05 13:08:12 +02:00
committed by GitHub
parent e6a8c0508e
commit 0bda5ba4cc
15 changed files with 1216 additions and 257 deletions

View File

@@ -304,7 +304,7 @@ def test_config_common_settings_timezone_none_when_coordinates_missing():
[0.0, 0.375, 0.5, 0.625, 0.75, 0.875, 1.0],
)
],
ValueError,
TypeError,
),
# Invalid index (out of bound)
(
@@ -316,7 +316,7 @@ def test_config_common_settings_timezone_none_when_coordinates_missing():
[0.0, 0.375, 0.5, 0.625, 0.75, 0.875, 1.0],
)
],
IndexError,
TypeError,
),
# Invalid index (no number)
(
@@ -346,14 +346,27 @@ def test_config_common_settings_timezone_none_when_coordinates_missing():
)
def test_set_nested_key(path, value, expected, exception, config_eos):
if not exception:
config_eos.set_config_value(path, value)
config_eos.set_nested_value(path, value)
for expected_path, expected_value in expected:
assert eval(f"config_eos.{expected_path}") == expected_value
actual_value = eval(f"config_eos.{expected_path}")
assert actual_value == expected_value, (
f"Expected {expected_value} at {expected_path}, but got {actual_value}"
)
else:
with pytest.raises(exception):
config_eos.set_config_value(path, value)
for expected_path, expected_value in expected:
assert eval(f"config_eos.{expected_path}") == expected_value
try:
config_eos.set_nested_value(path, value)
for expected_path, expected_value in expected:
actual_value = eval(f"config_eos.{expected_path}")
assert actual_value == expected_value, (
f"Expected {expected_value} at {expected_path}, but got {actual_value}"
)
pytest.fail(
f"Expected exception {exception} but none was raised. Set '{expected_path}' to '{actual_value}'"
)
except Exception as e:
assert isinstance(e, exception), (
f"Expected exception {exception}, but got {type(e)}: {e}"
)
@pytest.mark.parametrize(
@@ -378,10 +391,10 @@ def test_set_nested_key(path, value, expected, exception, config_eos):
)
def test_get_nested_key(path, expected_value, exception, config_eos):
if not exception:
assert config_eos.get_config_value(path) == expected_value
assert config_eos.get_nested_value(path) == expected_value
else:
with pytest.raises(exception):
config_eos.get_config_value(path)
config_eos.get_nested_value(path)
def test_merge_settings_from_dict_invalid(config_eos):

View File

@@ -368,7 +368,7 @@ def test_to_datetime(
# print(f"Result: {result} tz={result.timezone}")
# print(f"Compare: {compare}")
if expected_approximately:
assert compare.time_diff < 200
assert compare.time_diff < 300
else:
assert compare.equal == True

105
tests/test_eosdashconfig.py Normal file
View File

@@ -0,0 +1,105 @@
"""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
)

View File

@@ -138,23 +138,11 @@ def test_mixed_plane_configuration(settings):
assert settings.planes_peakpower == [5.0, 5000.0, 3.0]
def test_max_planes_limit(settings):
"""Test that the maximum number of planes is enforced."""
assert settings.max_planes == 6
# Create settings with more planes than allowed (should only recognize up to max)
plane_settings = [{"peakpower": 5.0} for _ in range(8)]
with pytest.raises(ValueError):
PVForecastCommonSettings(planes=plane_settings)
def test_invalid_plane_settings():
def test_none_plane_settings():
"""Test that optional parameters can be None for non-zero planes."""
with pytest.raises(ValueError):
PVForecastPlaneSetting(
peakpower=5.0,
albedo=None,
module_model=None,
userhorizon=None,
)
setting = PVForecastPlaneSetting(
peakpower=5.0,
albedo=None,
module_model=None,
userhorizon=None,
)

View File

@@ -10,6 +10,7 @@ from akkudoktoreos.core.pydantic import (
PydanticDateTimeData,
PydanticDateTimeDataFrame,
PydanticDateTimeSeries,
PydanticModelNestedValueMixin,
)
from akkudoktoreos.utils.datetimeutil import compare_datetimes, to_datetime
@@ -21,6 +22,129 @@ class PydanticTestModel(PydanticBaseModel):
optional_field: Optional[str] = Field(default=None, description="An optional field.")
class Address(PydanticBaseModel):
city: Optional[str] = None
postal_code: Optional[str] = None
class User(PydanticBaseModel):
name: str
addresses: Optional[list[Address]] = None
settings: Optional[dict[str, str]] = None
class TestPydanticModelNestedValueMixin:
"""Umbrella test class to group all test cases for `PydanticModelNestedValueMixin`."""
@pytest.fixture
def user_instance(self):
"""Fixture to initialize a sample User instance."""
return User(name="Alice", addresses=None, settings=None)
def test_get_key_types_for_simple_field(self):
"""Test _get_key_types for a simple string field."""
key_types = PydanticModelNestedValueMixin._get_key_types(User, "name")
assert key_types == [str], f"Expected [str], got {key_types}"
def test_get_key_types_for_list_of_models(self):
"""Test _get_key_types for a list of Address models."""
key_types = PydanticModelNestedValueMixin._get_key_types(User, "addresses")
assert key_types == [list, Address], f"Expected [list, Address], got {key_types}"
def test_get_key_types_for_dict_field(self):
"""Test _get_key_types for a dictionary field."""
key_types = PydanticModelNestedValueMixin._get_key_types(User, "settings")
assert key_types == [dict, str], f"Expected [dict, str], got {key_types}"
def test_get_key_types_for_optional_field(self):
"""Test _get_key_types correctly handles Optional fields."""
key_types = PydanticModelNestedValueMixin._get_key_types(Address, "city")
assert key_types == [str], f"Expected [str], got {key_types}"
def test_get_key_types_for_non_existent_field(self):
"""Test _get_key_types raises an error for non-existent field."""
with pytest.raises(TypeError):
PydanticModelNestedValueMixin._get_key_types(User, "unknown_field")
def test_set_nested_value_in_model(self, user_instance):
"""Test setting nested value in a model field (Address -> city)."""
assert user_instance.addresses is None
user_instance.set_nested_value("addresses/0/city", "New York")
assert user_instance.addresses is not None
assert user_instance.addresses[0].city == "New York", "The city should be set to 'New York'"
def test_set_nested_value_in_dict(self, user_instance):
"""Test setting nested value in a dictionary field (settings -> theme)."""
assert user_instance.settings is None
user_instance.set_nested_value("settings/theme", "dark")
assert user_instance.settings is not None
assert user_instance.settings["theme"] == "dark", "The theme should be set to 'dark'"
def test_set_nested_value_in_list(self, user_instance):
"""Test setting nested value in a list of models (addresses -> 1 -> city)."""
user_instance.set_nested_value("addresses/1/city", "Los Angeles")
# Check if the city in the second address is set correctly
assert user_instance.addresses[1].city == "Los Angeles", (
"The city at index 1 should be set to 'Los Angeles'"
)
def test_set_nested_value_in_optional_field(self, user_instance):
"""Test setting value in an Optional field (addresses)."""
user_instance.set_nested_value("addresses/0", Address(city="Chicago"))
# Check if the first address is set correctly
assert user_instance.addresses is not None
assert user_instance.addresses[0].city == "Chicago", "The city should be set to 'Chicago'"
def test_set_nested_value_with_empty_list(self):
"""Test setting value in an empty list of models."""
user = User(name="Bob", addresses=[])
user.set_nested_value("addresses/0/city", "Seattle")
assert user.addresses is not None
assert user.addresses[0].city == "Seattle", (
"The first address should have the city 'Seattle'"
)
def test_set_nested_value_with_missing_key_in_dict(self, user_instance):
"""Test setting value in a dict when the key does not exist."""
user_instance.set_nested_value("settings/language", "English")
assert user_instance.settings["language"] == "English", (
"The language setting should be 'English'"
)
def test_set_nested_value_for_non_existent_field(self):
"""Test attempting to set value for a non-existent field."""
user = User(name="John")
with pytest.raises(ValueError):
user.set_nested_value("non_existent_field", "Some Value")
def test_set_nested_value_with_invalid_type(self, user_instance):
"""Test setting value with an invalid type."""
with pytest.raises(ValueError):
user_instance.set_nested_value(
"addresses/0/city", 1234
) # city should be a string, not an integer
def test_set_nested_value_with_model_initialization(self):
"""Test setting a value in a model that should initialize a missing model."""
user = User(name="James", addresses=None)
user.set_nested_value("addresses/0/city", "Boston")
assert user.addresses is not None
assert user.addresses[0].city == "Boston", "The city should be set to 'Boston'"
assert isinstance(user.addresses[0], Address), (
"The first address should be an instance of Address"
)
class TestPydanticBaseModel:
def test_valid_pendulum_datetime(self):
dt = pendulum.now()