Skip to content

Commit 1003610

Browse files
fixed blob_base.py and blob_factory.py file test cases
1 parent cd098d4 commit 1003610

4 files changed

Lines changed: 107 additions & 355 deletions

File tree

src/backend/common/storage/blob_factory.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import asyncio
12
from typing import Optional
23

34
from common.config.config import Config # Load config
@@ -31,15 +32,14 @@ async def close_storage() -> None:
3132

3233

3334
# Local testing of config and code
34-
if __name__ == "__main__":
35-
# Example usage
36-
import asyncio
35+
async def main():
36+
storage = await BlobStorageFactory.get_storage()
37+
38+
# Use the storage instance
39+
blob = await storage.get_file("q1_informix.sql")
40+
print("Blob content:", blob)
3741

38-
async def main():
39-
storage = await BlobStorageFactory.get_storage()
40-
# Use the storage instance...
41-
blob = await storage.get_file("q1_informix.sql")
42-
print(blob)
43-
await BlobStorageFactory.close_storage()
42+
await BlobStorageFactory.close_storage()
4443

44+
if __name__ == "__main__":
4545
asyncio.run(main())

src/backend/sql_agents/process_batch.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
from fastapi import HTTPException
2424

2525

26-
from semantic_kernel.agents.azure_ai.azure_ai_agent import AzureAIAgent # pylint: disable=E0611
26+
from semantic_kernel.agents.azure_ai.azure_ai_agent import AzureAIAgent # pylint: disable=E0611
2727
from semantic_kernel.contents import AuthorRole
2828
from semantic_kernel.exceptions.service_exceptions import ServiceResponseException
2929

Lines changed: 49 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -1,128 +1,86 @@
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
36

4-
# Import the abstract base class from the production code.
5-
from common.storage.blob_base import BlobStorageBase
67

78
import pytest
8-
# Create a dummy concrete subclass of BlobStorageBase that calls the parent's abstract methods.
99

1010

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"""
1713

1814
async def upload_file(
1915
self,
2016
file_content: BinaryIO,
2117
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,
2420
) -> Dict[str, Any]:
25-
await super().upload_file(file_content, blob_path, content_type, metadata)
26-
# Return a dummy dictionary that simulates upload details.
2721
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}",
3127
}
3228

3329
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")
3731

3832
async def delete_file(self, blob_path: str) -> bool:
39-
await super().delete_file(blob_path)
40-
# Simulate a successful deletion.
4133
return True
4234

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]]:
4536
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"},
5339
]
5440

5541

56-
# tests cases with each method.
42+
@pytest.fixture
43+
def mock_blob_storage():
44+
"""Fixture to provide a MockBlobStorage instance"""
45+
return MockBlobStorage()
5746

5847

5948
@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")
6653

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
8358

8459

8560
@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")
9164

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"
9967

10068

10169
@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
11275

11376

11477
@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

Comments
 (0)