forked from pybricks/pybricksdev
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_pybricks.py
More file actions
192 lines (166 loc) · 7.08 KB
/
test_pybricks.py
File metadata and controls
192 lines (166 loc) · 7.08 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
"""Tests for the pybricks connection module."""
import asyncio
import contextlib
import os
import tempfile
from unittest.mock import AsyncMock, PropertyMock, patch
import pytest
from reactivex.subject import Subject
from pybricksdev.connections.pybricks import (
ConnectionState,
HubCapabilityFlag,
HubKind,
PybricksHubBLE,
StatusFlag,
)
class TestPybricksHub:
"""Tests for the PybricksHub base class functionality."""
@pytest.mark.asyncio
async def test_download_modern_protocol(self):
"""Test downloading with modern protocol and capability flags."""
hub = PybricksHubBLE("mock_device")
hub._mpy_abi_version = 6
hub._client = AsyncMock()
hub.get_capabilities = AsyncMock(return_value={"pybricks": {"mpy": True}})
hub.download_user_program = AsyncMock()
type(hub.connection_state_observable).value = PropertyMock(
return_value=ConnectionState.CONNECTED
)
hub._capability_flags = HubCapabilityFlag.USER_PROG_MULTI_FILE_MPY6
with contextlib.ExitStack() as stack:
# Create and manage temporary file
temp = stack.enter_context(
tempfile.NamedTemporaryFile(
suffix=".py", mode="w+", delete=False, encoding="utf-8"
)
)
temp.write("print('test')")
temp_path = temp.name
stack.callback(os.unlink, temp_path)
await hub.download(temp_path)
hub.download_user_program.assert_called_once()
@pytest.mark.asyncio
async def test_download_legacy_firmware(self):
"""Test downloading with legacy firmware."""
hub = PybricksHubBLE("mock_device")
hub._mpy_abi_version = None # Legacy firmware
hub._client = AsyncMock()
hub.download_user_program = AsyncMock()
hub.hub_kind = HubKind.BOOST
type(hub.connection_state_observable).value = PropertyMock(
return_value=ConnectionState.CONNECTED
)
hub._capability_flags = HubCapabilityFlag.USER_PROG_MULTI_FILE_MPY6
with contextlib.ExitStack() as stack:
# Create and manage temporary file
temp = stack.enter_context(
tempfile.NamedTemporaryFile(
suffix=".py", mode="w+", delete=False, encoding="utf-8"
)
)
temp.write("print('test')")
temp_path = temp.name
stack.callback(os.unlink, temp_path)
await hub.download(temp_path)
hub.download_user_program.assert_called_once()
@pytest.mark.asyncio
async def test_download_unsupported_capabilities(self):
"""Test downloading when hub doesn't support required capabilities."""
hub = PybricksHubBLE("mock_device")
hub._mpy_abi_version = 6
hub._client = AsyncMock()
hub.get_capabilities = AsyncMock(return_value={"pybricks": {"mpy": False}})
type(hub.connection_state_observable).value = PropertyMock(
return_value=ConnectionState.CONNECTED
)
hub._capability_flags = 0
with contextlib.ExitStack() as stack:
# Create and manage temporary file
temp = stack.enter_context(
tempfile.NamedTemporaryFile(
suffix=".py", mode="w+", delete=False, encoding="utf-8"
)
)
temp.write("print('test')")
temp_path = temp.name
stack.callback(os.unlink, temp_path)
with pytest.raises(
RuntimeError,
match="Hub is not compatible with any of the supported file formats",
):
await hub.download(temp_path)
@pytest.mark.asyncio
async def test_download_compile_error(self):
"""Test handling compilation errors."""
hub = PybricksHubBLE("mock_device")
hub._mpy_abi_version = 6
hub._client = AsyncMock()
hub.get_capabilities = AsyncMock(return_value={"pybricks": {"mpy": True}})
type(hub.connection_state_observable).value = PropertyMock(
return_value=ConnectionState.CONNECTED
)
hub._capability_flags = HubCapabilityFlag.USER_PROG_MULTI_FILE_MPY6
hub._max_user_program_size = 1000 # Set a reasonable size limit
with contextlib.ExitStack() as stack:
# Create and manage temporary file
temp = stack.enter_context(
tempfile.NamedTemporaryFile(
suffix=".py", mode="w+", delete=False, encoding="utf-8"
)
)
temp.write("print('test' # Missing closing parenthesis")
temp_path = temp.name
stack.callback(os.unlink, temp_path)
# Mock compile_multi_file to raise SyntaxError
stack.enter_context(
patch(
"pybricksdev.connections.pybricks.compile_multi_file",
side_effect=SyntaxError("invalid syntax"),
)
)
with pytest.raises(SyntaxError, match="invalid syntax"):
await hub.download(temp_path)
@pytest.mark.asyncio
async def test_run_modern_protocol(self):
"""Test running a program with modern protocol."""
hub = PybricksHubBLE("mock_device")
hub._mpy_abi_version = None # Use modern protocol
hub._client = AsyncMock()
hub.client = AsyncMock()
hub.get_capabilities = AsyncMock(return_value={"pybricks": {"mpy": True}})
hub.download_user_program = AsyncMock()
hub.start_user_program = AsyncMock()
hub.write_gatt_char = AsyncMock()
type(hub.connection_state_observable).value = PropertyMock(
return_value=ConnectionState.CONNECTED
)
hub._capability_flags = HubCapabilityFlag.USER_PROG_MULTI_FILE_MPY6
hub.hub_kind = HubKind.BOOST
# Mock the status observable to simulate program start and stop
status_subject = Subject()
hub.status_observable = status_subject
hub._stdout_line_queue = asyncio.Queue()
hub._enable_line_handler = True
with contextlib.ExitStack() as stack:
# Create and manage temporary file
temp = stack.enter_context(
tempfile.NamedTemporaryFile(
suffix=".py", mode="w+", delete=False, encoding="utf-8"
)
)
temp.write("print('test')")
temp_path = temp.name
stack.callback(os.unlink, temp_path)
# Start the run task
run_task = asyncio.create_task(hub.run(temp_path))
# Simulate program start
await asyncio.sleep(0.1)
status_subject.on_next(StatusFlag.USER_PROGRAM_RUNNING)
# Simulate program stop after a short delay
await asyncio.sleep(0.1)
status_subject.on_next(0) # Clear all flags
# Wait for run task to complete
await run_task
# Verify the expected calls were made
hub.download_user_program.assert_called_once()
hub.start_user_program.assert_called_once()