Update roborock tests to patch client before test setup (#136587)

This commit is contained in:
Allen Porter 2025-01-28 01:54:36 -08:00 committed by GitHub
parent 1ad2598c6f
commit b1fec51e2f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 53 additions and 44 deletions

View File

@ -2,7 +2,8 @@
from collections.abc import Generator
from copy import deepcopy
from unittest.mock import patch
from typing import Any
from unittest.mock import Mock, patch
import pytest
from roborock import RoborockCategory, RoomMapping
@ -139,6 +140,22 @@ def bypass_api_fixture() -> None:
yield
@pytest.fixture(name="send_message_side_effect")
def send_message_side_effect_fixture() -> Any:
"""Fixture to return a side effect for the send_message method."""
return None
@pytest.fixture(name="mock_send_message")
def mock_send_message_fixture(send_message_side_effect: Any) -> Mock:
"""Fixture to mock the send_message method."""
with patch(
"homeassistant.components.roborock.coordinator.RoborockLocalClientV1._send_command",
side_effect=send_message_side_effect,
) as mock_send_message:
yield mock_send_message
@pytest.fixture
def bypass_api_fixture_v1_only(bypass_api_fixture) -> None:
"""Bypass api for tests that require only having v1 devices."""

View File

@ -1,6 +1,6 @@
"""Test Roborock Switch platform."""
from unittest.mock import patch
from unittest.mock import Mock
import pytest
import roborock
@ -29,6 +29,7 @@ def platforms() -> list[Platform]:
)
async def test_update_success(
hass: HomeAssistant,
mock_send_message: Mock,
bypass_api_fixture,
setup_entry: MockConfigEntry,
entity_id: str,
@ -36,27 +37,22 @@ async def test_update_success(
"""Test turning switch entities on and off."""
# Ensure that the entity exist, as these test can pass even if there is no entity.
assert hass.states.get(entity_id) is not None
with patch(
"homeassistant.components.roborock.coordinator.RoborockLocalClientV1._send_command"
) as mock_send_message:
await hass.services.async_call(
"switch",
SERVICE_TURN_ON,
service_data=None,
blocking=True,
target={"entity_id": entity_id},
)
await hass.services.async_call(
"switch",
SERVICE_TURN_ON,
service_data=None,
blocking=True,
target={"entity_id": entity_id},
)
assert mock_send_message.assert_called_once
with patch(
"homeassistant.components.roborock.coordinator.RoborockLocalClientV1.send_message"
) as mock_send_message:
await hass.services.async_call(
"switch",
SERVICE_TURN_OFF,
service_data=None,
blocking=True,
target={"entity_id": entity_id},
)
mock_send_message.reset_mock()
await hass.services.async_call(
"switch",
SERVICE_TURN_OFF,
service_data=None,
blocking=True,
target={"entity_id": entity_id},
)
assert mock_send_message.assert_called_once
@ -67,8 +63,12 @@ async def test_update_success(
("switch.roborock_s7_maxv_status_indicator_light", SERVICE_TURN_OFF),
],
)
@pytest.mark.parametrize(
"send_message_side_effect", [roborock.exceptions.RoborockTimeout]
)
async def test_update_failed(
hass: HomeAssistant,
mock_send_message: Mock,
bypass_api_fixture,
setup_entry: MockConfigEntry,
entity_id: str,
@ -78,10 +78,6 @@ async def test_update_failed(
# Ensure that the entity exist, as these test can pass even if there is no entity.
assert hass.states.get(entity_id) is not None
with (
patch(
"homeassistant.components.roborock.coordinator.RoborockLocalClientV1._send_command",
side_effect=roborock.exceptions.RoborockTimeout,
) as mock_send_message,
pytest.raises(HomeAssistantError, match="Failed to update Roborock options"),
):
await hass.services.async_call(

View File

@ -1,7 +1,7 @@
"""Test Roborock Time platform."""
from datetime import time
from unittest.mock import patch
from unittest.mock import Mock
import pytest
import roborock
@ -29,6 +29,7 @@ def platforms() -> list[Platform]:
)
async def test_update_success(
hass: HomeAssistant,
mock_send_message: Mock,
bypass_api_fixture,
setup_entry: MockConfigEntry,
entity_id: str,
@ -36,16 +37,13 @@ async def test_update_success(
"""Test turning switch entities on and off."""
# Ensure that the entity exist, as these test can pass even if there is no entity.
assert hass.states.get(entity_id) is not None
with patch(
"homeassistant.components.roborock.coordinator.RoborockLocalClientV1._send_command"
) as mock_send_message:
await hass.services.async_call(
"time",
SERVICE_SET_VALUE,
service_data={"time": time(hour=1, minute=1)},
blocking=True,
target={"entity_id": entity_id},
)
await hass.services.async_call(
"time",
SERVICE_SET_VALUE,
service_data={"time": time(hour=1, minute=1)},
blocking=True,
target={"entity_id": entity_id},
)
assert mock_send_message.assert_called_once
@ -55,8 +53,12 @@ async def test_update_success(
("time.roborock_s7_maxv_do_not_disturb_begin"),
],
)
@pytest.mark.parametrize(
"send_message_side_effect", [roborock.exceptions.RoborockTimeout]
)
async def test_update_failure(
hass: HomeAssistant,
mock_send_message: Mock,
bypass_api_fixture,
setup_entry: MockConfigEntry,
entity_id: str,
@ -64,13 +66,7 @@ async def test_update_failure(
"""Test turning switch entities on and off."""
# Ensure that the entity exist, as these test can pass even if there is no entity.
assert hass.states.get(entity_id) is not None
with (
patch(
"homeassistant.components.roborock.coordinator.RoborockLocalClientV1._send_command",
side_effect=roborock.exceptions.RoborockTimeout,
) as mock_send_message,
pytest.raises(HomeAssistantError, match="Failed to update Roborock options"),
):
with pytest.raises(HomeAssistantError, match="Failed to update Roborock options"):
await hass.services.async_call(
"time",
SERVICE_SET_VALUE,