Skip to content

Commit 5560fb6

Browse files
authored
Refactor tests in GIOS (home-assistant#155756)
Co-authored-by: mik-laj <[email protected]>
1 parent 9808b6c commit 5560fb6

File tree

9 files changed

+323
-487
lines changed

9 files changed

+323
-487
lines changed

tests/components/gios/__init__.py

Lines changed: 9 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,16 @@
11
"""Tests for GIOS."""
22

3-
from unittest.mock import patch
4-
5-
from homeassistant.components.gios.const import DOMAIN
63
from homeassistant.core import HomeAssistant
74

8-
from tests.common import (
9-
MockConfigEntry,
10-
async_load_json_array_fixture,
11-
async_load_json_object_fixture,
12-
)
13-
14-
STATIONS = [
15-
{
16-
"Identyfikator stacji": 123,
17-
"Nazwa stacji": "Test Name 1",
18-
"WGS84 φ N": "99.99",
19-
"WGS84 λ E": "88.88",
20-
},
21-
{
22-
"Identyfikator stacji": 321,
23-
"Nazwa stacji": "Test Name 2",
24-
"WGS84 φ N": "77.77",
25-
"WGS84 λ E": "66.66",
26-
},
27-
]
28-
29-
30-
async def init_integration(
31-
hass: HomeAssistant, incomplete_data=False, invalid_indexes=False
32-
) -> MockConfigEntry:
33-
"""Set up the GIOS integration in Home Assistant."""
34-
entry = MockConfigEntry(
35-
domain=DOMAIN,
36-
title="Home",
37-
unique_id="123",
38-
data={"station_id": 123, "name": "Home"},
39-
entry_id="86129426118ae32020417a53712d6eef",
40-
)
5+
from tests.common import MockConfigEntry
416

42-
indexes = await async_load_json_object_fixture(hass, "indexes.json", DOMAIN)
43-
station = await async_load_json_array_fixture(hass, "station.json", DOMAIN)
44-
sensors = await async_load_json_object_fixture(hass, "sensors.json", DOMAIN)
45-
if incomplete_data:
46-
indexes["AqIndex"] = "foo"
47-
sensors["pm10"]["Lista danych pomiarowych"][0]["Wartość"] = None
48-
sensors["pm10"]["Lista danych pomiarowych"][1]["Wartość"] = None
49-
if invalid_indexes:
50-
indexes = {}
517

52-
with (
53-
patch(
54-
"homeassistant.components.gios.coordinator.Gios._get_stations",
55-
return_value=STATIONS,
56-
),
57-
patch(
58-
"homeassistant.components.gios.coordinator.Gios._get_station",
59-
return_value=station,
60-
),
61-
patch(
62-
"homeassistant.components.gios.coordinator.Gios._get_all_sensors",
63-
return_value=sensors,
64-
),
65-
patch(
66-
"homeassistant.components.gios.coordinator.Gios._get_indexes",
67-
return_value=indexes,
68-
),
69-
):
70-
entry.add_to_hass(hass)
71-
await hass.config_entries.async_setup(entry.entry_id)
72-
await hass.async_block_till_done()
8+
async def setup_integration(
9+
hass: HomeAssistant,
10+
mock_config_entry: MockConfigEntry,
11+
) -> None:
12+
"""Set up the GIOS integration for testing."""
13+
mock_config_entry.add_to_hass(hass)
7314

74-
return entry
15+
await hass.config_entries.async_setup(mock_config_entry.entry_id)
16+
await hass.async_block_till_done()

tests/components/gios/conftest.py

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
"""Fixtures for GIOS integration tests."""
2+
3+
from collections.abc import AsyncGenerator
4+
from unittest.mock import AsyncMock, MagicMock, patch
5+
6+
from gios.model import GiosSensors, GiosStation, Sensor as GiosSensor
7+
import pytest
8+
9+
from homeassistant.components.gios.const import DOMAIN
10+
from homeassistant.core import HomeAssistant
11+
12+
from . import setup_integration
13+
14+
from tests.common import MockConfigEntry
15+
16+
17+
@pytest.fixture
18+
def mock_config_entry() -> MockConfigEntry:
19+
"""Return the default mocked config entry."""
20+
return MockConfigEntry(
21+
domain=DOMAIN,
22+
title="Home",
23+
unique_id="123",
24+
data={"station_id": 123, "name": "Home"},
25+
entry_id="86129426118ae32020417a53712d6eef",
26+
)
27+
28+
29+
@pytest.fixture
30+
def mock_gios_sensors() -> GiosSensors:
31+
"""Return the default mocked gios sensors."""
32+
return GiosSensors(
33+
aqi=GiosSensor(name="AQI", id=None, index=None, value="good"),
34+
c6h6=GiosSensor(name="benzene", id=658, index="very_good", value=0.23789),
35+
co=GiosSensor(name="carbon monoxide", id=660, index="good", value=251.874),
36+
no=GiosSensor(name="nitrogen monoxide", id=664, index=None, value=5.1),
37+
no2=GiosSensor(name="nitrogen dioxide", id=665, index="good", value=7.13411),
38+
nox=GiosSensor(name="nitrogen oxides", id=666, index=None, value=5.5),
39+
o3=GiosSensor(name="ozone", id=667, index="good", value=95.7768),
40+
pm10=GiosSensor(
41+
name="particulate matter 10", id=14395, index="good", value=16.8344
42+
),
43+
pm25=GiosSensor(name="particulate matter 2.5", id=670, index="good", value=4),
44+
so2=GiosSensor(name="sulfur dioxide", id=672, index="very_good", value=4.35478),
45+
)
46+
47+
48+
@pytest.fixture
49+
def mock_gios_stations() -> dict[int, GiosStation]:
50+
"""Return the default mocked gios stations."""
51+
return {
52+
123: GiosStation(id=123, name="Test Name 1", latitude=99.99, longitude=88.88),
53+
321: GiosStation(id=321, name="Test Name 2", latitude=77.77, longitude=66.66),
54+
}
55+
56+
57+
@pytest.fixture
58+
async def mock_gios(
59+
hass: HomeAssistant,
60+
mock_gios_stations: dict[int, GiosStation],
61+
mock_gios_sensors: GiosSensors,
62+
) -> AsyncGenerator[MagicMock]:
63+
"""Yield a mocked GIOS client."""
64+
with (
65+
patch("homeassistant.components.gios.Gios", autospec=True) as mock_gios,
66+
patch("homeassistant.components.gios.config_flow.Gios", new=mock_gios),
67+
):
68+
mock_gios.create = AsyncMock(return_value=mock_gios)
69+
mock_gios.async_update = AsyncMock(return_value=mock_gios_sensors)
70+
mock_gios.measurement_stations = mock_gios_stations
71+
mock_gios.station_id = 123
72+
mock_gios.station_name = mock_gios_stations[mock_gios.station_id].name
73+
74+
yield mock_gios
75+
76+
77+
@pytest.fixture
78+
async def init_integration(
79+
hass: HomeAssistant,
80+
mock_config_entry: MockConfigEntry,
81+
mock_gios: MagicMock,
82+
) -> None:
83+
"""Set up the GIOS integration for testing."""
84+
await setup_integration(hass, mock_config_entry)

tests/components/gios/fixtures/indexes.json

Lines changed: 0 additions & 38 deletions
This file was deleted.

tests/components/gios/fixtures/sensors.json

Lines changed: 0 additions & 65 deletions
This file was deleted.

tests/components/gios/fixtures/station.json

Lines changed: 0 additions & 74 deletions
This file was deleted.

0 commit comments

Comments
 (0)