Skip to content

Commit 04e4888

Browse files
authored
Merge pull request #62 from pimoroni/patch-noise-and-test-fixes
Catch #61 with tests and fix
2 parents 87221e4 + 5a376dd commit 04e4888

File tree

4 files changed

+22
-62
lines changed

4 files changed

+22
-62
lines changed

library/enviroplus/noise.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ def get_noise_profile(self,
7676
amp_low = numpy.mean(magnitude[noise_floor:mid_start])
7777
amp_mid = numpy.mean(magnitude[mid_start:high_start])
7878
amp_high = numpy.mean(magnitude[high_start:noise_ceiling])
79-
amp_total = (low + mid + high) / 3.0
79+
amp_total = (amp_low + amp_mid + amp_high) / 3.0
8080

8181
return amp_low, amp_mid, amp_high, amp_total
8282

library/tests/conftest.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,23 @@ def __init__(self, i2c_bus):
1414
self.regs[0x00:0x01] = 0x0f, 0x00
1515

1616

17+
@pytest.fixture(scope='function', autouse=True)
18+
def cleanup():
19+
yield None
20+
try:
21+
del sys.modules['enviroplus']
22+
except KeyError:
23+
pass
24+
try:
25+
del sys.modules['enviroplus.noise']
26+
except KeyError:
27+
pass
28+
try:
29+
del sys.modules['enviroplus.gas']
30+
except KeyError:
31+
pass
32+
33+
1734
@pytest.fixture(scope='function', autouse=False)
1835
def GPIO():
1936
"""Mock RPi.GPIO module."""

library/tests/test_noise.py

Lines changed: 4 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,14 @@
1-
import sys
2-
import mock
31
import pytest
42

53

6-
def force_reimport(module):
7-
"""Force the module under test to be re-imported.
8-
9-
Because pytest runs all tests within the same scope (this makes me cry)
10-
we have to do some manual housekeeping to avoid tests polluting each other.
11-
12-
Since conftest.py already does some sys.modules mangling I see no reason not to
13-
do the same thing here.
14-
"""
15-
if "." in module:
16-
steps = module.split(".")
17-
else:
18-
steps = [module]
19-
20-
for i in range(len(steps)):
21-
module = ".".join(steps[0:i + 1])
22-
try:
23-
del sys.modules[module]
24-
except KeyError:
25-
pass
26-
27-
284
def test_noise_setup(sounddevice, numpy):
29-
force_reimport('enviroplus.noise')
305
from enviroplus.noise import Noise
316

327
noise = Noise(sample_rate=16000, duration=0.1)
338
del noise
349

3510

3611
def test_noise_get_amplitudes_at_frequency_ranges(sounddevice, numpy):
37-
# Ippity zippidy what is this farce
38-
# a curious function that makes my tests pass?
39-
force_reimport('enviroplus.noise')
4012
from enviroplus.noise import Noise
4113

4214
noise = Noise(sample_rate=16000, duration=0.1)
@@ -49,11 +21,10 @@ def test_noise_get_amplitudes_at_frequency_ranges(sounddevice, numpy):
4921

5022

5123
def test_noise_get_noise_profile(sounddevice, numpy):
52-
# Ippity zippidy what is this farce
53-
# a curious function that makes my tests pass?
54-
force_reimport('enviroplus.noise')
5524
from enviroplus.noise import Noise
5625

26+
numpy.mean.return_value = 10.0
27+
5728
noise = Noise(sample_rate=16000, duration=0.1)
5829
amp_low, amp_mid, amp_high, amp_total = noise.get_noise_profile(
5930
noise_floor=100,
@@ -63,11 +34,10 @@ def test_noise_get_noise_profile(sounddevice, numpy):
6334

6435
sounddevice.rec.assert_called_with(0.1 * 16000, samplerate=16000, blocking=True, channels=1, dtype='float64')
6536

37+
assert amp_total == 10.0
38+
6639

6740
def test_get_amplitude_at_frequency_range(sounddevice, numpy):
68-
# Ippity zippidy what is this farce
69-
# a curious function that makes my tests pass?
70-
force_reimport('enviroplus.noise')
7141
from enviroplus.noise import Noise
7242

7343
noise = Noise(sample_rate=16000, duration=0.1)

library/tests/test_setup.py

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,3 @@
1-
import sys
2-
import mock
3-
4-
5-
def force_reimport(module):
6-
"""Force the module under test to be re-imported.
7-
8-
Because pytest runs all tests within the same scope (this makes me cry)
9-
we have to do some manual housekeeping to avoid tests polluting each other.
10-
11-
Since conftest.py already does some sys.modules mangling I see no reason not to
12-
do the same thing here.
13-
"""
14-
if "." in module:
15-
steps = module.split(".")
16-
else:
17-
steps = [module]
18-
19-
for i in range(len(steps)):
20-
module = ".".join(steps[0:i + 1])
21-
try:
22-
del sys.modules[module]
23-
except KeyError:
24-
pass
25-
26-
271
def test_gas_setup(GPIO, smbus):
282
from enviroplus import gas
293
gas._is_setup = False
@@ -85,7 +59,6 @@ def test_gas_read_adc_str(GPIO, smbus):
8559

8660

8761
def test_gas_cleanup(GPIO, smbus):
88-
force_reimport('enviroplus.gas')
8962
from enviroplus import gas
9063

9164
gas.cleanup()

0 commit comments

Comments
 (0)