|
7 | 7 |
|
8 | 8 | from types import SimpleNamespace |
9 | 9 | from pathlib import Path |
10 | | -from unittest.mock import AsyncMock |
| 10 | +from unittest.mock import Mock, AsyncMock, MagicMock |
11 | 11 |
|
12 | 12 | import httpx |
13 | 13 | import pytest |
@@ -140,6 +140,30 @@ async def test_download(self, mock_async_client: AsyncMock) -> None: |
140 | 140 | assert result == b"file content" |
141 | 141 | mock_async_client.devboxes.download_file.assert_called_once() |
142 | 142 |
|
| 143 | + @pytest.mark.asyncio |
| 144 | + async def test_download_to_file(self, mock_async_client: AsyncMock, tmp_path: Path) -> None: |
| 145 | + """Test streaming file download writes to disk without buffering.""" |
| 146 | + dest = tmp_path / "out.bin" |
| 147 | + |
| 148 | + def _stream(path: object, *args: object, **kwargs: object) -> None: # noqa: ARG001 |
| 149 | + with open(path, "wb") as f: # type: ignore[arg-type] |
| 150 | + f.write(b"streamed content") |
| 151 | + |
| 152 | + mock_response = Mock() |
| 153 | + mock_response.stream_to_file = AsyncMock(side_effect=_stream) |
| 154 | + cm = MagicMock() |
| 155 | + cm.__aenter__ = AsyncMock(return_value=mock_response) |
| 156 | + cm.__aexit__ = AsyncMock(return_value=None) |
| 157 | + mock_async_client.devboxes.with_streaming_response.download_file = Mock(return_value=cm) |
| 158 | + |
| 159 | + devbox = AsyncDevbox(mock_async_client, "dbx_123") |
| 160 | + await devbox.file.download_to_file(dest, path="/path/to/file") |
| 161 | + |
| 162 | + assert dest.read_bytes() == b"streamed content" |
| 163 | + mock_response.stream_to_file.assert_awaited_once() |
| 164 | + call_kwargs = mock_async_client.devboxes.with_streaming_response.download_file.call_args[1] |
| 165 | + assert call_kwargs["path"] == "/path/to/file" |
| 166 | + |
143 | 167 | @pytest.mark.asyncio |
144 | 168 | async def test_upload(self, mock_async_client: AsyncMock, tmp_path: Path) -> None: |
145 | 169 | """Test file upload.""" |
|
0 commit comments