Skip to content

Commit 9fbe9b6

Browse files
committed
Tests: Increase test coverage to 100%
Signed-off-by: David Greaves <[email protected]>
1 parent fde83b2 commit 9fbe9b6

File tree

2 files changed

+166
-2
lines changed

2 files changed

+166
-2
lines changed

library/tests/conftest.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ def ws2811_led_get(ch, n):
3939
@pytest.fixture(scope='function', autouse=False)
4040
def _rpi_ws281x():
4141
_mock_rpi_ws281x.ws2811_init.return_value = 0
42+
_mock_rpi_ws281x.ws2811_render.return_value = 0
4243
sys.modules['_rpi_ws281x'] = _mock_rpi_ws281x
4344

4445
yield _mock_rpi_ws281x

library/tests/test_setup.py

Lines changed: 165 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,21 @@ def strip(_rpi_ws281x):
55
from rpi_ws281x import PixelStrip
66
strip = PixelStrip(10, 20)
77
strip.begin()
8+
strip.off()
89
yield strip
910

1011
def test_setup(_rpi_ws281x):
1112
from rpi_ws281x import PixelStrip
1213
strip = PixelStrip(10, 20)
1314
strip.begin()
1415

16+
def test_setup_compat(_rpi_ws281x):
17+
from rpi_ws281x import PixelStrip
18+
strip = PixelStrip(10, 20, strip_type=[1.0]*256)
19+
strip.begin()
20+
_rpi_ws281x.ws2811_channel_t_strip_type_set.assert_called_with(strip._channel,
21+
_rpi_ws281x.WS2811_STRIP_GRB)
22+
_rpi_ws281x.ws2811_channel_t_gamma_set.assert_called_with(strip._channel,[1.0]*256)
1523

1624
def test_setup_init_fail(_rpi_ws281x):
1725
from rpi_ws281x import PixelStrip
@@ -20,13 +28,50 @@ def test_setup_init_fail(_rpi_ws281x):
2028
with pytest.raises(RuntimeError):
2129
strip.begin()
2230

31+
def test_cleanup(_rpi_ws281x, strip):
32+
assert _rpi_ws281x.ws2811_fini.call_count == 0
33+
assert _rpi_ws281x.delete_ws2811_t.call_count == 0
34+
strip._cleanup()
35+
assert _rpi_ws281x.ws2811_fini.call_count == 1
36+
assert _rpi_ws281x.delete_ws2811_t.call_count == 1
37+
assert strip._leds is None
38+
assert strip._channel is None
39+
40+
def test_show(_rpi_ws281x, strip):
41+
assert _rpi_ws281x.ws2811_render.call_count == 1
42+
strip.show()
43+
assert _rpi_ws281x.ws2811_render.call_count == 2
44+
sub = strip.createPixelSubStrip(5, last=10)
45+
sub.show()
46+
assert _rpi_ws281x.ws2811_render.call_count == 3
47+
48+
_rpi_ws281x.ws2811_render.return_value = 1
49+
with pytest.raises(RuntimeError):
50+
sub.show()
51+
52+
def test_brightness(_rpi_ws281x, strip):
53+
assert _rpi_ws281x.ws2811_channel_t_brightness_get.call_count == 0
54+
strip.getBrightness()
55+
assert _rpi_ws281x.ws2811_channel_t_brightness_get.call_count == 1
56+
calls = _rpi_ws281x.ws2811_channel_t_brightness_set.call_count
57+
strip.setBrightness(20)
58+
assert _rpi_ws281x.ws2811_channel_t_brightness_set.call_count == calls + 1
59+
60+
def test_gamma(_rpi_ws281x, strip):
61+
calls = _rpi_ws281x.ws2811_channel_t_gamma_set.call_count
62+
strip.setGamma(1.0)
63+
assert _rpi_ws281x.ws2811_channel_t_gamma_set.call_count == calls
64+
strip.setGamma([1.0, 1.0])
65+
assert _rpi_ws281x.ws2811_channel_t_gamma_set.call_count == calls
66+
strip.setGamma([1.0] * 256)
67+
assert _rpi_ws281x.ws2811_channel_t_gamma_set.call_count == calls + 1
68+
2369

