Skip to content

Commit

Permalink
Update test cases to cope with mocking changed smbus imports in I2CDe…
Browse files Browse the repository at this point in the history
…vice and fix module interdependencies
  • Loading branch information
timcnicholls committed Aug 3, 2023
1 parent de8c047 commit b6433bf
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 34 deletions.
13 changes: 5 additions & 8 deletions tests/test_bme280.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,12 @@
import pytest

if sys.version_info[0] == 3:
from unittest.mock import Mock, MagicMock, call, patch
from unittest.mock import Mock, patch
else:
from mock import Mock, MagicMock, call, patch
from mock import Mock, patch

spidev_mock = MagicMock()
sys.modules['spidev'] = spidev_mock

smbus_mock = MagicMock()
sys.modules['smbus'] = smbus_mock
sys.modules['spidev'] = Mock()
sys.modules['smbus'] = Mock()

from odin_devices.bme280 import BME280

Expand Down Expand Up @@ -81,7 +78,7 @@ def assert_read_any_call(self, reg, length):
@pytest.fixture(scope="class")
def test_bme280_device_i2c():
"""Fixture used in device test cases."""
with patch('odin_devices.i2c_device.smbus.SMBus') as MockSMBus:
with patch('odin_devices.i2c_device.SMBus') as MockSMBus:
mock_smbus = MockSMBus.return_value
test_i2c_fixture = BME280TextFixtureI2C(mock_smbus)
yield test_i2c_fixture
Expand Down
33 changes: 18 additions & 15 deletions tests/test_i2c_device.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
"""Test cases for the I2CTContainer class from odin_devices.
Tim Nicholls, STFC Application Engineering Group
"""

import sys

import pytest

if sys.version_info[0] == 3: # pragma: no cover
from unittest.mock import Mock, MagicMock, call
from unittest.mock import Mock, patch
from importlib import reload
else: # pragma: no cover
from mock import Mock, MagicMock, call

smbus_mock = MagicMock()
sys.modules['smbus'] = smbus_mock

from odin_devices.i2c_device import I2CDevice, I2CException
from mock import Mock, patch
from imp import reload

# Initial mock of default smbus to allow i2c_device module to be imported
sys.modules['smbus'] = Mock()
import odin_devices.i2c_device

class dummy_cm():
def __enter__(self):
Expand All @@ -32,14 +31,18 @@ def __init__(self):
self.device_busnum = 1
self.device_address = 0x70
self.device_debug = True
self.device = I2CDevice(self.device_address, self.device_busnum, self.device_debug)
self.device = odin_devices.i2c_device.I2CDevice(self.device_address, self.device_busnum, self.device_debug)
self.device.pre_access = Mock()


@pytest.fixture(scope="class")
def test_i2c_device():
@pytest.fixture(scope="class", params=["smbus", "smbus2"])
def test_i2c_device(request):
"""Fixture used in driver test cases"""

# Mock out the parameterised smbus/smbus2 module and reload the i2c_device module
sys.modules[request.param] = Mock()
reload(odin_devices.i2c_device)

test_i2c_fixture = I2CDeviceTestFixture()
yield test_i2c_fixture

Expand Down Expand Up @@ -68,9 +71,9 @@ def test_device_init(self, test_i2c_device):
def test_change_default_bus(self, test_i2c_device):

default_i2c_bus = 0
I2CDevice.set_default_i2c_bus(default_i2c_bus)
odin_devices.i2c_device.I2CDevice.set_default_i2c_bus(default_i2c_bus)

new_device = I2CDevice(test_i2c_device.device_address, debug=test_i2c_device.device_debug)
new_device = odin_devices.i2c_device.I2CDevice(test_i2c_device.device_address, debug=test_i2c_device.device_debug)
assert default_i2c_bus == new_device.busnum

def test_pre_access_called(self, test_i2c_device):
Expand Down Expand Up @@ -113,7 +116,7 @@ def test_device_access(self, method, smbus_method, exc_mode, args, exp_rc, test_
elif exc_mode == self.EXC_MODE_TRAP:
side_effect = IOError('mocked error')
exc_enable = False
exp_rc = I2CDevice.ERROR
exp_rc = odin_devices.i2c_device.I2CDevice.ERROR
elif exc_mode == self.EXC_MODE_RAISE:
side_effect = IOError('mocked error')
exc_enable = True
Expand All @@ -127,7 +130,7 @@ def test_device_access(self, method, smbus_method, exc_mode, args, exp_rc, test_
exception_message = 'error from device'

if exc_enable:
with pytest.raises(I2CException) as excinfo:
with pytest.raises(odin_devices.i2c_device.I2CException) as excinfo:
rc = getattr(test_i2c_device.device, method)(*args)
assert rc == exp_rc
assert exception_message in excinfo.value
Expand Down
3 changes: 1 addition & 2 deletions tests/test_pac1921.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,9 @@
from mock import Mock, MagicMock, call, patch

sys.modules['smbus'] = MagicMock()
sys.modules['gpiod.Line'] = MagicMock()
sys.modules['gpiod'] = MagicMock()
import odin_devices.pac1921 # Needed so that module can be reloaded
from odin_devices.pac1921 import PAC1921, PAC1921_Synchronised_Array, Measurement_Type, OverflowException
import smbus
from odin_devices.i2c_device import I2CDevice
import gpiod

Expand Down
16 changes: 7 additions & 9 deletions tests/test_si570.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,8 @@
else: # pragma: no cover
from mock import Mock, call, patch

# sys.modules['smbus'] = Mock()
sys.modules['smbus'] = Mock()
from odin_devices.si570 import SI570
from odin_devices.i2c_device import I2CDevice, I2CException


def readList_side_effect(reg, length):
return [i for i in range(length)]


class si570TestFixture(object):
Expand All @@ -26,12 +21,15 @@ def __init__(self):
self.address = 0x5d
self.busnum = 1
self.model = SI570.SI570_C
with patch("odin_devices.i2c_device.I2CDevice.readU8") as mocked_readu8, \
patch("odin_devices.i2c_device.I2CDevice.readList") as mocked_readlist:
with patch("odin_devices.si570.SI570.readU8") as mocked_readu8, \
patch("odin_devices.si570.SI570.readList") as mocked_readlist:
mocked_readu8.return_value = 0
mocked_readlist.side_effect = readList_side_effect
mocked_readlist.side_effect = self.readList_side_effect
self.driver = SI570(self.address, self.model, busnum=self.busnum)

def readList_side_effect(self, reg, length):
return [i for i in range(length)]


@pytest.fixture(scope="class")
def test_si570_driver():
Expand Down

0 comments on commit b6433bf

Please sign in to comment.