|
1 | | -from datetime import datetime |
2 | | -from typing import Any, BinaryIO, Dict |
| 1 | +from io import BytesIO |
| 2 | +from typing import Any, BinaryIO, Dict, Optional |
| 3 | + |
| 4 | + |
| 5 | +from common.storage.blob_base import BlobStorageBase # Adjust import path as needed |
3 | 6 |
|
4 | | -# Import the abstract base class from the production code. |
5 | | -from common.storage.blob_base import BlobStorageBase |
6 | 7 |
|
7 | 8 | import pytest |
8 | | -# Create a dummy concrete subclass of BlobStorageBase that calls the parent's abstract methods. |
9 | 9 |
|
10 | 10 |
|
11 | | -class DummyBlobStorage(BlobStorageBase): |
12 | | - async def initialize(self) -> None: |
13 | | - # Call the parent (which is just a pass) |
14 | | - await super().initialize() |
15 | | - # Return a dummy value so we can verify our override is called. |
16 | | - return "initialized" |
| 11 | +class MockBlobStorage(BlobStorageBase): |
| 12 | + """Mock implementation of BlobStorageBase for testing""" |
17 | 13 |
|
18 | 14 | async def upload_file( |
19 | 15 | self, |
20 | 16 | file_content: BinaryIO, |
21 | 17 | blob_path: str, |
22 | | - content_type: str = None, |
23 | | - metadata: Dict[str, str] = None, |
| 18 | + content_type: Optional[str] = None, |
| 19 | + metadata: Optional[Dict[str, str]] = None, |
24 | 20 | ) -> Dict[str, Any]: |
25 | | - await super().upload_file(file_content, blob_path, content_type, metadata) |
26 | | - # Return a dummy dictionary that simulates upload details. |
27 | 21 | return { |
28 | | - "url": "https://dummy.blob.core.windows.net/dummy_container/" + blob_path, |
29 | | - "size": len(file_content), |
30 | | - "etag": "dummy_etag", |
| 22 | + "path": blob_path, |
| 23 | + "size": len(file_content.read()), |
| 24 | + "content_type": content_type or "application/octet-stream", |
| 25 | + "metadata": metadata or {}, |
| 26 | + "url": f"https://mockstorage.com/{blob_path}", |
31 | 27 | } |
32 | 28 |
|
33 | 29 | async def get_file(self, blob_path: str) -> BinaryIO: |
34 | | - await super().get_file(blob_path) |
35 | | - # Return dummy binary content. |
36 | | - return b"dummy content" |
| 30 | + return BytesIO(b"mock data") |
37 | 31 |
|
38 | 32 | async def delete_file(self, blob_path: str) -> bool: |
39 | | - await super().delete_file(blob_path) |
40 | | - # Simulate a successful deletion. |
41 | 33 | return True |
42 | 34 |
|
43 | | - async def list_files(self, prefix: str = None) -> list[Dict[str, Any]]: |
44 | | - await super().list_files(prefix) |
| 35 | + async def list_files(self, prefix: Optional[str] = None) -> list[Dict[str, Any]]: |
45 | 36 | return [ |
46 | | - { |
47 | | - "name": "dummy.txt", |
48 | | - "size": 123, |
49 | | - "created_at": datetime.now(), |
50 | | - "content_type": "text/plain", |
51 | | - "metadata": {"dummy": "value"}, |
52 | | - } |
| 37 | + {"name": "file1.txt", "size": 100, "content_type": "text/plain"}, |
| 38 | + {"name": "file2.jpg", "size": 200, "content_type": "image/jpeg"}, |
53 | 39 | ] |
54 | 40 |
|
55 | 41 |
|
56 | | -# tests cases with each method. |
| 42 | +@pytest.fixture |
| 43 | +def mock_blob_storage(): |
| 44 | + """Fixture to provide a MockBlobStorage instance""" |
| 45 | + return MockBlobStorage() |
57 | 46 |
|
58 | 47 |
|
59 | 48 | @pytest.mark.asyncio |
60 | | -async def test_initialize(): |
61 | | - storage = DummyBlobStorage() |
62 | | - result = await storage.initialize() |
63 | | - # Since the dummy override returns "initialized" after calling super(), |
64 | | - # we assert that the result equals that string. |
65 | | - assert result == "initialized" |
| 49 | +async def test_upload_file(mock_blob_storage): |
| 50 | + """Test upload_file method""" |
| 51 | + file_content = BytesIO(b"dummy data") |
| 52 | + result = await mock_blob_storage.upload_file(file_content, "test_blob.txt", "text/plain") |
66 | 53 |
|
67 | | - |
68 | | -@pytest.mark.asyncio |
69 | | -async def test_upload_file(): |
70 | | - storage = DummyBlobStorage() |
71 | | - content = b"hello world" |
72 | | - blob_path = "folder/hello.txt" |
73 | | - content_type = "text/plain" |
74 | | - metadata = {"key": "value"} |
75 | | - result = await storage.upload_file(content, blob_path, content_type, metadata) |
76 | | - # Verify that our dummy return value is as expected. |
77 | | - assert ( |
78 | | - result["url"] |
79 | | - == "https://dummy.blob.core.windows.net/dummy_container/" + blob_path |
80 | | - ) |
81 | | - assert result["size"] == len(content) |
82 | | - assert result["etag"] == "dummy_etag" |
| 54 | + assert result["path"] == "test_blob.txt" |
| 55 | + assert result["size"] == len(b"dummy data") |
| 56 | + assert result["content_type"] == "text/plain" |
| 57 | + assert "url" in result |
83 | 58 |
|
84 | 59 |
|
85 | 60 | @pytest.mark.asyncio |
86 | | -async def test_get_file(): |
87 | | - storage = DummyBlobStorage() |
88 | | - result = await storage.get_file("folder/hello.txt") |
89 | | - # Verify that we get the dummy binary content. |
90 | | - assert result == b"dummy content" |
| 61 | +async def test_get_file(mock_blob_storage): |
| 62 | + """Test get_file method""" |
| 63 | + result = await mock_blob_storage.get_file("test_blob.txt") |
91 | 64 |
|
92 | | - |
93 | | -@pytest.mark.asyncio |
94 | | -async def test_delete_file(): |
95 | | - storage = DummyBlobStorage() |
96 | | - result = await storage.delete_file("folder/hello.txt") |
97 | | - # Verify that deletion returns True. |
98 | | - assert result is True |
| 65 | + assert isinstance(result, BytesIO) |
| 66 | + assert result.read() == b"mock data" |
99 | 67 |
|
100 | 68 |
|
101 | 69 | @pytest.mark.asyncio |
102 | | -async def test_list_files(): |
103 | | - storage = DummyBlobStorage() |
104 | | - result = await storage.list_files("dummy") |
105 | | - # Verify that we receive a list with one item having a 'name' key. |
106 | | - assert isinstance(result, list) |
107 | | - assert len(result) == 1 |
108 | | - assert "dummy.txt" in result[0]["name"] |
109 | | - assert result[0]["size"] == 123 |
110 | | - assert result[0]["content_type"] == "text/plain" |
111 | | - assert result[0]["metadata"] == {"dummy": "value"} |
| 70 | +async def test_delete_file(mock_blob_storage): |
| 71 | + """Test delete_file method""" |
| 72 | + result = await mock_blob_storage.delete_file("test_blob.txt") |
| 73 | + |
| 74 | + assert result is True |
112 | 75 |
|
113 | 76 |
|
114 | 77 | @pytest.mark.asyncio |
115 | | -async def test_smoke_all_methods(): |
116 | | - storage = DummyBlobStorage() |
117 | | - init_val = await storage.initialize() |
118 | | - assert init_val == "initialized" |
119 | | - upload_val = await storage.upload_file( |
120 | | - b"data", "file.txt", "text/plain", {"a": "b"} |
121 | | - ) |
122 | | - assert upload_val["size"] == 4 |
123 | | - file_val = await storage.get_file("file.txt") |
124 | | - assert file_val == b"dummy content" |
125 | | - delete_val = await storage.delete_file("file.txt") |
126 | | - assert delete_val is True |
127 | | - list_val = await storage.list_files("file") |
128 | | - assert isinstance(list_val, list) |
| 78 | +async def test_list_files(mock_blob_storage): |
| 79 | + """Test list_files method""" |
| 80 | + result = await mock_blob_storage.list_files() |
| 81 | + |
| 82 | + assert len(result) == 2 |
| 83 | + assert result[0]["name"] == "file1.txt" |
| 84 | + assert result[1]["name"] == "file2.jpg" |
| 85 | + assert result[0]["size"] == 100 |
| 86 | + assert result[1]["size"] == 200 |
0 commit comments