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 @pytest.fixture
def mock_simple_nws(): def mock_simple_nws():
"""Mock pynws SimpleNWS with default values.""" """Mock pynws SimpleNWS with default values."""
# set RETRY_STOP and RETRY_INTERVAL to avoid retries inside pynws in tests
with patch("homeassistant.components.nws.SimpleNWS") as mock_nws: 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 = mock_nws.return_value
instance.set_station = AsyncMock(return_value=None) instance.set_station = AsyncMock(return_value=None)
instance.update_observation = AsyncMock(return_value=None) instance.update_observation = AsyncMock(return_value=None)
@ -29,7 +33,12 @@ def mock_simple_nws():
@pytest.fixture @pytest.fixture
def mock_simple_nws_times_out(): def mock_simple_nws_times_out():
"""Mock pynws SimpleNWS that 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 = mock_nws.return_value
instance.set_station = AsyncMock(side_effect=asyncio.TimeoutError) instance.set_station = AsyncMock(side_effect=asyncio.TimeoutError)
instance.update_observation = 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.""" """Tests for the NWS weather component."""
from datetime import timedelta from datetime import timedelta
from unittest.mock import patch
import aiohttp import aiohttp
from freezegun.api import FrozenDateTimeFactory from freezegun.api import FrozenDateTimeFactory
@ -24,7 +23,6 @@ from homeassistant.const import STATE_UNAVAILABLE, STATE_UNKNOWN
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as er from homeassistant.helpers import entity_registry as er
from homeassistant.setup import async_setup_component 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 homeassistant.util.unit_system import METRIC_SYSTEM, US_CUSTOMARY_SYSTEM
from .const import ( from .const import (
@ -127,10 +125,6 @@ async def test_data_caching_error_observation(
caplog, caplog,
) -> None: ) -> None:
"""Test caching of data with errors.""" """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( entry = MockConfigEntry(
@ -302,9 +296,6 @@ async def test_error_observation(
hass: HomeAssistant, mock_simple_nws, no_sensor hass: HomeAssistant, mock_simple_nws, no_sensor
) -> None: ) -> None:
"""Test error during update observation.""" """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 instance = mock_simple_nws.return_value
# first update fails # first update fails
instance.update_observation.side_effect = aiohttp.ClientError instance.update_observation.side_effect = aiohttp.ClientError