From a4b04a4914fb6c4dfb634d23ec4845eab4a9d733 Mon Sep 17 00:00:00 2001 From: Thomaz Date: Sat, 22 Jun 2024 17:07:41 -0700 Subject: [PATCH] Added tests --- tests/test_connection/test_connection_misc.py | 32 +++++++------------ 1 file changed, 11 insertions(+), 21 deletions(-) diff --git a/tests/test_connection/test_connection_misc.py b/tests/test_connection/test_connection_misc.py index 87d336d..3f50604 100644 --- a/tests/test_connection/test_connection_misc.py +++ b/tests/test_connection/test_connection_misc.py @@ -1,5 +1,6 @@ import logging import sys +from unittest import mock import pytest from serial import Serial @@ -50,24 +51,13 @@ def test_setting_password_does_not_log(caplog): assert "Dawkins" not in caplog.text, "Password found in log message" -# pytest can't delattr read_all? what? -# def test_pyserial_read_all_fix_for_34(monkeypatch): -# """ -# pyserial==3.5 has method .read_all -# pyserial==3.4 does not -# -# pygmc checks hasattr else it manually gets 'in_waiting' bytes and uses it in .read() -# """ -# cmd_response_map = {b"MATH": b"YES"} -# mock_dev = get_mock_dev(cmd_response_map) -# mock_dev_serial = Serial(mock_dev.port) -# -# connection = pygmc.connection.Connection( -# port="dummy", baudrate=123, serial_connection=mock_dev_serial -# ) -# -# # let's fake pyserial==3.4 -# monkeypatch.delattr(connection._con, "read_all") -# # The true test is getting a response with read_all deleted -# response = connection.get(b"MATH", wait_sleep=0) -# assert response == b"YES", "Unexpected response" +def test_connection_timeout_msg(caplog): + with caplog.at_level(logging.DEBUG, logger="pygmc"): + + with mock.patch("serial.Serial") as mock_serial: + mock_serial.side_effect = TimeoutError + + with pytest.raises(TimeoutError): + pygmc.connection.Connection("dummy", baudrate=123) + + assert "consider reconnecting USB" in caplog.text