Use snapshots in weather tests (#102297)

pull/102331/head
Kevin Stillhammer 2023-10-19 16:40:08 +02:00 committed by GitHub
parent 2d833fd6ea
commit dae742fba0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 175 additions and 108 deletions

View File

@ -0,0 +1,101 @@
# serializer version: 1
# name: test_forecasts[config0-1-weather]
dict({
'forecast': list([
dict({
'condition': 'cloudy',
'datetime': '2023-02-17T14:00:00+00:00',
'temperature': 14.2,
}),
]),
})
# ---
# name: test_forecasts[config0-1-weather].1
dict({
'forecast': list([
dict({
'condition': 'cloudy',
'datetime': '2023-02-17T14:00:00+00:00',
'temperature': 14.2,
}),
]),
})
# ---
# name: test_forecasts[config0-1-weather].2
dict({
'forecast': list([
dict({
'condition': 'fog',
'datetime': '2023-02-17T14:00:00+00:00',
'is_daytime': True,
'temperature': 14.2,
}),
]),
})
# ---
# name: test_forecasts[config0-1-weather].3
dict({
'forecast': list([
dict({
'condition': 'cloudy',
'datetime': '2023-02-17T14:00:00+00:00',
'temperature': 16.9,
}),
]),
})
# ---
# name: test_restore_weather_save_state
dict({
'last_apparent_temperature': None,
'last_cloud_coverage': None,
'last_dew_point': None,
'last_humidity': '25.0',
'last_ozone': None,
'last_pressure': None,
'last_temperature': '15.0',
'last_visibility': None,
'last_wind_bearing': None,
'last_wind_gust_speed': None,
'last_wind_speed': None,
})
# ---
# name: test_trigger_weather_services[config0-1-template]
dict({
'forecast': list([
dict({
'condition': 'sunny',
'datetime': '2023-10-19T06:50:05-07:00',
'precipitation': 20.0,
'temperature': 20.0,
'templow': 15.0,
}),
]),
})
# ---
# name: test_trigger_weather_services[config0-1-template].1
dict({
'forecast': list([
dict({
'condition': 'sunny',
'datetime': '2023-10-19T06:50:05-07:00',
'precipitation': 20.0,
'temperature': 20.0,
'templow': 15.0,
}),
]),
})
# ---
# name: test_trigger_weather_services[config0-1-template].2
dict({
'forecast': list([
dict({
'condition': 'sunny',
'datetime': '2023-10-19T06:50:05-07:00',
'is_daytime': True,
'precipitation': 20.0,
'temperature': 20.0,
'templow': 15.0,
}),
]),
})
# ---

View File

@ -2,6 +2,7 @@
from typing import Any from typing import Any
import pytest import pytest
from syrupy.assertion import SnapshotAssertion
from homeassistant.components.weather import ( from homeassistant.components.weather import (
ATTR_FORECAST, ATTR_FORECAST,
@ -112,7 +113,9 @@ async def test_template_state_text(hass: HomeAssistant, start_ha) -> None:
}, },
], ],
) )
async def test_forecasts(hass: HomeAssistant, start_ha) -> None: async def test_forecasts(
hass: HomeAssistant, start_ha, snapshot: SnapshotAssertion
) -> None:
"""Test forecast service.""" """Test forecast service."""
for attr, _v_attr, value in [ for attr, _v_attr, value in [
("sensor.temperature", ATTR_WEATHER_TEMPERATURE, 22.3), ("sensor.temperature", ATTR_WEATHER_TEMPERATURE, 22.3),
@ -163,15 +166,7 @@ async def test_forecasts(hass: HomeAssistant, start_ha) -> None:
blocking=True, blocking=True,
return_response=True, return_response=True,
) )
assert response == { assert response == snapshot
"forecast": [
{
"condition": "cloudy",
"datetime": "2023-02-17T14:00:00+00:00",
"temperature": 14.2,
}
]
}
response = await hass.services.async_call( response = await hass.services.async_call(
WEATHER_DOMAIN, WEATHER_DOMAIN,
SERVICE_GET_FORECAST, SERVICE_GET_FORECAST,
@ -179,15 +174,7 @@ async def test_forecasts(hass: HomeAssistant, start_ha) -> None:
blocking=True, blocking=True,
return_response=True, return_response=True,
) )
assert response == { assert response == snapshot
"forecast": [
{
"condition": "cloudy",
"datetime": "2023-02-17T14:00:00+00:00",
"temperature": 14.2,
}
]
}
response = await hass.services.async_call( response = await hass.services.async_call(
WEATHER_DOMAIN, WEATHER_DOMAIN,
SERVICE_GET_FORECAST, SERVICE_GET_FORECAST,
@ -195,16 +182,7 @@ async def test_forecasts(hass: HomeAssistant, start_ha) -> None:
blocking=True, blocking=True,
return_response=True, return_response=True,
) )
assert response == { assert response == snapshot
"forecast": [
{
"condition": "fog",
"datetime": "2023-02-17T14:00:00+00:00",
"temperature": 14.2,
"is_daytime": True,
}
]
}
hass.states.async_set( hass.states.async_set(
"weather.forecast", "weather.forecast",
@ -231,15 +209,7 @@ async def test_forecasts(hass: HomeAssistant, start_ha) -> None:
blocking=True, blocking=True,
return_response=True, return_response=True,
) )
assert response == { assert response == snapshot
"forecast": [
{
"condition": "cloudy",
"datetime": "2023-02-17T14:00:00+00:00",
"temperature": 16.9,
}
]
}
@pytest.mark.parametrize(("count", "domain"), [(1, WEATHER_DOMAIN)]) @pytest.mark.parametrize(("count", "domain"), [(1, WEATHER_DOMAIN)])
@ -263,7 +233,9 @@ async def test_forecasts(hass: HomeAssistant, start_ha) -> None:
], ],
) )
async def test_forecast_invalid( async def test_forecast_invalid(
hass: HomeAssistant, start_ha, caplog: pytest.LogCaptureFixture hass: HomeAssistant,
start_ha,
caplog: pytest.LogCaptureFixture,
) -> None: ) -> None:
"""Test invalid forecasts.""" """Test invalid forecasts."""
for attr, _v_attr, value in [ for attr, _v_attr, value in [
@ -336,7 +308,9 @@ async def test_forecast_invalid(
], ],
) )
async def test_forecast_invalid_is_daytime_missing_in_twice_daily( async def test_forecast_invalid_is_daytime_missing_in_twice_daily(
hass: HomeAssistant, start_ha, caplog: pytest.LogCaptureFixture hass: HomeAssistant,
start_ha,
caplog: pytest.LogCaptureFixture,
) -> None: ) -> None:
"""Test forecast service invalid when is_daytime missing in twice_daily forecast.""" """Test forecast service invalid when is_daytime missing in twice_daily forecast."""
for attr, _v_attr, value in [ for attr, _v_attr, value in [
@ -395,7 +369,9 @@ async def test_forecast_invalid_is_daytime_missing_in_twice_daily(
], ],
) )
async def test_forecast_invalid_datetime_missing( async def test_forecast_invalid_datetime_missing(
hass: HomeAssistant, start_ha, caplog: pytest.LogCaptureFixture hass: HomeAssistant,
start_ha,
caplog: pytest.LogCaptureFixture,
) -> None: ) -> None:
"""Test forecast service invalid when datetime missing.""" """Test forecast service invalid when datetime missing."""
for attr, _v_attr, value in [ for attr, _v_attr, value in [
@ -712,8 +688,12 @@ async def test_trigger_action(
}, },
], ],
) )
@pytest.mark.freeze_time("2023-10-19 13:50:05")
async def test_trigger_weather_services( async def test_trigger_weather_services(
hass: HomeAssistant, start_ha, entity_registry: er.EntityRegistry hass: HomeAssistant,
start_ha,
entity_registry: er.EntityRegistry,
snapshot: SnapshotAssertion,
) -> None: ) -> None:
"""Test trigger weather entity with services.""" """Test trigger weather entity with services."""
state = hass.states.get("weather.test") state = hass.states.get("weather.test")
@ -784,17 +764,7 @@ async def test_trigger_weather_services(
blocking=True, blocking=True,
return_response=True, return_response=True,
) )
assert response == { assert response == snapshot
"forecast": [
{
"datetime": now,
"condition": "sunny",
"precipitation": 20.0,
"temperature": 20.0,
"templow": 15.0,
}
],
}
response = await hass.services.async_call( response = await hass.services.async_call(
WEATHER_DOMAIN, WEATHER_DOMAIN,
@ -806,17 +776,7 @@ async def test_trigger_weather_services(
blocking=True, blocking=True,
return_response=True, return_response=True,
) )
assert response == { assert response == snapshot
"forecast": [
{
"datetime": now,
"condition": "sunny",
"precipitation": 20.0,
"temperature": 20.0,
"templow": 15.0,
}
],
}
response = await hass.services.async_call( response = await hass.services.async_call(
WEATHER_DOMAIN, WEATHER_DOMAIN,
@ -828,23 +788,11 @@ async def test_trigger_weather_services(
blocking=True, blocking=True,
return_response=True, return_response=True,
) )
assert response == { assert response == snapshot
"forecast": [
{
"datetime": now,
"condition": "sunny",
"precipitation": 20.0,
"temperature": 20.0,
"templow": 15.0,
"is_daytime": True,
}
],
}
async def test_restore_weather_save_state( async def test_restore_weather_save_state(
hass: HomeAssistant, hass: HomeAssistant, hass_storage: dict[str, Any], snapshot: SnapshotAssertion
hass_storage: dict[str, Any],
) -> None: ) -> None:
"""Test Restore saved state for Weather trigger template.""" """Test Restore saved state for Weather trigger template."""
assert await async_setup_component( assert await async_setup_component(
@ -881,19 +829,7 @@ async def test_restore_weather_save_state(
state = hass_storage[RESTORE_STATE_KEY]["data"][0]["state"] state = hass_storage[RESTORE_STATE_KEY]["data"][0]["state"]
assert state["entity_id"] == entity.entity_id assert state["entity_id"] == entity.entity_id
extra_data = hass_storage[RESTORE_STATE_KEY]["data"][0]["extra_data"] extra_data = hass_storage[RESTORE_STATE_KEY]["data"][0]["extra_data"]
assert extra_data == { assert extra_data == snapshot
"last_apparent_temperature": None,
"last_cloud_coverage": None,
"last_dew_point": None,
"last_humidity": "25.0",
"last_ozone": None,
"last_pressure": None,
"last_temperature": "15.0",
"last_visibility": None,
"last_wind_bearing": None,
"last_wind_gust_speed": None,
"last_wind_speed": None,
}
SAVED_ATTRIBUTES_1 = { SAVED_ATTRIBUTES_1 = {

View File

@ -0,0 +1,41 @@
# serializer version: 1
# name: test_get_forecast[daily-1]
dict({
'forecast': list([
dict({
'cloud_coverage': None,
'temperature': 38.0,
'templow': 38.0,
'uv_index': None,
'wind_bearing': None,
}),
]),
})
# ---
# name: test_get_forecast[hourly-2]
dict({
'forecast': list([
dict({
'cloud_coverage': None,
'temperature': 38.0,
'templow': 38.0,
'uv_index': None,
'wind_bearing': None,
}),
]),
})
# ---
# name: test_get_forecast[twice_daily-4]
dict({
'forecast': list([
dict({
'cloud_coverage': None,
'is_daytime': True,
'temperature': 38.0,
'templow': 38.0,
'uv_index': None,
'wind_bearing': None,
}),
]),
})
# ---

View File

@ -4,6 +4,7 @@ from datetime import datetime
from typing import Any from typing import Any
import pytest import pytest
from syrupy.assertion import SnapshotAssertion
from homeassistant.components.weather import ( from homeassistant.components.weather import (
ATTR_CONDITION_SUNNY, ATTR_CONDITION_SUNNY,
@ -854,14 +855,13 @@ async def test_forecast_twice_daily_missing_is_daytime(
@pytest.mark.parametrize( @pytest.mark.parametrize(
("forecast_type", "supported_features", "extra"), ("forecast_type", "supported_features"),
[ [
("daily", WeatherEntityFeature.FORECAST_DAILY, {}), ("daily", WeatherEntityFeature.FORECAST_DAILY),
("hourly", WeatherEntityFeature.FORECAST_HOURLY, {}), ("hourly", WeatherEntityFeature.FORECAST_HOURLY),
( (
"twice_daily", "twice_daily",
WeatherEntityFeature.FORECAST_TWICE_DAILY, WeatherEntityFeature.FORECAST_TWICE_DAILY,
{"is_daytime": True},
), ),
], ],
) )
@ -870,7 +870,7 @@ async def test_get_forecast(
enable_custom_integrations: None, enable_custom_integrations: None,
forecast_type: str, forecast_type: str,
supported_features: int, supported_features: int,
extra: dict[str, Any], snapshot: SnapshotAssertion,
) -> None: ) -> None:
"""Test get forecast service.""" """Test get forecast service."""
@ -891,18 +891,7 @@ async def test_get_forecast(
blocking=True, blocking=True,
return_response=True, return_response=True,
) )
assert response == { assert response == snapshot
"forecast": [
{
"cloud_coverage": None,
"temperature": 38.0,
"templow": 38.0,
"uv_index": None,
"wind_bearing": None,
}
| extra
],
}
async def test_get_forecast_no_forecast( async def test_get_forecast_no_forecast(