File: test_asetek.py

package info (click to toggle)
liquidctl 1.16.0-1
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 3,452 kB
  • sloc: python: 15,304; sh: 712; xml: 84; makefile: 4
file content (171 lines) | stat: -rw-r--r-- 5,761 bytes parent folder | download | duplicates (2)
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import pytest
from _testutils import MockPyusbDevice, MockRuntimeStorage

from collections import deque

from liquidctl.driver.asetek import Modern690Lc, Legacy690Lc, Hydro690Lc


@pytest.fixture
def mockModern690LcDevice():
    device = MockPyusbDevice()
    dev = Modern690Lc(device, 'Mock Modern 690LC')

    dev.connect()
    return dev


@pytest.fixture
def mockLegacy690LcDevice():
    device = MockPyusbDevice(vendor_id=0xffff, product_id=0xb200, bus=1, address=2)
    dev = Legacy690Lc(device, 'Mock Legacy 690LC')

    runtime_storage = MockRuntimeStorage(key_prefixes=['testing'])
    runtime_storage.store('leds_enabled', 0)

    dev.connect(runtime_storage=runtime_storage)
    return dev


def test_modern690Lc_device_not_totally_broken(mockModern690LcDevice):
    """A few reasonable example calls do not raise exceptions."""
    dev = mockModern690LcDevice

    dev.initialize()
    dev.get_status()
    dev.set_color(channel='led', mode='blinking', colors=iter([[3, 2, 1]]),
                  time_per_color=3, time_off=1, alert_threshold=42,
                  alert_color=[90, 80, 10])
    dev.set_color(channel='led', mode='rainbow', colors=[], speed=5)
    dev.set_speed_profile(channel='fan',
                          profile=iter([(20, 20), (30, 50), (40, 100)]))
    dev.set_fixed_speed(channel='pump', duty=50)


def test_modern690Lc_device_connect(mockModern690LcDevice):
    def mock_open():
        nonlocal opened
        opened = True
    mockModern690LcDevice.device.open = mock_open
    opened = False

    with mockModern690LcDevice.connect() as cm:
        assert cm == mockModern690LcDevice
        assert opened


def test_modern690Lc_device_begin_transaction(mockModern690LcDevice):
    mockModern690LcDevice.device._reset_sent()

    mockModern690LcDevice.get_status()

    (begin, _) = mockModern690LcDevice.device._sent_xfers
    xfer_type, bmRequestType, bRequest, wValue, wIndex, datalen = begin
    assert xfer_type == 'ctrl_transfer'
    assert bRequest == 2
    assert bmRequestType == 0x40
    assert wValue == 1
    assert wIndex == 0
    assert datalen is None


def test_modern690Lc_initialize_can_persist_settings(mockModern690LcDevice):
    dev = mockModern690LcDevice
    dev.device._reset_sent()

    dev.initialize(non_volatile=True)

    _begin, (cmd_msgtype, cmd_ep, cmd_data) = dev.device._sent_xfers[-2:]
    assert cmd_msgtype == 'write'
    assert cmd_ep == 2
    assert cmd_data == [0x21]


def test_modern690Lc_set_color_can_persist_settings(mockModern690LcDevice):
    dev = mockModern690LcDevice
    dev.device._reset_sent()

    dev.set_color(channel='led', mode='rainbow', colors=[], non_volatile=True)

    _begin, (cmd_msgtype, cmd_ep, cmd_data) = dev.device._sent_xfers[-2:]
    assert cmd_msgtype == 'write'
    assert cmd_ep == 2
    assert cmd_data == [0x21]


def test_modern690Lc_set_speed_profile_can_persist_settings(mockModern690LcDevice):
    dev = mockModern690LcDevice
    dev.device._reset_sent()

    dev.set_speed_profile(channel='fan', profile=[(20, 20), (30, 50), (40, 100)],
                          non_volatile=True)

    _begin, (cmd_msgtype, cmd_ep, cmd_data) = dev.device._sent_xfers[-2:]
    assert cmd_msgtype == 'write'
    assert cmd_ep == 2
    assert cmd_data == [0x21]


def test_modern690Lc_set_fixed_speed_can_persist_settings(mockModern690LcDevice):
    dev = mockModern690LcDevice
    dev.device._reset_sent()

    dev.set_fixed_speed(channel='pump', duty=50, non_volatile=True)

    _begin, (cmd_msgtype, cmd_ep, cmd_data) = dev.device._sent_xfers[-2:]
    assert cmd_msgtype == 'write'
    assert cmd_ep == 2
    assert cmd_data == [0x21]


def test_legacy690Lc_device_not_totally_broken(mockLegacy690LcDevice):
    """A few reasonable example calls do not raise exceptions."""
    dev = mockLegacy690LcDevice

    dev.initialize()
    status = dev.get_status()
    dev.set_color(channel='led', mode='blinking', colors=iter([[3, 2, 1]]),
                  time_per_color=3, time_off=1, alert_threshold=42,
                  alert_color=[90, 80, 10])
    dev.set_fixed_speed(channel='fan', duty=80)
    dev.set_fixed_speed(channel='pump', duty=50)


def test_legacy690Lc_device_matches_leviathan_updates(mockLegacy690LcDevice):
    dev = mockLegacy690LcDevice

    dev.initialize()
    dev.set_fixed_speed(channel='pump', duty=50)

    dev.device._reset_sent()
    dev.set_color(channel='led', mode='fading', colors=[[0, 0, 255], [0, 255, 0]],
                  time_per_color=1, alert_threshold=60, alert_color=[0, 0, 0])
    _begin, (color_msgtype, color_ep, color_data) = dev.device._sent_xfers
    assert color_msgtype == 'write'
    assert color_ep == 2
    assert color_data[0:12] == [0x10, 0, 0, 255, 0, 255, 0, 0, 0, 0, 0x3c, 1]

    dev.device._reset_sent()
    dev.set_fixed_speed(channel='fan', duty=50)
    _begin, pump_message, fan_message = dev.device._sent_xfers
    assert pump_message == ('write', 2, [0x13, 50])
    assert fan_message == ('write', 2, [0x12, 50])


def test_legacy690Lc_device_initialize_warns_on_non_volatile(mockLegacy690LcDevice, caplog):
    mockLegacy690LcDevice.initialize(non_volatile=True)

    assert 'device does not support non-volatile' in caplog.text


def test_legacy690Lc_device_set_color_warns_on_non_volatile(mockLegacy690LcDevice, caplog):
    mockLegacy690LcDevice.set_color(channel='led', mode='blinking',
                                    colors=iter([[3, 2, 1]]), non_volatile=True)

    assert 'device does not support non-volatile' in caplog.text


def test_legacy690Lc_device_set_fixed_speed_warns_on_non_volatile(mockLegacy690LcDevice, caplog):
    mockLegacy690LcDevice.set_fixed_speed(channel='fan', duty=80, non_volatile=True)

    assert 'device does not support non-volatile' in caplog.text