mirror of
https://github.com/Akkudoktor-EOS/EOS.git
synced 2026-11-25 15:06:20 +01:00
test(weather): build Open-Meteo test times with datetimeutil
The irradiance tests create their lookup times and the one-hour offset with to_datetime and to_duration, as the project rules require for new code, and share one helper for the mocked HTTP response. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Assisted-by: Claude Code (claude-opus-5-5)
This commit is contained in:
committed by
Bobby Noelte
co-authored by
Claude Opus 5.5
parent
926818fbd4
commit
bf8939a8b7
@@ -12,7 +12,7 @@ from loguru import logger
|
|||||||
from akkudoktoreos.core.cache import CacheFileStore
|
from akkudoktoreos.core.cache import CacheFileStore
|
||||||
from akkudoktoreos.core.coreabc import get_ems
|
from akkudoktoreos.core.coreabc import get_ems
|
||||||
from akkudoktoreos.prediction.weatheropenmeteo import WeatherOpenMeteo
|
from akkudoktoreos.prediction.weatheropenmeteo import WeatherOpenMeteo
|
||||||
from akkudoktoreos.utils.datetimeutil import to_datetime
|
from akkudoktoreos.utils.datetimeutil import to_datetime, to_duration
|
||||||
|
|
||||||
DIR_TESTDATA = Path(__file__).absolute().parent.joinpath("testdata")
|
DIR_TESTDATA = Path(__file__).absolute().parent.joinpath("testdata")
|
||||||
|
|
||||||
@@ -66,7 +66,7 @@ def clear_sky_openmeteo_json(openmeteo_json: dict[str, Any]) -> dict[str, Any]:
|
|||||||
hourly = data["hourly"]
|
hourly = data["hourly"]
|
||||||
stamps = pd.DatetimeIndex(hourly["time"], tz=data["timezone"])
|
stamps = pd.DatetimeIndex(hourly["time"], tz=data["timezone"])
|
||||||
location = pvlib.location.Location(data["latitude"], data["longitude"])
|
location = pvlib.location.Location(data["latitude"], data["longitude"])
|
||||||
minutes = pd.date_range(stamps[0] - pd.Timedelta(hours=1), stamps[-1], freq="1min")
|
minutes = pd.date_range(stamps[0] - to_duration("1 hour"), stamps[-1], freq="1min")
|
||||||
irradiance = location.get_clearsky(minutes, model="ineichen")
|
irradiance = location.get_clearsky(minutes, model="ineichen")
|
||||||
zenith = location.get_solarposition(minutes)["apparent_zenith"]
|
zenith = location.get_solarposition(minutes)["apparent_zenith"]
|
||||||
irradiance["bhi"] = irradiance["dni"] * np.cos(np.radians(zenith)).clip(lower=0.0)
|
irradiance["bhi"] = irradiance["dni"] * np.cos(np.radians(zenith)).clip(lower=0.0)
|
||||||
@@ -260,17 +260,22 @@ class TestOpenMeteoIrradiance:
|
|||||||
"""Irradiance describes the record time, DNI the beam on the plane normal to the sun."""
|
"""Irradiance describes the record time, DNI the beam on the plane normal to the sun."""
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
async def _update(provider: WeatherOpenMeteo, openmeteo_json: dict[str, Any]) -> None:
|
def _response(openmeteo_json: dict[str, Any]) -> Mock:
|
||||||
"""Update the provider from an Open-Meteo response, without network access."""
|
"""Return a mocked HTTP response carrying an Open-Meteo response."""
|
||||||
response = Mock()
|
response = Mock()
|
||||||
response.status_code = 200
|
response.status_code = 200
|
||||||
response.json.return_value = openmeteo_json
|
response.json.return_value = openmeteo_json
|
||||||
response.content = json.dumps(openmeteo_json)
|
response.content = json.dumps(openmeteo_json)
|
||||||
|
return response
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
async def _update(cls, provider: WeatherOpenMeteo, openmeteo_json: dict[str, Any]) -> None:
|
||||||
|
"""Update the provider from an Open-Meteo response, without network access."""
|
||||||
CacheFileStore().clear(clear_all=True)
|
CacheFileStore().clear(clear_all=True)
|
||||||
get_ems().set_start_datetime(
|
get_ems().set_start_datetime(
|
||||||
to_datetime("2026-03-02 09:00:00+01:00", in_timezone="Europe/Berlin")
|
to_datetime("2026-03-02 09:00:00+01:00", in_timezone="Europe/Berlin")
|
||||||
)
|
)
|
||||||
with patch("requests.get", return_value=response):
|
with patch("requests.get", return_value=cls._response(openmeteo_json)):
|
||||||
await provider.update_data(force_enable=True, force_update=True)
|
await provider.update_data(force_enable=True, force_update=True)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
@@ -284,10 +289,7 @@ class TestOpenMeteoIrradiance:
|
|||||||
self, provider: WeatherOpenMeteo, sample_openmeteo_1_json: dict[str, Any]
|
self, provider: WeatherOpenMeteo, sample_openmeteo_1_json: dict[str, Any]
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Request GHI, DNI and DHI at the indicated time, DNI on the normal plane."""
|
"""Request GHI, DNI and DHI at the indicated time, DNI on the normal plane."""
|
||||||
response = Mock()
|
response = self._response(sample_openmeteo_1_json)
|
||||||
response.status_code = 200
|
|
||||||
response.json.return_value = sample_openmeteo_1_json
|
|
||||||
response.content = json.dumps(sample_openmeteo_1_json)
|
|
||||||
with patch("requests.get", return_value=response) as mock_get:
|
with patch("requests.get", return_value=response) as mock_get:
|
||||||
provider._request_forecast(force_update=True) # type: ignore[call-arg]
|
provider._request_forecast(force_update=True) # type: ignore[call-arg]
|
||||||
|
|
||||||
@@ -343,13 +345,13 @@ class TestOpenMeteoIrradiance:
|
|||||||
|
|
||||||
# Late morning and noon: the value at the stamp, not the lower preceding-hour mean.
|
# Late morning and noon: the value at the stamp, not the lower preceding-hour mean.
|
||||||
for stamp in ("2026-03-02 11:00:00+01:00", "2026-03-02 12:00:00+01:00"):
|
for stamp in ("2026-03-02 11:00:00+01:00", "2026-03-02 12:00:00+01:00"):
|
||||||
moment = pd.Timestamp(stamp).tz_convert("UTC")
|
moment = to_datetime(stamp)
|
||||||
assert ghi[moment] == pytest.approx(clear_sky_ghi[moment], rel=0.01)
|
assert ghi[moment] == pytest.approx(clear_sky_ghi[moment], rel=0.01)
|
||||||
|
|
||||||
# After sunset there is no irradiance, although the preceding hour had sun.
|
# After sunset there is no irradiance, although the preceding hour had sun.
|
||||||
stamps = pd.DatetimeIndex(ghi.index)
|
stamps = pd.DatetimeIndex(ghi.index)
|
||||||
elevation = location.get_solarposition(stamps)["apparent_elevation"].to_numpy()
|
elevation = location.get_solarposition(stamps)["apparent_elevation"].to_numpy()
|
||||||
elevation_hour_before = location.get_solarposition(stamps - pd.Timedelta(hours=1))[
|
elevation_hour_before = location.get_solarposition(stamps - to_duration("1 hour"))[
|
||||||
"apparent_elevation"
|
"apparent_elevation"
|
||||||
].to_numpy()
|
].to_numpy()
|
||||||
after_sunset = (elevation < 0.0) & (elevation_hour_before > 0.0)
|
after_sunset = (elevation < 0.0) & (elevation_hour_before > 0.0)
|
||||||
|
|||||||
Reference in New Issue
Block a user