forked from arvydas/blinkstick-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_blinkstick.py
More file actions
311 lines (259 loc) · 10.1 KB
/
test_blinkstick.py
File metadata and controls
311 lines (259 loc) · 10.1 KB
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
from unittest.mock import MagicMock
import pytest
from blinkstick.colors import ColorFormat
from blinkstick.enums import BlinkStickVariant
from blinkstick.clients.blinkstick import BlinkStick
from pytest_mock import MockFixture
from blinkstick.exceptions import NotConnected
from tests.conftest import make_blinkstick
def test_instantiate():
"""Test that we can instantiate a BlinkStick object."""
bs = BlinkStick()
assert bs is not None
def test_all_methods_require_backend(make_blinkstick):
"""Test that all methods require a backend."""
bs = make_blinkstick()
bs.backend = None # noqa
class_methods = (
method
for method in dir(BlinkStick)
if callable(getattr(bs, method)) and not method.startswith("__")
)
for method_name in class_methods:
method = getattr(bs, method_name)
with pytest.raises(NotConnected):
method()
@pytest.mark.parametrize(
"serial, version_attribute, expected_variant, expected_variant_value",
[
("BS12345-1.0", 0x0000, BlinkStickVariant.BLINKSTICK, 1),
("BS12345-2.0", 0x0000, BlinkStickVariant.BLINKSTICK_PRO, 2),
(
"BS12345-3.0",
0x200,
BlinkStickVariant.BLINKSTICK_SQUARE,
4,
), # major version 3, version attribute 0x200 is BlinkStickSquare
(
"BS12345-3.0",
0x201,
BlinkStickVariant.BLINKSTICK_STRIP,
3,
), # major version 3 is BlinkStickStrip
("BS12345-3.0", 0x202, BlinkStickVariant.BLINKSTICK_NANO, 5),
("BS12345-3.0", 0x203, BlinkStickVariant.BLINKSTICK_FLEX, 6),
("BS12345-4.0", 0x0000, BlinkStickVariant.UNKNOWN, 0),
("BS12345-3.0", 0x9999, BlinkStickVariant.UNKNOWN, 0),
("BS12345-0.0", 0x0000, BlinkStickVariant.UNKNOWN, 0),
],
ids=[
"v1==BlinkStick",
"v2==BlinkStickPro",
"v3,0x200==BlinkStickSquare",
"v3,0x201==BlinkStickStrip",
"v3,0x202==BlinkStickNano",
"v3,0x203==BlinkStickFlex",
"v4==Unknown",
"v3,Unknown==Unknown",
"v0,0==Unknown",
],
)
def test_get_variant(
make_blinkstick, serial, version_attribute, expected_variant, expected_variant_value
):
bs = make_blinkstick()
synthesised_variant = BlinkStickVariant.from_version_attrs(
int(serial[-3]), version_attribute
)
bs.backend.get_variant = MagicMock(return_value=synthesised_variant)
assert bs.get_variant() == expected_variant
assert bs.get_variant().value == expected_variant_value
@pytest.mark.parametrize(
"expected_variant, expected_name",
[
(BlinkStickVariant.BLINKSTICK, "BlinkStick"),
(BlinkStickVariant.BLINKSTICK_PRO, "BlinkStick Pro"),
(BlinkStickVariant.BLINKSTICK_STRIP, "BlinkStick Strip"),
(BlinkStickVariant.BLINKSTICK_SQUARE, "BlinkStick Square"),
(BlinkStickVariant.BLINKSTICK_NANO, "BlinkStick Nano"),
(BlinkStickVariant.BLINKSTICK_FLEX, "BlinkStick Flex"),
(BlinkStickVariant.UNKNOWN, "Unknown"),
],
ids=[
"1==BlinkStick",
"2==BlinkStickPro",
"3==BlinkStickStrip",
"4==BlinkStickSquare",
"5==BlinkStickNano",
"6==BlinkStickFlex",
"0==Unknown",
],
)
def test_get_variant_string(make_blinkstick, expected_variant, expected_name):
"""Test get_variant method for version 0 returns BlinkStick.UNKNOWN (0)"""
bs = make_blinkstick()
bs.get_variant = MagicMock(return_value=expected_variant)
assert bs.get_variant_string() == expected_name
def test_get_color_rgb_color_format(mocker: MockFixture, make_blinkstick):
"""Test get_color with color_format='rgb'. We expect it to return the color in RGB format."""
bs = make_blinkstick()
mock_get_color_rgb = mocker.Mock(return_value=(255, 0, 0))
bs._get_color_rgb = mock_get_color_rgb
assert bs.get_color() == (255, 0, 0)
assert mock_get_color_rgb.call_count == 1
def test_get_color_hex_color_format(mocker: MockFixture, make_blinkstick):
"""Test get_color with color_format='hex'. We expect it to return the color in hex format."""
bs = make_blinkstick()
mock_get_color_hex = mocker.Mock(return_value="#ff0000")
bs._get_color_hex = mock_get_color_hex
assert bs.get_color(color_format="hex") == "#ff0000"
assert mock_get_color_hex.call_count == 1
def test_get_color_invalid_color_format(mocker: MockFixture, make_blinkstick):
"""Test get_color with invalid color_format. We expect it not to raise an exception, but to default to RGB."""
bs = make_blinkstick()
mock_get_color_rgb = mocker.Mock(return_value=(255, 0, 0))
bs._get_color_rgb = mock_get_color_rgb
bs.get_color(color_format="invalid_format")
assert mock_get_color_rgb.call_count == 1
def test_max_rgb_value_default(make_blinkstick):
"""Test that the default max_rgb_value is 255."""
bs = make_blinkstick()
assert bs.get_max_rgb_value() == 255
def test_max_rgb_value_not_class_attribute(make_blinkstick):
"""Test that the max_rgb_value is not a class attribute."""
bs = make_blinkstick()
assert not hasattr(BlinkStick, "max_rgb_value")
assert hasattr(bs, "max_rgb_value")
def test_set_and_get_max_rgb_value(make_blinkstick):
"""Test that we can set and get the max_rgb_value."""
# Create multiple instances of BlinkStick using the fixture
bs = make_blinkstick()
# Set different max_rgb_value for each instance
bs.set_max_rgb_value(100)
# Assert that each instance has its own max_rgb_value
assert bs.get_max_rgb_value() == 100
# Change the max_rgb_value again to ensure independence
bs.set_max_rgb_value(150)
# Assert the new values
assert bs.get_max_rgb_value() == 150
def test_set_max_rgb_value_bounds(make_blinkstick):
"""Test that set_max_rgb_value performs bounds checking."""
bs = make_blinkstick()
# Test setting a value within bounds
bs.set_max_rgb_value(100)
assert bs.get_max_rgb_value() == 100
# Test setting a value below the lower bound
bs.set_max_rgb_value(-1)
assert bs.get_max_rgb_value() == 0
# Test setting a value above the upper bound
bs.set_max_rgb_value(256)
assert bs.get_max_rgb_value() == 255
def test_set_max_rgb_value_type_checking(make_blinkstick):
"""Test that set_max_rgb_value performs type checking and coercion."""
bs = make_blinkstick()
# Test setting a valid integer value
bs.set_max_rgb_value(100)
assert bs.get_max_rgb_value() == 100
# Test setting a value that can be coerced to an integer
bs.set_max_rgb_value("150")
assert bs.get_max_rgb_value() == 150
# Test setting a value that cannot be coerced to an integer
with pytest.raises(ValueError):
bs.set_max_rgb_value("invalid")
# Test setting a float value
bs.set_max_rgb_value(100.5)
assert bs.get_max_rgb_value() == 100
def test_inverse_default(make_blinkstick):
"""Test that the default inverse is False."""
bs = make_blinkstick()
assert bs.get_inverse() == False
def test_inverse_not_class_attribute(make_blinkstick):
"""Test that the inverse is not a class attribute."""
bs = make_blinkstick()
assert not hasattr(BlinkStick, "inverse")
assert hasattr(bs, "inverse")
@pytest.mark.parametrize(
"input_value, expected_result",
[
pytest.param(True, True, id="True==True"),
pytest.param(False, False, id="False==False"),
],
)
def test_inverse_set_and_get(make_blinkstick, input_value, expected_result):
"""Test that we can set and get the inverse."""
bs = make_blinkstick()
bs.set_inverse(input_value)
assert bs.get_inverse() == expected_result
@pytest.mark.parametrize(
"input_value, expected_result",
[
pytest.param(True, True, id="True==True"),
pytest.param("True", True, id="StringTrue==True"),
pytest.param(1.0, True, id="1.0==True"),
pytest.param(0, False, id="0==False"),
pytest.param("False", False, id="StringFalse==False"),
pytest.param(False, False, id="False==False"),
pytest.param(0.0, False, id="0.0==False"),
pytest.param("", False, id="EmptyString==False"),
pytest.param([], False, id="EmptyList==False"),
pytest.param({}, False, id="EmptyDict==False"),
pytest.param(None, False, id="None==False"),
],
)
def test_set_inverse_type_checking(make_blinkstick, input_value, expected_result):
"""Test that set_inverse performs type checking and coercion."""
bs = make_blinkstick()
bs.set_inverse(input_value)
assert bs.get_inverse() == expected_result
@pytest.mark.parametrize(
"color_mode, ctrl_transfer_bytes, color, inverse, expected_color",
[
pytest.param(
ColorFormat.RGB,
(0, 255, 0, 0),
(255, 0, 0),
False,
(255, 0, 0),
id="RGB, NoInverse",
),
pytest.param(
ColorFormat.HEX,
(0, 255, 0, 0),
"#ff0000",
False,
"#ff0000",
id="Hex, NoInverse",
),
pytest.param(
ColorFormat.RGB,
(0, 255, 0, 0),
(255, 0, 0),
True,
(0, 255, 255),
id="RGB, Inverse",
),
pytest.param(
ColorFormat.HEX,
(0, 255, 0, 0),
"#ff0000",
True,
"#00ffff",
id="Hex, Inverse",
),
],
)
def test_inverse_correctly_inverts_rgb_color(
make_blinkstick, color_mode, ctrl_transfer_bytes, color, inverse, expected_color
):
"""Test that the color is correctly inverted when the inverse flag is set."""
bs = make_blinkstick()
# mock the backend control_transfer method to return the 3 bytes of the color
bs.backend.control_transfer = MagicMock(return_value=ctrl_transfer_bytes)
bs.set_inverse(inverse)
assert bs.get_color(color_mode=color_mode) == expected_color
def test_inverse_does_not_affect_max_rgb_value(make_blinkstick):
"""Test that the inverse flag does not affect the max_rgb_value."""
bs = make_blinkstick()
bs.set_max_rgb_value(100)
bs.set_inverse(True)
assert bs.get_max_rgb_value() == 100