Skip to content

Commit

Permalink
Refactor rainforest_raven coordinator tests (home-assistant#128591)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
epenet authored Oct 18, 2024
1 parent 356e090 commit 8c4b076
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 91 deletions.
90 changes: 0 additions & 90 deletions tests/components/rainforest_raven/test_coordinator.py

This file was deleted.

26 changes: 26 additions & 0 deletions tests/components/rainforest_raven/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from unittest.mock import AsyncMock

from aioraven.data import DeviceInfo as RAVenDeviceInfo
from aioraven.device import RAVEnConnectionError
import pytest
from syrupy.assertion import SnapshotAssertion

Expand Down Expand Up @@ -55,3 +56,28 @@ async def test_device_registry(
entries = dr.async_entries_for_config_entry(device_registry, entry.entry_id)
assert len(entries) == device_count
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
82 changes: 81 additions & 1 deletion tests/components/rainforest_raven/test_sensor.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
"""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
from syrupy.assertion import SnapshotAssertion

from homeassistant.const import STATE_UNAVAILABLE
from homeassistant.core import HomeAssistant
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")
Expand All @@ -20,3 +28,75 @@ async def test_sensors(
assert len(hass.states.async_all()) == 5

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

0 comments on commit 8c4b076

Please sign in to comment.