2470
def test_num_pixels(strip):
2571
assert len(strip[:]) == 10
2672
assert len(strip) == 10
2773
assert strip.numPixels() == 10
2874

29-
3075
def test_set_pixel(strip):
3176
from rpi_ws281x import RGBW
3277
strip[0] = RGBW(255, 0, 0)
@@ -36,14 +81,132 @@ def test_set_pixel(strip):
3681
assert strip.getPixelColorRGBW(0) == RGBW(255, 0, 0)
3782
assert strip.getPixelColorRGBW(0).r == 255
3883

39-
4084
def test_set_multiple(strip):
4185
from rpi_ws281x import RGBW
4286
strip[:] = RGBW(255, 0, 0)
4387
assert strip[:] == [RGBW(255, 0, 0)] * 10
4488

89+
def test_set_pixel_slice(strip):
90+
from rpi_ws281x import RGBW
91+
colours = [RGBW(i*10, 0, 0) for i in range(0, 10)]
92+
strip[0:10] = colours
93+
for i in range(0, 10):
94+
assert strip[i] == RGBW(i*10, 0, 0)
4595

4696
def test_set_odd(strip):
4797
from rpi_ws281x import RGBW
4898
strip[::2] = RGBW(255, 0, 0)
4999
assert strip[:] == [RGBW(255, 0, 0), RGBW(0, 0, 0)] * 5
100+
101+
102+
def test_create_substrip(strip):
103+
from rpi_ws281x import InvalidStrip
104+
with pytest.raises(InvalidStrip):
105+
sub = strip.createPixelSubStrip(100)
106+
with pytest.raises(InvalidStrip):
107+
sub = strip.createPixelSubStrip(5)
108+
with pytest.raises(InvalidStrip):
109+
sub = strip.createPixelSubStrip(5, 11)
110+
with pytest.raises(InvalidStrip):
111+
sub = strip.createPixelSubStrip(5, -2)
112+
with pytest.raises(InvalidStrip):
113+
sub = strip.createPixelSubStrip(5, num=6)
114+
with pytest.raises(InvalidStrip):
115+
sub = strip.createPixelSubStrip(-1, num=5)
116+
with pytest.raises(InvalidStrip):
117+
sub = strip.createPixelSubStrip(5, num=-2)
118+
sub = strip.createPixelSubStrip(5, last=10)
119+
assert len(sub[:]) == 5
120+
assert len(sub) == 5
121+
assert sub.numPixels() == 5
122+
sub = strip.createPixelSubStrip(5, num=5)
123+
assert len(sub[:]) == 5
124+
assert len(sub) == 5
125+
assert sub.numPixels() == 5
126+
127+
def test_adjust_pos(strip):
128+
sub = strip.createPixelSubStrip(5, 10)
129+
assert sub._adjust_pos(3) == 8
130+
assert sub._adjust_pos(-2) == 8
131+
assert sub._adjust_pos(slice(None, None)) == slice(5, 10) # [:]
132+
assert sub._adjust_pos(slice(0, None)) == slice(5, 10) # [0:]
133+
assert sub._adjust_pos(slice(None, 5)) == slice(5, 10) # [:5]
134+
135+
assert sub._adjust_pos(slice(-1, None)) == slice(9, 10) # [-1:] (start, step default to None)
136+
assert sub._adjust_pos(slice(-2, None)) == slice(8, 10) # [-2:]
137+
assert sub._adjust_pos(slice(None, -1)) == slice(5, 9) # [:-1]
138+
assert sub._adjust_pos(slice(None, -2)) == slice(5, 8) # [:-2]
139+
assert sub._adjust_pos(slice(-1, 0)) == slice(9, 5) # [-1:0]
140+
141+
assert sub._adjust_pos(slice(2, 4)) == slice(7, 9) # [2:4]
142+
assert sub._adjust_pos(slice(None, None, -2)) == slice(5, 10, -2) # [::-2]
143+
144+
def test_substrip_set(strip):
145+
from rpi_ws281x import RGBW
146+
sub = strip.createPixelSubStrip(5, 10)
147+
148+
sub[0] = RGBW(1, 1, 1)
149+
assert strip[5] == RGBW(1, 1, 1)
150+
151+
sub.setPixelColor(2, RGBW(2, 2, 2))
152+
assert strip[7] == RGBW(2, 2, 2)
153+
154+
sub.setPixelColorRGB(3, 3, 4, 5)
155+
assert strip[8] == RGBW(3, 4, 5)
156+
157+
strip.off()
158+
sub[1:4] = RGBW(1, 2, 3)
159+
assert strip[6:9] == sub[1:4]
160+
assert strip[6:9] == [RGBW(1, 2, 3)] * 3
161+
assert strip.getPixels() == [RGBW(0, 0, 0)] * 6 + [RGBW(1, 2, 3)] * 3 + [RGBW(0, 0, 0)]
162+
163+
strip.off()
164+
colours = [RGBW(i, i, i) for i in range(1, 4)]
165+
sub[1:4] = colours
166+
print(f"strip is {strip[:]}")
167+
168+
assert strip[6:9] == sub[1:4]
169+
assert strip[6:9] == colours
170+
171+
strip.off()
172+
sub = strip.createPixelSubStrip(2, 10)
173+
174+
sub[-1:0:-2] = [RGBW(i, i, i) for i in range(1, 9)]
175+
assert strip[:] == ([RGBW(0, 0, 0)]
176+
+ [RGBW(0, 0, 0)]
177+
+ [RGBW(0, 0, 0)]
178+
+ [RGBW(4, 4, 4)]
179+
+ [RGBW(0, 0, 0)]
180+
+ [RGBW(3, 3, 3)]
181+
+ [RGBW(0, 0, 0)]
182+
+ [RGBW(2, 2, 2)]
183+
+ [RGBW(0, 0, 0)]
184+
+ [RGBW(1, 1, 1)])
185+
186+
strip.off()
187+
sub[-1:2:-2] = [RGBW(i, i, i) for i in range(1, 9)]
188+
assert strip[:] == ([RGBW(0, 0, 0)]
189+
+ [RGBW(0, 0, 0)]
190+
+ [RGBW(0, 0, 0)]
191+
+ [RGBW(0, 0, 0)]
192+
+ [RGBW(0, 0, 0)]
193+
+ [RGBW(3, 3, 3)]
194+
+ [RGBW(0, 0, 0)]
195+
+ [RGBW(2, 2, 2)]
196+
+ [RGBW(0, 0, 0)]
197+
+ [RGBW(1, 1, 1)])
198+
199+
def test_RGBW():
200+
from rpi_ws281x import RGBW
201+
c = RGBW(0x50)
202+
assert c == 0x50
203+
assert c.r == 0x00 and c.g == 0x00 and c.b == 0x50 and c.w == 0x0
204+
c = RGBW(0x50, g=0x60)
205+
assert c.r == 0x50 and c.g == 0x60 and c.b == 0x00 and c.w == 0x00
206+
c = RGBW(0x50, b=0x60)
207+
assert c.r == 0x50 and c.g == 0x00 and c.b == 0x60 and c.w == 0x00
208+
c = RGBW(0x50, 0x60, 0x70)
209+
assert c.r == 0x50 and c.g == 0x60 and c.b == 0x70 and c.w == 0x00
210+
c = RGBW(0x50, 0x60, 0x70, 0x80)
211+
assert c.r == 0x50 and c.g == 0x60 and c.b == 0x70 and c.w == 0x80
212+
assert c == (0x80 << 24) + (0x50 << 16) + (0x60 << 8) + 0x70

0 commit comments

Comments
 (0)