Improve nws tests by centralizing and removing unneeded patching (#118052)

This commit is contained in:
MatthewFlamm 2024-05-25 07:50:15 -04:00 committed by GitHub
parent a89dcbc78b
commit 8fbe39f2a7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 54 additions and 54 deletions

View File

@ -11,8 +11,12 @@ from .const import DEFAULT_FORECAST, DEFAULT_OBSERVATION
@pytest.fixture
def mock_simple_nws():
"""Mock pynws SimpleNWS with default values."""
with patch("homeassistant.components.nws.SimpleNWS") as mock_nws:
# set RETRY_STOP and RETRY_INTERVAL to avoid retries inside pynws in tests
with (
patch("homeassistant.components.nws.SimpleNWS") as mock_nws,
patch("homeassistant.components.nws.coordinator.RETRY_STOP", 0),
patch("homeassistant.components.nws.coordinator.RETRY_INTERVAL", 0),
):
instance = mock_nws.return_value
instance.set_station = AsyncMock(return_value=None)
instance.update_observation = AsyncMock(return_value=None)
@ -29,7 +33,12 @@ def mock_simple_nws():
@pytest.fixture
def mock_simple_nws_times_out():
"""Mock pynws SimpleNWS that times out."""
with patch("homeassistant.components.nws.SimpleNWS") as mock_nws:
# set RETRY_STOP and RETRY_INTERVAL to avoid retries inside pynws in tests
with (
patch("homeassistant.components.nws.SimpleNWS") as mock_nws,
patch("homeassistant.components.nws.coordinator.RETRY_STOP", 0),
patch("homeassistant.components.nws.coordinator.RETRY_INTERVAL", 0),
):
instance = mock_nws.return_value
instance.set_station = AsyncMock(side_effect=asyncio.TimeoutError)
instance.update_observation = AsyncMock(side_effect=asyncio.TimeoutError)

View File

@ -1,7 +1,6 @@
"""Tests for the NWS weather component."""
from datetime import timedelta
from unittest.mock import patch
import aiohttp
from freezegun.api import FrozenDateTimeFactory
@ -24,7 +23,6 @@ from homeassistant.const import STATE_UNAVAILABLE, STATE_UNKNOWN
from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as er
from homeassistant.setup import async_setup_component
import homeassistant.util.dt as dt_util
from homeassistant.util.unit_system import METRIC_SYSTEM, US_CUSTOMARY_SYSTEM
from .const import (
@ -127,47 +125,43 @@ async def test_data_caching_error_observation(
caplog,
) -> None:
"""Test caching of data with errors."""
with (
patch("homeassistant.components.nws.coordinator.RETRY_STOP", 0),
patch("homeassistant.components.nws.coordinator.RETRY_INTERVAL", 0),
):
instance = mock_simple_nws.return_value
instance = mock_simple_nws.return_value
entry = MockConfigEntry(
domain=nws.DOMAIN,
data=NWS_CONFIG,
)
entry.add_to_hass(hass)
await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()
entry = MockConfigEntry(
domain=nws.DOMAIN,
data=NWS_CONFIG,
)
entry.add_to_hass(hass)
await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()
state = hass.states.get("weather.abc")
assert state.state == "sunny"
state = hass.states.get("weather.abc")
assert state.state == "sunny"
# data is still valid even when update fails
instance.update_observation.side_effect = NwsNoDataError("Test")
# data is still valid even when update fails
instance.update_observation.side_effect = NwsNoDataError("Test")
freezer.tick(DEFAULT_SCAN_INTERVAL + timedelta(seconds=100))
async_fire_time_changed(hass)
await hass.async_block_till_done()
freezer.tick(DEFAULT_SCAN_INTERVAL + timedelta(seconds=100))
async_fire_time_changed(hass)
await hass.async_block_till_done()
state = hass.states.get("weather.abc")
assert state.state == "sunny"
state = hass.states.get("weather.abc")
assert state.state == "sunny"
assert (
"NWS observation update failed, but data still valid. Last success: "
in caplog.text
)
assert (
"NWS observation update failed, but data still valid. Last success: "
in caplog.text
)
# data is no longer valid after OBSERVATION_VALID_TIME
freezer.tick(OBSERVATION_VALID_TIME + timedelta(seconds=1))
async_fire_time_changed(hass)
await hass.async_block_till_done()
# data is no longer valid after OBSERVATION_VALID_TIME
freezer.tick(OBSERVATION_VALID_TIME + timedelta(seconds=1))
async_fire_time_changed(hass)
await hass.async_block_till_done()
state = hass.states.get("weather.abc")
assert state.state == STATE_UNAVAILABLE
state = hass.states.get("weather.abc")
assert state.state == STATE_UNAVAILABLE
assert "Error fetching NWS observation station ABC data: Test" in caplog.text
assert "Error fetching NWS observation station ABC data: Test" in caplog.text
async def test_no_data_error_observation(
@ -302,26 +296,23 @@ async def test_error_observation(
hass: HomeAssistant, mock_simple_nws, no_sensor
) -> None:
"""Test error during update observation."""
utc_time = dt_util.utcnow()
with patch("homeassistant.components.nws.coordinator.utcnow") as mock_utc:
mock_utc.return_value = utc_time
instance = mock_simple_nws.return_value
# first update fails
instance.update_observation.side_effect = aiohttp.ClientError
instance = mock_simple_nws.return_value
# first update fails
instance.update_observation.side_effect = aiohttp.ClientError
entry = MockConfigEntry(
domain=nws.DOMAIN,
data=NWS_CONFIG,
)
entry.add_to_hass(hass)
await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()
entry = MockConfigEntry(
domain=nws.DOMAIN,
data=NWS_CONFIG,
)
entry.add_to_hass(hass)
await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()
instance.update_observation.assert_called_once()
instance.update_observation.assert_called_once()
state = hass.states.get("weather.abc")
assert state
assert state.state == STATE_UNAVAILABLE
state = hass.states.get("weather.abc")
assert state
assert state.state == STATE_UNAVAILABLE
async def test_new_config_entry(hass: HomeAssistant, no_sensor) -> None: