Skip to content

Commit

Permalink
Use Parametrizing in Matter tests (#126759)
Browse files Browse the repository at this point in the history
* Overhaul matter tests

* Overhaul

* Remove matter_client where obsolete

* Move snapshots to the top

* Use usefixtures

* Add Valve
  • Loading branch information
joostlek authored Sep 26, 2024
1 parent 6c539cd commit 6e12726
Show file tree
Hide file tree
Showing 19 changed files with 392 additions and 806 deletions.
44 changes: 13 additions & 31 deletions tests/components/matter/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import asyncio
from collections.abc import AsyncGenerator
from typing import Any
from unittest.mock import AsyncMock, MagicMock, patch

from matter_server.client.models.node import MatterNode
Expand Down Expand Up @@ -86,39 +87,20 @@ async def matter_devices(
return await setup_integration_with_node_fixture(hass, request.param, matter_client)


@pytest.fixture(name="door_lock")
async def door_lock_fixture(
hass: HomeAssistant, matter_client: MagicMock
) -> MatterNode:
"""Fixture for a door lock node."""
return await setup_integration_with_node_fixture(hass, "door_lock", matter_client)


@pytest.fixture(name="smoke_detector")
async def smoke_detector_fixture(
hass: HomeAssistant, matter_client: MagicMock
) -> MatterNode:
"""Fixture for a smoke detector node."""
return await setup_integration_with_node_fixture(
hass, "smoke_detector", matter_client
)
@pytest.fixture
def attributes() -> dict[str, Any]:
"""Return common attributes for all nodes."""
return {}


@pytest.fixture(name="door_lock_with_unbolt")
async def door_lock_with_unbolt_fixture(
hass: HomeAssistant, matter_client: MagicMock
) -> MatterNode:
"""Fixture for a door lock node with unbolt feature."""
return await setup_integration_with_node_fixture(
hass, "door_lock_with_unbolt", matter_client
)


@pytest.fixture(name="eve_contact_sensor_node")
async def eve_contact_sensor_node_fixture(
hass: HomeAssistant, matter_client: MagicMock
@pytest.fixture
async def matter_node(
hass: HomeAssistant,
matter_client: MagicMock,
node_fixture: str,
attributes: dict[str, Any],
) -> MatterNode:
"""Fixture for a contact sensor node."""
"""Fixture for a Matter node."""
return await setup_integration_with_node_fixture(
hass, "eve_contact_sensor", matter_client
hass, node_fixture, matter_client, attributes
)
57 changes: 15 additions & 42 deletions tests/components/matter/test_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@
from homeassistant.core import HomeAssistant
from homeassistant.helpers import device_registry as dr

from .common import create_node_from_fixture, setup_integration_with_node_fixture
from .common import create_node_from_fixture

from tests.common import MockConfigEntry


@pytest.mark.usefixtures("matter_node")
# This tests needs to be adjusted to remove lingering tasks
@pytest.mark.parametrize("expected_lingering_tasks", [True])
@pytest.mark.parametrize(
Expand All @@ -30,17 +31,9 @@
async def test_device_registry_single_node_device(
hass: HomeAssistant,
device_registry: dr.DeviceRegistry,
matter_client: MagicMock,
node_fixture: str,
name: str,
) -> None:
"""Test bridge devices are set up correctly with via_device."""
await setup_integration_with_node_fixture(
hass,
node_fixture,
matter_client,
)

entry = device_registry.async_get_device(
identifiers={
(DOMAIN, "deviceid_00000000000004D2-0000000000000001-MatterNodeDevice")
Expand All @@ -60,20 +53,15 @@ async def test_device_registry_single_node_device(
assert entry.serial_number == "12345678"


@pytest.mark.usefixtures("matter_node")
# This tests needs to be adjusted to remove lingering tasks
@pytest.mark.parametrize("expected_lingering_tasks", [True])
@pytest.mark.parametrize("node_fixture", ["on_off_plugin_unit"])
async def test_device_registry_single_node_device_alt(
hass: HomeAssistant,
device_registry: dr.DeviceRegistry,
matter_client: MagicMock,
) -> None:
"""Test additional device with different attribute values."""
await setup_integration_with_node_fixture(
hass,
"on_off_plugin_unit",
matter_client,
)

entry = device_registry.async_get_device(
identifiers={
(DOMAIN, "deviceid_00000000000004D2-0000000000000001-MatterNodeDevice")
Expand All @@ -89,19 +77,14 @@ async def test_device_registry_single_node_device_alt(
assert entry.serial_number is None


@pytest.mark.usefixtures("matter_node")
@pytest.mark.skip("Waiting for a new test fixture")
@pytest.mark.parametrize("node_fixture", ["fake_bridge_two_light"])
async def test_device_registry_bridge(
hass: HomeAssistant,
device_registry: dr.DeviceRegistry,
matter_client: MagicMock,
) -> None:
"""Test bridge devices are set up correctly with via_device."""
await setup_integration_with_node_fixture(
hass,
"fake_bridge_two_light",
matter_client,
)

# Validate bridge
bridge_entry = device_registry.async_get_device(
identifiers={(DOMAIN, "mock-hub-id")}
Expand Down Expand Up @@ -141,12 +124,12 @@ async def test_device_registry_bridge(
assert device2_entry.sw_version == "1.49.1"


@pytest.mark.usefixtures("integration")
# This tests needs to be adjusted to remove lingering tasks
@pytest.mark.parametrize("expected_lingering_tasks", [True])
async def test_node_added_subscription(
hass: HomeAssistant,
matter_client: MagicMock,
integration: MagicMock,
) -> None:
"""Test subscription to new devices work."""
assert matter_client.subscribe_events.call_count == 5
Expand All @@ -168,30 +151,20 @@ async def test_node_added_subscription(
assert entity_state


@pytest.mark.usefixtures("matter_node")
@pytest.mark.parametrize("node_fixture", ["air_purifier"])
async def test_device_registry_single_node_composed_device(
hass: HomeAssistant,
matter_client: MagicMock,
device_registry: dr.DeviceRegistry,
) -> None:
"""Test that a composed device within a standalone node only creates one HA device entry."""
await setup_integration_with_node_fixture(
hass,
"air_purifier",
matter_client,
)
dev_reg = dr.async_get(hass)
assert len(dev_reg.devices) == 1
assert len(device_registry.devices) == 1


async def test_multi_endpoint_name(
hass: HomeAssistant,
matter_client: MagicMock,
) -> None:
@pytest.mark.usefixtures("matter_node")
@pytest.mark.parametrize("node_fixture", ["multi_endpoint_light"])
async def test_multi_endpoint_name(hass: HomeAssistant) -> None:
"""Test that the entity name gets postfixed if the device has multiple primary endpoints."""
await setup_integration_with_node_fixture(
hass,
"multi_endpoint_light",
matter_client,
)
entity_state = hass.states.get("light.inovelli_light_1")
assert entity_state
assert entity_state.name == "Inovelli Light (1)"
Expand All @@ -200,7 +173,7 @@ async def test_multi_endpoint_name(
assert entity_state.name == "Inovelli Light (6)"


async def test_get_clean_name_() -> None:
async def test_get_clean_name() -> None:
"""Test get_clean_name helper.
Test device names that are assigned to `null`
Expand Down
47 changes: 15 additions & 32 deletions tests/components/matter/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@
from homeassistant.core import HomeAssistant
from homeassistant.helpers import device_registry as dr

from .common import setup_integration_with_node_fixture

from tests.common import MockConfigEntry
from tests.typing import WebSocketGenerator

Expand Down Expand Up @@ -197,21 +195,18 @@ async def test_set_wifi_credentials(
)


@pytest.mark.usefixtures("matter_node")
# This tests needs to be adjusted to remove lingering tasks
@pytest.mark.parametrize("expected_lingering_tasks", [True])
# setup (mock) integration with a random node fixture
@pytest.mark.parametrize("node_fixture", ["onoff_light"])
async def test_node_diagnostics(
hass: HomeAssistant,
hass_ws_client: WebSocketGenerator,
device_registry: dr.DeviceRegistry,
matter_client: MagicMock,
) -> None:
"""Test the node diagnostics command."""
# setup (mock) integration with a random node fixture
await setup_integration_with_node_fixture(
hass,
"onoff_light",
matter_client,
)
# get the device registry entry for the mocked node
entry = device_registry.async_get_device(
identifiers={
Expand Down Expand Up @@ -271,21 +266,18 @@ async def test_node_diagnostics(
assert msg["error"]["code"] == ERROR_NODE_NOT_FOUND


@pytest.mark.usefixtures("matter_node")
# This tests needs to be adjusted to remove lingering tasks
@pytest.mark.parametrize("expected_lingering_tasks", [True])
# setup (mock) integration with a random node fixture
@pytest.mark.parametrize("node_fixture", ["onoff_light"])
async def test_ping_node(
hass: HomeAssistant,
hass_ws_client: WebSocketGenerator,
device_registry: dr.DeviceRegistry,
matter_client: MagicMock,
) -> None:
"""Test the ping_node command."""
# setup (mock) integration with a random node fixture
await setup_integration_with_node_fixture(
hass,
"onoff_light",
matter_client,
)
# get the device registry entry for the mocked node
entry = device_registry.async_get_device(
identifiers={
Expand Down Expand Up @@ -331,21 +323,18 @@ async def test_ping_node(
assert msg["error"]["code"] == ERROR_NODE_NOT_FOUND


@pytest.mark.usefixtures("matter_node")
# This tests needs to be adjusted to remove lingering tasks
@pytest.mark.parametrize("expected_lingering_tasks", [True])
# setup (mock) integration with a random node fixture
@pytest.mark.parametrize("node_fixture", ["onoff_light"])
async def test_open_commissioning_window(
hass: HomeAssistant,
hass_ws_client: WebSocketGenerator,
device_registry: dr.DeviceRegistry,
matter_client: MagicMock,
) -> None:
"""Test the open_commissioning_window command."""
# setup (mock) integration with a random node fixture
await setup_integration_with_node_fixture(
hass,
"onoff_light",
matter_client,
)
# get the device registry entry for the mocked node
entry = device_registry.async_get_device(
identifiers={
Expand Down Expand Up @@ -397,21 +386,18 @@ async def test_open_commissioning_window(
assert msg["error"]["code"] == ERROR_NODE_NOT_FOUND


@pytest.mark.usefixtures("matter_node")
# This tests needs to be adjusted to remove lingering tasks
@pytest.mark.parametrize("expected_lingering_tasks", [True])
# setup (mock) integration with a random node fixture
@pytest.mark.parametrize("node_fixture", ["onoff_light"])
async def test_remove_matter_fabric(
hass: HomeAssistant,
hass_ws_client: WebSocketGenerator,
device_registry: dr.DeviceRegistry,
matter_client: MagicMock,
) -> None:
"""Test the remove_matter_fabric command."""
# setup (mock) integration with a random node fixture
await setup_integration_with_node_fixture(
hass,
"onoff_light",
matter_client,
)
# get the device registry entry for the mocked node
entry = device_registry.async_get_device(
identifiers={
Expand Down Expand Up @@ -453,21 +439,18 @@ async def test_remove_matter_fabric(
assert msg["error"]["code"] == ERROR_NODE_NOT_FOUND


@pytest.mark.usefixtures("matter_node")
# This tests needs to be adjusted to remove lingering tasks
@pytest.mark.parametrize("expected_lingering_tasks", [True])
# setup (mock) integration with a random node fixture
@pytest.mark.parametrize("node_fixture", ["onoff_light"])
async def test_interview_node(
hass: HomeAssistant,
hass_ws_client: WebSocketGenerator,
device_registry: dr.DeviceRegistry,
matter_client: MagicMock,
) -> None:
"""Test the interview_node command."""
# setup (mock) integration with a random node fixture
await setup_integration_with_node_fixture(
hass,
"onoff_light",
matter_client,
)
# get the device registry entry for the mocked node
entry = device_registry.async_get_device(
identifiers={
Expand Down
Loading

0 comments on commit 6e12726

Please sign in to comment.