Skip to content

Commit

Permalink
QA: Update tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
Gadgetoid committed Feb 16, 2024
1 parent 08294fe commit 140ed1d
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 10 deletions.
39 changes: 32 additions & 7 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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']
del sys.modules['gpiodevice']
del sys.modules['gpiod.line']
del sys.modules['gpiod']
6 changes: 3 additions & 3 deletions tests/test_setup.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit 140ed1d

Please sign in to comment.