Add test helper to snapshot a platform (#115880)

* Add test helper to snapshot a platform

* Add test helper to snapshot a platform
This commit is contained in:
Joost Lekkerkerker 2024-04-20 14:49:57 +02:00 committed by GitHub
parent 8f73422ce5
commit 16e31d8f74
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 24 additions and 9 deletions

View file

@ -22,6 +22,7 @@ from unittest.mock import AsyncMock, Mock, patch
from aiohttp.test_utils import unused_port as get_test_instance_port # noqa: F401 from aiohttp.test_utils import unused_port as get_test_instance_port # noqa: F401
import pytest import pytest
from syrupy import SnapshotAssertion
import voluptuous as vol import voluptuous as vol
from homeassistant import auth, bootstrap, config_entries, loader from homeassistant import auth, bootstrap, config_entries, loader
@ -1733,3 +1734,22 @@ def setup_test_component_platform(
mock_platform(hass, f"test.{domain}", platform, built_in=built_in) mock_platform(hass, f"test.{domain}", platform, built_in=built_in)
return platform return platform
async def snapshot_platform(
hass: HomeAssistant,
entity_registry: er.EntityRegistry,
snapshot: SnapshotAssertion,
config_entry_id: str,
) -> None:
"""Snapshot a platform."""
entity_entries = er.async_entries_for_config_entry(entity_registry, config_entry_id)
assert entity_entries
assert (
len({entity_entry.domain for entity_entry in entity_entries}) == 1
), "Please limit the loaded platforms to 1 platform."
for entity_entry in entity_entries:
assert entity_entry == snapshot(name=f"{entity_entry.entity_id}-entry")
assert entity_entry.disabled_by is None, "Please enable all entities."
assert (state := hass.states.get(entity_entry.entity_id))
assert state == snapshot(name=f"{entity_entry.entity_id}-state")

View file

@ -21,7 +21,7 @@ from . import (
setup_integration, setup_integration,
) )
from tests.common import MockConfigEntry, async_fire_time_changed from tests.common import MockConfigEntry, async_fire_time_changed, snapshot_platform
@pytest.mark.freeze_time("2023-10-21") @pytest.mark.freeze_time("2023-10-21")
@ -36,15 +36,10 @@ async def test_all_entities(
"""Test all entities.""" """Test all entities."""
with patch("homeassistant.components.withings.PLATFORMS", [Platform.SENSOR]): with patch("homeassistant.components.withings.PLATFORMS", [Platform.SENSOR]):
await setup_integration(hass, polling_config_entry) await setup_integration(hass, polling_config_entry)
entity_entries = er.async_entries_for_config_entry(
entity_registry, polling_config_entry.entry_id
)
assert entity_entries await snapshot_platform(
for entity_entry in entity_entries: hass, entity_registry, snapshot, polling_config_entry.entry_id
assert entity_entry == snapshot(name=f"{entity_entry.entity_id}-entry") )
assert (state := hass.states.get(entity_entry.entity_id))
assert state == snapshot(name=f"{entity_entry.entity_id}-state")
async def test_update_failed( async def test_update_failed(