Skip to content

Commit 7e6bdd0

Browse files
authored
Merge pull request #28 from pimoroni/tests-and-qa
Tests and QA
2 parents 3a48112 + 5806466 commit 7e6bdd0

6 files changed

Lines changed: 149 additions & 50 deletions

File tree

examples/compensated-temperature.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,6 @@
44
import bme680
55
from subprocess import PIPE, Popen
66

7-
try:
8-
from smbus2 import SMBus
9-
except ImportError:
10-
from smbus import SMBus
117

128
print("""compensated-temperature.py - Use the CPU temperature to compensate temperature
139
readings from the BME680 sensor. Method adapted from Initial State's Enviro pHAT
@@ -31,12 +27,14 @@
3127
sensor.set_temperature_oversample(bme680.OS_8X)
3228
sensor.set_filter(bme680.FILTER_SIZE_3)
3329

30+
3431
# Gets the CPU temperature in degrees C
3532
def get_cpu_temperature():
3633
process = Popen(['vcgencmd', 'measure_temp'], stdout=PIPE)
3734
output, _error = process.communicate()
3835
return float(output[output.index('=') + 1:output.rindex("'")])
3936

37+
4038
factor = 1.0 # Smaller numbers adjust temp down, vice versa
4139
smooth_size = 10 # Dampens jitter due to rapid CPU temp changes
4240

examples/temperature-offset.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
sensor.set_temperature_oversample(bme680.OS_8X)
2323
sensor.set_filter(bme680.FILTER_SIZE_3)
2424

25+
2526
def display_data(offset=0):
2627
sensor.set_temp_offset(offset)
2728
sensor.get_sensor_data()
@@ -32,6 +33,7 @@ def display_data(offset=0):
3233
print(output)
3334
print('')
3435

36+
3537
print('Initial readings')
3638
display_data()
3739

library/setup.cfg

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@ ignore =
1212
E501
1313
F403
1414
F405
15+
W504

library/tests/conftest.py

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
import sys
2+
import mock
3+
import pytest
4+
import bme680
5+
from bme680.constants import CalibrationData
6+
7+
8+
class MockSMBus:
9+
"""Mock a basic non-presence SMBus device to cause BME680 to fail.
10+
11+
Returns 0 in all cases, so that CHIP_ID will never match.
12+
13+
"""
14+
15+
def __init__(self, bus): # noqa D107
16+
pass
17+
18+
def read_byte_data(self, addr, register):
19+
"""Return 0 for all read attempts."""
20+
return 0
21+
22+
23+
class MockSMBusPresent:
24+
"""Mock enough of the BME680 for the library to initialise and test."""
25+
26+
def __init__(self, bus):
27+
"""Initialise with test data."""
28+
self.regs = [0 for _ in range(256)]
29+
self.regs[bme680.CHIP_ID_ADDR] = bme680.CHIP_ID
30+
31+
def read_byte_data(self, addr, register):
32+
"""Read a single byte from fake registers."""
33+
return self.regs[register]
34+
35+
def write_byte_data(self, addr, register, value):
36+
"""Write a single byte to fake registers."""
37+
self.regs[register] = value
38+
39+
def read_i2c_block_data(self, addr, register, length):
40+
"""Read up to length bytes from register."""
41+
return self.regs[register:register + length]
42+
43+
44+
@pytest.fixture(scope='function', autouse=False)
45+
def smbus_notpresent():
46+
"""Mock smbus module."""
47+
smbus = mock.MagicMock()
48+
smbus.SMBus = MockSMBus
49+
sys.modules['smbus'] = smbus
50+
yield smbus
51+
del sys.modules['smbus']
52+
53+
54+
@pytest.fixture(scope='function', autouse=False)
55+
def smbus():
56+
"""Mock smbus module."""
57+
smbus = mock.MagicMock()
58+
smbus.SMBus = MockSMBusPresent
59+
sys.modules['smbus'] = smbus
60+
yield smbus
61+
del sys.modules['smbus']
62+
63+
64+
@pytest.fixture(scope='function', autouse=False)
65+
def calibration():
66+
"""Mock bme680 calibration."""
67+
calibration = CalibrationData()
68+
# Dump of calibration data borrowed from:
69+
# https://github.com/pimoroni/bme680-python/issues/11
70+
data = {
71+
'par_gh1': -30,
72+
'par_gh2': -24754,
73+
'par_gh3': 18,
74+
'par_h1': 676,
75+
'par_h2': 1029,
76+
'par_h3': 0,
77+
'par_h4': 45,
78+
'par_h5': 20,
79+
'par_h6': 120,
80+
'par_h7': -100,
81+
'par_p1': 36673,
82+
'par_p10': 30,
83+
'par_p2': -10515,
84+
'par_p3': 88,
85+
'par_p4': 7310,
86+
'par_p5': -129,
87+
'par_p6': 30,
88+
'par_p7': 46,
89+
'par_p8': -3177,
90+
'par_p9': -2379,
91+
'par_t1': 26041,
92+
'par_t2': 26469,
93+
'par_t3': 3,
94+
'range_sw_err': 0,
95+
'res_heat_range': 1,
96+
'res_heat_val': 48,
97+
't_fine': 136667
98+
}
99+
for k, v in data.items():
100+
setattr(calibration, k, v)
101+
return calibration

library/tests/test_compensation.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import bme680
2+
3+
4+
def test_calc_temperature(smbus, calibration):
5+
"""Validate temperature calculation against mock calibration data."""
6+
sensor = bme680.BME680()
7+
sensor.calibration_data = calibration
8+
assert sensor._calc_temperature(501240) == 2669
9+
assert sensor.calibration_data.t_fine == 136667
10+
11+
12+
def test_calc_pressure(smbus, calibration):
13+
"""Validate pressure calculation against mock calibration data."""
14+
sensor = bme680.BME680()
15+
sensor.calibration_data = calibration
16+
sensor._calc_temperature(501240)
17+
assert sensor._calc_pressure(353485) == 98712
18+
19+
20+
def test_calc_humidity(smbus, calibration):
21+
"""Validate humidity calculation against mock calibration data."""
22+
sensor = bme680.BME680()
23+
sensor.calibration_data = calibration
24+
sensor._calc_temperature(501240)
25+
assert sensor._calc_humidity(19019) == 42402
26+
27+
28+
def test_calc_gas_resistance(smbus, calibration):
29+
"""Validate gas calculation against mock calibration data."""
30+
sensor = bme680.BME680()
31+
sensor.calibration_data = calibration
32+
assert int(sensor._calc_gas_resistance(0, 0)) == 12946860
33+
34+
35+
def test_temp_offset(smbus, calibration):
36+
"""Validate temperature calculation with offset against mock calibration data."""
37+
sensor = bme680.BME680()
38+
sensor.calibration_data = calibration
39+
sensor.set_temp_offset(1.99)
40+
assert sensor._calc_temperature(501240) == 2669 + 199
41+
assert sensor.calibration_data.t_fine == 146830

library/tests/test_setup.py

Lines changed: 2 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,13 @@
1-
import sys
2-
import mock
31
import pytest
42
import bme680
53

64

7-
class MockSMBus:
8-
"""Mock a basic non-presence SMBus device to cause BME680 to fail.
9-
10-
Returns 0 in all cases, so that CHIP_ID will never match.
11-
12-
"""
13-
14-
def __init__(self, bus): # noqa D107
15-
pass
16-
17-
def read_byte_data(self, addr, register):
18-
"""Return 0 for all read attempts."""
19-
return 0
20-
21-
22-
class MockSMBusPresent:
23-
"""Mock enough of the BME680 for the library to initialise and test."""
24-
25-
def __init__(self, bus):
26-
"""Initialise with test data."""
27-
self.regs = [0 for _ in range(256)]
28-
self.regs[bme680.CHIP_ID_ADDR] = bme680.CHIP_ID
29-
30-
def read_byte_data(self, addr, register):
31-
"""Read a single byte from fake registers."""
32-
return self.regs[register]
33-
34-
def write_byte_data(self, addr, register, value):
35-
"""Write a single byte to fake registers."""
36-
self.regs[register] = value
37-
38-
def read_i2c_block_data(self, addr, register, length):
39-
"""Read up to length bytes from register."""
40-
return self.regs[register:register + length]
41-
42-
43-
def test_setup_not_present():
5+
def test_setup_not_present(smbus_notpresent):
446
"""Mock the adbsence of a BME680 and test initialisation."""
45-
sys.modules['smbus'] = mock.MagicMock()
46-
sys.modules['smbus'].SMBus = MockSMBus
47-
487
with pytest.raises(RuntimeError):
498
sensor = bme680.BME680() # noqa F841
509

5110

52-
def test_setup_mock_present():
11+
def test_setup_mock_present(smbus):
5312
"""Mock the presence of a BME680 and test initialisation."""
54-
sys.modules['smbus'] = mock.MagicMock()
55-
sys.modules['smbus'].SMBus = MockSMBusPresent
56-
5713
sensor = bme680.BME680() # noqa F841

0 commit comments

Comments
 (0)