Skip to content

Commit

Permalink
Tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
Gadgetoid committed Apr 25, 2023
1 parent a214d70 commit 5e972be
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 0 deletions.
55 changes: 55 additions & 0 deletions library/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import pytest
import mock
import sys

_mock_rpi_ws281x = mock.MagicMock()
channels = {}


def ws2811_channel_get(new_ws2811_t, ch):
if ch not in channels:
channels[ch] = {'new_ws2811_t': new_ws2811_t}
return channels[ch]


def ws2811_channel_t_count_set(ch, count):
ch['count'] = count
ch['leds'] = [0 for _ in range(count)]


def ws2811_channel_t_count_get(ch):
return ch['count']


def ws2811_led_set(ch, n, rgbw):
ch['leds'][n] = rgbw


def ws2811_led_get(ch, n):
return ch['leds'][n]


_mock_rpi_ws281x.ws2811_channel_t_count_set = ws2811_channel_t_count_set
_mock_rpi_ws281x.ws2811_channel_t_count_get = ws2811_channel_t_count_get
_mock_rpi_ws281x.ws2811_channel_get = ws2811_channel_get
_mock_rpi_ws281x.ws2811_led_set = ws2811_led_set
_mock_rpi_ws281x.ws2811_led_get = ws2811_led_get


@pytest.fixture(scope='function', autouse=False)
def _rpi_ws281x():
_mock_rpi_ws281x.ws2811_init.return_value = 0
sys.modules['_rpi_ws281x'] = _mock_rpi_ws281x

yield _mock_rpi_ws281x
del sys.modules['_rpi_ws281x']
_mock_rpi_ws281x.reset_mock()
channels.clear()


@pytest.fixture(scope='function', autouse=True)
def rpi_ws281x(_rpi_ws281x):
if "." not in sys.path:
sys.path.append(".")
yield None
del sys.modules['rpi_ws281x']
52 changes: 52 additions & 0 deletions library/tests/test_setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import pytest


def test_setup(_rpi_ws281x):
from rpi_ws281x import PixelStrip
strip = PixelStrip(10, 20)
strip.begin()


def test_setup_init_fail(_rpi_ws281x):
from rpi_ws281x import PixelStrip
_rpi_ws281x.ws2811_init.return_value = 1
strip = PixelStrip(10, 20)
with pytest.raises(RuntimeError):
strip.begin()


def test_num_pixels(_rpi_ws281x):
from rpi_ws281x import PixelStrip
strip = PixelStrip(10, 20)
strip.begin()
assert len(strip[:]) == 10
assert len(strip) == 10
assert strip.numPixels() == 10


def test_set_pixel(_rpi_ws281x):
from rpi_ws281x import PixelStrip, RGBW
strip = PixelStrip(10, 20)
strip.begin()
strip[0] = RGBW(255, 0, 0)
assert strip[0] == strip.getPixelColor(0)
assert strip[0] == RGBW(255, 0, 0)
assert strip.getPixelColorRGB(0) == RGBW(255, 0, 0)
assert strip.getPixelColorRGBW(0) == RGBW(255, 0, 0)
assert strip.getPixelColorRGBW(0).r == 255


def test_set_multiple(_rpi_ws281x):
from rpi_ws281x import PixelStrip, RGBW
strip = PixelStrip(10, 20)
strip.begin()
strip[:] = RGBW(255, 0, 0)
assert strip[:] == [RGBW(255, 0, 0)] * 10


def test_set_odd(_rpi_ws281x):
from rpi_ws281x import PixelStrip, RGBW
strip = PixelStrip(10, 20)
strip.begin()
strip[::2] = RGBW(255, 0, 0)
assert strip[:] == [RGBW(255, 0, 0), RGBW(0, 0, 0)] * 5

0 comments on commit 5e972be

Please sign in to comment.