mirror of
https://github.com/home-assistant/core.git
synced 2025-07-22 20:57:21 +00:00
Improve nws tests by centralizing and removing unneeded patch
ing (#118052)
This commit is contained in:
parent
a89dcbc78b
commit
8fbe39f2a7
@ -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)
|
||||||
|
@ -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,47 +125,43 @@ async def test_data_caching_error_observation(
|
|||||||
caplog,
|
caplog,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Test caching of data with errors."""
|
"""Test caching of data with errors."""
|
||||||
with (
|
instance = mock_simple_nws.return_value
|
||||||
patch("homeassistant.components.nws.coordinator.RETRY_STOP", 0),
|
|
||||||
patch("homeassistant.components.nws.coordinator.RETRY_INTERVAL", 0),
|
|
||||||
):
|
|
||||||
instance = mock_simple_nws.return_value
|
|
||||||
|
|
||||||
entry = MockConfigEntry(
|
entry = MockConfigEntry(
|
||||||
domain=nws.DOMAIN,
|
domain=nws.DOMAIN,
|
||||||
data=NWS_CONFIG,
|
data=NWS_CONFIG,
|
||||||
)
|
)
|
||||||
entry.add_to_hass(hass)
|
entry.add_to_hass(hass)
|
||||||
await hass.config_entries.async_setup(entry.entry_id)
|
await hass.config_entries.async_setup(entry.entry_id)
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
state = hass.states.get("weather.abc")
|
state = hass.states.get("weather.abc")
|
||||||
assert state.state == "sunny"
|
assert state.state == "sunny"
|
||||||
|
|
||||||
# data is still valid even when update fails
|
# data is still valid even when update fails
|
||||||
instance.update_observation.side_effect = NwsNoDataError("Test")
|
instance.update_observation.side_effect = NwsNoDataError("Test")
|
||||||
|
|
||||||
freezer.tick(DEFAULT_SCAN_INTERVAL + timedelta(seconds=100))
|
freezer.tick(DEFAULT_SCAN_INTERVAL + timedelta(seconds=100))
|
||||||
async_fire_time_changed(hass)
|
async_fire_time_changed(hass)
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
state = hass.states.get("weather.abc")
|
state = hass.states.get("weather.abc")
|
||||||
assert state.state == "sunny"
|
assert state.state == "sunny"
|
||||||
|
|
||||||
assert (
|
assert (
|
||||||
"NWS observation update failed, but data still valid. Last success: "
|
"NWS observation update failed, but data still valid. Last success: "
|
||||||
in caplog.text
|
in caplog.text
|
||||||
)
|
)
|
||||||
|
|
||||||
# data is no longer valid after OBSERVATION_VALID_TIME
|
# data is no longer valid after OBSERVATION_VALID_TIME
|
||||||
freezer.tick(OBSERVATION_VALID_TIME + timedelta(seconds=1))
|
freezer.tick(OBSERVATION_VALID_TIME + timedelta(seconds=1))
|
||||||
async_fire_time_changed(hass)
|
async_fire_time_changed(hass)
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
state = hass.states.get("weather.abc")
|
state = hass.states.get("weather.abc")
|
||||||
assert state.state == STATE_UNAVAILABLE
|
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(
|
async def test_no_data_error_observation(
|
||||||
@ -302,26 +296,23 @@ 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()
|
instance = mock_simple_nws.return_value
|
||||||
with patch("homeassistant.components.nws.coordinator.utcnow") as mock_utc:
|
# first update fails
|
||||||
mock_utc.return_value = utc_time
|
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(
|
entry = MockConfigEntry(
|
||||||
domain=nws.DOMAIN,
|
domain=nws.DOMAIN,
|
||||||
data=NWS_CONFIG,
|
data=NWS_CONFIG,
|
||||||
)
|
)
|
||||||
entry.add_to_hass(hass)
|
entry.add_to_hass(hass)
|
||||||
await hass.config_entries.async_setup(entry.entry_id)
|
await hass.config_entries.async_setup(entry.entry_id)
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
instance.update_observation.assert_called_once()
|
instance.update_observation.assert_called_once()
|
||||||
|
|
||||||
state = hass.states.get("weather.abc")
|
state = hass.states.get("weather.abc")
|
||||||
assert state
|
assert state
|
||||||
assert state.state == STATE_UNAVAILABLE
|
assert state.state == STATE_UNAVAILABLE
|
||||||
|
|
||||||
|
|
||||||
async def test_new_config_entry(hass: HomeAssistant, no_sensor) -> None:
|
async def test_new_config_entry(hass: HomeAssistant, no_sensor) -> None:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user