Repackage to latest boilerplate.

This commit is contained in:
Phil Howard
2023-11-17 09:11:10 +00:00
parent 2b0c8aefd7
commit bebe4504ed
28 changed files with 541 additions and 309 deletions

73
tests/conftest.py Normal file
View File

@@ -0,0 +1,73 @@
"""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['grow']
except KeyError:
pass
try:
del sys.modules['grow.moisture']
except KeyError:
pass
try:
del sys.modules['grow.pump']
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']

47
tests/test_lock.py Normal file
View File

@@ -0,0 +1,47 @@
import time
def test_pumps_actually_stop(GPIO, smbus):
from grow.pump import Pump, global_lock
ch1 = Pump(channel=1)
ch1.dose(speed=0.5, timeout=0.05, blocking=False)
time.sleep(0.1)
assert ch1.get_speed() == 0
def test_pumps_are_mutually_exclusive(GPIO, smbus):
from grow.pump import Pump, global_lock
ch1 = Pump(channel=1)
ch2 = Pump(channel=2)
ch3 = Pump(channel=3)
ch1.dose(speed=0.5, timeout=1.0, blocking=False)
assert global_lock.locked() is True
assert ch2.dose(speed=0.5) is False
assert ch2.dose(speed=0.5, blocking=False) is False
assert ch3.dose(speed=0.5) is False
assert ch3.dose(speed=0.5, blocking=False) is False
def test_pumps_run_sequentially(GPIO, smbus):
from grow.pump import Pump, global_lock
ch1 = Pump(channel=1)
ch2 = Pump(channel=2)
ch3 = Pump(channel=3)
assert ch1.dose(speed=0.5, timeout=0.1, blocking=False) is True
assert global_lock.locked() is True
time.sleep(0.3)
assert ch2.dose(speed=0.5, timeout=0.1, blocking=False) is True
assert global_lock.locked() is True
time.sleep(0.3)
assert ch3.dose(speed=0.5, timeout=0.1, blocking=False) is True
assert global_lock.locked() is True
time.sleep(0.3)

50
tests/test_setup.py Normal file
View File

@@ -0,0 +1,50 @@
import mock
def test_moisture_setup(GPIO, smbus):
from grow.moisture import Moisture
ch1 = Moisture(channel=1)
ch2 = Moisture(channel=2)
ch3 = Moisture(channel=3)
GPIO.setup.assert_has_calls([
mock.call(ch1._gpio_pin, GPIO.IN),
mock.call(ch2._gpio_pin, GPIO.IN),
mock.call(ch3._gpio_pin, GPIO.IN)
])
def test_moisture_read(GPIO, smbus):
from grow.moisture import Moisture
assert Moisture(channel=1).saturation == 1.0
assert Moisture(channel=2).saturation == 1.0
assert Moisture(channel=3).saturation == 1.0
assert Moisture(channel=1).moisture == 0
assert Moisture(channel=2).moisture == 0
assert Moisture(channel=3).moisture == 0
def test_pump_setup(GPIO, smbus):
from grow.pump import PUMP_PWM_FREQ, Pump
ch1 = Pump(channel=1)
ch2 = Pump(channel=2)
ch3 = Pump(channel=3)
GPIO.setup.assert_has_calls([
mock.call(ch1._gpio_pin, GPIO.OUT, initial=GPIO.LOW),
mock.call(ch2._gpio_pin, GPIO.OUT, initial=GPIO.LOW),
mock.call(ch3._gpio_pin, GPIO.OUT, initial=GPIO.LOW)
])
GPIO.PWM.assert_has_calls([
mock.call(ch1._gpio_pin, PUMP_PWM_FREQ),
mock.call().start(0),
mock.call(ch2._gpio_pin, PUMP_PWM_FREQ),
mock.call().start(0),
mock.call(ch3._gpio_pin, PUMP_PWM_FREQ),
mock.call().start(0)
])