File: test_pins_data.py

package info (click to toggle)
gpiozero 2.0.1-0.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 17,192 kB
  • sloc: python: 15,355; makefile: 246
file content (297 lines) | stat: -rw-r--r-- 11,885 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
# vim: set fileencoding=utf-8:
#
# GPIO Zero: a library for controlling the Raspberry Pi's GPIO pins
#
# Copyright (c) 2016-2023 Dave Jones <dave@waveform.org.uk>
# Copyright (c) 2019 Ben Nuttall <ben@bennuttall.com>
# Copyright (c) 2018 Martchus <martchus@gmx.net>
#
# SPDX-License-Identifier: BSD-3-Clause

import io
import re
import errno
import pytest
from unittest import mock

import gpiozero.pins.data
import gpiozero.pins.local
from gpiozero.pins.local import LocalPiFactory
from gpiozero.pins.pi import PiBoardInfo
from gpiozero.pins.style import Style
from gpiozero.compat import frozendict
from gpiozero import *


def test_pi_revision():
    with mock.patch('gpiozero.devices.Device.pin_factory', LocalPiFactory()):
        # Can't use MockPin for this as we want something that'll actually try
        # and read /proc/device-tree/system/linux,revision and /proc/cpuinfo
        # (MockPin simply parrots the 3B's data); LocalPiFactory is used as we
        # can definitely instantiate it (strictly speaking it's abstract but
        # we're only interested in the BoardInfo stuff)
        with mock.patch('io.open') as m:
            m.return_value.__enter__.side_effect = [
                # Pretend /proc/device-tree/system/linux,revision doesn't
                # exist, and that /proc/cpuinfo contains the Revision: 0002
                # after some filler
                IOError(errno.ENOENT, 'File not found'),
                ['lots of irrelevant', 'lines', 'followed by', 'Revision: 0002', 'Serial:  xxxxxxxxxxx']
            ]
            assert Device.pin_factory.board_info.revision == '0002'
            # LocalPiFactory caches the revision (because realistically it
            # isn't going to change at runtime); we need to wipe it here though
            Device.pin_factory._info = None
            m.return_value.__enter__.side_effect = [
                IOError(errno.ENOENT, 'File not found'),
                ['Revision: a21042']
            ]
            assert Device.pin_factory.board_info.revision == 'a21042'
            # Check over-volting result (some argument over whether this is 7
            # or 8 character result; make sure both work)
            Device.pin_factory._info = None
            m.return_value.__enter__.side_effect = [
                IOError(errno.ENOENT, 'File not found'),
                ['Revision: 1000003']
            ]
            assert Device.pin_factory.board_info.revision == '0003'
            Device.pin_factory._info = None
            m.return_value.__enter__.side_effect = [
                IOError(errno.ENOENT, 'File not found'),
                ['Revision: 100003']
            ]
            assert Device.pin_factory.board_info.revision == '0003'
            # Check we complain loudly if we can't access linux,revision
            Device.pin_factory._info = None
            m.return_value.__enter__.side_effect = [
                # Pretend /proc/device-tree/system/linux,revision doesn't
                # exist, and that /proc/cpuinfo contains the Revision: 0002
                # after some filler
                IOError(errno.EACCES, 'Permission denied'),
                ['Revision: 100003']
            ]
            with pytest.raises(IOError):
                Device.pin_factory.board_info
            # Check that parsing /proc/device-tree/system/linux,revision also
            # works properly
            Device.pin_factory._info = None
            m.return_value.__enter__.side_effect = None
            m.return_value.__enter__.return_value = io.BytesIO(b'\x00\xa2\x20\xd3')
            assert Device.pin_factory.board_info.revision == 'a220d3'
            # Check that if everything's a bust we raise PinUnknownPi
            with pytest.raises(PinUnknownPi):
                Device.pin_factory._info = None
                m.return_value.__enter__.return_value = None
                m.return_value.__enter__.side_effect = [
                    IOError(errno.ENOENT, 'File not found'),
                    ['nothing', 'relevant']
                ]
                Device.pin_factory.board_info
            with pytest.raises(PinUnknownPi):
                PiBoardInfo.from_revision(0xfff)

