mirror of
https://github.com/cmur2/python-bme680.git
synced 2025-06-29 00:30:31 +02:00
Test suites and code QA fixes
This commit is contained in:
8
library/tests/setup.cfg
Normal file
8
library/tests/setup.cfg
Normal file
@ -0,0 +1,8 @@
|
||||
[flake8]
|
||||
exclude =
|
||||
__pycache__
|
||||
ignore =
|
||||
D100 # Do not require docstrings
|
||||
E501
|
||||
F403
|
||||
F405
|
57
library/tests/test_setup.py
Normal file
57
library/tests/test_setup.py
Normal file
@ -0,0 +1,57 @@
|
||||
import sys
|
||||
import mock
|
||||
import pytest
|
||||
import bme680
|
||||
|
||||
|
||||
class MockSMBus:
|
||||
"""Mock a basic non-presence SMBus device to cause BME680 to fail.
|
||||
|
||||
Returns 0 in all cases, so that CHIP_ID will never match.
|
||||
|
||||
"""
|
||||
|
||||
def __init__(self, bus): # noqa D107
|
||||
pass
|
||||
|
||||
def read_byte_data(self, addr, register):
|
||||
"""Return 0 for all read attempts."""
|
||||
return 0
|
||||
|
||||
|
||||
class MockSMBusPresent:
|
||||
"""Mock enough of the BME680 for the library to initialise and test."""
|
||||
|
||||
def __init__(self, bus):
|
||||
"""Initialise with test data."""
|
||||
self.regs = [0 for _ in range(256)]
|
||||
self.regs[bme680.CHIP_ID_ADDR] = bme680.CHIP_ID
|
||||
|
||||
def read_byte_data(self, addr, register):
|
||||
"""Read a single byte from fake registers."""
|
||||
return self.regs[register]
|
||||
|
||||
def write_byte_data(self, addr, register, value):
|
||||
"""Write a single byte to fake registers."""
|
||||
self.regs[register] = value
|
||||
|
||||
def read_i2c_block_data(self, addr, register, length):
|
||||
"""Read up to length bytes from register."""
|
||||
return self.regs[register:register + length]
|
||||
|
||||
|
||||
def test_setup_not_present():
|
||||
"""Mock the adbsence of a BME680 and test initialisation."""
|
||||
sys.modules['smbus'] = mock.MagicMock()
|
||||
sys.modules['smbus'].SMBus = MockSMBus
|
||||
|
||||
with pytest.raises(RuntimeError):
|
||||
sensor = bme680.BME680() # noqa F841
|
||||
|
||||
|
||||
def test_setup_mock_present():
|
||||
"""Mock the presence of a BME680 and test initialisation."""
|
||||
sys.modules['smbus'] = mock.MagicMock()
|
||||
sys.modules['smbus'].SMBus = MockSMBusPresent
|
||||
|
||||
sensor = bme680.BME680() # noqa F841
|
Reference in New Issue
Block a user