From: Phil Howard Date: Tue, 17 Mar 2020 12:31:33 +0000 (+0000) Subject: Test noise, fix gas X-Git-Url: https://zdv2.bktei.com/gitweb/EVA-2020-02-2.git/commitdiff_plain/e9c93677beeb6c6416d8f15aa2bd29ce3cd4b726?ds=inline;hp=be4d0fc9022240b5047654523aab877cbaa8a73d Test noise, fix gas --- diff --git a/library/tests/conftest.py b/library/tests/conftest.py index 8a3ffb4..b026172 100644 --- a/library/tests/conftest.py +++ b/library/tests/conftest.py @@ -54,3 +54,20 @@ def atexit(): yield atexit del sys.modules['atexit'] + +@pytest.fixture(scope='function', autouse=False) +def sounddevice(): + """Mock sounddevice module.""" + sounddevice = mock.MagicMock() + sys.modules['sounddevice'] = sounddevice + yield sounddevice + del sys.modules['sounddevice'] + + +@pytest.fixture(scope='function', autouse=False) +def numpy(): + """Mock numpy module.""" + numpy = mock.MagicMock() + sys.modules['numpy'] = numpy + yield numpy + del sys.modules['numpy'] diff --git a/library/tests/test_noise.py b/library/tests/test_noise.py new file mode 100644 index 0000000..c93f8cc --- /dev/null +++ b/library/tests/test_noise.py @@ -0,0 +1,78 @@ +import sys +import mock +import pytest + + +def force_reimport(module): + """Force the module under test to be re-imported. + + Because pytest runs all tests within the same scope (this makes me cry) + we have to do some manual housekeeping to avoid tests polluting each other. + + Since conftest.py already does some sys.modules mangling I see no reason not to + do the same thing here. + """ + if "." in module: + steps = module.split(".") + else: + steps = [module] + + for i in range(len(steps)): + module = ".".join(steps[0:i + 1]) + try: + del sys.modules[module] + except KeyError: + pass + + +def test_noise_setup(sounddevice, numpy): + force_reimport('enviroplus.noise') + from enviroplus.noise import Noise + + noise = Noise(sample_rate=16000, duration=0.1) + del noise + + +def test_noise_get_amplitudes_at_frequency_ranges(sounddevice, numpy): + # Ippity zippidy what is this farce + # a curious function that makes my tests pass? + force_reimport('enviroplus.noise') + from enviroplus.noise import Noise + + noise = Noise(sample_rate=16000, duration=0.1) + noise.get_amplitudes_at_frequency_ranges([ + (100, 500), + (501, 1000) + ]) + + sounddevice.rec.assert_called_with(0.1 * 16000, samplerate=16000, blocking=True, channels=1, dtype='float64') + + +def test_noise_get_noise_profile(sounddevice, numpy): + # Ippity zippidy what is this farce + # a curious function that makes my tests pass? + force_reimport('enviroplus.noise') + from enviroplus.noise import Noise + + noise = Noise(sample_rate=16000, duration=0.1) + amp_low, amp_mid, amp_high, amp_total = noise.get_noise_profile( + noise_floor=100, + low=0.12, + mid=0.36, + high=None) + + sounddevice.rec.assert_called_with(0.1 * 16000, samplerate=16000, blocking=True, channels=1, dtype='float64') + + +def test_get_amplitude_at_frequency_range(sounddevice, numpy): + # Ippity zippidy what is this farce + # a curious function that makes my tests pass? + force_reimport('enviroplus.noise') + from enviroplus.noise import Noise + + noise = Noise(sample_rate=16000, duration=0.1) + + noise.get_amplitude_at_frequency_range(0, 8000) + + with pytest.raises(ValueError): + noise.get_amplitude_at_frequency_range(0, 16000) diff --git a/library/tests/test_setup.py b/library/tests/test_setup.py index 95080b6..6b6658c 100644 --- a/library/tests/test_setup.py +++ b/library/tests/test_setup.py @@ -2,6 +2,28 @@ import sys import mock +def force_reimport(module): + """Force the module under test to be re-imported. + + Because pytest runs all tests within the same scope (this makes me cry) + we have to do some manual housekeeping to avoid tests polluting each other. + + Since conftest.py already does some sys.modules mangling I see no reason not to + do the same thing here. + """ + if "." in module: + steps = module.split(".") + else: + steps = [module] + + for i in range(len(steps)): + module = ".".join(steps[0:i + 1]) + try: + del sys.modules[module] + except KeyError: + pass + + def test_gas_setup(GPIO, smbus): from enviroplus import gas gas._is_setup = False @@ -49,7 +71,8 @@ def test_gas_read_adc_default_gain(GPIO, smbus): gas._is_setup = False gas.enable_adc(True) - assert gas.read_adc() == 0.255 + gas.set_adc_gain(gas.MICS6814_GAIN) + assert gas.read_adc() == 0.765 def test_gas_read_adc_str(GPIO, smbus): @@ -59,3 +82,12 @@ def test_gas_read_adc_str(GPIO, smbus): gas.enable_adc(True) gas.set_adc_gain(2.048) assert 'ADC' in str(gas.read_all()) + + +def test_gas_cleanup(GPIO, smbus): + force_reimport('enviroplus.gas') + from enviroplus import gas + + gas.cleanup() + + GPIO.output.assert_called_with(gas.MICS6814_HEATER_PIN, 0)