forked from fossasia/pslab-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_serial_handler.py
More file actions
137 lines (93 loc) · 4.07 KB
/
test_serial_handler.py
File metadata and controls
137 lines (93 loc) · 4.07 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
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
import pytest
from serial import SerialException
from serial.tools.list_ports_common import ListPortInfo
import pslab.protocol as CP
from pslab.serial_handler import detect, SerialHandler
VERSION = "PSLab vMOCK\n"
PORT = "mock_port"
PORT2 = "mock_port_2"
def mock_ListPortInfo(found=True, multiple=False):
if found:
if multiple:
yield from [ListPortInfo(device=PORT), ListPortInfo(device=PORT2)]
else:
yield ListPortInfo(device=PORT)
else:
return
@pytest.fixture
def mock_serial(mocker):
serial_patch = mocker.patch("pslab.serial_handler.serial.Serial")
serial_patch().readline.return_value = VERSION.encode()
serial_patch().is_open = False
return serial_patch
@pytest.fixture
def mock_handler(mocker, mock_serial, mock_list_ports):
mocker.patch("pslab.serial_handler.SerialHandler.check_serial_access_permission")
mock_list_ports.grep.return_value = mock_ListPortInfo()
return SerialHandler()
@pytest.fixture
def mock_list_ports(mocker):
return mocker.patch("pslab.serial_handler.list_ports")
def test_detect(mocker, mock_serial, mock_list_ports):
mock_list_ports.grep.return_value = mock_ListPortInfo(multiple=True)
assert len(detect()) == 2
def test_connect_scan_port(mocker, mock_serial, mock_list_ports):
mock_list_ports.grep.return_value = mock_ListPortInfo()
mocker.patch("pslab.serial_handler.SerialHandler.check_serial_access_permission")
SerialHandler()
mock_serial().open.assert_called()
def test_connect_scan_failure(mocker, mock_serial, mock_list_ports):
mock_list_ports.grep.return_value = mock_ListPortInfo(found=False)
mocker.patch("pslab.serial_handler.SerialHandler.check_serial_access_permission")
with pytest.raises(SerialException):
SerialHandler()
def test_connect_multiple_connected(mocker, mock_serial, mock_list_ports):
mock_list_ports.grep.return_value = mock_ListPortInfo(multiple=True)
mocker.patch("pslab.serial_handler.SerialHandler.check_serial_access_permission")
with pytest.raises(RuntimeError):
SerialHandler()
def test_disconnect(mock_serial, mock_handler):
mock_handler.disconnect()
mock_serial().close.assert_called()
def test_reconnect(mock_serial, mock_handler, mock_list_ports):
mock_list_ports.grep.return_value = mock_ListPortInfo()
mock_handler.reconnect()
mock_serial().close.assert_called()
def test_get_version(mock_serial, mock_handler):
mock_handler.get_version()
mock_serial().write.assert_called_with(CP.GET_VERSION)
assert mock_handler.version == VERSION
def test_get_ack_success(mock_serial, mock_handler):
success = 1
mock_serial().read.return_value = CP.Byte.pack(success)
assert mock_handler.get_ack() == success
def test_get_ack_failure(mock_serial, mock_handler):
mock_serial().read.return_value = b""
with pytest.raises(SerialException):
mock_handler.get_ack()
def test_send_bytes(mock_serial, mock_handler):
mock_handler.send_byte(CP.Byte.pack(0xFF))
mock_serial().write.assert_called_with(CP.Byte.pack(0xFF))
def test_send_byte(mock_serial, mock_handler):
mock_handler.send_byte(0xFF)
mock_serial().write.assert_called_with(CP.Byte.pack(0xFF))
def test_receive(mock_serial, mock_handler):
mock_serial().read.return_value = CP.Byte.pack(0xFF)
r = mock_handler.get_byte()
mock_serial().read.assert_called_with(1)
assert r == 0xFF
def test_receive_failure(mock_serial, mock_handler):
mock_serial().read.return_value = b""
with pytest.raises(SerialException):
mock_handler.get_byte()
def test_wait_for_data(mock_serial, mock_handler):
mock_serial().in_waiting = True
assert mock_handler.wait_for_data()
def test_wait_for_data_timeout(mock_serial, mock_handler):
mock_serial().in_waiting = False
assert not mock_handler.wait_for_data()
def test_get_integer_unsupported_size(mock_serial, mock_handler):
with pytest.raises(ValueError):
mock_handler._get_integer_type(size=3)
def test_list_ports(mock_serial, mock_handler):
assert isinstance(mock_handler._list_ports(), list)