File: test_led.py

package info (click to toggle)
python-asusrouter 1.21.3-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,856 kB
  • sloc: python: 20,497; makefile: 3
file content (74 lines) | stat: -rw-r--r-- 1,875 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
"""Tests for the led module."""

from unittest.mock import AsyncMock, MagicMock, patch

import pytest

from asusrouter.modules.endpoint import Endpoint
from asusrouter.modules.identity import AsusDevice
from asusrouter.modules.led import AsusLED, keep_state, set_state


@pytest.mark.asyncio
async def test_set_state() -> None:
    """Test set_state."""

    # Arrange
    callback = AsyncMock(return_value=True)
    state = AsusLED.ON
    expect_modify = False

    # Act
    result = await set_state(callback, state, expect_modify=expect_modify)

    # Assert
    callback.assert_called_once_with(
        service="start_ctrl_led",
        arguments={"led_val": state.value},
        apply=True,
        expect_modify=expect_modify,
    )
    assert result is True


@pytest.mark.asyncio
@pytest.mark.parametrize(
    ("identity", "state", "expected", "set_state_calls"),
    [
        (None, AsusLED.OFF, False, 0),
        (MagicMock(spec=AsusDevice, endpoints={}), AsusLED.OFF, False, 0),
        (
            MagicMock(spec=AsusDevice, endpoints={Endpoint.SYSINFO: None}),
            AsusLED.ON,
            False,
            0,
        ),
        (
            MagicMock(
                spec=AsusDevice, endpoints={Endpoint.SYSINFO: "sysinfo"}
            ),
            AsusLED.OFF,
            True,
            2,
        ),
    ],
)
async def test_keep_state(
    identity: AsusDevice | None,
    state: AsusLED,
    expected: bool,
    set_state_calls: int,
) -> None:
    """Test keep_state."""

    # Arrange
    callback = AsyncMock(return_value=True)
    with patch(
        "asusrouter.modules.led.set_state", new_callable=AsyncMock
    ) as mock_set_state:
        # Act
        result = await keep_state(callback, state, identity=identity)

        # Assert
        assert result is expected
        assert mock_set_state.call_count == set_state_calls