Skip to content

Commit 473531a

Browse files
committed
Update tests for APA102
1 parent 4f9fad1 commit 473531a

2 files changed

Lines changed: 60 additions & 90 deletions

File tree

library/tests/conftest.py

Lines changed: 55 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,55 @@
1-
"""Test configuration.
2-
These allow the mocking of various Python modules
3-
that might otherwise have runtime side-effects.
4-
"""
5-
import sys
6-
import mock
7-
import pytest
8-
9-
10-
@pytest.fixture(scope='function', autouse=False)
11-
def GPIO():
12-
"""Mock RPi.GPIO module."""
13-
GPIO = mock.MagicMock()
14-
# Fudge for Python < 37 (possibly earlier)
15-
sys.modules['RPi'] = mock.Mock()
16-
sys.modules['RPi'].GPIO = GPIO
17-
sys.modules['RPi.GPIO'] = GPIO
18-
yield GPIO
19-
del sys.modules['RPi']
20-
del sys.modules['RPi.GPIO']
21-
22-
23-
@pytest.fixture(scope='function', autouse=False)
24-
def plasma():
25-
"""Mock plasma module."""
26-
plasma = mock.MagicMock()
27-
sys.modules['plasma'] = plasma
28-
yield plasma
29-
del sys.modules['plasma']
30-
31-
32-
@pytest.fixture(scope='function', autouse=False)
33-
def spidev():
34-
"""Mock spidev module."""
35-
spidev = mock.MagicMock()
36-
sys.modules['spidev'] = spidev
37-
yield spidev
38-
del sys.modules['spidev']
39-
40-
41-
@pytest.fixture(scope='function', autouse=False)
42-
def atexit():
43-
"""Mock atexit module."""
44-
atexit = mock.MagicMock()
45-
sys.modules['atexit'] = atexit
46-
yield atexit
47-
del sys.modules['atexit']
48-
1+
"""Test configuration.
2+
These allow the mocking of various Python modules
3+
that might otherwise have runtime side-effects.
4+
"""
5+
import sys
6+
import mock
7+
import pytest
8+
9+
10+
@pytest.fixture(scope='function', autouse=False)
11+
def FanShim():
12+
import fanshim
13+
yield fanshim.FanShim
14+
del sys.modules['fanshim']
15+
16+
17+
@pytest.fixture(scope='function', autouse=False)
18+
def GPIO():
19+
"""Mock RPi.GPIO module."""
20+
GPIO = mock.MagicMock()
21+
# Fudge for Python < 37 (possibly earlier)
22+
sys.modules['RPi'] = mock.Mock()
23+
sys.modules['RPi'].GPIO = GPIO
24+
sys.modules['RPi.GPIO'] = GPIO
25+
yield GPIO
26+
del sys.modules['RPi']
27+
del sys.modules['RPi.GPIO']
28+
29+
30+
@pytest.fixture(scope='function', autouse=False)
31+
def apa102():
32+
"""Mock APA102 module."""
33+
apa102 = mock.MagicMock()
34+
sys.modules['apa102'] = apa102
35+
yield apa102
36+
del sys.modules['apa102']
37+
38+
39+
@pytest.fixture(scope='function', autouse=False)
40+
def spidev():
41+
"""Mock spidev module."""
42+
spidev = mock.MagicMock()
43+
sys.modules['spidev'] = spidev
44+
yield spidev
45+
del sys.modules['spidev']
46+
47+
48+
@pytest.fixture(scope='function', autouse=False)
49+
def atexit():
50+
"""Mock atexit module."""
51+
atexit = mock.MagicMock()
52+
sys.modules['atexit'] = atexit
53+
yield atexit
54+
del sys.modules['atexit']
55+

library/tests/test_setup.py

Lines changed: 5 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,7 @@
22
import sys
33

44

5-
def force_reimport(module):
6-
"""Force the module under test to be re-imported.
7-
8-
Because pytest runs all tests within the same scope (this makes me cry)
9-
we have to do some manual housekeeping to avoid tests polluting each other.
10-
11-
In this case, the first `from fanshim import FanShim` would import both plasma
12-
and RPi.GPIO from the first test run fixtures. Since we want *clean* fixtures
13-
for each test this is not only no good but the results are outright weird-
14-
15-
IE: functions we expect to be called will have no calls because FanShim
16-
receives an entirely different mock object to the one we're validating against.
17-
18-
Since conftest.py already does some sys.modules mangling I see no reason not to
19-
do the same thing here.
20-
"""
21-
try:
22-
del sys.modules[module]
23-
except KeyError:
24-
pass
25-
26-
27-
def test_setup(GPIO, plasma):
28-
force_reimport('fanshim')
29-
30-
from fanshim import FanShim
5+
def test_setup(GPIO, apa102, FanShim):
316
fanshim = FanShim()
327

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

40-
plasma.legacy.set_clear_on_exit.assert_called_once_with(True)
41-
plasma.legacy.set_light_count.assert_called_once_with(1)
42-
plasma.legacy.set_light.assert_called_once_with(0, 0, 0, 0)
15+
apa102.APA102.assert_called_once_with(1, 15, 14, None, brightness=0.05)
4316

4417

45-
def test_button_disable(GPIO, plasma):
46-
force_reimport('fanshim')
47-
48-
from fanshim import FanShim
18+
def test_button_disable(GPIO, apa102, FanShim):
4919
fanshim = FanShim(disable_button=True)
5020

5121
GPIO.setwarnings.assert_called_once_with(False)
5222
GPIO.setmode.assert_called_once_with(GPIO.BCM)
5323
GPIO.setup.assert_called_once_with(fanshim._pin_fancontrol, GPIO.OUT)
5424

5525

56-
def test_led_disable(GPIO, plasma):
57-
force_reimport('fanshim')
58-
59-
from fanshim import FanShim
26+
def test_led_disable(GPIO, apa102, FanShim):
6027
fanshim = FanShim(disable_led=True)
6128

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

69-
plasma.legacy.set_clear_on_exit.assert_not_called()
70-
plasma.legacy.set_light_count.assert_not_called()
71-
plasma.legacy.set_light.assert_not_called()
36+
assert not apa102.APA102.called
7237

7338
fanshim.set_light(0, 0, 0)
74-
plasma.legacy.set_light.assert_not_called()
75-
plasma.legacy.show.assert_not_called()

0 commit comments

Comments
 (0)