mirror of
https://github.com/KevinMidboe/python-gpiozero.git
synced 2025-12-08 20:39:01 +00:00
Fix #204 and start readying the release
Also re-numbers energenie sockets 1-4 (as noted by @bennuttall in comments to #239), and adds several "real pins" tests and board tests. The bad-PWM stuff is currently disabled as it causes segfaults when running the tests and I can't seem to trace the cause at the moment. Finally, I've tweaked the deb config to suggest gpiozero, removed spidev as a mandatory dep (which'll fix installs on wheezy for py3), and there's some more miscellaneous last-minute stuff here that I can't recall...
This commit is contained in:
@@ -5,7 +5,10 @@ from __future__ import (
|
||||
division,
|
||||
)
|
||||
str = type('')
|
||||
|
||||
try:
|
||||
range = xrange
|
||||
except NameError:
|
||||
pass
|
||||
|
||||
import io
|
||||
import subprocess
|
||||
@@ -13,6 +16,10 @@ import subprocess
|
||||
import pytest
|
||||
|
||||
from gpiozero import PinFixedPull, PinInvalidPull, PinInvalidFunction
|
||||
try:
|
||||
from math import isclose
|
||||
except ImportError:
|
||||
from gpiozero.compat import isclose
|
||||
|
||||
|
||||
# This module assumes you've wired the following GPIO pins together
|
||||
@@ -56,7 +63,6 @@ try:
|
||||
except ImportError:
|
||||
NativePin = None
|
||||
|
||||
|
||||
@pytest.fixture(scope='module', params=PIN_CLASSES)
|
||||
def pin_class(request):
|
||||
# pigpiod needs to be running for PiGPIOPin
|
||||
@@ -70,13 +76,13 @@ def pin_class(request):
|
||||
request.addfinalizer(kill_pigpiod)
|
||||
return request.param
|
||||
|
||||
@pytest.fixture
|
||||
@pytest.fixture(scope='function')
|
||||
def pins(request, pin_class):
|
||||
# Why return both pins in a single fixture? If we defined one fixture for
|
||||
# each pin then pytest will (correctly) test RPiGPIOPin(22) against
|
||||
# NativePin(27) and so on. This isn't supported, so we don't test it
|
||||
test_pin = pin_class(22)
|
||||
input_pin = pin_class(27)
|
||||
test_pin = pin_class(TEST_PIN)
|
||||
input_pin = pin_class(INPUT_PIN)
|
||||
input_pin.function = 'input'
|
||||
input_pin.pull = 'down'
|
||||
def fin():
|
||||
@@ -88,8 +94,8 @@ def pins(request, pin_class):
|
||||
|
||||
def test_pin_numbers(pins):
|
||||
test_pin, input_pin = pins
|
||||
assert test_pin.number == 22
|
||||
assert input_pin.number == 27
|
||||
assert test_pin.number == TEST_PIN
|
||||
assert input_pin.number == INPUT_PIN
|
||||
|
||||
def test_function_bad(pins):
|
||||
test_pin, input_pin = pins
|
||||
@@ -125,12 +131,18 @@ def test_pull_bad(pins):
|
||||
test_pin.function = 'input'
|
||||
with pytest.raises(PinInvalidPull):
|
||||
test_pin.pull = 'foo'
|
||||
with pytest.raises(PinInvalidPull):
|
||||
test_pin.input_with_pull('foo')
|
||||
|
||||
def test_pull_down_warning(pin_class):
|
||||
# XXX This assumes we're on a vaguely modern Pi and not a compute module
|
||||
# Might want to refine this with the pi-info database
|
||||
pin = pin_class(2)
|
||||
try:
|
||||
with pytest.raises(PinFixedPull):
|
||||
pin.pull = 'down'
|
||||
with pytest.raises(PinFixedPull):
|
||||
pin.input_with_pull('down')
|
||||
finally:
|
||||
pin.close()
|
||||
|
||||
@@ -141,3 +153,25 @@ def test_input_with_pull(pins):
|
||||
test_pin.input_with_pull('down')
|
||||
assert input_pin.state == 0
|
||||
|
||||
@pytest.mark.skipif(True, reason='causes segfaults')
|
||||
def test_bad_duty_cycle(pins):
|
||||
test_pin, input_pin = pins
|
||||
if test_pin.__class__.__name__ == 'NativePin':
|
||||
pytest.skip("native pin doesn't support PWM")
|
||||
test_pin.function = 'output'
|
||||
test_pin.frequency = 100
|
||||
with pytest.raises(ValueError):
|
||||
test_pin.state = 1.1
|
||||
|
||||
def test_duty_cycles(pins):
|
||||
test_pin, input_pin = pins
|
||||
if test_pin.__class__.__name__ == 'NativePin':
|
||||
pytest.skip("native pin doesn't support PWM")
|
||||
test_pin.function = 'output'
|
||||
test_pin.frequency = 100
|
||||
for duty_cycle in (0.0, 0.1, 0.5, 1.0):
|
||||
test_pin.state = duty_cycle
|
||||
assert test_pin.state == duty_cycle
|
||||
total = sum(input_pin.state for i in range(20000))
|
||||
assert isclose(total / 20000, duty_cycle, rel_tol=0.1, abs_tol=0.1)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user