Update tests

This commit is contained in:
Phil Howard
2021-12-06 15:25:51 +00:00
parent 2db821bc04
commit 314f973dcb
7 changed files with 72 additions and 67 deletions

View File

@@ -5,13 +5,14 @@ on:
push:
branches:
- master
- rotation_mirror
jobs:
test:
runs-on: ubuntu-latest
strategy:
matrix:
python: [2.7, 3.7, 3.9]
python: [3.7, 3.8, 3.9]
steps:
- uses: actions/checkout@v2

View File

@@ -7,6 +7,13 @@ import mock
import pytest
@pytest.fixture(scope='function', autouse=False)
def ST7735():
import ST7735
yield ST7735
del sys.modules['ST7735']
@pytest.fixture(scope='function', autouse=False)
def GPIO():
"""Mock RPi.GPIO module."""
@@ -32,7 +39,5 @@ def spidev():
@pytest.fixture(scope='function', autouse=False)
def numpy():
"""Mock numpy module."""
numpy = mock.MagicMock()
sys.modules['numpy'] = numpy
import numpy
yield numpy
del sys.modules['numpy']

View File

@@ -1,17 +1,19 @@
from tools import force_reimport
def test_128_64_0(GPIO, spidev, numpy):
force_reimport('ST7735')
import ST7735
def test_128_64_0(GPIO, spidev, numpy, ST7735):
display = ST7735.ST7735(port=0, cs=0, dc=24, width=128, height=64, rotation=0)
assert display.width == 128
assert display.height == 64
display.set_window(0, 0)
def test_128_64_90(GPIO, spidev, numpy):
force_reimport('ST7735')
import ST7735
def test_128_64_90(GPIO, spidev, numpy, ST7735):
display = ST7735.ST7735(port=0, cs=0, dc=24, width=128, height=64, rotation=90)
assert display.width == 64
assert display.height == 128
display.set_window(0, 0)
def test_128_64_180(GPIO, spidev, numpy, ST7735):
display = ST7735.ST7735(port=0, cs=0, dc=24, width=128, height=64, rotation=180)
assert display.width == 128
assert display.height == 64
display.set_window(0, 0)

View File

@@ -1,25 +1,53 @@
import mock
from tools import force_reimport
def test_display(GPIO, spidev, numpy):
force_reimport('ST7735')
import ST7735
def test_display(GPIO, spidev, numpy, ST7735):
display = ST7735.ST7735(port=0, cs=0, dc=24)
numpy.dstack().flatten().tolist.return_value = [0xff, 0x00, 0xff, 0x00]
display.display(mock.MagicMock())
image = mock.MagicMock()
image.convert.return_value = [
[255, 0, 0],
[0, 255, 0],
[0, 0, 255]
]
spidev.reset_mock()
display.display(image)
spidev.SpiDev().xfer3.assert_called_with([0xff, 0x00, 0xff, 0x00])
spidev.SpiDev().xfer3.assert_has_calls(
(mock.call(b'\xf8\x00\x07\xe0\x00\x1f'),)
)
def test_color565(GPIO, spidev, numpy):
force_reimport('ST7735')
import ST7735
assert ST7735.color565(255, 255, 255) == 0xFFFF
def test_image_to_data_uint8(GPIO, spidev, numpy, ST7735):
display = ST7735.ST7735(port=0, cs=0, dc=24)
image = numpy.array([
[255, 0, 0],
[0, 255, 0],
[0, 0, 255]
])
spidev.reset_mock()
display.display(image)
spidev.SpiDev().xfer3.assert_has_calls(
(mock.call(b'\xf8\x00\x07\xe0\x00\x1f'),)
)
def test_image_to_data(GPIO, spidev, numpy):
force_reimport('ST7735')
numpy.dstack().flatten().tolist.return_value = []
import ST7735
assert ST7735.image_to_data(mock.MagicMock()) == []
def test_image_to_data_uint16(GPIO, spidev, numpy, ST7735):
display = ST7735.ST7735(port=0, cs=0, dc=24)
image = numpy.array([
[255, 0, 0],
[0, 255, 0],
[0, 0, 255]
], dtype='uint16')
spidev.reset_mock()
display.display(image)
spidev.SpiDev().xfer3.assert_has_calls(
(mock.call(b'\xf8\x00\x07\xe0\x00\x1f'),)
)
def test_image_to_data(GPIO, spidev, numpy, ST7735):
image = mock.MagicMock()
image.convert.return_value = [255, 0, 255]
assert ST7735.image_to_data(image) == b"\xf8\x1f"

View File

@@ -1,10 +1,7 @@
import mock
from tools import force_reimport
def test_setup(GPIO, spidev, numpy):
force_reimport('ST7735')
import ST7735
def test_setup(GPIO, spidev, numpy, ST7735):
display = ST7735.ST7735(port=0, cs=0, dc=24)
del display
@@ -14,16 +11,12 @@ def test_setup(GPIO, spidev, numpy):
], any_order=True)
def test_setup_no_invert(GPIO, spidev, numpy):
force_reimport('ST7735')
import ST7735
def test_setup_no_invert(GPIO, spidev, numpy, ST7735):
display = ST7735.ST7735(port=0, cs=0, dc=24, invert=False)
del display
def test_setup_with_backlight(GPIO, spidev, numpy):
force_reimport('ST7735')
import ST7735
def test_setup_with_backlight(GPIO, spidev, numpy, ST7735):
display = ST7735.ST7735(port=0, cs=0, dc=24, backlight=4)
GPIO.setup.assert_called_with(4, GPIO.OUT)
@@ -39,9 +32,7 @@ def test_setup_with_backlight(GPIO, spidev, numpy):
], any_order=True)
def test_setup_with_reset(GPIO, spidev, numpy):
force_reimport('ST7735')
import ST7735
def test_setup_with_reset(GPIO, spidev, numpy, ST7735):
display = ST7735.ST7735(port=0, cs=0, dc=24, rst=4)
GPIO.setup.assert_called_with(4, GPIO.OUT)
del display

View File

@@ -1,23 +0,0 @@
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.
Since conftest.py already does some sys.modules mangling I see no reason not to
do the same thing here.
"""
if "." in module:
steps = module.split(".")
else:
steps = [module]
for i in range(len(steps)):
module = ".".join(steps[0:i + 1])
try:
del sys.modules[module]
except KeyError:
pass

View File

@@ -1,5 +1,5 @@
[tox]
envlist = py{27,37,39},qa
envlist = py{37,38,39},qa
skip_missing_interpreters = True
[testenv]
@@ -9,6 +9,7 @@ commands =
coverage report
deps =
mock
numpy
pytest>=3.1
pytest-cov
@@ -21,4 +22,4 @@ commands =
deps =
check-manifest
flake8
twine
twine