Skip to content

Commit 5e972be

Browse files
committed
Tests.
1 parent a214d70 commit 5e972be

File tree

2 files changed

+107
-0
lines changed

2 files changed

+107
-0
lines changed

library/tests/conftest.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import pytest
2+
import mock
3+
import sys
4+
5+
_mock_rpi_ws281x = mock.MagicMock()
6+
channels = {}
7+
8+
9+
def ws2811_channel_get(new_ws2811_t, ch):
10+
if ch not in channels:
11+
channels[ch] = {'new_ws2811_t': new_ws2811_t}
12+
return channels[ch]
13+
14+
15+
def ws2811_channel_t_count_set(ch, count):
16+
ch['count'] = count
17+
ch['leds'] = [0 for _ in range(count)]
18+
19+
20+
def ws2811_channel_t_count_get(ch):
21+
return ch['count']
22+
23+
24+
def ws2811_led_set(ch, n, rgbw):
25+
ch['leds'][n] = rgbw
26+
27+
28+
def ws2811_led_get(ch, n):
29+
return ch['leds'][n]
30+
31+
32+
_mock_rpi_ws281x.ws2811_channel_t_count_set = ws2811_channel_t_count_set
33+
_mock_rpi_ws281x.ws2811_channel_t_count_get = ws2811_channel_t_count_get
34+
_mock_rpi_ws281x.ws2811_channel_get = ws2811_channel_get
35+
_mock_rpi_ws281x.ws2811_led_set = ws2811_led_set
36+
_mock_rpi_ws281x.ws2811_led_get = ws2811_led_get
37+
38+
39+
@pytest.fixture(scope='function', autouse=False)
40+
def _rpi_ws281x():
41+
_mock_rpi_ws281x.ws2811_init.return_value = 0
42+
sys.modules['_rpi_ws281x'] = _mock_rpi_ws281x
43+
44+
yield _mock_rpi_ws281x
45+
del sys.modules['_rpi_ws281x']
46+
_mock_rpi_ws281x.reset_mock()
47+
channels.clear()
48+
49+
50+
@pytest.fixture(scope='function', autouse=True)
51+
def rpi_ws281x(_rpi_ws281x):
52+
if "." not in sys.path:
53+
sys.path.append(".")
54+
yield None
55+
del sys.modules['rpi_ws281x']

library/tests/test_setup.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import pytest
2+
3+
4+
def test_setup(_rpi_ws281x):
5+
from rpi_ws281x import PixelStrip
6+
strip = PixelStrip(10, 20)
7+
strip.begin()
8+
9+
10+
def test_setup_init_fail(_rpi_ws281x):
11+
from rpi_ws281x import PixelStrip
12+
_rpi_ws281x.ws2811_init.return_value = 1
13+
strip = PixelStrip(10, 20)
14+
with pytest.raises(RuntimeError):
15+
strip.begin()
16+
17+
18+
def test_num_pixels(_rpi_ws281x):
19+
from rpi_ws281x import PixelStrip
20+
strip = PixelStrip(10, 20)
21+
strip.begin()
22+
assert len(strip[:]) == 10
23+
assert len(strip) == 10
24+
assert strip.numPixels() == 10
25+
26+
27+
def test_set_pixel(_rpi_ws281x):
28+
from rpi_ws281x import PixelStrip, RGBW
29+
strip = PixelStrip(10, 20)
30+
strip.begin()
31+
strip[0] = RGBW(255, 0, 0)
32+
assert strip[0] == strip.getPixelColor(0)
33+
assert strip[0] == RGBW(255, 0, 0)
34+
assert strip.getPixelColorRGB(0) == RGBW(255, 0, 0)
35+
assert strip.getPixelColorRGBW(0) == RGBW(255, 0, 0)
36+
assert strip.getPixelColorRGBW(0).r == 255
37+
38+
39+
def test_set_multiple(_rpi_ws281x):
40+
from rpi_ws281x import PixelStrip, RGBW
41+
strip = PixelStrip(10, 20)
42+
strip.begin()
43+
strip[:] = RGBW(255, 0, 0)
44+
assert strip[:] == [RGBW(255, 0, 0)] * 10
45+
46+
47+
def test_set_odd(_rpi_ws281x):
48+
from rpi_ws281x import PixelStrip, RGBW
49+
strip = PixelStrip(10, 20)
50+
strip.begin()
51+
strip[::2] = RGBW(255, 0, 0)
52+
assert strip[:] == [RGBW(255, 0, 0), RGBW(0, 0, 0)] * 5

0 commit comments

Comments
 (0)