Skip to content

Commit

Permalink
make hardware emulation reversible
Browse files Browse the repository at this point in the history
fixes an issue that can be caused by side effects of certain tests
  • Loading branch information
klamann committed Sep 23, 2024
1 parent 4ff1ac3 commit eb7ec00
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 7 deletions.
31 changes: 26 additions & 5 deletions src/raspi_poe_mon/mock.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,32 @@
logger = logging.getLogger('raspi_poe_mon')


def monkeypatch():
"""monkeypatch all real hardware so the mock version is used instead. restart to undo"""
serial.i2c = MockFan
device.ssd1306 = MockDisplay
render.canvas = MockCanvas
class HardwarePatcher:
"""allows to monkeypatch all real hardware so the mock versions are used instead"""

real_i2c = serial.i2c
real_ssd1306 = device.ssd1306
real_canvas = render.canvas

@classmethod
def patch(cls):
serial.i2c = MockFan
device.ssd1306 = MockDisplay
render.canvas = MockCanvas

@classmethod
def restore(cls):
serial.i2c = cls.real_i2c
device.ssd1306 = cls.real_ssd1306
render.canvas = cls.real_canvas

@classmethod
def is_real(cls) -> bool:
return (
serial.i2c == cls.real_i2c
and device.ssd1306 == cls.real_ssd1306
and render.canvas == cls.real_canvas
)


class MockDisplay:
Expand Down
6 changes: 4 additions & 2 deletions src/raspi_poe_mon/poe_hat.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from luma.oled import device
from PIL.ImageDraw import ImageDraw

from raspi_poe_mon import mock
from raspi_poe_mon.mock import HardwarePatcher


class PoeHat:
Expand All @@ -25,7 +25,7 @@ def __init__(self, dry_run=False, brightness=100) -> None:
self.display: Optional[device.ssd1306] = None
self.fan_state = False
if self.dry_run:
mock.monkeypatch()
HardwarePatcher.patch()

def fan_connect(self, force=False):
if force or self.i2c_fan is None:
Expand Down Expand Up @@ -69,3 +69,5 @@ def draw(self) -> ImageDraw:
def cleanup(self):
self.fan_off()
self.display_clear()
if self.dry_run:
HardwarePatcher.restore()
5 changes: 5 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from raspi_poe_mon import main
from raspi_poe_mon.main import app
from raspi_poe_mon.mock import HardwarePatcher
from raspi_poe_mon.version import __version__

runner = CliRunner()
Expand Down Expand Up @@ -43,9 +44,13 @@ def validate_help(result: Result, contains: str):

@pytest.mark.timeout(2)
def test_dry_run():
assert HardwarePatcher.is_real()
main.run(dry_run=True, verbose=True, frame_time=0.02, timeout=0.05)
assert HardwarePatcher.is_real()


@pytest.mark.timeout(2)
def test_profiling():
assert HardwarePatcher.is_real()
main.run(dry_run=True, profiling=True, frame_time=0.05, timeout=0.1)
assert HardwarePatcher.is_real()
9 changes: 9 additions & 0 deletions tests/test_mock.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from raspi_poe_mon.mock import HardwarePatcher


def test_hardware_patcher():
assert HardwarePatcher.is_real() is True
HardwarePatcher.patch()
assert HardwarePatcher.is_real() is False
HardwarePatcher.restore()
assert HardwarePatcher.is_real() is True

0 comments on commit eb7ec00

Please sign in to comment.