@pytest.mark.filterwarnings('ignore::DeprecationWarning')
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 r.ethernet == 1
    assert not r.wifi
    assert not r.bluetooth
    assert r.csi == 1
    assert r.dsi == 1
    r = pi_info('9000f1')
    assert r.model == '???'
    assert r.pcb_revision == '1.1'
    assert r.memory == 512
    assert r.manufacturer == 'Sony'
    assert r.storage == 'MicroSD'
    assert r.usb == 4
    assert r.ethernet == 1
    assert not r.wifi
    assert not r.bluetooth
    assert r.csi == 1
    assert r.dsi == 1
    assert repr(r).startswith('PiBoardInfo(revision=')
    assert 'headers=...' in repr(r)

@pytest.mark.filterwarnings('ignore::DeprecationWarning')
def test_pi_info_other_types():
    assert pi_info(b'9000f1') == pi_info(0x9000f1)

def test_find_pin():
    board_info = PiBoardInfo.from_revision(0xa21041)
    assert {('J8', 1), ('J8', 17)} == {
        (head.name, pin.number)
        for (head, pin) in board_info.find_pin('3V3')
    }
    assert {('J8', 3)} == {
        (head.name, pin.number)
        for (head, pin) in board_info.find_pin('GPIO2')
    }
    assert set() == {
        (head.name, pin.number)
        for (head, pin) in board_info.find_pin('GPIO47')
    }

@pytest.mark.filterwarnings('ignore::DeprecationWarning')
def test_physical_pins():
    # Assert physical pins for some well-known Pi's; a21041 is a Pi2B
    board_info = PiBoardInfo.from_revision(0xa21041)
    assert board_info.physical_pins('3V3') == {('J8', 1), ('J8', 17)}
    assert board_info.physical_pins('GPIO2') == {('J8', 3)}
    assert board_info.physical_pins('GPIO47') == set()

@pytest.mark.filterwarnings('ignore::DeprecationWarning')
def test_physical_pin():
    board_info = PiBoardInfo.from_revision(0xa21041)
    with pytest.raises(PinMultiplePins):
        assert board_info.physical_pin('GND')
    assert board_info.physical_pin('GPIO3') == ('J8', 5)
    with pytest.raises(PinNoPins):
        assert board_info.physical_pin('GPIO47')

@pytest.mark.filterwarnings('ignore::DeprecationWarning')
def test_pulled_up():
    board_info = PiBoardInfo.from_revision(0xa21041)
    assert board_info.pulled_up('GPIO2')
    assert not board_info.pulled_up('GPIO4')
    assert not board_info.pulled_up('GPIO47')

def test_pull():
    board_info = PiBoardInfo.from_revision(0xa21041)
    for header, pin in board_info.find_pin('GPIO2'):
        assert pin.pull == 'up'
    for header, pin in board_info.find_pin('GPIO4'):
        assert pin.pull == ''
    for header, pin in board_info.find_pin('GPIO47'):
        assert pin.pull == ''

def test_pprint_content(capsys):
    PiBoardInfo.from_revision(0x900092).pprint(color=False)
    cap = capsys.readouterr()
    assert ('-' + 'o' * 20) in cap.out # first header row
    assert (' ' + '1' + 'o' * 19) in cap.out # second header row
    assert 'PiZero' in cap.out
    assert 'V1.2' in cap.out # PCB revision
    assert '900092' in cap.out # Pi revision
    assert 'BCM2835' in cap.out # SoC name
    PiBoardInfo.from_revision(0x0002).pprint(color=False)
    cap = capsys.readouterr()
    assert ('o' * 13 + ' ') in cap.out # first header row
    assert ('1' + 'o' * 12 + ' ') in cap.out # second header row
    assert 'Pi Model' in cap.out
    assert 'B  V1.0' in cap.out # PCB revision
    assert '0002' in cap.out # Pi revision
    assert 'BCM2835' in cap.out # SOC name
    PiBoardInfo.from_revision(0x0014).headers['SODIMM'].pprint(color=False)
    cap = capsys.readouterr()
    assert len(cap.out.splitlines()) == 100

def test_format_content(capsys):
    board_info = PiBoardInfo.from_revision(0x900092)
    board_info.pprint(color=False)
    cap = capsys.readouterr()
    assert f'{board_info:mono}\n' == cap.out
    board_info.pprint(color=True)
    cap = capsys.readouterr()
    assert f'{board_info:color full}\n' == cap.out
    with pytest.raises(ValueError):
        f'{board_info:color foo}'

