mirror of
https://github.com/Akkudoktor-EOS/EOS.git
synced 2026-10-17 11:31:56 +00:00
fix: surface DVhub feed-in tariff update failures (#1357)
* fix: surface DVhub feed-in tariff update failures * test: annotate DVhub sample records for mypy * fix: require real feed-in tariff coverage in GENETIC0 * test: satisfy GENETIC0 tariff coverage checks * test: patch DVhub storage method on its class
This commit is contained in:
@@ -1,16 +1,20 @@
|
||||
"""Tests for the dvhub.online feed-in tariff provider."""
|
||||
|
||||
import os
|
||||
from unittest.mock import MagicMock, patch
|
||||
from typing import Any
|
||||
from unittest.mock import AsyncMock, MagicMock, patch
|
||||
|
||||
import pytest
|
||||
import requests
|
||||
|
||||
from akkudoktoreos.core.coreabc import get_ems
|
||||
from akkudoktoreos.prediction.feedintariffdvhubonline import (
|
||||
FeedInTariffDvhubOnline,
|
||||
FeedInTariffDvhubOnlineCommonSettings,
|
||||
)
|
||||
from akkudoktoreos.utils.datetimeutil import to_datetime
|
||||
|
||||
SAMPLE = {
|
||||
SAMPLE: dict[str, list[dict[str, Any]]] = {
|
||||
"data": [
|
||||
{"ts": "2026-07-19T12:00:00.000Z", "price": 42.5}, # EUR/MWh
|
||||
{"ts": "2026-07-19T12:15:00.000Z", "price": -5.69}, # negative slot
|
||||
@@ -23,9 +27,11 @@ SAMPLE = {
|
||||
@pytest.fixture
|
||||
def provider(config_eos):
|
||||
config_eos.merge_settings_from_dict(
|
||||
{"feedintariff": {"provider": "FeedInTariffDvhubOnline"}}
|
||||
{"feedintariff": {"provider": "FeedInTariffDvhubOnline"}, "prediction": {"hours": 2}}
|
||||
)
|
||||
get_ems().set_start_datetime(to_datetime("2026-07-19T12:00:00+00:00"))
|
||||
provider = FeedInTariffDvhubOnline()
|
||||
provider.highest_orig_datetime = None
|
||||
assert provider.enabled()
|
||||
provider._db_reset_state()
|
||||
return provider
|
||||
@@ -61,6 +67,58 @@ class TestFeedInTariffDvhubOnline:
|
||||
start_date="2026-07-19", end_date="2026-07-21", force_update=True
|
||||
)
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_update_stores_only_published_slots(self, provider, monkeypatch):
|
||||
monkeypatch.setattr(provider, "_request_forecast", lambda **_: SAMPLE)
|
||||
|
||||
await provider._update_data(force_update=True)
|
||||
|
||||
stored = await provider.key_to_raw_series(key="feed_in_tariff_wh")
|
||||
assert len(stored) == 2
|
||||
assert stored.tolist() == pytest.approx([42.5 / 1_000_000, -5.69 / 1_000_000])
|
||||
assert provider.highest_orig_datetime == to_datetime(SAMPLE["data"][1]["ts"])
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@pytest.mark.parametrize(
|
||||
"error",
|
||||
[requests.HTTPError("400 Bad Request"), ValueError("invalid JSON"), RuntimeError("bug")],
|
||||
)
|
||||
async def test_update_propagates_failure_despite_existing_history(
|
||||
self, provider, monkeypatch, error
|
||||
):
|
||||
previous = to_datetime(SAMPLE["data"][0]["ts"])
|
||||
provider.highest_orig_datetime = previous
|
||||
|
||||
def fail(**kwargs):
|
||||
raise error
|
||||
|
||||
monkeypatch.setattr(provider, "_request_forecast", fail)
|
||||
with pytest.raises(type(error), match=str(error)):
|
||||
await provider._update_data(force_update=True)
|
||||
assert provider.highest_orig_datetime == previous
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_update_rejects_empty_response_with_existing_history(self, provider, monkeypatch):
|
||||
provider.highest_orig_datetime = to_datetime(SAMPLE["data"][0]["ts"])
|
||||
monkeypatch.setattr(provider, "_request_forecast", lambda **_: {"data": []})
|
||||
|
||||
with pytest.raises(ValueError, match="No dvhub.online feed-in tariff data"):
|
||||
await provider._update_data(force_update=True)
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_update_does_not_mark_failed_storage_as_fresh(self, provider, monkeypatch):
|
||||
previous = to_datetime(SAMPLE["data"][0]["ts"])
|
||||
provider.highest_orig_datetime = previous
|
||||
monkeypatch.setattr(provider, "_request_forecast", lambda **_: SAMPLE)
|
||||
with patch.object(
|
||||
FeedInTariffDvhubOnline,
|
||||
"key_from_series",
|
||||
new=AsyncMock(side_effect=RuntimeError("storage failed")),
|
||||
):
|
||||
with pytest.raises(RuntimeError, match="storage failed"):
|
||||
await provider._update_data(force_update=True)
|
||||
assert provider.highest_orig_datetime == previous
|
||||
|
||||
|
||||
@pytest.mark.skipif(
|
||||
os.environ.get("EOS_DVHUB_ONLINE_LIVE") != "1",
|
||||
|
||||
Reference in New Issue
Block a user