-
Notifications
You must be signed in to change notification settings - Fork 118
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
197 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
"""Test configuration. | ||
These allow the mocking of various Python modules | ||
that might otherwise have runtime side-effects. | ||
""" | ||
import sys | ||
import mock | ||
import pytest | ||
|
||
from tools import MockSMBus | ||
|
||
|
||
@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.MagicMock() | ||
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 smbus2(): | ||
"""Mock smbus2 module.""" | ||
sys.modules['smbus2'] = mock.Mock() | ||
sys.modules['smbus2'].SMBus = MockSMBus | ||
yield MockSMBus | ||
del sys.modules['smbus2'] | ||
|
||
|
||
@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 tkinter(): | ||
"""Mock tkinter module.""" | ||
tkinter = mock.MagicMock() | ||
sys.modules['tkinter'] = tkinter | ||
yield tkinter | ||
del sys.modules['tkinter'] | ||
|
||
|
||
@pytest.fixture(scope='function', autouse=False) | ||
def PIL(): | ||
"""Mock PIL module.""" | ||
PIL = mock.MagicMock() | ||
sys.modules['PIL'] = PIL | ||
yield PIL | ||
del sys.modules['PIL'] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,67 +1,59 @@ | ||
"""Initialization tests for Inky.""" | ||
import sys | ||
import mock | ||
from tools import MockSMBus | ||
|
||
|
||
def mockery(): | ||
"""Mock requires modules.""" | ||
sys.modules['RPi'] = mock.Mock() | ||
sys.modules['RPi.GPIO'] = mock.Mock() | ||
sys.modules['spidev'] = mock.Mock() | ||
sys.modules['smbus2'] = mock.Mock() | ||
sys.modules['smbus2'].SMBus = MockSMBus | ||
def test_init_mock_phat_black(tkinter, PIL): | ||
"""Test initialisation of InkyMockPHAT with 'black' colour choice.""" | ||
from inky import InkyMockPHAT | ||
|
||
InkyMockPHAT('black') | ||
|
||
def test_init_phat_black(): | ||
"""Test initialisation of InkyPHAT with 'black' colour choice.""" | ||
mockery() | ||
|
||
def test_init_mock_what_black(tkinter, PIL): | ||
"""Test initialisation of InkyMockWHAT with 'black' colour choice.""" | ||
from inky import InkyMockWHAT | ||
|
||
InkyMockWHAT('black') | ||
|
||
|
||
def test_init_phat_black(spidev, smbus2): | ||
"""Test initialisation of InkyPHAT with 'black' colour choice.""" | ||
from inky import InkyPHAT | ||
|
||
InkyPHAT('black') | ||
|
||
|
||
def test_init_phat_red(): | ||
def test_init_phat_red(spidev, smbus2): | ||
"""Test initialisation of InkyPHAT with 'red' colour choice.""" | ||
mockery() | ||
|
||
from inky import InkyPHAT | ||
|
||
InkyPHAT('red') | ||
|
||
|
||
def test_init_phat_yellow(): | ||
def test_init_phat_yellow(spidev, smbus2): | ||
"""Test initialisation of InkyPHAT with 'yellow' colour choice.""" | ||
mockery() | ||
|
||
from inky import InkyPHAT | ||
|
||
InkyPHAT('red') | ||
|
||
|
||
def test_init_what_black(): | ||
def test_init_what_black(spidev, smbus2): | ||
"""Test initialisation of InkyWHAT with 'black' colour choice.""" | ||
mockery() | ||
|
||
from inky import InkyWHAT | ||
|
||
InkyWHAT('black') | ||
|
||
|
||
def test_init_what_red(): | ||
def test_init_what_red(spidev, smbus2): | ||
"""Test initialisation of InkyWHAT with 'red' colour choice.""" | ||
mockery() | ||
|
||
from inky import InkyWHAT | ||
|
||
InkyWHAT('red') | ||
|
||
|
||
def test_init_what_yellow(): | ||
def test_init_what_yellow(spidev, smbus2): | ||
"""Test initialisation of InkyWHAT with 'yellow' colour choice.""" | ||
mockery() | ||
|
||
from inky import InkyWHAT | ||
|
||
InkyWHAT('red') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
"""Install helper tests for Inky. | ||
These validate that, in case of a missing package, an ImportError is raised. | ||
They don't actually validate that our special message is produced! | ||
""" | ||
import sys | ||
import mock | ||
import pytest | ||
|
||
|
||
def test_mock_phat_no_tkinter(): | ||
"""Test initialisation of InkyMockPHAT without tkinter.""" | ||
from inky import InkyMockPHAT | ||
|
||
with pytest.raises(ImportError): | ||
InkyMockPHAT('black') | ||
|
||
|
||
def test_mock_what_no_tkinter(): | ||
"""Test initialisation of InkyMockWHAT without tkinter.""" | ||
from inky import InkyMockWHAT | ||
|
||
with pytest.raises(ImportError): | ||
InkyMockWHAT('black') | ||
|
||
|
||
def test_mock_phat_no_pil(tkinter): | ||
"""Test initialisation of InkyMockPHAT without PIL.""" | ||
from inky import InkyMockPHAT | ||
|
||
with pytest.raises(ImportError): | ||
InkyMockPHAT('black') | ||
|
||
|
||
def test_mock_what_no_pil(tkinter): | ||
"""Test initialisation of InkyMockWHAT without PIL.""" | ||
from inky import InkyMockWHAT | ||
|
||
with pytest.raises(ImportError): | ||
InkyMockWHAT('black') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
"""Install helper tests for Inky. | ||
These validate that, in case of a missing package, an ImportError is raised. | ||
They don't actually validate that our special message is produced! | ||
""" | ||
import sys | ||
import mock | ||
import pytest | ||
from inky.mock import InkyMock, InkyMockPHAT, InkyMockWHAT | ||
|
||
|
||
class InkyMockFAIL(InkyMock): | ||
"""Inky wHAT e-Ink Display Simulator.""" | ||
|
||
WIDTH = 999 | ||
HEIGHT = 999 | ||
|
||
WHITE = 0 | ||
BLACK = 1 | ||
RED = 2 | ||
YELLOW = 2 | ||
|
||
def _simulate(self, region): | ||
pass | ||
|
||
|
||
def test_mock_invalid_size(tkinter, PIL): | ||
"""Test a class with an invalid size raises a ValueError.""" | ||
with pytest.raises(ValueError): | ||
InkyMockFAIL('black') | ||
|
||
|
||
def test_mock_invalid_colour(tkinter, PIL): | ||
"""Test that instantiating with an invalid colour raises a ValueError.""" | ||
with pytest.raises(ValueError): | ||
InkyMockPHAT('octarine') | ||
|
||
|
||
def test_mock_show_phat(tkinter, PIL): | ||
"""Test that show doesn't explode.""" | ||
inky = InkyMockPHAT('red') | ||
inky.show() | ||
|
||
|
||
def test_mock_show_what(tkinter, PIL): | ||
"""Test that show doesn't explode.""" | ||
inky = InkyMockWHAT('red') | ||
inky.show() | ||
|
||
|
||
def test_mock_show_180(tkinter, PIL): | ||
"""Test that show doesn't explode.""" | ||
inky = InkyMockPHAT('red', h_flip=True, v_flip=True) | ||
inky.show() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters