Skip to content

Commit

Permalink
Update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Gadgetoid committed Dec 6, 2021
1 parent a48271c commit 826dd48
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 67 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 8 additions & 3 deletions library/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@
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."""
Expand All @@ -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']
20 changes: 11 additions & 9 deletions library/tests/test_dimensions.py
Original file line number Diff line number Diff line change
@@ -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)
60 changes: 44 additions & 16 deletions library/tests/test_features.py
Original file line number Diff line number Diff line change
@@ -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([0xf8, 0x00, 0x07, 0xe0, 0x00, 0x1f]),)
)


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([0xf8, 0x00, 0x07, 0xe0, 0x00, 0x1f]),)
)


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([0xf8, 0x00, 0x07, 0xe0, 0x00, 0x1f]),)
)


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(GPIO, spidev, numpy, ST7735):
image = mock.MagicMock()
image.convert.return_value = [255, 0, 255]
assert ST7735.image_to_data(image) == b"\xf8\x1f"
17 changes: 4 additions & 13 deletions library/tests/test_setup.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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)

Expand All @@ -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
23 changes: 0 additions & 23 deletions library/tests/tools.py

This file was deleted.

5 changes: 3 additions & 2 deletions library/tox.ini
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[tox]
envlist = py{27,37,39},qa
envlist = py{37,38,39},qa
skip_missing_interpreters = True

[testenv]
Expand All @@ -9,6 +9,7 @@ commands =
coverage report
deps =
mock
numpy
pytest>=3.1
pytest-cov

Expand All @@ -21,4 +22,4 @@ commands =
deps =
check-manifest
flake8
twine
twine

0 comments on commit 826dd48

Please sign in to comment.