1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418
|
"""Test GraphQL API models for EnergyZero."""
from datetime import UTC, date, datetime, time, timedelta, timezone
from json import loads
import pytest
from aresponses import ResponsesMockServer
from syrupy.assertion import SnapshotAssertion
from energyzero import (
EnergyPrices,
EnergyZero,
EnergyZeroNoDataError,
PriceType,
TimeRange,
)
from . import load_fixtures
############################################################################
## GraphQL API model tests ##
############################################################################
@pytest.mark.freeze_time("2025-05-31 15:00:00+01:00")
async def test_graphql_electricity_model(
aresponses: ResponsesMockServer,
snapshot: SnapshotAssertion,
graphql_energyzero_client: EnergyZero,
) -> None:
"""Test the electricity model at 15:00:00 CET."""
aresponses.add(
"api.energyzero.nl",
"/v1/gql",
"POST",
aresponses.Response(
status=200,
headers={"Content-Type": "application/json"},
text=load_fixtures("graphql/energy.json"),
),
)
today = date(2025, 5, 31)
energy: EnergyPrices = await graphql_energyzero_client.get_electricity_prices(
start_date=today,
end_date=today,
price_type=PriceType.ALL_IN,
)
assert energy == snapshot
assert isinstance(energy, EnergyPrices)
assert isinstance(energy.timestamp_prices, list)
# Electricity prices
assert energy.extreme_prices[1] == 0.3895111
assert energy.extreme_prices[0] == 0.1408077
assert energy.average_price == 0.24064782499999993
assert energy.current_price == 0.1566829
assert energy.pct_of_max_price == 40.23
assert energy.time_ranges_priced_equal_or_lower == 6
# The next hour price
next_hour = datetime(2025, 5, 31, 15, 0, tzinfo=UTC)
assert energy.price_at_time(next_hour) == 0.20090840000000001
assert energy.lowest_price_time_range == TimeRange(
datetime.combine(today, time(11, 0, 0), UTC),
datetime.combine(today, time(12, 0, 0), UTC),
)
assert energy.highest_price_time_range == TimeRange(
datetime.combine(today, time(19, 0, 0), UTC),
datetime.combine(today, time(20, 0, 0), UTC),
)
@pytest.mark.freeze_time("2025-01-01 00:30:00+01:00")
async def test_graphql_electricity_none_date(
aresponses: ResponsesMockServer,
snapshot: SnapshotAssertion,
graphql_energyzero_client: EnergyZero,
) -> None:
"""Test when there is no data for the current datetime."""
aresponses.add(
"api.energyzero.nl",
"/v1/gql",
"POST",
aresponses.Response(
status=200,
headers={"Content-Type": "application/json"},
text=load_fixtures("graphql/energy.json"),
),
)
today = date(2025, 5, 31)
energy: EnergyPrices = await graphql_energyzero_client.get_electricity_prices(
start_date=today,
end_date=today,
price_type=PriceType.MARKET,
)
assert energy == snapshot
assert isinstance(energy, EnergyPrices)
assert energy.current_price is None
@pytest.mark.freeze_time("2025-05-31 00:30:00+01:00")
async def test_graphql_electricity_midnight_cest(
aresponses: ResponsesMockServer,
snapshot: SnapshotAssertion,
graphql_energyzero_client: EnergyZero,
) -> None:
"""Test the electricity model between 00:00 and 01:00 with in CEST."""
aresponses.add(
"api.energyzero.nl",
"/v1/gql",
"POST",
aresponses.Response(
status=200,
headers={"Content-Type": "application/json"},
text=load_fixtures("graphql/energy.json"),
),
)
today = date(2025, 5, 31)
energy: EnergyPrices = await graphql_energyzero_client.get_electricity_prices(
start_date=today,
end_date=today,
price_type=PriceType.ALL_IN,
)
assert energy == snapshot
assert isinstance(energy, EnergyPrices)
# Price at 22:30:00 UTC
assert energy.current_price == 0.2749604
async def test_graphql_no_electricity_data(
aresponses: ResponsesMockServer, graphql_energyzero_client: EnergyZero
) -> None:
"""Raise exception when there is no data."""
aresponses.add(
"api.energyzero.nl",
"/v1/gql",
"POST",
aresponses.Response(
status=200,
headers={"Content-Type": "application/json"},
text=load_fixtures("graphql/no_data.json"),
),
)
today = date(2025, 5, 31)
with pytest.raises(EnergyZeroNoDataError):
await graphql_energyzero_client.get_electricity_prices(
start_date=today,
end_date=today,
)
@pytest.mark.freeze_time("2025-05-31 15:00:00+01:00")
async def test_graphql_gas_model(
aresponses: ResponsesMockServer,
snapshot: SnapshotAssertion,
graphql_energyzero_client: EnergyZero,
) -> None:
"""Test the gas model at 15:00:00 CET."""
aresponses.add(
"api.energyzero.nl",
"/v1/gql",
"POST",
aresponses.Response(
status=200,
headers={"Content-Type": "application/json"},
text=load_fixtures("graphql/gas.json"),
),
)
today = date(2025, 5, 31)
gas: EnergyPrices = await graphql_energyzero_client.get_gas_prices(
start_date=today, end_date=today, price_type=PriceType.ALL_IN
)
assert gas == snapshot
assert isinstance(gas, EnergyPrices)
assert isinstance(gas.timestamp_prices, list)
assert gas.extreme_prices[1] == 1.21367051296348
assert gas.extreme_prices[0] == 1.19915429151276
# The next hour price
next_hour = datetime(2025, 5, 31, 15, 0, tzinfo=UTC)
assert gas.price_at_time(next_hour) == 1.19915429151276
@pytest.mark.freeze_time("2025-05-31 04:00:00+01:00")
async def test_graphql_gas_morning_model(
aresponses: ResponsesMockServer,
snapshot: SnapshotAssertion,
graphql_energyzero_client: EnergyZero,
) -> None:
"""Test the gas model in the morning at 04:00:00 CET."""
aresponses.add(
"api.energyzero.nl",
"/v1/gql",
"POST",
aresponses.Response(
status=200,
headers={"Content-Type": "application/json"},
text=load_fixtures("graphql/gas.json"),
),
)
today = date(2025, 5, 31)
gas: EnergyPrices = await graphql_energyzero_client.get_gas_prices(
start_date=today,
end_date=today,
price_type=PriceType.ALL_IN,
)
assert gas == snapshot
assert isinstance(gas, EnergyPrices)
assert isinstance(gas.timestamp_prices, list)
@pytest.mark.freeze_time("2025-01-01 00:30:00+01:00")
async def test_graphql_gas_none_date(
aresponses: ResponsesMockServer,
snapshot: SnapshotAssertion,
graphql_energyzero_client: EnergyZero,
) -> None:
"""Test when there is no data for the current datetime."""
aresponses.add(
"api.energyzero.nl",
"/v1/gql",
"POST",
aresponses.Response(
status=200,
headers={"Content-Type": "application/json"},
text=load_fixtures("graphql/gas.json"),
),
)
today = date(2025, 5, 31)
gas: EnergyPrices = await graphql_energyzero_client.get_gas_prices(
start_date=today,
end_date=today,
price_type=PriceType.ALL_IN,
)
assert gas == snapshot
assert isinstance(gas, EnergyPrices)
assert gas.current_price is None
async def test_graphql_no_gas_data(
aresponses: ResponsesMockServer, graphql_energyzero_client: EnergyZero
) -> None:
"""Test if a response without any data throws the correct exception."""
aresponses.add(
"api.energyzero.nl",
"/v1/gql",
"POST",
aresponses.Response(
status=200,
headers={"Content-Type": "application/json"},
text=load_fixtures("graphql/no_data.json"),
),
)
today = date(2025, 5, 31)
with pytest.raises(EnergyZeroNoDataError):
await graphql_energyzero_client.get_gas_prices(
start_date=today,
end_date=today,
)
async def test_graphql_requires_end_date_for_electricity(
graphql_energyzero_client: EnergyZero,
) -> None:
"""GraphQL backend should require an end date for electricity."""
today = date(2025, 5, 31)
with pytest.raises(ValueError, match="end_date is required"):
await graphql_energyzero_client.get_electricity_prices(start_date=today)
async def test_graphql_requires_end_date_for_gas(
graphql_energyzero_client: EnergyZero,
) -> None:
"""GraphQL backend should require an end date for gas."""
today = date(2025, 5, 31)
with pytest.raises(ValueError, match="end_date is required"):
await graphql_energyzero_client.get_gas_prices(start_date=today)
def test_graphql_price_type_variants() -> None:
"""Ensure new PriceType variants produce the expected values."""
data = {
"energyMarketPrices": {
"prices": [
{
"from": "2025-01-01T00:00:00Z",
"till": "2025-01-01T01:00:00Z",
"energyPriceExcl": 0.10,
"energyPriceIncl": 0.12,
"additionalCosts": [
{"priceExcl": 0.01, "priceIncl": 0.02},
],
"vat": 0.06,
}
]
}
}
market = EnergyPrices.from_dict(data, PriceType.MARKET)
assert market.average_price == pytest.approx(0.10)
market_vat = EnergyPrices.from_dict(data, PriceType.MARKET_WITH_VAT)
assert market_vat.average_price == pytest.approx(0.12)
all_in_excl_vat = EnergyPrices.from_dict(data, PriceType.ALL_IN_EXCL_VAT)
assert all_in_excl_vat.average_price == pytest.approx(0.11)
all_in = EnergyPrices.from_dict(data, PriceType.ALL_IN)
assert all_in.average_price == pytest.approx(0.14)
async def test_timerange_astimezone() -> None:
"""Test if astimezone returns the correct new time range."""
tz_from = timezone.min
tz_to = timezone.max
range_start = datetime.now(tz=tz_from)
range_end = range_start + timedelta(hours=6)
range_from_tz = TimeRange(range_start, range_end)
range_to_tz = range_from_tz.astimezone(tz_to)
assert range_from_tz.start_including.tzinfo == tz_from
assert range_from_tz.end_excluding.tzinfo == tz_from
assert range_to_tz.start_including.tzinfo == tz_to
assert range_to_tz.end_excluding.tzinfo == tz_to
async def test_timerange_str() -> None:
"""Test if the string representation for a TimeRange is correct."""
range_from_tz = TimeRange(
datetime(year=2025, month=1, day=2, hour=10, minute=9, second=8, tzinfo=UTC),
datetime(year=2025, month=3, day=4, hour=7, minute=6, second=5, tzinfo=UTC),
)
assert f"{range_from_tz}" == "2025-01-02 10:09:08 - 2025-03-04 07:06:05"
@pytest.mark.freeze_time("2025-01-01 00:30:00+01:00")
async def test_graphql_electricity_no_prices(
aresponses: ResponsesMockServer,
snapshot: SnapshotAssertion,
graphql_energyzero_client: EnergyZero,
) -> None:
"""Test when there is no data for the current datetime."""
aresponses.add(
"api.energyzero.nl",
"/v1/gql",
"POST",
aresponses.Response(
status=200,
headers={"Content-Type": "application/json"},
text=load_fixtures("graphql/energy_no_prices.json"),
),
)
today = date(2025, 5, 31)
energy: EnergyPrices = await graphql_energyzero_client.get_electricity_prices(
start_date=today,
end_date=today,
price_type=PriceType.ALL_IN,
)
assert energy == snapshot
# All properties should raise EnergyZeroNoDataError when no prices available
for prop in [
"extreme_prices",
"highest_price_time_range",
"lowest_price_time_range",
"pct_of_max_price",
"time_ranges_priced_equal_or_lower",
]:
with pytest.raises(EnergyZeroNoDataError, match="No prices available"):
getattr(energy, prop)
async def test_empty_energyprices() -> None:
"""Verify that empty EnergyPrices raises EnergyZeroNoDataError where applicable."""
prices = EnergyPrices(dict[TimeRange, float](), 0)
assert prices.current_price is None
assert len(prices.timestamp_prices) == 0
assert isinstance(prices.utcnow(), datetime)
assert prices.price_at_time(datetime.now(UTC)) is None
# All properties should raise EnergyZeroNoDataError when no prices available
for prop in [
"extreme_prices",
"highest_price_time_range",
"lowest_price_time_range",
"pct_of_max_price",
"time_ranges_priced_equal_or_lower",
]:
with pytest.raises(EnergyZeroNoDataError, match="No prices available"):
getattr(prices, prop)
@pytest.mark.freeze_time("2025-05-31 15:00:00+01:00")
async def test_energyprices_fromdict() -> None:
"""Verify that an empty EnergyPrices returns None where applicable."""
data = loads(load_fixtures("graphql/energy.json"))["data"]
prices = EnergyPrices.from_dict(data, PriceType.ALL_IN)
today = date(2025, 5, 31)
assert prices.current_price == 0.1566829
assert prices.extreme_prices == (0.1408077, 0.3895111)
assert prices.lowest_price_time_range == TimeRange(
datetime.combine(today, time(11, 0, 0), UTC),
datetime.combine(today, time(12, 0, 0), UTC),
)
assert prices.highest_price_time_range == TimeRange(
datetime.combine(today, time(19, 0, 0), UTC),
datetime.combine(today, time(20, 0, 0), UTC),
)
assert prices.pct_of_max_price == 40.23
assert len(prices.timestamp_prices) == 24
assert prices.time_ranges_priced_equal_or_lower == 6
|