|
| 1 | +"""tests/test_s3client.py""" |
| 2 | + |
| 3 | +# ruff: noqa: PLR2004, SLF001 |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | +from timdex_dataset_api.utils import S3Client |
| 8 | + |
| 9 | + |
| 10 | +def test_s3client_init(): |
| 11 | + """Test S3Client initialization.""" |
| 12 | + client = S3Client() |
| 13 | + assert client.resource is not None |
| 14 | + |
| 15 | + |
| 16 | +def test_s3client_init_with_minio_env(caplog, monkeypatch): |
| 17 | + """Test S3Client initialization with MinIO environment variables.""" |
| 18 | + caplog.set_level("DEBUG") |
| 19 | + |
| 20 | + monkeypatch.setenv("MINIO_S3_ENDPOINT_URL", "http://localhost:9000") |
| 21 | + monkeypatch.setenv("MINIO_USERNAME", "minioadmin") |
| 22 | + monkeypatch.setenv("MINIO_PASSWORD", "minioadmin") |
| 23 | + monkeypatch.setenv("MINIO_REGION", "us-east-1") |
| 24 | + |
| 25 | + client = S3Client() |
| 26 | + assert client.resource is not None |
| 27 | + assert "MinIO env vars detected, using for S3Client" in caplog.text |
| 28 | + |
| 29 | + |
| 30 | +def test_split_s3_uri(): |
| 31 | + """Test _split_s3_uri method.""" |
| 32 | + client = S3Client() |
| 33 | + bucket, key = client._split_s3_uri("s3://timdex/path/to/file.txt") |
| 34 | + assert bucket == "timdex" |
| 35 | + assert key == "path/to/file.txt" |
| 36 | + |
| 37 | + |
| 38 | +def test_split_s3_uri_invalid(): |
| 39 | + """Test _split_s3_uri method with invalid URI.""" |
| 40 | + client = S3Client() |
| 41 | + with pytest.raises(ValueError, match="Invalid S3 URI"): |
| 42 | + client._split_s3_uri("timdex/path/to/file.txt") |
| 43 | + |
| 44 | + |
| 45 | +def test_upload_download_file(mock_s3_resource, tmp_path): |
| 46 | + """Test upload_file and download_file methods.""" |
| 47 | + client = S3Client() |
| 48 | + |
| 49 | + # Create a test file |
| 50 | + test_file = tmp_path / "test.txt" |
| 51 | + test_file.write_text("test content") |
| 52 | + |
| 53 | + # Upload the file |
| 54 | + s3_uri = "s3://timdex/test.txt" |
| 55 | + client.upload_file(test_file, s3_uri) |
| 56 | + |
| 57 | + # Download the file to a different location |
| 58 | + download_path = tmp_path / "downloaded.txt" |
| 59 | + client.download_file(s3_uri, download_path) |
| 60 | + |
| 61 | + # Verify the content |
| 62 | + assert download_path.read_text() == "test content" |
| 63 | + |
| 64 | + |
| 65 | +def test_delete_file(mock_s3_resource, tmp_path): |
| 66 | + """Test delete_file method.""" |
| 67 | + client = S3Client() |
| 68 | + |
| 69 | + # Create and upload a test file |
| 70 | + test_file = tmp_path / "test.txt" |
| 71 | + test_file.write_text("test content") |
| 72 | + s3_uri = "s3://timdex/test.txt" |
| 73 | + client.upload_file(test_file, s3_uri) |
| 74 | + |
| 75 | + # Delete the file |
| 76 | + client.delete_file(s3_uri) |
| 77 | + |
| 78 | + # Verify the file is deleted |
| 79 | + bucket = mock_s3_resource.Bucket("timdex") |
| 80 | + objects = list(bucket.objects.all()) |
| 81 | + assert len(objects) == 0 |
| 82 | + |
| 83 | + |
| 84 | +def test_delete_folder(mock_s3_resource, tmp_path): |
| 85 | + """Test delete_folder method.""" |
| 86 | + client = S3Client() |
| 87 | + |
| 88 | + # Create and upload test files |
| 89 | + for i in range(3): |
| 90 | + test_file = tmp_path / f"test{i}.txt" |
| 91 | + test_file.write_text(f"test content {i}") |
| 92 | + s3_uri = f"s3://timdex/folder/test{i}.txt" |
| 93 | + client.upload_file(test_file, s3_uri) |
| 94 | + |
| 95 | + # Upload a file outside the folder |
| 96 | + other_file = tmp_path / "other.txt" |
| 97 | + other_file.write_text("other content") |
| 98 | + client.upload_file(other_file, "s3://timdex/other.txt") |
| 99 | + |
| 100 | + # Delete the folder |
| 101 | + deleted_keys = client.delete_folder("s3://timdex/folder/") |
| 102 | + |
| 103 | + # Verify only folder contents are deleted |
| 104 | + assert len(deleted_keys) == 3 |
| 105 | + assert all(key.startswith("folder/") for key in deleted_keys) |
| 106 | + |
| 107 | + bucket = mock_s3_resource.Bucket("timdex") |
| 108 | + objects = list(bucket.objects.all()) |
| 109 | + assert len(objects) == 1 |
| 110 | + assert objects[0].key == "other.txt" |
0 commit comments