Skip to content

Commit

Permalink
Update tests for APA102
Browse files Browse the repository at this point in the history
  • Loading branch information
Gadgetoid committed Aug 14, 2020
1 parent 4f9fad1 commit 473531a
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 90 deletions.
103 changes: 55 additions & 48 deletions library/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,48 +1,55 @@
"""Test configuration.
These allow the mocking of various Python modules
that might otherwise have runtime side-effects.
"""
import sys
import mock
import pytest


@pytest.fixture(scope='function', autouse=False)
def GPIO():
"""Mock RPi.GPIO module."""
GPIO = mock.MagicMock()
# Fudge for Python < 37 (possibly earlier)
sys.modules['RPi'] = mock.Mock()
sys.modules['RPi'].GPIO = GPIO
sys.modules['RPi.GPIO'] = GPIO
yield GPIO
del sys.modules['RPi']
del sys.modules['RPi.GPIO']


@pytest.fixture(scope='function', autouse=False)
def plasma():
"""Mock plasma module."""
plasma = mock.MagicMock()
sys.modules['plasma'] = plasma
yield plasma
del sys.modules['plasma']


@pytest.fixture(scope='function', autouse=False)
def spidev():
"""Mock spidev module."""
spidev = mock.MagicMock()
sys.modules['spidev'] = spidev
yield spidev
del sys.modules['spidev']


@pytest.fixture(scope='function', autouse=False)
def atexit():
"""Mock atexit module."""
atexit = mock.MagicMock()
sys.modules['atexit'] = atexit
yield atexit
del sys.modules['atexit']

"""Test configuration.
These allow the mocking of various Python modules
that might otherwise have runtime side-effects.
"""
import sys
import mock
import pytest


@pytest.fixture(scope='function', autouse=False)
def FanShim():
import fanshim
yield fanshim.FanShim
del sys.modules['fanshim']


@pytest.fixture(scope='function', autouse=False)
def GPIO():
"""Mock RPi.GPIO module."""
GPIO = mock.MagicMock()
# Fudge for Python < 37 (possibly earlier)
sys.modules['RPi'] = mock.Mock()
sys.modules['RPi'].GPIO = GPIO
sys.modules['RPi.GPIO'] = GPIO
yield GPIO
del sys.modules['RPi']
del sys.modules['RPi.GPIO']


@pytest.fixture(scope='function', autouse=False)
def apa102():
"""Mock APA102 module."""
apa102 = mock.MagicMock()
sys.modules['apa102'] = apa102
yield apa102
del sys.modules['apa102']


@pytest.fixture(scope='function', autouse=False)
def spidev():
"""Mock spidev module."""
spidev = mock.MagicMock()
sys.modules['spidev'] = spidev
yield spidev
del sys.modules['spidev']


@pytest.fixture(scope='function', autouse=False)
def atexit():
"""Mock atexit module."""
atexit = mock.MagicMock()
sys.modules['atexit'] = atexit
yield atexit
del sys.modules['atexit']

47 changes: 5 additions & 42 deletions library/tests/test_setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,7 @@
import sys


def force_reimport(module):
"""Force the module under test to be re-imported.
Because pytest runs all tests within the same scope (this makes me cry)
we have to do some manual housekeeping to avoid tests polluting each other.
In this case, the first `from fanshim import FanShim` would import both plasma
and RPi.GPIO from the first test run fixtures. Since we want *clean* fixtures
for each test this is not only no good but the results are outright weird-
IE: functions we expect to be called will have no calls because FanShim
receives an entirely different mock object to the one we're validating against.
Since conftest.py already does some sys.modules mangling I see no reason not to
do the same thing here.
"""
try:
del sys.modules[module]
except KeyError:
pass


def test_setup(GPIO, plasma):
force_reimport('fanshim')

from fanshim import FanShim
def test_setup(GPIO, apa102, FanShim):
fanshim = FanShim()

GPIO.setwarnings.assert_called_once_with(False)
Expand All @@ -37,26 +12,18 @@ def test_setup(GPIO, plasma):
mock.call(fanshim._pin_button, GPIO.IN, pull_up_down=GPIO.PUD_UP)
])

plasma.legacy.set_clear_on_exit.assert_called_once_with(True)
plasma.legacy.set_light_count.assert_called_once_with(1)
plasma.legacy.set_light.assert_called_once_with(0, 0, 0, 0)
apa102.APA102.assert_called_once_with(1, 15, 14, None, brightness=0.05)


def test_button_disable(GPIO, plasma):
force_reimport('fanshim')

from fanshim import FanShim
def test_button_disable(GPIO, apa102, FanShim):
fanshim = FanShim(disable_button=True)

GPIO.setwarnings.assert_called_once_with(False)
GPIO.setmode.assert_called_once_with(GPIO.BCM)
GPIO.setup.assert_called_once_with(fanshim._pin_fancontrol, GPIO.OUT)


def test_led_disable(GPIO, plasma):
force_reimport('fanshim')

from fanshim import FanShim
def test_led_disable(GPIO, apa102, FanShim):
fanshim = FanShim(disable_led=True)

GPIO.setwarnings.assert_called_once_with(False)
Expand All @@ -66,10 +33,6 @@ def test_led_disable(GPIO, plasma):
mock.call(fanshim._pin_button, GPIO.IN, pull_up_down=GPIO.PUD_UP)
])

plasma.legacy.set_clear_on_exit.assert_not_called()
plasma.legacy.set_light_count.assert_not_called()
plasma.legacy.set_light.assert_not_called()
assert not apa102.APA102.called

fanshim.set_light(0, 0, 0)
plasma.legacy.set_light.assert_not_called()
plasma.legacy.show.assert_not_called()

0 comments on commit 473531a

Please sign in to comment.