Skip to content

Commit

Permalink
Use snapshot assertion in rainforest_raven diagnostic tests (home-ass…
Browse files Browse the repository at this point in the history
  • Loading branch information
epenet authored Oct 18, 2024
1 parent 10d26bf commit 5580c3f
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 52 deletions.
5 changes: 4 additions & 1 deletion tests/components/rainforest_raven/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"""Tests for the Rainforest RAVEn component."""

from unittest.mock import AsyncMock

from homeassistant.components.rainforest_raven.const import DOMAIN
from homeassistant.const import CONF_DEVICE, CONF_MAC

Expand All @@ -14,7 +16,7 @@
SUMMATION,
)

from tests.common import AsyncMock, MockConfigEntry
from tests.common import MockConfigEntry


def create_mock_device() -> AsyncMock:
Expand Down Expand Up @@ -42,4 +44,5 @@ def create_mock_entry(no_meters: bool = False) -> MockConfigEntry:
CONF_DEVICE: DISCOVERY_INFO.device,
CONF_MAC: [] if no_meters else [METER_INFO[None].meter_mac_id.hex()],
},
entry_id="01JADXBJSPYEBAFPKGXDJWZBQ8",
)
107 changes: 107 additions & 0 deletions tests/components/rainforest_raven/snapshots/test_diagnostics.ambr
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
# serializer version: 1
# name: test_entry_diagnostics
dict({
'config_entry': dict({
'data': dict({
'device': '/dev/ttyACM0',
'mac': '**REDACTED**',
}),
'disabled_by': None,
'discovery_keys': dict({
}),
'domain': 'rainforest_raven',
'entry_id': '01JADXBJSPYEBAFPKGXDJWZBQ8',
'minor_version': 1,
'options': dict({
}),
'pref_disable_new_entities': False,
'pref_disable_polling': False,
'source': 'user',
'title': 'Mock Title',
'unique_id': None,
'version': 1,
}),
'data': dict({
'Meters': dict({
'**REDACTED0**': dict({
'CurrentSummationDelivered': dict({
'device_mac_id': '**REDACTED**',
'meter_mac_id': '**REDACTED**',
'summation_delivered': '23456.7890',
'summation_received': '00000.0000',
'time_stamp': None,
}),
'InstantaneousDemand': dict({
'demand': '1.2345',
'device_mac_id': '**REDACTED**',
'meter_mac_id': '**REDACTED**',
'time_stamp': None,
}),
'PriceCluster': dict({
'currency': dict({
'__type': "<enum 'Currency'>",
'repr': "<Currency.USD: 'USD'>",
}),
'device_mac_id': '**REDACTED**',
'meter_mac_id': '**REDACTED**',
'price': '0.10',
'rate_label': 'Set by user',
'tier': 3,
'tier_label': 'Set by user',
'time_stamp': None,
}),
}),
}),
'NetworkInfo': dict({
'channel': 13,
'coord_mac_id': None,
'description': None,
'device_mac_id': '**REDACTED**',
'ext_pan_id': None,
'link_strength': 100,
'short_addr': None,
'status': None,
'status_code': None,
}),
}),
})
# ---
# name: test_entry_diagnostics_no_meters
dict({
'config_entry': dict({
'data': dict({
'device': '/dev/ttyACM0',
'mac': '**REDACTED**',
}),
'disabled_by': None,
'discovery_keys': dict({
}),
'domain': 'rainforest_raven',
'entry_id': '01JADXBJSPYEBAFPKGXDJWZBQ8',
'minor_version': 1,
'options': dict({
}),
'pref_disable_new_entities': False,
'pref_disable_polling': False,
'source': 'user',
'title': 'Mock Title',
'unique_id': None,
'version': 1,
}),
'data': dict({
'Meters': dict({
}),
'NetworkInfo': dict({
'channel': 13,
'coord_mac_id': None,
'description': None,
'device_mac_id': '**REDACTED**',
'ext_pan_id': None,
'link_strength': 100,
'short_addr': None,
'status': None,
'status_code': None,
}),
}),
})
# ---
66 changes: 15 additions & 51 deletions tests/components/rainforest_raven/test_diagnostics.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
"""Test the Rainforest Eagle diagnostics."""

from dataclasses import asdict
from unittest.mock import AsyncMock

import pytest
from syrupy.assertion import SnapshotAssertion
from syrupy.filters import props

from homeassistant.components.diagnostics import REDACTED
from homeassistant.const import CONF_MAC
from homeassistant.core import HomeAssistant

from . import create_mock_entry
from .const import DEMAND, NETWORK_INFO, PRICE_CLUSTER, SUMMATION

from tests.common import MockConfigEntry
from tests.components.diagnostics import get_diagnostics_for_config_entry
from tests.typing import ClientSessionGenerator


@pytest.fixture
async def mock_entry_no_meters(hass: HomeAssistant, mock_device):
async def mock_entry_no_meters(
hass: HomeAssistant, mock_device: AsyncMock
) -> MockConfigEntry:
"""Mock a RAVEn config entry with no meters."""
mock_entry = create_mock_entry(True)
mock_entry.add_to_hass(hass)
Expand All @@ -28,61 +30,23 @@ async def mock_entry_no_meters(hass: HomeAssistant, mock_device):
async def test_entry_diagnostics_no_meters(
hass: HomeAssistant,
hass_client: ClientSessionGenerator,
mock_device,
mock_entry_no_meters,
mock_entry_no_meters: MockConfigEntry,
snapshot: SnapshotAssertion,
) -> None:
"""Test RAVEn diagnostics before the coordinator has updated."""
result = await get_diagnostics_for_config_entry(
hass, hass_client, mock_entry_no_meters
)

config_entry_dict = mock_entry_no_meters.as_dict()
config_entry_dict["data"][CONF_MAC] = REDACTED

assert result == {
"config_entry": config_entry_dict | {"discovery_keys": {}},
"data": {
"Meters": {},
"NetworkInfo": {**asdict(NETWORK_INFO), "device_mac_id": REDACTED},
},
}
assert result == snapshot(exclude=props("created_at", "modified_at"))


async def test_entry_diagnostics(
hass: HomeAssistant, hass_client: ClientSessionGenerator, mock_device, mock_entry
hass: HomeAssistant,
hass_client: ClientSessionGenerator,
mock_entry: MockConfigEntry,
snapshot: SnapshotAssertion,
) -> None:
"""Test RAVEn diagnostics."""
result = await get_diagnostics_for_config_entry(hass, hass_client, mock_entry)

config_entry_dict = mock_entry.as_dict()
config_entry_dict["data"][CONF_MAC] = REDACTED

assert result == {
"config_entry": config_entry_dict | {"discovery_keys": {}},
"data": {
"Meters": {
"**REDACTED0**": {
"CurrentSummationDelivered": {
**asdict(SUMMATION),
"device_mac_id": REDACTED,
"meter_mac_id": REDACTED,
},
"InstantaneousDemand": {
**asdict(DEMAND),
"device_mac_id": REDACTED,
"meter_mac_id": REDACTED,
},
"PriceCluster": {
**asdict(PRICE_CLUSTER),
"device_mac_id": REDACTED,
"meter_mac_id": REDACTED,
"currency": {
"__type": str(type(PRICE_CLUSTER.currency)),
"repr": repr(PRICE_CLUSTER.currency),
},
},
},
},
"NetworkInfo": {**asdict(NETWORK_INFO), "device_mac_id": REDACTED},
},
}
assert result == snapshot(exclude=props("created_at", "modified_at"))

0 comments on commit 5580c3f

Please sign in to comment.