44
55import asyncio
66import logging
7+ from typing import Any , Generator
78from unittest .mock import AsyncMock , MagicMock , PropertyMock , patch
89
910import pytest
1516
1617
1718@pytest .fixture (autouse = True )
18- def mock_serial ():
19+ def mock_serial () -> Generator [ None , Any , None ] :
1920 with patch ("smpclient.transport.serial.Serial" ):
2021 yield
2122
2223
2324def test_constructor () -> None :
24- t = SMPSerialTransport (
25- max_smp_encoded_frame_size = 512 , line_length = 128 , line_buffers = 4
26- )
25+ t = SMPSerialTransport (max_smp_encoded_frame_size = 512 , line_length = 128 , line_buffers = 4 )
2726 assert t .mtu == 512
2827 assert t .max_unencoded_size < 512
2928
@@ -71,9 +70,7 @@ async def test_send() -> None:
7170
7271 await t .send (r .BYTES )
7372 t ._conn .write .assert_called_once ()
74- assert (
75- p .call_count == 2
76- ) # called twice since out buffer was not drained on first call
73+ assert p .call_count == 2 # called twice since out buffer was not drained on first call
7774
7875
7976@pytest .mark .asyncio
@@ -256,10 +253,7 @@ async def test_disconnect_flushes_partial_smp_as_serial(
256253 await t .disconnect ()
257254 messages = {r .message for r in caplog .records }
258255 assert "/dev/ttyACM0: Normal serial" in messages
259- assert any (
260- b"AAAA" in r .message .encode ("utf-8" , errors = "ignore" )
261- for r in caplog .records
262- )
256+ assert any (b"AAAA" in r .message .encode ("utf-8" , errors = "ignore" ) for r in caplog .records )
263257
264258
265259@pytest .mark .asyncio
@@ -280,16 +274,11 @@ async def bad_callback(data: bytes) -> None:
280274 t = SMPSerialTransport (serial_line_callback = bad_callback )
281275 await t .connect ("/dev/ttyACM0" , timeout_s = 1.0 )
282276
283- t ._conn .read_all = MagicMock ( # type: ignore
284- side_effect = [b"Line 1\n " , b"Line 2\n " ]
285- )
277+ t ._conn .read_all = MagicMock (side_effect = [b"Line 1\n " , b"Line 2\n " ]) # type: ignore
286278
287279 with caplog .at_level (logging .ERROR ):
288280 await asyncio .sleep (0.05 ) # Flush mock serial.
289- assert any (
290- "Serial data callback raised an exception" in r .message
291- for r in caplog .records
292- )
281+ assert any ("Serial data callback raised an exception" in r .message for r in caplog .records )
293282
294283 await t .disconnect ()
295284
0 commit comments