yield atexit\r
del sys.modules['atexit']\r
\r
+\r
+@pytest.fixture(scope='function', autouse=False)\r
+def sounddevice():\r
+ """Mock sounddevice module."""\r
+ sounddevice = mock.MagicMock()\r
+ sys.modules['sounddevice'] = sounddevice\r
+ yield sounddevice\r
+ del sys.modules['sounddevice']\r
+\r
+\r
+@pytest.fixture(scope='function', autouse=False)\r
+def numpy():\r
+ """Mock numpy module."""\r
+ numpy = mock.MagicMock()\r
+ sys.modules['numpy'] = numpy\r
+ yield numpy\r
+ del sys.modules['numpy']\r
--- /dev/null
+import sys\r
+import mock\r
+import pytest\r
+\r
+\r
+def force_reimport(module):\r
+ """Force the module under test to be re-imported.\r
+\r
+ Because pytest runs all tests within the same scope (this makes me cry)\r
+ we have to do some manual housekeeping to avoid tests polluting each other.\r
+\r
+ Since conftest.py already does some sys.modules mangling I see no reason not to\r
+ do the same thing here.\r
+ """\r
+ if "." in module:\r
+ steps = module.split(".")\r
+ else:\r
+ steps = [module]\r
+ \r
+ for i in range(len(steps)):\r
+ module = ".".join(steps[0:i + 1])\r
+ try:\r
+ del sys.modules[module]\r
+ except KeyError:\r
+ pass\r
+\r
+\r
+def test_noise_setup(sounddevice, numpy):\r
+ force_reimport('enviroplus.noise')\r
+ from enviroplus.noise import Noise\r
+\r
+ noise = Noise(sample_rate=16000, duration=0.1)\r
+ del noise\r
+\r
+\r
+def test_noise_get_amplitudes_at_frequency_ranges(sounddevice, numpy):\r
+ # Ippity zippidy what is this farce\r
+ # a curious function that makes my tests pass?\r
+ force_reimport('enviroplus.noise')\r
+ from enviroplus.noise import Noise\r
+\r
+ noise = Noise(sample_rate=16000, duration=0.1)\r
+ noise.get_amplitudes_at_frequency_ranges([\r
+ (100, 500),\r
+ (501, 1000)\r
+ ])\r
+\r
+ sounddevice.rec.assert_called_with(0.1 * 16000, samplerate=16000, blocking=True, channels=1, dtype='float64')\r
+\r
+\r
+def test_noise_get_noise_profile(sounddevice, numpy):\r
+ # Ippity zippidy what is this farce\r
+ # a curious function that makes my tests pass?\r
+ force_reimport('enviroplus.noise')\r
+ from enviroplus.noise import Noise\r
+\r
+ noise = Noise(sample_rate=16000, duration=0.1)\r
+ amp_low, amp_mid, amp_high, amp_total = noise.get_noise_profile(\r
+ noise_floor=100,\r
+ low=0.12,\r
+ mid=0.36,\r
+ high=None)\r
+\r
+ sounddevice.rec.assert_called_with(0.1 * 16000, samplerate=16000, blocking=True, channels=1, dtype='float64')\r
+\r
+\r
+def test_get_amplitude_at_frequency_range(sounddevice, numpy):\r
+ # Ippity zippidy what is this farce\r
+ # a curious function that makes my tests pass?\r
+ force_reimport('enviroplus.noise')\r
+ from enviroplus.noise import Noise\r
+\r
+ noise = Noise(sample_rate=16000, duration=0.1)\r
+\r
+ noise.get_amplitude_at_frequency_range(0, 8000)\r
+\r
+ with pytest.raises(ValueError):\r
+ noise.get_amplitude_at_frequency_range(0, 16000)\r
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
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):
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)