|
| 1 | +"""Tests for the DoNotDisturbTrait class.""" |
| 2 | + |
| 3 | +from unittest.mock import AsyncMock |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | +from roborock.containers import DnDTimer |
| 8 | +from roborock.devices.traits.dnd import DoNotDisturbTrait |
| 9 | +from roborock.devices.v1_rpc_channel import V1RpcChannel |
| 10 | +from roborock.roborock_typing import RoborockCommand |
| 11 | + |
| 12 | + |
| 13 | +@pytest.fixture |
| 14 | +def mock_rpc_channel() -> AsyncMock: |
| 15 | + """Create a mock RPC channel.""" |
| 16 | + mock_channel = AsyncMock(spec=V1RpcChannel) |
| 17 | + # Ensure send_command is an AsyncMock that returns awaitable coroutines |
| 18 | + mock_channel.send_command = AsyncMock() |
| 19 | + return mock_channel |
| 20 | + |
| 21 | + |
| 22 | +@pytest.fixture |
| 23 | +def dnd_trait(mock_rpc_channel: AsyncMock) -> DoNotDisturbTrait: |
| 24 | + """Create a DoNotDisturbTrait instance with mocked dependencies.""" |
| 25 | + return DoNotDisturbTrait(mock_rpc_channel) |
| 26 | + |
| 27 | + |
| 28 | +@pytest.fixture |
| 29 | +def sample_dnd_timer() -> DnDTimer: |
| 30 | + """Create a sample DnDTimer for testing.""" |
| 31 | + return DnDTimer( |
| 32 | + start_hour=22, |
| 33 | + start_minute=0, |
| 34 | + end_hour=8, |
| 35 | + end_minute=0, |
| 36 | + enabled=1, |
| 37 | + ) |
| 38 | + |
| 39 | + |
| 40 | +def test_trait_name(dnd_trait: DoNotDisturbTrait) -> None: |
| 41 | + """Test that the trait has the correct name.""" |
| 42 | + assert dnd_trait.name == "do_not_disturb" |
| 43 | + |
| 44 | + |
| 45 | +async def test_get_dnd_timer_success( |
| 46 | + dnd_trait: DoNotDisturbTrait, mock_rpc_channel: AsyncMock, sample_dnd_timer: DnDTimer |
| 47 | +) -> None: |
| 48 | + """Test successfully getting DnD timer settings.""" |
| 49 | + # Setup mock to return the sample DnD timer |
| 50 | + mock_rpc_channel.send_command.return_value = sample_dnd_timer |
| 51 | + |
| 52 | + # Call the method |
| 53 | + result = await dnd_trait.get_dnd_timer() |
| 54 | + |
| 55 | + # Verify the result |
| 56 | + assert result == sample_dnd_timer |
| 57 | + assert result.start_hour == 22 |
| 58 | + assert result.start_minute == 0 |
| 59 | + assert result.end_hour == 8 |
| 60 | + assert result.end_minute == 0 |
| 61 | + assert result.enabled == 1 |
| 62 | + |
| 63 | + # Verify the RPC call was made correctly |
| 64 | + mock_rpc_channel.send_command.assert_called_once_with(RoborockCommand.GET_DND_TIMER, response_type=DnDTimer) |
| 65 | + |
| 66 | + |
| 67 | +async def test_get_dnd_timer_disabled(dnd_trait: DoNotDisturbTrait, mock_rpc_channel: AsyncMock) -> None: |
| 68 | + """Test getting DnD timer when it's disabled.""" |
| 69 | + disabled_timer = DnDTimer( |
| 70 | + start_hour=22, |
| 71 | + start_minute=0, |
| 72 | + end_hour=8, |
| 73 | + end_minute=0, |
| 74 | + enabled=0, |
| 75 | + ) |
| 76 | + mock_rpc_channel.send_command.return_value = disabled_timer |
| 77 | + |
| 78 | + result = await dnd_trait.get_dnd_timer() |
| 79 | + |
| 80 | + assert result.enabled == 0 |
| 81 | + mock_rpc_channel.send_command.assert_called_once_with(RoborockCommand.GET_DND_TIMER, response_type=DnDTimer) |
| 82 | + |
| 83 | + |
| 84 | +async def test_set_dnd_timer_success( |
| 85 | + dnd_trait: DoNotDisturbTrait, mock_rpc_channel: AsyncMock, sample_dnd_timer: DnDTimer |
| 86 | +) -> None: |
| 87 | + """Test successfully setting DnD timer settings.""" |
| 88 | + # Call the method |
| 89 | + await dnd_trait.set_dnd_timer(sample_dnd_timer) |
| 90 | + |
| 91 | + # Verify the RPC call was made correctly with dataclass converted to dict |
| 92 | + |
| 93 | + expected_params = { |
| 94 | + "startHour": 22, |
| 95 | + "startMinute": 0, |
| 96 | + "endHour": 8, |
| 97 | + "endMinute": 0, |
| 98 | + "enabled": 1, |
| 99 | + } |
| 100 | + mock_rpc_channel.send_command.assert_called_once_with(RoborockCommand.SET_DND_TIMER, params=expected_params) |
| 101 | + |
| 102 | + |
| 103 | +async def test_clear_dnd_timer_success(dnd_trait: DoNotDisturbTrait, mock_rpc_channel: AsyncMock) -> None: |
| 104 | + """Test successfully clearing DnD timer settings.""" |
| 105 | + # Call the method |
| 106 | + await dnd_trait.clear_dnd_timer() |
| 107 | + |
| 108 | + # Verify the RPC call was made correctly |
| 109 | + mock_rpc_channel.send_command.assert_called_once_with(RoborockCommand.CLOSE_DND_TIMER) |
| 110 | + |
| 111 | + |
| 112 | +async def test_get_dnd_timer_propagates_exception(dnd_trait: DoNotDisturbTrait, mock_rpc_channel: AsyncMock) -> None: |
| 113 | + """Test that exceptions from RPC channel are propagated in get_dnd_timer.""" |
| 114 | + from roborock.exceptions import RoborockException |
| 115 | + |
| 116 | + # Setup mock to raise an exception |
| 117 | + mock_rpc_channel.send_command.side_effect = RoborockException("Communication error") |
| 118 | + |
| 119 | + # Verify the exception is propagated |
| 120 | + with pytest.raises(RoborockException, match="Communication error"): |
| 121 | + await dnd_trait.get_dnd_timer() |
| 122 | + |
| 123 | + |
| 124 | +async def test_set_dnd_timer_propagates_exception( |
| 125 | + dnd_trait: DoNotDisturbTrait, mock_rpc_channel: AsyncMock, sample_dnd_timer: DnDTimer |
| 126 | +) -> None: |
| 127 | + """Test that exceptions from RPC channel are propagated in set_dnd_timer.""" |
| 128 | + from roborock.exceptions import RoborockException |
| 129 | + |
| 130 | + # Setup mock to raise an exception |
| 131 | + mock_rpc_channel.send_command.side_effect = RoborockException("Communication error") |
| 132 | + |
| 133 | + # Verify the exception is propagated |
| 134 | + with pytest.raises(RoborockException, match="Communication error"): |
| 135 | + await dnd_trait.set_dnd_timer(sample_dnd_timer) |
| 136 | + |
| 137 | + |
| 138 | +async def test_clear_dnd_timer_propagates_exception(dnd_trait: DoNotDisturbTrait, mock_rpc_channel: AsyncMock) -> None: |
| 139 | + """Test that exceptions from RPC channel are propagated in clear_dnd_timer.""" |
| 140 | + from roborock.exceptions import RoborockException |
| 141 | + |
| 142 | + # Setup mock to raise an exception |
| 143 | + mock_rpc_channel.send_command.side_effect = RoborockException("Communication error") |
| 144 | + |
| 145 | + # Verify the exception is propagated |
| 146 | + with pytest.raises(RoborockException, match="Communication error"): |
| 147 | + await dnd_trait.clear_dnd_timer() |
0 commit comments