mirror of
https://github.com/KevinMidboe/python-gpiozero.git
synced 2025-10-29 17:50:37 +00:00
Make tests work reliably on the Pi
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.
This commit is contained in:
@@ -25,131 +25,121 @@ def teardown_function(function):
|
||||
|
||||
def test_input_initial_values():
|
||||
pin = MockPin(2)
|
||||
device = InputDevice(pin, pull_up=True)
|
||||
assert pin.function == 'input'
|
||||
assert pin.pull == 'up'
|
||||
assert device.pull_up
|
||||
device.close()
|
||||
device = InputDevice(pin, pull_up=False)
|
||||
assert pin.pull == 'down'
|
||||
assert not device.pull_up
|
||||
device.close()
|
||||
with InputDevice(pin, pull_up=True) as device:
|
||||
assert pin.function == 'input'
|
||||
assert pin.pull == 'up'
|
||||
assert device.pull_up
|
||||
device.close()
|
||||
device = InputDevice(pin, pull_up=False)
|
||||
assert pin.pull == 'down'
|
||||
assert not device.pull_up
|
||||
|
||||
def test_input_is_active():
|
||||
pin = MockPin(2)
|
||||
device = InputDevice(pin, pull_up=True)
|
||||
pin.drive_high()
|
||||
assert not device.is_active
|
||||
pin.drive_low()
|
||||
assert device.is_active
|
||||
with InputDevice(pin, pull_up=True) as device:
|
||||
pin.drive_high()
|
||||
assert not device.is_active
|
||||
pin.drive_low()
|
||||
assert device.is_active
|
||||
|
||||
def test_input_pulled_up():
|
||||
pin = MockPulledUpPin(2)
|
||||
with pytest.raises(PinFixedPull):
|
||||
device = InputDevice(pin, pull_up=False)
|
||||
InputDevice(pin, pull_up=False)
|
||||
|
||||
def test_input_event_activated():
|
||||
event = Event()
|
||||
pin = MockPin(2)
|
||||
device = DigitalInputDevice(pin)
|
||||
device.when_activated = lambda: event.set()
|
||||
assert not event.wait(0)
|
||||
pin.drive_high()
|
||||
assert event.wait(0)
|
||||
with DigitalInputDevice(pin) as device:
|
||||
device.when_activated = lambda: event.set()
|
||||
assert not event.wait(0)
|
||||
pin.drive_high()
|
||||
assert event.wait(0)
|
||||
|
||||
def test_input_event_deactivated():
|
||||
event = Event()
|
||||
pin = MockPin(2)
|
||||
device = DigitalInputDevice(pin)
|
||||
device.when_deactivated = lambda: event.set()
|
||||
assert not event.wait(0)
|
||||
pin.drive_high()
|
||||
assert not event.wait(0)
|
||||
pin.drive_low()
|
||||
assert event.wait(0)
|
||||
with DigitalInputDevice(pin) as device:
|
||||
device.when_deactivated = lambda: event.set()
|
||||
assert not event.wait(0)
|
||||
pin.drive_high()
|
||||
assert not event.wait(0)
|
||||
pin.drive_low()
|
||||
assert event.wait(0)
|
||||
|
||||
def test_input_wait_active():
|
||||
pin = MockPin(2)
|
||||
device = DigitalInputDevice(pin)
|
||||
pin.drive_high()
|
||||
assert device.wait_for_active(1)
|
||||
assert not device.wait_for_inactive(0)
|
||||
with DigitalInputDevice(pin) as device:
|
||||
pin.drive_high()
|
||||
assert device.wait_for_active(1)
|
||||
assert not device.wait_for_inactive(0)
|
||||
|
||||
def test_input_wait_inactive():
|
||||
pin = MockPin(2)
|
||||
device = DigitalInputDevice(pin)
|
||||
assert device.wait_for_inactive(1)
|
||||
assert not device.wait_for_active(0)
|
||||
with DigitalInputDevice(pin) as device:
|
||||
assert device.wait_for_inactive(1)
|
||||
assert not device.wait_for_active(0)
|
||||
|
||||
def test_input_smoothed_attrib():
|
||||
pin = MockPin(2)
|
||||
device = SmoothedInputDevice(pin, threshold=0.5, queue_len=5, partial=False)
|
||||
assert device.threshold == 0.5
|
||||
assert device.queue_len == 5
|
||||
assert not device.partial
|
||||
device._queue.start()
|
||||
assert not device.is_active
|
||||
|
||||
def test_input_smoothed_silly():
|
||||
pin = MockPin(2)
|
||||
with pytest.raises(InputDeviceError):
|
||||
device = SmoothedInputDevice(pin, threshold=-1)
|
||||
device = SmoothedInputDevice(pin)
|
||||
del device._queue.stopping
|
||||
with pytest.raises(AttributeError):
|
||||
device.close()
|
||||
with SmoothedInputDevice(pin, threshold=0.5, queue_len=5, partial=False) as device:
|
||||
assert device.threshold == 0.5
|
||||
assert device.queue_len == 5
|
||||
assert not device.partial
|
||||
device._queue.start()
|
||||
assert not device.is_active
|
||||
|
||||
def test_input_smoothed_values():
|
||||
pin = MockPin(2)
|
||||
device = SmoothedInputDevice(pin)
|
||||
device._queue.start()
|
||||
assert not device.is_active
|
||||
pin.drive_high()
|
||||
assert device.wait_for_active(1)
|
||||
pin.drive_low()
|
||||
assert device.wait_for_inactive(1)
|
||||
with SmoothedInputDevice(pin) as device:
|
||||
device._queue.start()
|
||||
assert not device.is_active
|
||||
pin.drive_high()
|
||||
assert device.wait_for_active(1)
|
||||
pin.drive_low()
|
||||
assert device.wait_for_inactive(1)
|
||||
|
||||
def test_input_button():
|
||||
pin = MockPin(2)
|
||||
button = Button(pin)
|
||||
assert pin.pull == 'up'
|
||||
assert not button.is_pressed
|
||||
pin.drive_low()
|
||||
assert button.is_pressed
|
||||
assert button.wait_for_press(1)
|
||||
pin.drive_high()
|
||||
assert not button.is_pressed
|
||||
assert button.wait_for_release(1)
|
||||
with Button(pin) as button:
|
||||
assert pin.pull == 'up'
|
||||
assert not button.is_pressed
|
||||
pin.drive_low()
|
||||
assert button.is_pressed
|
||||
assert button.wait_for_press(1)
|
||||
pin.drive_high()
|
||||
assert not button.is_pressed
|
||||
assert button.wait_for_release(1)
|
||||
|
||||
def test_input_line_sensor():
|
||||
pin = MockPin(2)
|
||||
sensor = LineSensor(pin)
|
||||
pin.drive_low() # logic is inverted for line sensor
|
||||
assert sensor.wait_for_line(1)
|
||||
assert sensor.line_detected
|
||||
pin.drive_high()
|
||||
assert sensor.wait_for_no_line(1)
|
||||
assert not sensor.line_detected
|
||||
with LineSensor(pin) as sensor:
|
||||
pin.drive_low() # logic is inverted for line sensor
|
||||
assert sensor.wait_for_line(1)
|
||||
assert sensor.line_detected
|
||||
pin.drive_high()
|
||||
assert sensor.wait_for_no_line(1)
|
||||
assert not sensor.line_detected
|
||||
|
||||
def test_input_motion_sensor():
|
||||
pin = MockPin(2)
|
||||
sensor = MotionSensor(pin)
|
||||
pin.drive_high()
|
||||
assert sensor.wait_for_motion(1)
|
||||
assert sensor.motion_detected
|
||||
pin.drive_low()
|
||||
assert sensor.wait_for_no_motion(1)
|
||||
assert not sensor.motion_detected
|
||||
with MotionSensor(pin) as sensor:
|
||||
pin.drive_high()
|
||||
assert sensor.wait_for_motion(1)
|
||||
assert sensor.motion_detected
|
||||
pin.drive_low()
|
||||
assert sensor.wait_for_no_motion(1)
|
||||
assert not sensor.motion_detected
|
||||
|
||||
@pytest.mark.skipif(hasattr(sys, 'pypy_version_info'),
|
||||
reason='timing is too random on pypy')
|
||||
def test_input_light_sensor():
|
||||
pin = MockChargingPin(2)
|
||||
sensor = LightSensor(pin)
|
||||
pin.charge_time = 0.1
|
||||
assert sensor.wait_for_dark(1)
|
||||
pin.charge_time = 0.0
|
||||
assert sensor.wait_for_light(1)
|
||||
with LightSensor(pin) as sensor:
|
||||
pin.charge_time = 0.1
|
||||
assert sensor.wait_for_dark(1)
|
||||
pin.charge_time = 0.0
|
||||
assert sensor.wait_for_light(1)
|
||||
|
||||
@pytest.mark.skipif(hasattr(sys, 'pypy_version_info'),
|
||||
reason='timing is too random on pypy')
|
||||
@@ -162,22 +152,22 @@ def test_input_distance_sensor():
|
||||
DistanceSensor(echo_pin, trig_pin, max_distance=-1)
|
||||
# normal queue len is large (because the sensor is *really* jittery) but
|
||||
# we want quick tests and we've got precisely controlled pins :)
|
||||
sensor = DistanceSensor(echo_pin, trig_pin, queue_len=5, max_distance=1)
|
||||
assert sensor.max_distance == 1
|
||||
assert sensor.trigger is trig_pin
|
||||
assert sensor.echo is echo_pin
|
||||
assert sensor.wait_for_out_of_range(1)
|
||||
assert not sensor.in_range
|
||||
assert sensor.distance == 1.0 # should be waay before max-distance so this should work
|
||||
trig_pin.echo_time = 0.0
|
||||
assert sensor.wait_for_in_range(1)
|
||||
assert sensor.in_range
|
||||
assert sensor.distance < sensor.threshold_distance # depending on speed of machine, may not reach 0 here
|
||||
sensor.threshold_distance = 0.1
|
||||
assert sensor.threshold_distance == 0.1
|
||||
with pytest.raises(ValueError):
|
||||
sensor.max_distance = -1
|
||||
sensor.max_distance = 20
|
||||
assert sensor.max_distance == 20
|
||||
assert sensor.threshold_distance == 0.1
|
||||
with DistanceSensor(echo_pin, trig_pin, queue_len=5, max_distance=1) as sensor:
|
||||
assert sensor.max_distance == 1
|
||||
assert sensor.trigger is trig_pin
|
||||
assert sensor.echo is echo_pin
|
||||
assert sensor.wait_for_out_of_range(1)
|
||||
assert not sensor.in_range
|
||||
assert sensor.distance == 1.0 # should be waay before max-distance so this should work
|
||||
trig_pin.echo_time = 0.0
|
||||
assert sensor.wait_for_in_range(1)
|
||||
assert sensor.in_range
|
||||
assert sensor.distance < sensor.threshold_distance # depending on speed of machine, may not reach 0 here
|
||||
sensor.threshold_distance = 0.1
|
||||
assert sensor.threshold_distance == 0.1
|
||||
with pytest.raises(ValueError):
|
||||
sensor.max_distance = -1
|
||||
sensor.max_distance = 20
|
||||
assert sensor.max_distance == 20
|
||||
assert sensor.threshold_distance == 0.1
|
||||
|
||||
|
||||
Reference in New Issue
Block a user