-
Notifications
You must be signed in to change notification settings - Fork 77
Expand file tree
/
Copy pathtest_backend_bluetooth.py
More file actions
216 lines (179 loc) · 6.05 KB
/
test_backend_bluetooth.py
File metadata and controls
216 lines (179 loc) · 6.05 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
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
# test_backend_bluetooth -- Test nxt.backend.bluetooth module
# Copyright (C) 2021 Nicolas Schodet
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
from unittest.mock import Mock, call, patch
import pytest
import nxt.backend.bluetooth
@pytest.fixture
def msock():
sock = Mock(
spec_set=(
"connect",
"close",
"send",
"recv",
)
)
return sock
@pytest.fixture
def mbluetooth(msock):
class BluetoothError(Exception):
pass
bluetooth = Mock()
bluetooth.BluetoothSocket.return_value = msock
bluetooth.BluetoothError = BluetoothError
return bluetooth
@pytest.fixture
def mbluetooth_import(mbluetooth):
orig_import = __import__
def mocked_import(name, *args):
if name == "bluetooth":
return mbluetooth
return orig_import(name, *args)
with patch("builtins.__import__", new=mocked_import):
yield mocked_import
@pytest.fixture
def mbluetooth_import_error():
orig_import = __import__
def mocked_import(name, *args):
if name == "bluetooth":
raise ImportError("mocked")
return orig_import(name, *args)
with patch("builtins.__import__", new=mocked_import):
yield mocked_import
@pytest.fixture
def mbluetooth_import_not_supported():
orig_import = __import__
def mocked_import(name, *args):
if name == "bluetooth":
raise Exception("mocked")
return orig_import(name, *args)
with patch("builtins.__import__", new=mocked_import):
yield mocked_import
def test_bluetooth(mbluetooth, mbluetooth_import, msock):
# Instantiate backend.
backend = nxt.backend.bluetooth.get_backend()
# Find brick.
mbluetooth.discover_devices.return_value = [
b"00:01:02:03:04:05",
]
bricks = list(backend.find(blah="blah"))
assert len(bricks) == 1
brick = bricks[0]
assert mbluetooth.BluetoothSocket.called
assert msock.connect.called
sock = brick._sock
# str.
assert str(sock) == "Bluetooth (00:01:02:03:04:05)"
# Send.
some_bytes = bytes.fromhex("01020304")
some_len = bytes.fromhex("0400")
some_bytes_with_len = some_len + some_bytes
sock.send(some_bytes)
assert msock.send.call_args == call(some_bytes_with_len)
# Recv.
msock.recv.side_effect = [some_len, some_bytes]
r = sock.recv()
assert r == some_bytes
assert msock.recv.called
# Close.
brick.close()
assert msock.close.called
# Duplicated close.
sock.close()
def test_bluetooth_by_name(mbluetooth, mbluetooth_import, msock):
# Instantiate backend.
backend = nxt.backend.bluetooth.get_backend()
# Find brick.
mbluetooth.discover_devices.return_value = [
("00:01:02:03:04:05", "NXT"),
("00:01:02:03:04:06", "NXT"),
("00:01:02:03:04:07", "NXT2"),
]
bricks = list(backend.find(name="NXT2", blah="blah"))
assert len(bricks) == 1
brick = bricks[0]
assert mbluetooth.BluetoothSocket.called
assert msock.connect.called
sock = brick._sock
# str.
assert str(sock) == "Bluetooth (00:01:02:03:04:07)"
# Close.
brick.close()
def test_bluetooth_by_host(mbluetooth, mbluetooth_import, msock):
# Instantiate backend.
backend = nxt.backend.bluetooth.get_backend()
# Find brick.
bricks = list(backend.find(host="00:01:02:03:04:05", blah="blah"))
assert len(bricks) == 1
brick = bricks[0]
assert not mbluetooth.discover_devices.called
assert mbluetooth.BluetoothSocket.called
assert msock.connect.called
sock = brick._sock
# str.
assert str(sock) == "Bluetooth (00:01:02:03:04:05)"
# Close.
brick.close()
def test_bluetooth_by_host_and_name(mbluetooth, mbluetooth_import, msock):
# Instantiate backend.
backend = nxt.backend.bluetooth.get_backend()
# Find brick.
bricks = list(backend.find(host="00:01:02:03:04:05", name="NXT"))
assert len(bricks) == 1
brick = bricks[0]
assert not mbluetooth.discover_devices.called
assert mbluetooth.BluetoothSocket.called
assert msock.connect.called
sock = brick._sock
# str.
assert str(sock) == "Bluetooth (00:01:02:03:04:05)"
# Close.
brick.close()
def test_bluetooth_not_present(mbluetooth_import_error):
assert nxt.backend.bluetooth.get_backend() is None
def test_bluetooth_not_supported(mbluetooth_import_not_supported):
assert nxt.backend.bluetooth.get_backend() is None
def test_bluetooth_cant_connect(mbluetooth, mbluetooth_import, msock):
backend = nxt.backend.bluetooth.get_backend()
mbluetooth.discover_devices.return_value = [
"00:01:02:03:04:05",
]
msock.connect.side_effect = [mbluetooth.BluetoothError]
bricks = list(backend.find(blah="blah"))
assert len(bricks) == 0
def test_bluetooth_cant_discover(mbluetooth, mbluetooth_import, msock):
backend = nxt.backend.bluetooth.get_backend()
mbluetooth.discover_devices.side_effect = [OSError]
bricks = list(backend.find(blah="blah"))
assert len(bricks) == 0
mbluetooth.discover_devices.side_effect = [mbluetooth.BluetoothError]
bricks = list(backend.find(blah="blah"))
assert len(bricks) == 0
@pytest.mark.nxt("bluetooth")
def test_bluetooth_real():
# Instantiate backend.
backend = nxt.backend.bluetooth.get_backend()
# Find brick.
bricks = list(backend.find())
assert len(bricks) > 0, "no NXT found"
brick = bricks[0]
sock = brick._sock
# str.
assert str(sock).startswith("Bluetooth (")
# Send.
sock.send(bytes.fromhex("019b"))
# Recv.
r = sock.recv()
assert r.startswith(bytes.fromhex("029b00"))
# Close.
brick.close()