forked from romilly/quick2wire-python-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_gpio.py
More file actions
102 lines (68 loc) · 2.87 KB
/
test_gpio.py
File metadata and controls
102 lines (68 loc) · 2.87 KB
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
import os
from quick2wire.gpio import pins, In, Out, PullDown, gpio_admin
import pytest
@pytest.mark.gpio
@pytest.mark.loopback
class TestGPIO:
def test_pin_must_be_opened_before_use_and_is_unusable_after_being_closed(self):
pin = pins.pin(0)
with pytest.raises(IOError):
pin.value
pin.open()
try:
pin.value
finally:
pin.close()
with pytest.raises(IOError):
pin.value
def test_opens_and_closes_itself_when_used_as_a_context_manager(self):
pin = pins.pin(0)
with pin:
pin.value
with pytest.raises(IOError):
pin.value
def test_exports_gpio_device_to_userspace_when_opened_and_unexports_when_closed(self):
with pins.pin(0) as pin:
assert os.path.exists('/sys/class/gpio/gpio17/value')
assert not os.path.exists('/sys/class/gpio/gpio17/value')
def test_can_set_and_query_direction_of_pin_when_open(self):
with pins.pin(0) as pin:
pin.direction = Out
assert pin.direction == Out
assert content_of("/sys/class/gpio/gpio17/direction") == "out\n"
pin.direction = In
assert pin.direction == In
assert content_of("/sys/class/gpio/gpio17/direction") == "in\n"
def test_can_set_direction_on_construction(self):
pin = pins.pin(0, Out)
assert pin.direction == Out
assert not os.path.exists("/sys/class/gpio/gpio17/direction")
with pin:
assert content_of("/sys/class/gpio/gpio17/direction") == "out\n"
assert pin.direction == Out
def test_setting_value_of_output_pin_writes_to_device_file(self):
with pins.pin(0) as pin:
pin.direction = Out
pin.value = 1
assert pin.value == 1
assert content_of('/sys/class/gpio/gpio17/value') == '1\n'
pin.value = 0
assert pin.value == 0
assert content_of('/sys/class/gpio/gpio17/value') == '0\n'
def test_direction_and_value_of_pin_is_reset_when_closed(self):
with pins.pin(0, Out) as pin:
pin.value = 1
gpio_admin("export", 17, PullDown)
try:
assert content_of('/sys/class/gpio/gpio17/value') == '0\n'
assert content_of('/sys/class/gpio/gpio17/direction') == 'in\n'
finally:
gpio_admin("unexport", 17)
def test_cannot_get_a_pin_with_an_invalid_index(self):
with pytest.raises(IndexError):
pins.pin(-1)
with pytest.raises(IndexError):
pins.pin(len(pins))
def content_of(filename):
with open(filename, 'r') as f:
return f.read()