mirror of
https://github.com/KevinMidboe/python-gpiozero.git
synced 2025-10-29 17:50:37 +00:00
While the tests work well on a PC or Travis, the Pi (where I ought to be running them!) has some issues with the timing tests. Need to relax the tolerance of the "assert_states_and_times" method to 0.05 seconds otherwise it periodically fails even on something reasonably quick like a Pi 2 (less failures on a Pi 3 but still occasionally). Also reduced default fps to 25; if the default timing occasionally fails on a Pi 2 it's evidently too fast for a Pi 1 and shouldn't be the default; 25 also doesn't look any different to me on a pulsing LED. There's also a bunch of miscellaneous fixes in here; last minute typos and chart re-gens for the 1.2 release.
130 lines
3.3 KiB
Python
130 lines
3.3 KiB
Python
from __future__ import (
|
|
unicode_literals,
|
|
absolute_import,
|
|
print_function,
|
|
division,
|
|
)
|
|
str = type('')
|
|
|
|
|
|
import pytest
|
|
|
|
from gpiozero.pins.mock import MockPin
|
|
from gpiozero import *
|
|
|
|
|
|
def teardown_function(function):
|
|
MockPin.clear_pins()
|
|
|
|
# TODO add more devices tests!
|
|
|
|
def test_device_no_pin():
|
|
with pytest.raises(GPIOPinMissing):
|
|
device = GPIODevice()
|
|
|
|
def test_device_init():
|
|
pin = MockPin(2)
|
|
with GPIODevice(pin) as device:
|
|
assert not device.closed
|
|
assert device.pin == pin
|
|
|
|
def test_device_init_twice_same_pin():
|
|
pin = MockPin(2)
|
|
with GPIODevice(pin) as device:
|
|
with pytest.raises(GPIOPinInUse):
|
|
device2 = GPIODevice(pin)
|
|
|
|
def test_device_init_twice_different_pin():
|
|
pin = MockPin(2)
|
|
pin2 = MockPin(3)
|
|
with GPIODevice(pin) as device:
|
|
with GPIODevice(pin2) as device2:
|
|
pass
|
|
|
|
def test_device_close():
|
|
pin = MockPin(2)
|
|
device = GPIODevice(pin)
|
|
device.close()
|
|
assert device.closed
|
|
assert device.pin is None
|
|
|
|
def test_device_reopen_same_pin():
|
|
pin = MockPin(2)
|
|
device = GPIODevice(pin)
|
|
device.close()
|
|
device2 = GPIODevice(pin)
|
|
assert not device2.closed
|
|
assert device2.pin == pin
|
|
assert device.closed
|
|
assert device.pin is None
|
|
device2.close()
|
|
|
|
def test_device_repr():
|
|
pin = MockPin(2)
|
|
with GPIODevice(pin) as device:
|
|
assert repr(device) == '<gpiozero.GPIODevice object on pin %s, is_active=False>' % pin
|
|
|
|
def test_device_repr_after_close():
|
|
pin = MockPin(2)
|
|
device = GPIODevice(pin)
|
|
device.close()
|
|
assert repr(device) == '<gpiozero.GPIODevice object closed>'
|
|
|
|
def test_device_unknown_attr():
|
|
pin = MockPin(2)
|
|
with GPIODevice(pin) as device:
|
|
with pytest.raises(AttributeError):
|
|
device.foo = 1
|
|
|
|
def test_device_context_manager():
|
|
pin = MockPin(2)
|
|
with GPIODevice(pin) as device:
|
|
assert not device.closed
|
|
assert device.closed
|
|
|
|
def test_composite_device_sequence():
|
|
with CompositeDevice(
|
|
InputDevice(MockPin(2)),
|
|
InputDevice(MockPin(3))
|
|
) as device:
|
|
assert len(device) == 2
|
|
assert device[0].pin.number == 2
|
|
assert device[1].pin.number == 3
|
|
assert device.tuple._fields == ('_0', '_1')
|
|
|
|
def test_composite_device_values():
|
|
with CompositeDevice(
|
|
InputDevice(MockPin(2)),
|
|
InputDevice(MockPin(3))
|
|
) 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(MockPin(2)),
|
|
bar=InputDevice(MockPin(3)),
|
|
_order=('foo', 'bar')
|
|
) as device:
|
|
assert device.tuple._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)
|
|
|
|
def test_composite_device_read_only():
|
|
device = CompositeDevice(
|
|
foo=InputDevice(MockPin(2)),
|
|
bar=InputDevice(MockPin(3))
|
|
)
|
|
with pytest.raises(AttributeError):
|
|
device.foo = 1
|
|
|