def test_pprint_headers(capsys):
    assert len(PiBoardInfo.from_revision(0x0002).headers) == 3
    assert len(PiBoardInfo.from_revision(0x000e).headers) == 5
    assert len(PiBoardInfo.from_revision(0x900092).headers) == 3
    PiBoardInfo.from_revision(0x0002).pprint()
    cap = capsys.readouterr()
    assert 'P1:\n' in cap.out
    assert 'P5:\n' not in cap.out
    PiBoardInfo.from_revision(0x000e).pprint()
    cap = capsys.readouterr()
    assert 'P1:\n' in cap.out
    assert 'P5:\n' in cap.out
    PiBoardInfo.from_revision(0x900092).pprint()
    cap = capsys.readouterr()
    assert 'J8:\n' in cap.out
    assert 'P1:\n' not in cap.out
    assert 'P5:\n' not in cap.out

def test_format_headers(capsys):
    board_info = PiBoardInfo.from_revision(0xc03131)
    board_info.headers['J8'].pprint(color=False)
    cap = capsys.readouterr()
    assert f'{board_info.headers["J8"]:mono}\n' == cap.out
    board_info.headers['J8'].pprint(color=True)
    cap = capsys.readouterr()
    assert f'{board_info.headers["J8"]:color}\n' == cap.out
    with pytest.raises(ValueError):
        f'{board_info.headers["J8"]:mono foo}'

def test_pprint_color(capsys):
    board_info = PiBoardInfo.from_revision(0x900092)
    board_info.pprint(color=False)
    cap = capsys.readouterr()
    assert '\x1b[0m' not in cap.out
    board_info.pprint(color=True)
    cap = capsys.readouterr()
    assert '\x1b[0m' in cap.out

def test_pprint_style_detect():
    board_info = PiBoardInfo.from_revision(0x900092)
    with mock.patch('sys.stdout') as stdout:
        stdout.output = []
        stdout.write = lambda buf: stdout.output.append(buf)
        stdout.fileno.side_effect = IOError('not a real file')
        board_info.pprint()
        s = ''.join(stdout.output)
        assert '\x1b[0m' not in s # default should output mono
        with mock.patch('os.isatty') as isatty:
            isatty.return_value = True
            stdout.fileno.side_effect = None
            stdout.fileno.return_value = 1
            stdout.output = []
            board_info.pprint()
            s = ''.join(stdout.output)
            assert '\x1b[0m' in s # default should now output color

def test_style_parser():
    with pytest.raises(ValueError):
        Style.from_style_content('mono color full')
    with pytest.raises(ValueError):
        f'{Style():foo on bar}'

def test_pprint_missing_pin(capsys):
    header = HeaderInfo('FOO', 4, 2, {
        1: PinInfo(1, '5V',    {'5V'}, '', 1, 1, set()),
        2: PinInfo(2, 'GND',   {'GND'}, '', 1, 2, set()),
        # Pin 3 is deliberately missing
        4: PinInfo(4, 'GPIO1', {'GPIO1'}, '',   2, 2, {'gpio'}),
        5: PinInfo(5, 'GPIO2', {'GPIO2'}, 'up', 3, 1, {'gpio'}),
        6: PinInfo(6, 'GPIO3', {'GPIO3'}, 'up', 3, 2, {'gpio'}),
        7: PinInfo(7, '3V3',   {'3V3'}, '', 4, 1, set()),
        8: PinInfo(8, 'GND',   {'GND'}, '', 4, 2, set()),
        })
    header.pprint()
    cap = capsys.readouterr()
    for i in range(1, 9):
        if i == 3:
            assert '(3)' not in cap.out
        else:
            assert f'({i:d})' in cap.out

def test_pprint_rows_cols():
    board_info = PiBoardInfo.from_revision(0x900092)
    assert f'{board_info.headers["J8"]:row1}' == '1o'
    assert f'{board_info.headers["J8"]:row2}' == 'oo'
    board_info = PiBoardInfo.from_revision(0x0002)
    assert f'{board_info.headers["P1"]:col1}' == '1oooooooooooo'
    assert f'{board_info.headers["P1"]:col2}' == 'ooooooooooooo'
    with pytest.raises(ValueError):
        f'{board_info.headers["P1"]:row16}'
    with pytest.raises(ValueError):
        f'{board_info.headers["P1"]:col3}'