mirror of
https://github.com/KevinMidboe/python-gpiozero.git
synced 2025-10-29 17:50:37 +00:00
The real pin tests were broken by the new factory stuff. This commit fixes them up, and fixes up a few other bits besides (like why the pigpio PWM tests were failing, why RPi.GPIO sometimes segfaulted on PWM tests, etc.) It also causes the real pin tests to run against MockPin (thanks to @lurch for the suggestion!). This required some tweaks to MockPin to make it emulate physically pulled up pins itself (which in turn necessitated changing quite a few pin numbers in the main test suite because we were using 2 and 3 everywhere), and to allow one MockPin to drive another. Anyway, everything's working now including all the tests on a Pi (haven't tried RPIO yet, but only because I'm on a Pi3 - everything else works with overall coverage of 88% :).
135 lines
3.6 KiB
Python
135 lines
3.6 KiB
Python
from __future__ import (
|
|
unicode_literals,
|
|
absolute_import,
|
|
print_function,
|
|
division,
|
|
)
|
|
str = type('')
|
|
|
|
import warnings
|
|
|
|
import pytest
|
|
|
|
from gpiozero import *
|
|
|
|
|
|
def teardown_function(function):
|
|
Device._pin_factory.reset()
|
|
|
|
|
|
# TODO add more devices tests!
|
|
|
|
def test_device_bad_pin():
|
|
with pytest.raises(GPIOPinMissing):
|
|
device = GPIODevice()
|
|
with pytest.raises(PinInvalidPin):
|
|
device = GPIODevice(60)
|
|
|
|
def test_device_non_physical():
|
|
with warnings.catch_warnings(record=True) as w:
|
|
device = GPIODevice(37)
|
|
assert len(w) == 1
|
|
assert w[0].category == PinNonPhysical
|
|
|
|
def test_device_init():
|
|
pin = Device._pin_factory.pin(2)
|
|
with GPIODevice(pin) as device:
|
|
assert not device.closed
|
|
assert device.pin == pin
|
|
|
|
def test_device_init_twice_same_pin():
|
|
with GPIODevice(2) as device:
|
|
with pytest.raises(GPIOPinInUse):
|
|
GPIODevice(2)
|
|
|
|
def test_device_init_twice_different_pin():
|
|
with GPIODevice(2) as device:
|
|
with GPIODevice(3) as device2:
|
|
pass
|
|
|
|
def test_device_close():
|
|
device = GPIODevice(2)
|
|
# Don't use "with" here; we're testing close explicitly
|
|
device.close()
|
|
assert device.closed
|
|
assert device.pin is None
|
|
|
|
def test_device_reopen_same_pin():
|
|
pin = Device._pin_factory.pin(2)
|
|
with GPIODevice(pin) as device:
|
|
pass
|
|
with GPIODevice(pin) as device2:
|
|
assert not device2.closed
|
|
assert device2.pin is pin
|
|
assert device.closed
|
|
assert device.pin is None
|
|
|
|
def test_device_repr():
|
|
with GPIODevice(4) as device:
|
|
assert repr(device) == '<gpiozero.GPIODevice object on pin %s, is_active=False>' % device.pin
|
|
|
|
def test_device_repr_after_close():
|
|
with GPIODevice(2) as device:
|
|
pass
|
|
assert repr(device) == '<gpiozero.GPIODevice object closed>'
|
|
|
|
def test_device_unknown_attr():
|
|
with GPIODevice(2) as device:
|
|
with pytest.raises(AttributeError):
|
|
device.foo = 1
|
|
|
|
def test_device_context_manager():
|
|
with GPIODevice(2) as device:
|
|
assert not device.closed
|
|
assert device.closed
|
|
|
|
def test_composite_device_sequence():
|
|
with CompositeDevice(
|
|
InputDevice(4),
|
|
InputDevice(5)
|
|
) as device:
|
|
assert len(device) == 2
|
|
assert device[0].pin.number == 4
|
|
assert device[1].pin.number == 5
|
|
assert device.namedtuple._fields == ('_0', '_1')
|
|
|
|
def test_composite_device_values():
|
|
with CompositeDevice(
|
|
InputDevice(4),
|
|
InputDevice(5)
|
|
) as device:
|
|
assert device.value == (0, 0)
|
|
assert not device.is_active
|
|
device[0].pin.drive_high()
|
|
assert device.value == (1, 0)
|
|
assert device.is_active
|
|
|
|
def test_composite_device_named():
|
|
with CompositeDevice(
|
|
foo=InputDevice(4),
|
|
bar=InputDevice(5),
|
|
_order=('foo', 'bar')
|
|
) as device:
|
|
assert device.namedtuple._fields == ('foo', 'bar')
|
|
assert device.value == (0, 0)
|
|
assert not device.is_active
|
|
|
|
def test_composite_device_bad_init():
|
|
with pytest.raises(ValueError):
|
|
CompositeDevice(foo=1, bar=2, _order=('foo',))
|
|
with pytest.raises(ValueError):
|
|
CompositeDevice(close=1)
|
|
with pytest.raises(ValueError):
|
|
CompositeDevice(2)
|
|
with pytest.raises(ValueError):
|
|
CompositeDevice(Device._pin_factory.pin(2))
|
|
|
|
def test_composite_device_read_only():
|
|
with CompositeDevice(
|
|
foo=InputDevice(4),
|
|
bar=InputDevice(5)
|
|
) as device:
|
|
with pytest.raises(AttributeError):
|
|
device.foo = 1
|
|
|