8000 Modernize tests for smhi (#139334) · home-assistant/core@4858b21 · GitHub
[go: up one dir, main page]

Skip to content

Commit 4858b21

Browse files
Modernize tests for smhi (#139334)
* Modernize tests for smhi * Fixes * Mods * Fix weather * Coverage 100% * Fix init test * Fixes * Fixes * Remove waits
1 parent 192aa76 commit 4858b21

File tree

5 files changed

+336
-349
lines changed

5 files changed

+336
-349
lines changed
Lines changed: 125 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,137 @@
11
"""Provide common smhi fixtures."""
22

3+
from __future__ import annotations
4+
5+
from collections.abc import AsyncGenerator, Generator
6+
import json
7+
from typing import Any
8+
from unittest.mock import AsyncMock, MagicMock, patch
9+
10+
from pysmhi.smhi_forecast import SMHIForecast, SMHIPointForecast
311
import pytest
412

13+
from homeassistant.components.smhi import PLATFORMS
514
from homeassistant.components.smhi.const import DOMAIN
15+
from homeassistant.const import CONF_LATITUDE, CONF_LOCATION, CONF_LONGITUDE, Platform
16+
from homeassistant.core import HomeAssistant
17+
18+
from . import TEST_CONFIG
19+
20+
from tests.common import MockConfigEntry, load_fixture
21+
from tests.test_util.aiohttp import AiohttpClientMocker
22+
23+
24+
@pytest.fixture
25+
def mock_setup_entry() -> Generator[AsyncMock]:
26+
"""Override async_setup_entry."""
27+
with patch(
28+
"homeassistant.components.smhi.async_setup_entry", return_value=True
29+
) as mock_setup_entry:
30+
yield mock_setup_entry
31+
32+
33+
@pytest.fixture(name="load_platforms")
34+
async def patch_platform_constant() -> list[Platform]:
35+
"""Return list of platforms to load."""
36+
return PLATFORMS
37+
38+
39+
@pytest.fixture
40+
async def load_int(
41+
hass: HomeAssistant,
42+
mock_client: SMHIPointForecast,
43+
load_platforms: list[Platform],
44+
) -> MockConfigEntry:
45+
"""Set up the SMHI integration."""
46+
hass.config.latitude = "59.32624"
47+
hass.config.longitude = "17.84197"
48+
config_entry = MockConfigEntry(
49+
domain=DOMAIN,
50+
data=TEST_CONFIG,
51+
entry_id="01JMZDH8N5PFHGJNYKKYCSCWER",
52+
unique_id="59.32624-17.84197",
53+
version=3,
54+
title="Test",
55+
)
56+
57+
config_entry.add_to_hass(hass)
58+
59+
with patch("homeassistant.components.smhi.PLATFORMS", load_platforms):
60+
await hass.config_entries.async_setup(config_entry.entry_id)
61+
await hass.async_block_till_done()
62+
63+
return config_entry
64+
65+
66+
@pytest.fixture(name="mock_client")
67+
async def get_client(
68+
hass: HomeAssistant,
69+
get_data: tuple[list[SMHIForecast], list[SMHIForecast], list[SMHIForecast]],
70+
) -> AsyncGenerator[MagicMock]:
71+
"""Mock SMHIPointForecast client."""
72+
73+
with (
74+
patch(
75+
"homeassistant.components.smhi.coordinator.SMHIPointForecast",
76+
autospec=True,
77+
) as mock_client,
78+
patch(
79+
"homeassistant.components.smhi.config_flow.SMHIPointForecast",
80+
return_value=mock_client.return_value,
81+
),
82+
):
83+
client = mock_client.return_value
84+
client.async_get_daily_forecast.return_value = get_data[0]
85+
client.async_get_twice_daily_forecast.return_value = get_data[1]
86+
client.async_get_hourly_forecast.return_value = get_data[2]
87+
yield client
88+
89+
90+
@pytest.fixture(name="get_data")
91+
async def get_data_from_library(
92+
hass: HomeAssistant,
93+
aioclient_mock: AiohttpClientMocker,
94+
load_json: dict[str, Any],
95+
) -> AsyncGenerator[tuple[list[SMHIForecast], list[SMHIForecast], list[SMHIForecast]]]:
96+
"""Get data from api."""
97+
client = SMHIPointForecast(
98+
TEST_CONFIG[CONF_LOCATION][CONF_LONGITUDE],
99+
TEST_CONFIG[CONF_LOCATION][CONF_LATITUDE],
100+
aioclient_mock.create_session(hass.loop),
101+
)
102+
with patch.object(
103+
client._api,
104+
"async_get_data",
105+
return_value=load_json,
106+
):
107+
data_daily = await client.async_get_daily_forecast()
108+
data_twice_daily = await client.async_get_twice_daily_forecast()
109+
data_hourly = await client.async_get_hourly_forecast()
6110

7-
from tests.common import load_fixture
111+
yield (data_daily, data_twice_daily, data_hourly)
112+
await client._api._session.close()
8113

9114

10-
@pytest.fixture(scope="package")
11-
def api_response():
12-
"""Return an API response."""
13-
return load_fixture("smhi.json", DOMAIN)
115+
@pytest.fixture(name="load_json")
116+
def load_json_from_fixture(
117+
load_data: tuple[str, str, str],
118+
to_load: int,
119+
) -> dict[str, Any]:
120+
"""Load fixture with json data and return."""
121+
return json.loads(load_data[to_load])
14122

15123

16-
@pytest.fixture(scope="package")
17-
def api_response_night():
18-
"""Return an API response for night only."""
19-
return load_fixture("smhi_night.json", DOMAIN)
124+
@pytest.fixture(name="load_data", scope="package")
125+
def load_data_from_fixture() -> tuple[str, str, str]:
126+
"""Load fixture with fixture data and return."""
127+
return (
128+
load_fixture("smhi.json", "smhi"),
129+
load_fixture("smhi_night.json", "smhi"),
130+
load_fixture("smhi_short.json", "smhi"),
131+
)
20132

21133

22-
@pytest.fixture(scope="package")
23-
def api_response_lack_data():
24-
"""Return an API response."""
25-
return load_fixture("smhi_short.json", DOMAIN)
134+
@pytest.fixture
135+
def to_load() -> int:
136+
"""Fixture to load."""
137+
return 0

tests/components/smhi/snapshots/test_weather.ambr

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# serializer version: 1
2-
# name: test_clear_night[clear-night_forecast]
2+
# name: test_clear_night[1][clear-night_forecast]
33
dict({
44
'weather.smhi_test': dict({
55
'forecast': list([
@@ -59,11 +59,11 @@
5959
}),
6060
})
6161
# ---
62-
# name: test_clear_night[clear_night]
62+
# name: test_clear_night[1][clear_night]
6363
ReadOnlyDict({
6464
'attribution': 'Swedish weather institute (SMHI)',
6565
'cloud_coverage': 100,
66-
'friendly_name': 'test',
66+
'friendly_name': 'Test',
6767
'humidity': 100,
6868
'precipitation_unit': <UnitOfPrecipitationDepth.MILLIMETERS: 'mm'>,
6969
'pressure': 992.4,
@@ -80,7 +80,7 @@
8080
'wind_speed_unit': <UnitOfSpeed.KILOMETERS_PER_HOUR: 'km/h'>,
8181
})
8282
# ---
83-
# name: test_forecast_service[get_forecasts]
83+
# name: test_forecast_service[load_platforms0]
8484
dict({
8585
'weather.smhi_test': dict({
8686
'forecast': list([
@@ -218,7 +218,7 @@
218218
}),
219219
})
220220
# ---
221-
# name: test_forecast_services
221+
# name: test_forecast_services[load_platforms0]
222222
dict({
223223
'cloud_coverage': 100,
224224
'condition': 'cloudy',
@@ -233,7 +233,7 @@
233233
'wind_speed': 10.08,
234234
})
235235
# ---
236-
# name: test_forecast_services.1
236+
# name: test_forecast_services[load_platforms0].1
237237
dict({
238238
'cloud_coverage': 75,
239239
'condition': 'partlycloudy',
@@ -248,7 +248,7 @@
248248
'wind_speed': 14.76,
249249
})
250250
# ---
251-
# name: test_forecast_services.2
251+
# name: test_forecast_services[load_platforms0].2
252252
dict({
253253
'cloud_coverage': 100,
254254
'condition': 'fog',
@@ -263,7 +263,7 @@
263263
'wind_speed': 9.72,
264264
})
265265
# ---
266-
# name: test_forecast_services.3
266+
# name: test_forecast_services[load_platforms0].3
267267
dict({
268268
'cloud_coverage': 100,
269269
'condition': 'cloudy',
@@ -278,11 +278,11 @@
278278
'wind_speed': 12.24,
279279
})
280280
# ---
281-
# name: test_setup_hass
281+
# name: test_setup_hass[load_platforms0]
282282
ReadOnlyDict({
283283
'attribution': 'Swedish weather institute (SMHI)',
284284
'cloud_coverage': 100,
285-
'friendly_name': 'test',
285+
'friendly_name': 'Test',
286286
'humidity': 100,
287287
'precipitation_unit': <UnitOfPrecipitationDepth.MILLIMETERS: 'mm'>,
288288
'pressure': 992.4,

0 commit comments

Comments
 (0)
0