CI: Fixup tests.

This commit is contained in:
Phil Howard
2025-02-26 14:11:49 +00:00
parent 3ff4f65a61
commit ff8bd71fd5
3 changed files with 38 additions and 30 deletions

View File

@@ -26,12 +26,24 @@ def cleanup():
@pytest.fixture(scope='function', autouse=False)
def GPIO():
def gpiod():
"""Mock gpiod module."""
gpiod = mock.MagicMock()
sys.modules['gpiod'] = gpiod
yield gpiod
del sys.modules['gpiod']
sys.modules["gpiod"] = mock.Mock()
sys.modules["gpiod.line"] = mock.Mock()
yield sys.modules["gpiod"]
del sys.modules["gpiod.line"]
del sys.modules["gpiod"]
@pytest.fixture(scope="function", autouse=False)
def gpiodevice():
gpiodevice = mock.Mock()
gpiodevice.get_pins_for_platform.return_value = [(mock.Mock(), 0), (mock.Mock(), 0), (mock.Mock(), 0)]
gpiodevice.get_pin.return_value = (mock.Mock(), 0)
sys.modules["gpiodevice"] = gpiodevice
yield gpiodevice
del sys.modules["gpiodevice"]
@pytest.fixture(scope='function', autouse=False)

View File

@@ -1,8 +1,9 @@
import time
def test_pumps_actually_stop(gpiod, smbus2):
def test_pumps_actually_stop(gpiod, gpiodevice, smbus2):
from grow.pump import Pump
from grow.pwm import PWM
ch1 = Pump(channel=1)
@@ -10,9 +11,13 @@ def test_pumps_actually_stop(gpiod, smbus2):
time.sleep(0.1)
assert ch1.get_speed() == 0
PWM.stop_thread()
def test_pumps_are_mutually_exclusive(gpiod, smbus2):
def test_pumps_are_mutually_exclusive(gpiod, gpiodevice, smbus2):
from grow.pump import Pump, global_lock
from grow.pwm import PWM
ch1 = Pump(channel=1)
ch2 = Pump(channel=2)
@@ -28,9 +33,13 @@ def test_pumps_are_mutually_exclusive(gpiod, smbus2):
assert ch3.dose(speed=0.5) is False
assert ch3.dose(speed=0.5, blocking=False) is False
PWM.stop_thread()
def test_pumps_run_sequentially(gpiod, smbus2):
def test_pumps_run_sequentially(gpiod, gpiodevice, smbus2):
from grow.pump import Pump, global_lock
from grow.pwm import PWM
ch1 = Pump(channel=1)
ch2 = Pump(channel=2)
@@ -45,3 +54,5 @@ def test_pumps_run_sequentially(gpiod, smbus2):
assert ch3.dose(speed=0.5, timeout=0.1, blocking=False) is True
assert global_lock.locked() is True
time.sleep(0.3)
PWM.stop_thread()

View File

@@ -1,21 +1,16 @@
import mock
def test_moisture_setup(gpiod, smbus2):
def test_moisture_setup(gpiod, gpiodevice, smbus2):
from grow.moisture import Moisture
from datetime import timedelta
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(gpiod, smbus2):
def test_moisture_read(gpiod, gpiodevice, smbus2):
from grow.moisture import Moisture
assert Moisture(channel=1).saturation == 1.0
@@ -27,24 +22,14 @@ def test_moisture_read(gpiod, smbus2):
assert Moisture(channel=3).moisture == 0
def test_pump_setup(gpiod, smbus2):
def test_pump_setup(gpiod, gpiodevice, smbus2):
from grow.pump import PUMP_PWM_FREQ, Pump
from grow.pwm import PWM
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)
])
# Threads. Not even once.
PWM.stop_thread()
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)
])