Skip to content

Commit

Permalink
test: enhance display tests
Browse files Browse the repository at this point in the history
  • Loading branch information
u8slvn committed Jul 7, 2019
1 parent 731a658 commit ecedf9e
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 39 deletions.
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
requests==2.20.0
lxml==4.2.1
luma.led_matrix==1.0.5
luma.led-matrix==1.4.0
pyyaml>=4.2b1
loguru==0.3.0
35 changes: 18 additions & 17 deletions rerwatcher/display.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@

from abc import abstractmethod, ABC

from luma.core.interface.serial import spi, noop
from luma.core.legacy import text
from luma.core.legacy.font import proportional, LCD_FONT
from luma.core.interface import serial
from luma.core.legacy import text, font
from luma.core.render import canvas
from luma.led_matrix.device import max7219
from luma.led_matrix import device


class DiplayTypeNotStupported(NotImplementedError):
class DisplayTypeNotSupportedError(NotImplementedError):
pass


Expand All @@ -28,9 +27,9 @@ def print(self, messages):

class MatrixDisplay(DisplayDevice):
def __init__(self):
serial = spi(port=0, device=0, gpio=noop())
self._device = max7219(
serial, width=64, height=16, block_orientation=-90, rotate=0
_serial = serial.spi(port=0, device=0, gpio=serial.noop())
self._device = device.max7219(
_serial, width=64, height=16, block_orientation=-90, rotate=0
)
self._device.contrast(32)

Expand All @@ -39,17 +38,19 @@ def print(self, messages):
for message in messages:
text(
draw, (0, 9), message.text(),
fill="white", font=proportional(LCD_FONT)
fill="white", font=font.proportional(font.LCD_FONT)
)


class DisplayDeviceFactory(ABC):
@staticmethod
def build(config):
type = config['device']['type']
if type == 'matrix':
return MatrixDisplay()
if type == 'console':
return ConsoleDisplay()

raise DiplayTypeNotStupported
def build(config: dict):
display = {
'matrix': MatrixDisplay,
'console': ConsoleDisplay,
}.get(config['device']['type'])

if not display:
raise DisplayTypeNotSupportedError

return display()
13 changes: 13 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
def mock_config(monkeypatch):
def load_config():
return FAKE_CONFIG

monkeypatch.setattr(RerWatcher, 'load_config', load_config)


Expand All @@ -52,4 +53,16 @@ def __init__(self, text):
def mock_requests(monkeypatch):
def get():
return FAKE_RESPONSE

monkeypatch.setattr(requests, 'get', get)


@pytest.fixture(scope='function')
def mock_luma(mocker):
serial = mocker.patch('rerwatcher.display.serial')
device = mocker.patch('rerwatcher.display.device')
text = mocker.patch('rerwatcher.display.text')
font = mocker.patch('rerwatcher.display.font')
canvas = mocker.patch('rerwatcher.display.canvas')

return serial, device, text, font, canvas
44 changes: 23 additions & 21 deletions tests/test_display.py
Original file line number Diff line number Diff line change
@@ -1,50 +1,52 @@
#!/usr/bin/env python3
# coding: utf-8

from unittest.mock import patch, Mock

import pytest

from rerwatcher import display
from tests.conftest import FAKE_CONFIG


class TestMatrixDisplay:
def test_print_on_matrix(self, mocker, mock_luma):
messages = [mocker.Mock(**{'text.return_value': 'foo'})]
matrix = display.MatrixDisplay()
*_, text, _, canvas = mock_luma

matrix.print(messages)

canvas.assert_called_once()
text.assert_called_once()


class TestConsoleDisplay:
@patch('builtins.print')
def test_print_on_console(self, print):
message = Mock()
message.text.return_value = 'FOO'
messages = [message]
console_display = display.ConsoleDisplay()
def test_print_on_console(self, mocker):
bi_print = mocker.patch('builtins.print')
messages = [mocker.Mock(**{'text.return_value': 'foo'})]
console = display.ConsoleDisplay()

console_display.print(messages)
console.print(messages)

print.assert_called_with('FOO')
bi_print.assert_called_with('foo')


class TestDisplayDeviceFactory:
@patch('rerwatcher.display.ConsoleDisplay')
def test_device_builder_console_display(self, console_display):
def test_device_builder_console_display(self):
FAKE_CONFIG['device']['type'] = 'console'
console_display.return_value = 'FOO-CONSOLE'

device = display.DisplayDeviceFactory.build(FAKE_CONFIG)

assert device == 'FOO-CONSOLE'
assert isinstance(device, display.ConsoleDisplay)

@patch('rerwatcher.display.MatrixDisplay')
def test_device_builder_matrix_display(self, matrix_display):
def test_device_builder_matrix_display(self, mock_luma):
FAKE_CONFIG['device']['type'] = 'matrix'
matrix_display.return_value = 'FOO-MATRIX'

device = display.DisplayDeviceFactory.build(FAKE_CONFIG)

assert device == 'FOO-MATRIX'
assert isinstance(device, display.MatrixDisplay)

def test_device_builder_fail(self):
FAKE_CONFIG['device']['type'] = 'foo'

with pytest.raises(display.DiplayTypeNotStupported) as error:
with pytest.raises(display.DisplayTypeNotSupportedError):
display.DisplayDeviceFactory.build(FAKE_CONFIG)

assert error.typename == 'DiplayTypeNotStupported'

0 comments on commit ecedf9e

Please sign in to comment.