From 140ed1dc0c424798dce04126afd570da26225b5d Mon Sep 17 00:00:00 2001 From: Phil Howard Date: Fri, 16 Feb 2024 00:22:46 +0000 Subject: [PATCH] QA: Update tests. --- tests/conftest.py | 39 ++++++++++++++++++++++++++++++++------- tests/test_setup.py | 6 +++--- 2 files changed, 35 insertions(+), 10 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index b2d592c..e89d154 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -2,6 +2,28 @@ import mock import pytest +from i2cdevice import MockSMBus + + +class MySMBus(MockSMBus): + def __init__(self, bus): + MockSMBus.__init__(self, bus) + # Set the uppermost bit of the CONFIG register + # to indicate an "inactive/start" status + self.regs[0] = [0x67, 0x20] + self.regs[1] = 0b10000000 | 0b01010000 + + def read_i2c_block_data(self, i2c_address, register, length): + if register in (0x00,): + return self.regs[register] + else: + return self.regs[register:register + length] + + def write_i2c_block_data(self, i2c_address, register, values): + if register in (0x00,): + self.regs[register] = values + else: + self.regs[register:register + len(values)] = values @pytest.fixture(scope='function') @@ -27,15 +49,18 @@ def sn3218(): @pytest.fixture(scope='function') def smbus(): - sys.modules['smbus'] = mock.MagicMock() - yield sys.modules['smbus'] - del sys.modules['smbus'] + sys.modules["smbus2"] = mock.Mock() + sys.modules["smbus2"].SMBus = MySMBus + yield MySMBus + del sys.modules["smbus2"] @pytest.fixture(scope='function') def gpio(): - sys.modules['RPi'] = mock.Mock() - sys.modules['RPi.GPIO'] = mock.MagicMock() + sys.modules['gpiod'] = mock.Mock() + sys.modules['gpiod.line'] = mock.Mock() + sys.modules['gpiodevice'] = mock.MagicMock() yield - del sys.modules['RPi'] - del sys.modules['RPi.GPIO'] \ No newline at end of file + del sys.modules['gpiodevice'] + del sys.modules['gpiod.line'] + del sys.modules['gpiod'] \ No newline at end of file diff --git a/tests/test_setup.py b/tests/test_setup.py index c17eb0f..39b5e77 100644 --- a/tests/test_setup.py +++ b/tests/test_setup.py @@ -1,11 +1,11 @@ -def test_setup(gpio, smbus, sn3218, ads1015, automationhat): +def test_setup(gpio, smbus, sn3218, automationhat): automationhat.setup() -def test_analog(gpio, smbus, sn3218, ads1015, automationhat): +def test_analog(gpio, smbus, sn3218, automationhat): automationhat.setup() - ads1015.ADS1015().get_voltage.return_value = 3.3 + # ads1015.ADS1015().get_voltage.return_value = 3.3 # VCC = 3.3, GAIN = 4.096, FS = 2.027, Max Voltage = 25.85 # output ~= ((1 << 11) - 1) / 2047.0 * 2096.0 / 3300.0 * 25.85