mirror of
https://github.com/Akkudoktor-EOS/EOS.git
synced 2026-07-19 08:18:50 +00:00
FAstAPI is an async framework. Data may be imported and exported, load and save, set and get asynchronously. Prevent interleaving data operations to corrupt the data. In the previous design sync and async data access was intermixed leading to data corruption. The basic data classes DataSequence and DataContainer and the derived classes like Provider and Measurement now are async. Data access is protected by several async locks. To support the async design of the data classes the database interface became async. The energy management is also adapted to the new async design. Optimization is still off-loaded to another thread, but the prepration for the optimization and the post optimization actions now follow the async design. Adapter operations are now also protected by async locks. Tests were adapted to the async design and new tests were created. Besides this major fix several other improvements and fixes are included in this PR. * fix: key_to_dict/list/array only regard data records with key value set. Before the exclusion of no value data records was only done if the dropna flag was set. * fix: test for visual result pdf generation Due to updates in the library the generated charts text was a little bit different. Adapt the test to create the comaprison pdf in the test data durectory and update the reference pdf. * chore: Remove MutableMapping from DataSequence and DataContainer. Mutable Mapping does not fit to the now async design. * chore: Add NoDB database backend This backend implements the full database backend interface but performs no actual persistence. It is intended for configurations where database persistence is disabled (`provider=None`). * chore: Improve measurement data import testing with real world scenarios. Added two new endpoints to support testing. * chore: Add mermaid to supported documentation tools * chore: Add documentation about async design * chore: Add documentation about generic data handling Covers the basics of measurement and prediction time series data handling. * chore: Add empty lines around markdown lists. * chore: sync pre-commit config to updated package versions Signed-off-by: Bobby Noelte <b0661n0e17e@gmail.com>
121 lines
4.0 KiB
Python
121 lines
4.0 KiB
Python
import json
|
|
from unittest.mock import call, patch
|
|
|
|
import pendulum
|
|
import pytest
|
|
import requests
|
|
|
|
from akkudoktoreos.prediction.pvforecastvrm import (
|
|
PVForecastVrm,
|
|
VrmForecastRecords,
|
|
VrmForecastResponse,
|
|
)
|
|
|
|
|
|
@pytest.fixture
|
|
def pvforecast_instance(config_eos):
|
|
# Settings for PVForecastVrm
|
|
settings = {
|
|
"pvforecast": {
|
|
"provider": "PVForecastVrm",
|
|
"provider_settings": {
|
|
"PVForecastVrm": {
|
|
"pvforecast_vrm_token": "dummy-token",
|
|
"pvforecast_vrm_idsite": 12345,
|
|
},
|
|
}
|
|
}
|
|
}
|
|
config_eos.merge_settings_from_dict(settings)
|
|
# start_datetime initialize
|
|
start_dt = pendulum.datetime(2025, 1, 1, tz='Europe/Berlin')
|
|
|
|
# create PVForecastVrm-instance with config and start_datetime
|
|
pv = PVForecastVrm(config=config_eos.load, start_datetime=start_dt)
|
|
|
|
return pv
|
|
|
|
|
|
def mock_forecast_response():
|
|
"""Return a fake VrmForecastResponse with sample data."""
|
|
return VrmForecastResponse(
|
|
success=True,
|
|
records=VrmForecastRecords(
|
|
vrm_consumption_fc=[],
|
|
solar_yield_forecast=[
|
|
(pendulum.datetime(2025, 1, 1, 0, 0, tz='Europe/Berlin').int_timestamp * 1000, 120.0),
|
|
(pendulum.datetime(2025, 1, 1, 1, 0, tz='Europe/Berlin').int_timestamp * 1000, 130.0)
|
|
]
|
|
),
|
|
totals={}
|
|
)
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_update_data_updates_dc_and_ac_power(pvforecast_instance):
|
|
with patch.object(pvforecast_instance, "_request_forecast", return_value=mock_forecast_response()), \
|
|
patch.object(PVForecastVrm, "update_value") as mock_update:
|
|
|
|
await pvforecast_instance._update_data()
|
|
|
|
# Check that update_value was called correctly
|
|
assert mock_update.call_count == 2
|
|
|
|
expected_calls = [
|
|
call(
|
|
pendulum.datetime(2025, 1, 1, 0, 0, tz='Europe/Berlin'),
|
|
{"pvforecast_dc_power": 120.0, "pvforecast_ac_power": 115.2}
|
|
),
|
|
call(
|
|
pendulum.datetime(2025, 1, 1, 1, 0, tz='Europe/Berlin'),
|
|
{"pvforecast_dc_power": 130.0, "pvforecast_ac_power": 124.8}
|
|
),
|
|
]
|
|
|
|
mock_update.assert_has_calls(expected_calls, any_order=False)
|
|
|
|
|
|
def test_validate_data_accepts_valid_json():
|
|
"""Test that _validate_data doesn't raise with valid input."""
|
|
response = mock_forecast_response()
|
|
json_data = response.model_dump_json()
|
|
|
|
validated = PVForecastVrm._validate_data(json_data)
|
|
assert validated.success
|
|
assert len(validated.records.solar_yield_forecast) == 2
|
|
|
|
|
|
def test_validate_data_invalid_json_raises():
|
|
"""Test that _validate_data raises with invalid input."""
|
|
invalid_json = json.dumps({"success": True}) # missing 'records'
|
|
with pytest.raises(ValueError) as exc_info:
|
|
PVForecastVrm._validate_data(invalid_json)
|
|
assert "Field:" in str(exc_info.value)
|
|
assert "records" in str(exc_info.value)
|
|
|
|
|
|
def test_request_forecast_raises_on_http_error(pvforecast_instance):
|
|
"""Ensure _request_forecast raises RuntimeError on HTTP failure."""
|
|
with patch("requests.get", side_effect=requests.Timeout("Request timed out")) as mock_get:
|
|
with pytest.raises(RuntimeError) as exc_info:
|
|
pvforecast_instance._request_forecast(0, 1)
|
|
|
|
assert "Failed to fetch pvforecast" in str(exc_info.value)
|
|
mock_get.assert_called_once()
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_update_data_skips_on_empty_forecast(pvforecast_instance):
|
|
"""Ensure no update_value calls are made if no forecast data is present."""
|
|
empty_response = VrmForecastResponse(
|
|
success=True,
|
|
records=VrmForecastRecords(vrm_consumption_fc=[], solar_yield_forecast=[]),
|
|
totals={}
|
|
)
|
|
|
|
with patch.object(pvforecast_instance, "_request_forecast", return_value=empty_response), \
|
|
patch.object(PVForecastVrm, "update_value") as mock_update:
|
|
|
|
await pvforecast_instance._update_data()
|
|
mock_update.assert_not_called()
|