Coverage for tests/test_energy.py: 100%
26 statements
« prev ^ index » next coverage.py v7.16.2, created at 2026-11-13 19:28 +0000
« prev ^ index » next coverage.py v7.16.2, created at 2026-11-13 19:28 +0000
1"""Tests for energy endpoints."""
3import pytest
4from httpx import ASGITransport, AsyncClient, Response
6from data_api.api.api import app
7from data_api.domain.energy.services import ELECTRICITY_USAGE_DATA
10class TestEnergyEndpoints:
11 """Tests for energy endpoints."""
13 @pytest.fixture
14 async def client(self):
15 """Create async test client."""
16 async with AsyncClient(
17 transport=ASGITransport(app=app), base_url="http://test", follow_redirects=True
18 ) as client:
19 yield client
21 @pytest.mark.parametrize("path", ["/energy/electricity"])
22 async def test_get_electricity_usage(self, client: AsyncClient, mock_upstream, path):
23 values = {1: 1234, 2: 0, 3: -1}
25 def handler(request):
26 system_id = int(request.url.path.removesuffix(".aspx")[-1])
27 return Response(200, text=f'<img alt="kW: {values[system_id]:,}">')
29 mock_upstream(handler)
30 response = await client.get(path)
31 assert response.status_code == 200
32 for item, system in zip(response.json(), ELECTRICITY_USAGE_DATA, strict=True):
33 assert item == {
34 "name": system["name"],
35 "data": values[system["id"]],
36 "capacity": system["capacity"],
37 "unit": "kW",
38 "last_updated": item["last_updated"],
39 }
40 assert item["last_updated"]
42 @pytest.mark.parametrize("path", ["/energy/electricity"])
43 async def test_upstream_unavailable(self, client: AsyncClient, path):
44 response = await client.get(path)
45 assert response.status_code == 502
46 assert response.json() == {"detail": "Upstream service unavailable"}