1
0
mirror of https://github.com/esphome/esphome.git synced 2025-09-23 05:32:22 +01:00
This commit is contained in:
J. Nick Koston
2025-09-21 10:14:40 -06:00
parent 0cae1f28b0
commit 2aa0ebd1d2

View File

@@ -95,6 +95,13 @@ def mock_open_file() -> Generator[tuple[Mock, MagicMock]]:
yield mock_open, mock_file yield mock_open, mock_file
@pytest.fixture
def mock_socket_constructor(mock_socket: Mock) -> Generator[Mock]:
"""Mock socket.socket constructor to return our mock socket."""
with patch("socket.socket", return_value=mock_socket) as mock_constructor:
yield mock_constructor
def test_recv_decode_with_decode(mock_socket: Mock) -> None: def test_recv_decode_with_decode(mock_socket: Mock) -> None:
"""Test recv_decode with decode=True returns list.""" """Test recv_decode with decode=True returns list."""
mock_socket.recv.return_value = b"\x01\x02\x03" mock_socket.recv.return_value = b"\x01\x02\x03"
@@ -387,15 +394,15 @@ def test_perform_ota_upload_error(mock_socket: Mock, mock_file: io.BytesIO) -> N
espota2.perform_ota(mock_socket, "", mock_file, "test.bin") espota2.perform_ota(mock_socket, "", mock_file, "test.bin")
@pytest.mark.usefixtures("mock_socket_constructor", "mock_resolve_ip")
def test_run_ota_impl_successful( def test_run_ota_impl_successful(
mock_socket: Mock, tmp_path: Path, mock_resolve_ip: Mock, mock_perform_ota: Mock mock_socket: Mock, tmp_path: Path, mock_perform_ota: Mock
) -> None: ) -> None:
"""Test run_ota_impl_ with successful upload.""" """Test run_ota_impl_ with successful upload."""
# Create a real firmware file # Create a real firmware file
firmware_file = tmp_path / "firmware.bin" firmware_file = tmp_path / "firmware.bin"
firmware_file.write_bytes(b"firmware content") firmware_file.write_bytes(b"firmware content")
with patch("socket.socket", return_value=mock_socket):
# Run OTA with real file path # Run OTA with real file path
result_code, result_host = espota2.run_ota_impl_( result_code, result_host = espota2.run_ota_impl_(
"test.local", 3232, "password", str(firmware_file) "test.local", 3232, "password", str(firmware_file)
@@ -420,9 +427,8 @@ def test_run_ota_impl_successful(
assert call_args[3] == str(firmware_file) assert call_args[3] == str(firmware_file)
def test_run_ota_impl_connection_failed( @pytest.mark.usefixtures("mock_socket_constructor", "mock_resolve_ip")
mock_socket: Mock, tmp_path: Path, mock_resolve_ip: Mock def test_run_ota_impl_connection_failed(mock_socket: Mock, tmp_path: Path) -> None:
) -> None:
"""Test run_ota_impl_ when connection fails.""" """Test run_ota_impl_ when connection fails."""
mock_socket.connect.side_effect = OSError("Connection refused") mock_socket.connect.side_effect = OSError("Connection refused")
@@ -430,7 +436,6 @@ def test_run_ota_impl_connection_failed(
firmware_file = tmp_path / "firmware.bin" firmware_file = tmp_path / "firmware.bin"
firmware_file.write_bytes(b"firmware content") firmware_file.write_bytes(b"firmware content")
with patch("socket.socket", return_value=mock_socket):
result_code, result_host = espota2.run_ota_impl_( result_code, result_host = espota2.run_ota_impl_(
"test.local", 3232, "password", str(firmware_file) "test.local", 3232, "password", str(firmware_file)
) )