mocking in tests
Some checks failed
Python CI / test (3.12) (push) Failing after 41s
Python CI / test (3.13) (push) Failing after 39s

This commit is contained in:
Duncan Tourolle 2025-11-10 18:23:19 +01:00
parent 37cebe8126
commit be3aed6e5e

View File

@ -5,13 +5,23 @@ Tests the DisplayHAL interface and EReaderDisplayHAL implementation.
""" """
import pytest import pytest
from unittest.mock import AsyncMock, MagicMock, patch import sys
from unittest.mock import AsyncMock, MagicMock, patch, Mock
from PIL import Image from PIL import Image
from dreader_hal.hal import DisplayHAL from dreader_hal.hal import DisplayHAL
from dreader_hal.ereader_hal import EReaderDisplayHAL
from dreader_hal.types import GestureType, TouchEvent from dreader_hal.types import GestureType, TouchEvent
# Mock the hardware driver modules before importing ereader_hal
# This prevents import errors when the hardware modules can't be loaded
sys.modules['dreader_hal.display.it8951'] = Mock()
sys.modules['dreader_hal.touch.ft5xx6'] = Mock()
sys.modules['dreader_hal.sensors.bma400'] = Mock()
sys.modules['dreader_hal.rtc.pcf8523'] = Mock()
sys.modules['dreader_hal.power.ina219'] = Mock()
from dreader_hal.ereader_hal import EReaderDisplayHAL
class TestDisplayHALInterface: class TestDisplayHALInterface:
"""Tests for DisplayHAL abstract interface.""" """Tests for DisplayHAL abstract interface."""
@ -39,12 +49,12 @@ class TestEReaderDisplayHAL:
@pytest.fixture @pytest.fixture
def mock_components(self): def mock_components(self):
"""Create mock hardware components.""" """Create mock hardware components."""
# Since imports are now lazy (inside __init__), we need to patch the actual modules # Create mock driver classes
with patch('dreader_hal.display.it8951.IT8951DisplayDriver') as mock_display, \ mock_display_class = MagicMock()
patch('dreader_hal.touch.ft5xx6.FT5xx6TouchDriver') as mock_touch, \ mock_touch_class = MagicMock()
patch('dreader_hal.sensors.bma400.BMA400OrientationSensor') as mock_orientation, \ mock_orientation_class = MagicMock()
patch('dreader_hal.rtc.pcf8523.PCF8523RTC') as mock_rtc, \ mock_rtc_class = MagicMock()
patch('dreader_hal.power.ina219.INA219PowerMonitor') as mock_power: mock_power_class = MagicMock()
# Set up the mocks to return mock instances with async methods # Set up the mocks to return mock instances with async methods
mock_display_instance = MagicMock() mock_display_instance = MagicMock()
@ -55,32 +65,39 @@ class TestEReaderDisplayHAL:
mock_display_instance.sleep = AsyncMock() mock_display_instance.sleep = AsyncMock()
mock_display_instance.wake = AsyncMock() mock_display_instance.wake = AsyncMock()
mock_display_instance.refresh_count = 0 mock_display_instance.refresh_count = 0
mock_display.return_value = mock_display_instance mock_display_class.return_value = mock_display_instance
mock_touch_instance = MagicMock() mock_touch_instance = MagicMock()
mock_touch_instance.initialize = AsyncMock() mock_touch_instance.initialize = AsyncMock()
mock_touch_instance.cleanup = AsyncMock() mock_touch_instance.cleanup = AsyncMock()
mock_touch_instance.get_touch_event = AsyncMock(return_value=None) mock_touch_instance.get_touch_event = AsyncMock(return_value=None)
mock_touch_instance.set_polling_rate = AsyncMock() mock_touch_instance.set_polling_rate = AsyncMock()
mock_touch.return_value = mock_touch_instance mock_touch_class.return_value = mock_touch_instance
mock_orientation_instance = MagicMock() mock_orientation_instance = MagicMock()
mock_orientation_instance.initialize = AsyncMock() mock_orientation_instance.initialize = AsyncMock()
mock_orientation_instance.cleanup = AsyncMock() mock_orientation_instance.cleanup = AsyncMock()
mock_orientation_instance.current_angle = 0 mock_orientation_instance.current_angle = 0
mock_orientation.return_value = mock_orientation_instance mock_orientation_class.return_value = mock_orientation_instance
mock_rtc_instance = MagicMock() mock_rtc_instance = MagicMock()
mock_rtc_instance.initialize = AsyncMock() mock_rtc_instance.initialize = AsyncMock()
mock_rtc_instance.cleanup = AsyncMock() mock_rtc_instance.cleanup = AsyncMock()
mock_rtc.return_value = mock_rtc_instance mock_rtc_class.return_value = mock_rtc_instance
mock_power_instance = MagicMock() mock_power_instance = MagicMock()
mock_power_instance.initialize = AsyncMock() mock_power_instance.initialize = AsyncMock()
mock_power_instance.cleanup = AsyncMock() mock_power_instance.cleanup = AsyncMock()
mock_power_instance.get_battery_percent = AsyncMock(return_value=75.0) mock_power_instance.get_battery_percent = AsyncMock(return_value=75.0)
mock_power_instance.is_low_battery = AsyncMock(return_value=False) mock_power_instance.is_low_battery = AsyncMock(return_value=False)
mock_power.return_value = mock_power_instance mock_power_class.return_value = mock_power_instance
# Inject the mock classes into the sys.modules mocks
sys.modules['dreader_hal.display.it8951'].IT8951DisplayDriver = mock_display_class
sys.modules['dreader_hal.touch.ft5xx6'].FT5xx6TouchDriver = mock_touch_class
sys.modules['dreader_hal.sensors.bma400'].BMA400OrientationSensor = mock_orientation_class
sys.modules['dreader_hal.rtc.pcf8523'].PCF8523RTC = mock_rtc_class
sys.modules['dreader_hal.power.ina219'].INA219PowerMonitor = mock_power_class
yield { yield {
'display': mock_display_instance, 'display': mock_display_instance,