mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 11:17:21 +00:00
Refactor rainforest_raven coordinator tests (#128591)
* Refactor rainforest_raven tests * Remove assert * Cleanup freezer * Drop un-needed coordinator properties * Cleanup remaining coordinator tests * Improve * Revert _DEVICE_TIMEOUT * Ensure 100% coverage * Use async_fire_time_changed
This commit is contained in:
parent
356e09091d
commit
8c4b076746
@ -1,90 +0,0 @@
|
|||||||
"""Tests for the Rainforest RAVEn data coordinator."""
|
|
||||||
|
|
||||||
import asyncio
|
|
||||||
import functools
|
|
||||||
from unittest.mock import AsyncMock
|
|
||||||
|
|
||||||
from aioraven.device import RAVEnConnectionError
|
|
||||||
import pytest
|
|
||||||
|
|
||||||
from homeassistant.components.rainforest_raven.coordinator import RAVEnDataCoordinator
|
|
||||||
from homeassistant.config_entries import ConfigEntryState
|
|
||||||
from homeassistant.core import HomeAssistant
|
|
||||||
from homeassistant.exceptions import ConfigEntryNotReady
|
|
||||||
|
|
||||||
from . import create_mock_entry
|
|
||||||
|
|
||||||
|
|
||||||
async def test_coordinator_cache_device(
|
|
||||||
hass: HomeAssistant, mock_device: AsyncMock
|
|
||||||
) -> None:
|
|
||||||
"""Test that the device isn't re-opened for subsequent refreshes."""
|
|
||||||
entry = create_mock_entry()
|
|
||||||
entry._async_set_state(hass, ConfigEntryState.SETUP_IN_PROGRESS, None)
|
|
||||||
coordinator = RAVEnDataCoordinator(hass, entry)
|
|
||||||
|
|
||||||
await coordinator.async_config_entry_first_refresh()
|
|
||||||
assert mock_device.get_network_info.call_count == 1
|
|
||||||
assert mock_device.open.call_count == 1
|
|
||||||
|
|
||||||
await coordinator.async_refresh()
|
|
||||||
assert mock_device.get_network_info.call_count == 2
|
|
||||||
assert mock_device.open.call_count == 1
|
|
||||||
|
|
||||||
|
|
||||||
async def test_coordinator_device_error_setup(
|
|
||||||
hass: HomeAssistant, mock_device: AsyncMock
|
|
||||||
) -> None:
|
|
||||||
"""Test handling of a device error during initialization."""
|
|
||||||
entry = create_mock_entry()
|
|
||||||
entry._async_set_state(hass, ConfigEntryState.SETUP_IN_PROGRESS, None)
|
|
||||||
coordinator = RAVEnDataCoordinator(hass, entry)
|
|
||||||
|
|
||||||
mock_device.get_network_info.side_effect = RAVEnConnectionError
|
|
||||||
with pytest.raises(ConfigEntryNotReady):
|
|
||||||
await coordinator.async_config_entry_first_refresh()
|
|
||||||
|
|
||||||
|
|
||||||
async def test_coordinator_device_error_update(
|
|
||||||
hass: HomeAssistant, mock_device: AsyncMock
|
|
||||||
) -> None:
|
|
||||||
"""Test handling of a device error during an update."""
|
|
||||||
entry = create_mock_entry()
|
|
||||||
entry._async_set_state(hass, ConfigEntryState.SETUP_IN_PROGRESS, None)
|
|
||||||
coordinator = RAVEnDataCoordinator(hass, entry)
|
|
||||||
|
|
||||||
await coordinator.async_config_entry_first_refresh()
|
|
||||||
assert coordinator.last_update_success is True
|
|
||||||
|
|
||||||
mock_device.get_network_info.side_effect = RAVEnConnectionError
|
|
||||||
await coordinator.async_refresh()
|
|
||||||
assert coordinator.last_update_success is False
|
|
||||||
|
|
||||||
|
|
||||||
async def test_coordinator_device_timeout_update(
|
|
||||||
hass: HomeAssistant, mock_device: AsyncMock
|
|
||||||
) -> None:
|
|
||||||
"""Test handling of a device timeout during an update."""
|
|
||||||
entry = create_mock_entry()
|
|
||||||
entry._async_set_state(hass, ConfigEntryState.SETUP_IN_PROGRESS, None)
|
|
||||||
coordinator = RAVEnDataCoordinator(hass, entry)
|
|
||||||
|
|
||||||
await coordinator.async_config_entry_first_refresh()
|
|
||||||
assert coordinator.last_update_success is True
|
|
||||||
|
|
||||||
mock_device.get_network_info.side_effect = functools.partial(asyncio.sleep, 10)
|
|
||||||
await coordinator.async_refresh()
|
|
||||||
assert coordinator.last_update_success is False
|
|
||||||
|
|
||||||
|
|
||||||
async def test_coordinator_comm_error(
|
|
||||||
hass: HomeAssistant, mock_device: AsyncMock
|
|
||||||
) -> None:
|
|
||||||
"""Test handling of an error parsing or reading raw device data."""
|
|
||||||
entry = create_mock_entry()
|
|
||||||
entry._async_set_state(hass, ConfigEntryState.SETUP_IN_PROGRESS, None)
|
|
||||||
coordinator = RAVEnDataCoordinator(hass, entry)
|
|
||||||
|
|
||||||
mock_device.synchronize.side_effect = RAVEnConnectionError
|
|
||||||
with pytest.raises(ConfigEntryNotReady):
|
|
||||||
await coordinator.async_config_entry_first_refresh()
|
|
@ -3,6 +3,7 @@
|
|||||||
from unittest.mock import AsyncMock
|
from unittest.mock import AsyncMock
|
||||||
|
|
||||||
from aioraven.data import DeviceInfo as RAVenDeviceInfo
|
from aioraven.data import DeviceInfo as RAVenDeviceInfo
|
||||||
|
from aioraven.device import RAVEnConnectionError
|
||||||
import pytest
|
import pytest
|
||||||
from syrupy.assertion import SnapshotAssertion
|
from syrupy.assertion import SnapshotAssertion
|
||||||
|
|
||||||
@ -55,3 +56,28 @@ async def test_device_registry(
|
|||||||
entries = dr.async_entries_for_config_entry(device_registry, entry.entry_id)
|
entries = dr.async_entries_for_config_entry(device_registry, entry.entry_id)
|
||||||
assert len(entries) == device_count
|
assert len(entries) == device_count
|
||||||
assert entries == snapshot
|
assert entries == snapshot
|
||||||
|
|
||||||
|
|
||||||
|
async def test_synchronize_error(hass: HomeAssistant, mock_device: AsyncMock) -> None:
|
||||||
|
"""Test handling of an error parsing or reading raw device data."""
|
||||||
|
entry = create_mock_entry()
|
||||||
|
entry.add_to_hass(hass)
|
||||||
|
|
||||||
|
mock_device.synchronize.side_effect = RAVEnConnectionError
|
||||||
|
|
||||||
|
await hass.config_entries.async_setup(entry.entry_id)
|
||||||
|
|
||||||
|
assert entry.state is ConfigEntryState.SETUP_RETRY
|
||||||
|
|
||||||
|
|
||||||
|
async def test_get_network_info_error(
|
||||||
|
hass: HomeAssistant, mock_device: AsyncMock
|
||||||
|
) -> None:
|
||||||
|
"""Test handling of a device error during initialization."""
|
||||||
|
entry = create_mock_entry()
|
||||||
|
entry.add_to_hass(hass)
|
||||||
|
|
||||||
|
mock_device.get_network_info.side_effect = RAVEnConnectionError
|
||||||
|
await hass.config_entries.async_setup(entry.entry_id)
|
||||||
|
|
||||||
|
assert entry.state is ConfigEntryState.SETUP_RETRY
|
||||||
|
@ -1,12 +1,20 @@
|
|||||||
"""Tests for the Rainforest RAVEn sensors."""
|
"""Tests for the Rainforest RAVEn sensors."""
|
||||||
|
|
||||||
|
from datetime import timedelta
|
||||||
|
from unittest.mock import AsyncMock
|
||||||
|
|
||||||
|
from aioraven.device import RAVEnConnectionError
|
||||||
|
from freezegun.api import FrozenDateTimeFactory
|
||||||
import pytest
|
import pytest
|
||||||
from syrupy.assertion import SnapshotAssertion
|
from syrupy.assertion import SnapshotAssertion
|
||||||
|
|
||||||
|
from homeassistant.const import STATE_UNAVAILABLE
|
||||||
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 tests.common import MockConfigEntry, snapshot_platform
|
from .const import NETWORK_INFO
|
||||||
|
|
||||||
|
from tests.common import MockConfigEntry, async_fire_time_changed, snapshot_platform
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.usefixtures("mock_entry")
|
@pytest.mark.usefixtures("mock_entry")
|
||||||
@ -20,3 +28,75 @@ async def test_sensors(
|
|||||||
assert len(hass.states.async_all()) == 5
|
assert len(hass.states.async_all()) == 5
|
||||||
|
|
||||||
await snapshot_platform(hass, entity_registry, snapshot, mock_entry.entry_id)
|
await snapshot_platform(hass, entity_registry, snapshot, mock_entry.entry_id)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.usefixtures("mock_entry")
|
||||||
|
async def test_device_update_error(
|
||||||
|
hass: HomeAssistant,
|
||||||
|
mock_device: AsyncMock,
|
||||||
|
freezer: FrozenDateTimeFactory,
|
||||||
|
) -> None:
|
||||||
|
"""Test handling of a device error during an update."""
|
||||||
|
mock_device.get_network_info.side_effect = (RAVEnConnectionError, NETWORK_INFO)
|
||||||
|
|
||||||
|
states = hass.states.async_all()
|
||||||
|
assert len(states) == 5
|
||||||
|
assert all(state.state != STATE_UNAVAILABLE for state in states)
|
||||||
|
|
||||||
|
freezer.tick(timedelta(seconds=60))
|
||||||
|
async_fire_time_changed(hass)
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
|
states = hass.states.async_all()
|
||||||
|
assert len(states) == 5
|
||||||
|
assert all(state.state == STATE_UNAVAILABLE for state in states)
|
||||||
|
|
||||||
|
freezer.tick(timedelta(seconds=60))
|
||||||
|
async_fire_time_changed(hass)
|
||||||
|
|
||||||
|
states = hass.states.async_all()
|
||||||
|
assert len(states) == 5
|
||||||
|
assert all(state.state != STATE_UNAVAILABLE for state in states)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.usefixtures("mock_entry")
|
||||||
|
async def test_device_update_timeout(
|
||||||
|
hass: HomeAssistant, mock_device: AsyncMock, freezer: FrozenDateTimeFactory
|
||||||
|
) -> None:
|
||||||
|
"""Test handling of a device timeout during an update."""
|
||||||
|
mock_device.get_network_info.side_effect = (TimeoutError, NETWORK_INFO)
|
||||||
|
|
||||||
|
states = hass.states.async_all()
|
||||||
|
assert len(states) == 5
|
||||||
|
assert all(state.state != STATE_UNAVAILABLE for state in states)
|
||||||
|
|
||||||
|
freezer.tick(timedelta(seconds=60))
|
||||||
|
async_fire_time_changed(hass)
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
|
states = hass.states.async_all()
|
||||||
|
assert len(states) == 5
|
||||||
|
assert all(state.state == STATE_UNAVAILABLE for state in states)
|
||||||
|
|
||||||
|
freezer.tick(timedelta(seconds=60))
|
||||||
|
async_fire_time_changed(hass)
|
||||||
|
|
||||||
|
states = hass.states.async_all()
|
||||||
|
assert len(states) == 5
|
||||||
|
assert all(state.state != STATE_UNAVAILABLE for state in states)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.usefixtures("mock_entry")
|
||||||
|
async def test_device_cache(
|
||||||
|
hass: HomeAssistant, mock_device: AsyncMock, freezer: FrozenDateTimeFactory
|
||||||
|
) -> None:
|
||||||
|
"""Test that the device isn't re-opened for subsequent refreshes."""
|
||||||
|
assert mock_device.get_network_info.call_count == 1
|
||||||
|
assert mock_device.open.call_count == 1
|
||||||
|
|
||||||
|
freezer.tick(timedelta(seconds=60))
|
||||||
|
async_fire_time_changed(hass)
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
|
assert mock_device.get_network_info.call_count == 2
|
||||||
|
assert mock_device.open.call_count == 1
|
||||||
|
Loading…
x
Reference in New Issue
Block a user