Files
python-gpiozero/tests/test_pins_data.py
Dave Jones 6cc308e44a Fix #354, fix #389
Overhaul the pi_info system:

Pin factories are now capable of generating pi_info themselves (although
currently they all just look up the revision and call pi_info with a
specific one).

PiGPIOPin will now return pi_info for the remote pi which can be
specified by parameter or implicitly by the environment vars.

Overvolted Pis should work properly no matter what (some argument over
whether the revision 7 or 8 chars in this case; both should work). Added
some minor tweaks for the new camera-capable Pi Zero

Finally, added a bunch of tests for pins.data
2016-08-29 11:48:23 +01:00

91 lines
3.4 KiB
Python

from __future__ import (
unicode_literals,
absolute_import,
print_function,
division,
)
str = type('')
import pytest
from mock import patch, MagicMock
import gpiozero.devices
import gpiozero.pins.data
import gpiozero.pins.native
from gpiozero.pins.data import pi_info
from gpiozero import PinMultiplePins, PinNoPins, PinUnknownPi
def test_pi_revision():
save_factory = gpiozero.devices.pin_factory
try:
# Can't use MockPin for this as we want something that'll actually try
# and read /proc/cpuinfo (MockPin simply parrots the 2B's data);
# NativePin is used as we're guaranteed to be able to import it
gpiozero.devices.pin_factory = gpiozero.pins.native.NativePin
with patch('io.open') as m:
m.return_value.__enter__.return_value = ['lots of irrelevant', 'lines', 'followed by', 'Revision: 0002', 'Serial: xxxxxxxxxxx']
assert pi_info().revision == '0002'
# LocalPin caches the revision (because realistically it isn't going to
# change at runtime); we need to wipe it here though
gpiozero.pins.native.NativePin._PI_REVISION = None
m.return_value.__enter__.return_value = ['Revision: a21042']
assert pi_info().revision == 'a21042'
# Check over-volting result (some argument over whether this is 7 or
# 8 character result; make sure both work)
gpiozero.pins.native.NativePin._PI_REVISION = None
m.return_value.__enter__.return_value = ['Revision: 1000003']
assert pi_info().revision == '0003'
gpiozero.pins.native.NativePin._PI_REVISION = None
m.return_value.__enter__.return_value = ['Revision: 100003']
assert pi_info().revision == '0003'
with pytest.raises(PinUnknownPi):
m.return_value.__enter__.return_value = ['nothing', 'relevant', 'at all']
gpiozero.pins.native.NativePin._PI_REVISION = None
pi_info()
with pytest.raises(PinUnknownPi):
pi_info('0fff')
finally:
gpiozero.devices.pin_factory = save_factory
def test_pi_info():
r = pi_info('900011')
assert r.model == 'B'
assert r.pcb_revision == '1.0'
assert r.memory == 512
assert r.manufacturer == 'Sony'
assert r.storage == 'SD'
assert r.usb == 2
assert not r.wifi
assert not r.bluetooth
assert r.csi == 1
assert r.dsi == 1
with pytest.raises(PinUnknownPi):
pi_info('9000f1')
def test_pi_info_other_types():
with pytest.raises(PinUnknownPi):
pi_info(b'9000f1')
with pytest.raises(PinUnknownPi):
pi_info(0x9000f1)
def test_physical_pins():
# Assert physical pins for some well-known Pi's; a21041 is a Pi2B
assert pi_info('a21041').physical_pins('3V3') == {('P1', 1), ('P1', 17)}
assert pi_info('a21041').physical_pins('GPIO2') == {('P1', 3)}
assert pi_info('a21041').physical_pins('GPIO47') == set()
def test_physical_pin():
with pytest.raises(PinMultiplePins):
assert pi_info('a21041').physical_pin('GND')
assert pi_info('a21041').physical_pin('GPIO3') == ('P1', 5)
with pytest.raises(PinNoPins):
assert pi_info('a21041').physical_pin('GPIO47')
def test_pulled_up():
assert pi_info('a21041').pulled_up('GPIO2')
assert not pi_info('a21041').pulled_up('GPIO4')
assert not pi_info('a21041').pulled_up('GPIO47')