|
2 | 2 |
|
3 | 3 | from __future__ import annotations |
4 | 4 |
|
| 5 | +import io |
| 6 | +import tarfile |
5 | 7 | from types import SimpleNamespace |
6 | 8 | from pathlib import Path |
7 | 9 | from unittest.mock import AsyncMock |
@@ -327,6 +329,161 @@ async def test_upload_from_file_missing_path(self, mock_async_client: AsyncMock, |
327 | 329 | with pytest.raises(OSError, match="Failed to read file"): |
328 | 330 | await client.upload_from_file(missing_file) |
329 | 331 |
|
| 332 | + @pytest.mark.asyncio |
| 333 | + async def test_upload_from_dir( |
| 334 | + self, mock_async_client: AsyncMock, object_view: MockObjectView, tmp_path: Path |
| 335 | + ) -> None: |
| 336 | + """Test upload_from_dir method.""" |
| 337 | + mock_async_client.objects.create = AsyncMock(return_value=object_view) |
| 338 | + mock_async_client.objects.complete = AsyncMock(return_value=object_view) |
| 339 | + |
| 340 | + # Create a temporary directory with some files |
| 341 | + test_dir = tmp_path / "test_directory" |
| 342 | + test_dir.mkdir() |
| 343 | + (test_dir / "file1.txt").write_text("content1") |
| 344 | + (test_dir / "file2.txt").write_text("content2") |
| 345 | + subdir = test_dir / "subdir" |
| 346 | + subdir.mkdir() |
| 347 | + (subdir / "file3.txt").write_text("content3") |
| 348 | + |
| 349 | + http_client = AsyncMock() |
| 350 | + mock_response = create_mock_httpx_response() |
| 351 | + http_client.put = AsyncMock(return_value=mock_response) |
| 352 | + mock_async_client._client = http_client |
| 353 | + |
| 354 | + client = AsyncStorageObjectOps(mock_async_client) |
| 355 | + obj = await client.upload_from_dir(test_dir, name="archive.tar.gz", metadata={"key": "value"}) |
| 356 | + |
| 357 | + assert isinstance(obj, AsyncStorageObject) |
| 358 | + assert obj.id == "obj_123" |
| 359 | + mock_async_client.objects.create.assert_awaited_once_with( |
| 360 | + name="archive.tar.gz", |
| 361 | + content_type="tgz", |
| 362 | + metadata={"key": "value"}, |
| 363 | + ttl_ms=None, |
| 364 | + ) |
| 365 | + # Verify that put was called with tarball content |
| 366 | + http_client.put.assert_awaited_once() |
| 367 | + call_args = http_client.put.call_args |
| 368 | + assert call_args[0][0] == object_view.upload_url |
| 369 | + |
| 370 | + # Verify it's a valid gzipped tarball |
| 371 | + uploaded_content = call_args[1]["content"] |
| 372 | + with tarfile.open(fileobj=io.BytesIO(uploaded_content), mode="r:gz") as tar: |
| 373 | + members = tar.getmembers() |
| 374 | + member_names = [m.name for m in members] |
| 375 | + # Should contain our test files (may include directory entries) |
| 376 | + assert any("file1.txt" in name for name in member_names) |
| 377 | + assert any("file2.txt" in name for name in member_names) |
| 378 | + assert any("file3.txt" in name for name in member_names) |
| 379 | + |
| 380 | + mock_async_client.objects.complete.assert_awaited_once() |
| 381 | + |
| 382 | + @pytest.mark.asyncio |
| 383 | + async def test_upload_from_dir_default_name( |
| 384 | + self, mock_async_client: AsyncMock, object_view: MockObjectView, tmp_path: Path |
| 385 | + ) -> None: |
| 386 | + """Test upload_from_dir uses directory name by default.""" |
| 387 | + mock_async_client.objects.create = AsyncMock(return_value=object_view) |
| 388 | + mock_async_client.objects.complete = AsyncMock(return_value=object_view) |
| 389 | + |
| 390 | + test_dir = tmp_path / "my_folder" |
| 391 | + test_dir.mkdir() |
| 392 | + (test_dir / "file.txt").write_text("content") |
| 393 | + |
| 394 | + http_client = AsyncMock() |
| 395 | + mock_response = create_mock_httpx_response() |
| 396 | + http_client.put = AsyncMock(return_value=mock_response) |
| 397 | + mock_async_client._client = http_client |
| 398 | + |
| 399 | + client = AsyncStorageObjectOps(mock_async_client) |
| 400 | + obj = await client.upload_from_dir(test_dir) |
| 401 | + |
| 402 | + assert isinstance(obj, AsyncStorageObject) |
| 403 | + # Name should be directory name + .tar.gz |
| 404 | + mock_async_client.objects.create.assert_awaited_once() |
| 405 | + call_args = mock_async_client.objects.create.call_args |
| 406 | + assert call_args[1]["name"] == "my_folder.tar.gz" |
| 407 | + assert call_args[1]["content_type"] == "tgz" |
| 408 | + |
| 409 | + @pytest.mark.asyncio |
| 410 | + async def test_upload_from_dir_with_ttl( |
| 411 | + self, mock_async_client: AsyncMock, object_view: MockObjectView, tmp_path: Path |
| 412 | + ) -> None: |
| 413 | + """Test upload_from_dir with TTL.""" |
| 414 | + from datetime import timedelta |
| 415 | + |
| 416 | + mock_async_client.objects.create = AsyncMock(return_value=object_view) |
| 417 | + mock_async_client.objects.complete = AsyncMock(return_value=object_view) |
| 418 | + |
| 419 | + test_dir = tmp_path / "temp_dir" |
| 420 | + test_dir.mkdir() |
| 421 | + (test_dir / "file.txt").write_text("temporary content") |
| 422 | + |
| 423 | + http_client = AsyncMock() |
| 424 | + mock_response = create_mock_httpx_response() |
| 425 | + http_client.put = AsyncMock(return_value=mock_response) |
| 426 | + mock_async_client._client = http_client |
| 427 | + |
| 428 | + client = AsyncStorageObjectOps(mock_async_client) |
| 429 | + obj = await client.upload_from_dir(test_dir, ttl=timedelta(hours=2)) |
| 430 | + |
| 431 | + assert isinstance(obj, AsyncStorageObject) |
| 432 | + mock_async_client.objects.create.assert_awaited_once() |
| 433 | + call_args = mock_async_client.objects.create.call_args |
| 434 | + # 2 hours = 7200 seconds = 7200000 milliseconds |
| 435 | + assert call_args[1]["ttl_ms"] == 7200000 |
| 436 | + |
| 437 | + @pytest.mark.asyncio |
| 438 | + async def test_upload_from_dir_empty_directory( |
| 439 | + self, mock_async_client: AsyncMock, object_view: MockObjectView, tmp_path: Path |
| 440 | + ) -> None: |
| 441 | + """Test upload_from_dir with empty directory.""" |
| 442 | + mock_async_client.objects.create = AsyncMock(return_value=object_view) |
| 443 | + mock_async_client.objects.complete = AsyncMock(return_value=object_view) |
| 444 | + |
| 445 | + test_dir = tmp_path / "empty_dir" |
| 446 | + test_dir.mkdir() |
| 447 | + |
| 448 | + http_client = AsyncMock() |
| 449 | + mock_response = create_mock_httpx_response() |
| 450 | + http_client.put = AsyncMock(return_value=mock_response) |
| 451 | + mock_async_client._client = http_client |
| 452 | + |
| 453 | + client = AsyncStorageObjectOps(mock_async_client) |
| 454 | + obj = await client.upload_from_dir(test_dir) |
| 455 | + |
| 456 | + assert isinstance(obj, AsyncStorageObject) |
| 457 | + assert obj.id == "obj_123" |
| 458 | + mock_async_client.objects.create.assert_awaited_once() |
| 459 | + http_client.put.assert_awaited_once() |
| 460 | + mock_async_client.objects.complete.assert_awaited_once() |
| 461 | + |
| 462 | + @pytest.mark.asyncio |
| 463 | + async def test_upload_from_dir_with_string_path( |
| 464 | + self, mock_async_client: AsyncMock, object_view: MockObjectView, tmp_path: Path |
| 465 | + ) -> None: |
| 466 | + """Test upload_from_dir with string path instead of Path object.""" |
| 467 | + mock_async_client.objects.create = AsyncMock(return_value=object_view) |
| 468 | + mock_async_client.objects.complete = AsyncMock(return_value=object_view) |
| 469 | + |
| 470 | + test_dir = tmp_path / "string_path_dir" |
| 471 | + test_dir.mkdir() |
| 472 | + (test_dir / "file.txt").write_text("content") |
| 473 | + |
| 474 | + http_client = AsyncMock() |
| 475 | + mock_response = create_mock_httpx_response() |
| 476 | + http_client.put = AsyncMock(return_value=mock_response) |
| 477 | + mock_async_client._client = http_client |
| 478 | + |
| 479 | + client = AsyncStorageObjectOps(mock_async_client) |
| 480 | + # Pass string path instead of Path object |
| 481 | + obj = await client.upload_from_dir(str(test_dir)) |
| 482 | + |
| 483 | + assert isinstance(obj, AsyncStorageObject) |
| 484 | + assert obj.id == "obj_123" |
| 485 | + mock_async_client.objects.create.assert_awaited_once() |
| 486 | + |
330 | 487 |
|
331 | 488 | class TestAsyncRunloopSDK: |
332 | 489 | """Tests for AsyncRunloopSDK class.""" |
|
0 commit comments