Initial commit

This commit is contained in:
Paul Beech
2020-05-12 04:24:11 +01:00
commit bcf2c73bdf
50 changed files with 2866 additions and 0 deletions

90
library/tests/conftest.py Normal file
View File

@@ -0,0 +1,90 @@
"""Test configuration.
These allow the mocking of various Python modules
that might otherwise have runtime side-effects.
"""
import sys
import mock
import pytest
from i2cdevice import MockSMBus
class SMBusFakeDevice(MockSMBus):
def __init__(self, i2c_bus):
MockSMBus.__init__(self, i2c_bus)
self.regs[0x00:0x01] = 0x0f, 0x00
@pytest.fixture(scope='function', autouse=True)
def cleanup():
yield None
try:
del sys.modules['enviroplus']
except KeyError:
pass
try:
del sys.modules['enviroplus.noise']
except KeyError:
pass
try:
del sys.modules['enviroplus.gas']
except KeyError:
pass
@pytest.fixture(scope='function', autouse=False)
def GPIO():
"""Mock RPi.GPIO module."""
GPIO = mock.MagicMock()
# Fudge for Python < 37 (possibly earlier)
sys.modules['RPi'] = mock.Mock()
sys.modules['RPi'].GPIO = GPIO
sys.modules['RPi.GPIO'] = GPIO
yield GPIO
del sys.modules['RPi']
del sys.modules['RPi.GPIO']
@pytest.fixture(scope='function', autouse=False)
def spidev():
"""Mock spidev module."""
spidev = mock.MagicMock()
sys.modules['spidev'] = spidev
yield spidev
del sys.modules['spidev']
@pytest.fixture(scope='function', autouse=False)
def smbus():
"""Mock smbus module."""
smbus = mock.MagicMock()
smbus.SMBus = SMBusFakeDevice
sys.modules['smbus'] = smbus
yield smbus
del sys.modules['smbus']
@pytest.fixture(scope='function', autouse=False)
def atexit():
"""Mock atexit module."""
atexit = mock.MagicMock()
sys.modules['atexit'] = 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']

View File

@@ -0,0 +1,48 @@
import pytest
def test_noise_setup(sounddevice, numpy):
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):
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):
from enviroplus.noise import Noise
numpy.mean.return_value = 10.0
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')
assert amp_total == 10.0
def test_get_amplitude_at_frequency_range(sounddevice, numpy):
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)

View File

@@ -0,0 +1,66 @@
def test_gas_setup(GPIO, smbus):
from enviroplus import gas
gas._is_setup = False
gas.setup()
gas.setup()
def test_gas_read_all(GPIO, smbus):
from enviroplus import gas
gas._is_setup = False
result = gas.read_all()
assert type(result.oxidising) == float
assert int(result.oxidising) == 16641
assert type(result.reducing) == float
assert int(result.reducing) == 16727
assert type(result.nh3) == float
assert int(result.nh3) == 16813
assert "Oxidising" in str(result)
def test_gas_read_each(GPIO, smbus):
from enviroplus import gas
gas._is_setup = False
assert int(gas.read_oxidising()) == 16641
assert int(gas.read_reducing()) == 16727
assert int(gas.read_nh3()) == 16813
def test_gas_read_adc(GPIO, smbus):
from enviroplus import gas
gas._is_setup = False
gas.enable_adc(True)
gas.set_adc_gain(2.048)
assert gas.read_adc() == 0.255
def test_gas_read_adc_default_gain(GPIO, smbus):
from enviroplus import gas
gas._is_setup = False
gas.enable_adc(True)
gas.set_adc_gain(gas.MICS6814_GAIN)
assert gas.read_adc() == 0.765
def test_gas_read_adc_str(GPIO, smbus):
from enviroplus import gas
gas._is_setup = False
gas.enable_adc(True)
gas.set_adc_gain(2.048)
assert 'ADC' in str(gas.read_all())
def test_gas_cleanup(GPIO, smbus):
from enviroplus import gas
gas.cleanup()
GPIO.output.assert_called_with(gas.MICS6814_HEATER_PIN, 0)