Skip to content

Commit 78e43f3

Browse files
committed
chore: Update error building tests
1 parent 5db13b8 commit 78e43f3

File tree

1 file changed

+15
-33
lines changed

1 file changed

+15
-33
lines changed

tests/devices/traits/b01/q7/test_init.py

Lines changed: 15 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,21 @@ def __init__(self) -> None:
3131
self.msg_id = 123456789
3232
self.seq = 2020
3333

34-
def build(self, message: dict[str, Any] | str) -> RoborockMessage:
34+
def build(self, data: dict[str, Any] | str, code: int | None = None) -> RoborockMessage:
35+
"""Build an encoded B01 RPC response message."""
36+
message = {
37+
"msgId": str(self.msg_id),
38+
"data": data,
39+
}
40+
if code is not None:
41+
message["code"] = code
42+
return self._build_dps(message)
43+
44+
45+
def _build_dps(self, message: dict[str, Any] | str) -> RoborockMessage:
3546
"""Build an encoded B01 RPC response message."""
3647
dps_payload = {
37-
"dps": {
38-
"10000": json.dumps(
39-
{
40-
"msgId": str(self.msg_id),
41-
"data": message,
42-
}
43-
)
44-
}
48+
"dps": { "10000": json.dumps(message) }
4549
}
4650
self.seq += 1
4751
return RoborockMessage(
@@ -180,32 +184,10 @@ async def test_send_decoded_command_non_dict_response(fake_channel: FakeChannel,
180184

181185
async def test_send_decoded_command_error_code(fake_channel: FakeChannel, message_builder: B01MessageBuilder):
182186
"""Test that non-zero error codes from device are properly handled."""
183-
error_code = 5001
184-
185-
dps_payload = {
186-
"dps": {
187-
"10000": json.dumps(
188-
{
189-
"msgId": str(message_builder.msg_id),
190-
"code": error_code,
191-
"data": {},
192-
}
193-
)
194-
}
195-
}
196-
message = RoborockMessage(
197-
protocol=RoborockMessageProtocol.RPC_RESPONSE,
198-
payload=pad(
199-
json.dumps(dps_payload).encode(),
200-
AES.block_size,
201-
),
202-
version=b"B01",
203-
seq=2022,
204-
)
205-
187+
message = message_builder.build({}, code=5001)
206188
fake_channel.response_queue.append(message)
207189

208-
with pytest.raises(RoborockException, match=f"B01 command failed with code {error_code}"):
190+
with pytest.raises(RoborockException, match=f"B01 command failed with code 5001"):
209191
await send_decoded_command(fake_channel, Q7RequestMessage(dps=10000, command="prop.get", params=[])) # type: ignore[arg-type]
210192

211193

0 commit comments

Comments
 (0)