From f0df2fff218862c7d2ffefac8a42dba30422278e Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sat, 9 Dec 2023 18:00:35 -1000 Subject: [PATCH] cleanup more tests --- tests/test_log_runner.py | 36 ++++++++++++++--------------------- tests/test_reconnect_logic.py | 20 +++++++++---------- 2 files changed, 23 insertions(+), 33 deletions(-) diff --git a/tests/test_log_runner.py b/tests/test_log_runner.py index 902037f..1fdf826 100644 --- a/tests/test_log_runner.py +++ b/tests/test_log_runner.py @@ -30,7 +30,11 @@ from .common import ( @pytest.mark.asyncio -async def test_log_runner(event_loop: asyncio.AbstractEventLoop, conn: APIConnection): +async def test_log_runner( + event_loop: asyncio.AbstractEventLoop, + conn: APIConnection, + aiohappyeyeballs_start_connection, +): """Test the log runner logic.""" loop = asyncio.get_event_loop() protocol: APIPlaintextFrameHelper | None = None @@ -69,13 +73,9 @@ async def test_log_runner(event_loop: asyncio.AbstractEventLoop, conn: APIConnec await original_subscribe_logs(*args, **kwargs) subscribed.set() - with patch( - "aioesphomeapi.connection.aiohappyeyeballs.start_connection" - ), patch.object( + with patch.object( loop, "create_connection", side_effect=_create_mock_transport_protocol - ), patch.object( - cli, "subscribe_logs", _wait_subscribe_cli - ): + ), patch.object(cli, "subscribe_logs", _wait_subscribe_cli): stop = await async_run(cli, on_log, aio_zeroconf_instance=async_zeroconf) await connected.wait() protocol = cli._connection._frame_helper @@ -100,6 +100,7 @@ async def test_log_runner_reconnects_on_disconnect( event_loop: asyncio.AbstractEventLoop, conn: APIConnection, caplog: pytest.LogCaptureFixture, + aiohappyeyeballs_start_connection, ) -> None: """Test the log runner reconnects on disconnect.""" loop = asyncio.get_event_loop() @@ -139,13 +140,9 @@ async def test_log_runner_reconnects_on_disconnect( await original_subscribe_logs(*args, **kwargs) subscribed.set() - with patch( - "aioesphomeapi.connection.aiohappyeyeballs.start_connection" - ), patch.object( + with patch.object( loop, "create_connection", side_effect=_create_mock_transport_protocol - ), patch.object( - cli, "subscribe_logs", _wait_subscribe_cli - ): + ), patch.object(cli, "subscribe_logs", _wait_subscribe_cli): stop = await async_run(cli, on_log, aio_zeroconf_instance=async_zeroconf) await connected.wait() protocol = cli._connection._frame_helper @@ -181,6 +178,7 @@ async def test_log_runner_reconnects_on_subscribe_failure( event_loop: asyncio.AbstractEventLoop, conn: APIConnection, caplog: pytest.LogCaptureFixture, + aiohappyeyeballs_start_connection, ) -> None: """Test the log runner reconnects on subscribe failure.""" loop = asyncio.get_event_loop() @@ -222,9 +220,7 @@ async def test_log_runner_reconnects_on_subscribe_failure( with patch.object( cli, "disconnect", partial(cli.disconnect, force=True) ), patch.object(cli, "subscribe_logs", _wait_and_fail_subscribe_cli): - with patch( - "aioesphomeapi.connection.aiohappyeyeballs.start_connection" - ), patch.object( + with patch.object( loop, "create_connection", side_effect=_create_mock_transport_protocol ): stop = await async_run(cli, on_log, aio_zeroconf_instance=async_zeroconf) @@ -237,13 +233,9 @@ async def test_log_runner_reconnects_on_subscribe_failure( assert cli._connection is None - with patch( - "aioesphomeapi.connection.aiohappyeyeballs.start_connection" - ), patch.object( + with patch.object( loop, "create_connection", side_effect=_create_mock_transport_protocol - ), patch.object( - cli, "subscribe_logs" - ): + ), patch.object(cli, "subscribe_logs"): connected.clear() await asyncio.sleep(0) async_fire_time_changed( diff --git a/tests/test_reconnect_logic.py b/tests/test_reconnect_logic.py index 0fb1ecd..7b8f67b 100644 --- a/tests/test_reconnect_logic.py +++ b/tests/test_reconnect_logic.py @@ -672,7 +672,9 @@ async def test_reconnect_logic_stop_callback_waits_for_handshake( @pytest.mark.asyncio -async def test_handling_unexpected_disconnect(event_loop: asyncio.AbstractEventLoop): +async def test_handling_unexpected_disconnect( + event_loop: asyncio.AbstractEventLoop, aiohappyeyeballs_start_connection +): """Test the disconnect callback fires with expected_disconnect=False.""" loop = asyncio.get_event_loop() protocol: APIPlaintextFrameHelper | None = None @@ -710,9 +712,7 @@ async def test_handling_unexpected_disconnect(event_loop: asyncio.AbstractEventL name="fake", ) - with patch( - "aioesphomeapi.connection.aiohappyeyeballs.start_connection" - ), patch.object( + with patch.object( loop, "create_connection", side_effect=partial(_create_mock_transport_protocol, transport, connected), @@ -728,9 +728,7 @@ async def test_handling_unexpected_disconnect(event_loop: asyncio.AbstractEventL assert cli._connection.is_connected is True await asyncio.sleep(0) - with patch( - "aioesphomeapi.connection.aiohappyeyeballs.start_connection" - ), patch.object( + with patch.object( loop, "create_connection", side_effect=partial(_create_mock_transport_protocol, transport, connected), @@ -750,7 +748,9 @@ async def test_handling_unexpected_disconnect(event_loop: asyncio.AbstractEventL @pytest.mark.asyncio async def test_backoff_on_encryption_error( - event_loop: asyncio.AbstractEventLoop, caplog: pytest.LogCaptureFixture + event_loop: asyncio.AbstractEventLoop, + caplog: pytest.LogCaptureFixture, + aiohappyeyeballs_start_connection, ) -> None: """Test we backoff on encryption error.""" loop = asyncio.get_event_loop() @@ -789,9 +789,7 @@ async def test_backoff_on_encryption_error( name="fake", ) - with patch( - "aioesphomeapi.connection.aiohappyeyeballs.start_connection" - ), patch.object( + with patch.object( loop, "create_connection", side_effect=partial(_create_mock_transport_protocol, transport, connected),