Skip to content

Commit 3e090de

Browse files
feat: added unit test cases for config.py, database_base.py, database_factory.py and blob_azure.py file
1 parent cdc9d30 commit 3e090de

6 files changed

Lines changed: 423 additions & 422 deletions

File tree

src/backend/common/database/database_base.py

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -12,55 +12,55 @@ class DatabaseBase(ABC):
1212

1313
@abstractmethod
1414
async def initialize_cosmos(self) -> None:
15-
"""Initialize the cosmosdb client and create container if needed."""
16-
pass
15+
"""Initialize the cosmosdb client and create container if needed"""
16+
pass # pragma: no cover
1717

1818
@abstractmethod
1919
async def create_batch(self, user_id: str, batch_id: uuid.UUID) -> BatchRecord:
20-
"""Create a new conversion batch."""
21-
pass
20+
"""Create a new conversion batch"""
21+
pass # pragma: no cover
2222

2323
@abstractmethod
2424
async def get_file_logs(self, file_id: str) -> Dict:
25-
"""Retrieve all logs for a file."""
26-
pass
25+
"""Retrieve all logs for a file"""
26+
pass # pragma: no cover
2727

2828
@abstractmethod
2929
async def get_batch_from_id(self, batch_id: str) -> Dict:
30-
"""Retrieve all logs for a file."""
31-
pass
30+
"""Retrieve all logs for a file"""
31+
pass # pragma: no cover
3232

3333
@abstractmethod
3434
async def get_batch_files(self, batch_id: str) -> List[Dict]:
35-
"""Retrieve all files for a batch."""
36-
pass
35+
"""Retrieve all files for a batch"""
36+
pass # pragma: no cover
3737

3838
@abstractmethod
3939
async def delete_file_logs(self, file_id: str) -> None:
40-
"""Delete all logs for a file."""
41-
pass
40+
"""Delete all logs for a file"""
41+
pass # pragma: no cover
4242

4343
@abstractmethod
4444
async def get_user_batches(self, user_id: str) -> Dict:
45-
"""Retrieve all batches for a user."""
46-
pass
45+
"""Retrieve all batches for a user"""
46+
pass # pragma: no cover
4747

4848
@abstractmethod
4949
async def add_file(
5050
self, batch_id: uuid.UUID, file_id: uuid.UUID, file_name: str, storage_path: str
5151
) -> FileRecord:
52-
"""Add a file entry to the database."""
53-
pass
52+
"""Add a file entry to the database"""
53+
pass # pragma: no cover
5454

5555
@abstractmethod
5656
async def get_batch(self, user_id: str, batch_id: str) -> Optional[Dict]:
57-
"""Retrieve a batch and its associated files."""
58-
pass
57+
"""Retrieve a batch and its associated files"""
58+
pass # pragma: no cover
5959

6060
@abstractmethod
6161
async def get_file(self, file_id: str) -> Optional[Dict]:
62-
"""Retrieve a file entry along with its logs."""
63-
pass
62+
"""Retrieve a file entry along with its logs"""
63+
pass # pragma: no cover
6464

6565
@abstractmethod
6666
async def add_file_log(
@@ -72,39 +72,39 @@ async def add_file_log(
7272
agent_type: AgentType,
7373
author_role: AuthorRole,
7474
) -> None:
75-
"""Log a file status update."""
76-
pass
75+
"""Log a file status update"""
76+
pass # pragma: no cover
7777

7878
@abstractmethod
7979
async def update_file(self, file_record: FileRecord) -> None:
80-
"""Update file record."""
81-
pass
80+
"""update file record"""
81+
pass # pragma: no cover
8282

8383
@abstractmethod
8484
async def update_batch(self, batch_record: BatchRecord) -> BatchRecord:
85-
pass
85+
pass # pragma: no cover
8686

8787
@abstractmethod
8888
async def delete_all(self, user_id: str) -> None:
89-
"""Delete all batches, files, and logs for a user."""
90-
pass
89+
"""Delete all batches, files, and logs for a user"""
90+
pass # pragma: no cover
9191

9292
@abstractmethod
9393
async def delete_batch(self, user_id: str, batch_id: str) -> None:
94-
"""Delete a batch along with its files and logs."""
95-
pass
94+
"""Delete a batch along with its files and logs"""
95+
pass # pragma: no cover
9696

9797
@abstractmethod
9898
async def delete_file(self, user_id: str, batch_id: str, file_id: str) -> None:
99-
"""Delete a file and its logs, and update batch file count."""
100-
pass
99+
"""Delete a file and its logs, and update batch file count"""
100+
pass # pragma: no cover
101101

102102
@abstractmethod
103103
async def get_batch_history(self, user_id: str, batch_id: str) -> List[Dict]:
104-
"""Retrieve all logs for a batch."""
105-
pass
104+
"""Retrieve all logs for a batch"""
105+
pass # pragma: no cover
106106

107107
@abstractmethod
108108
async def close(self) -> None:
109-
"""Close database connection."""
110-
pass
109+
"""Close database connection"""
110+
pass # pragma: no cover

src/backend/common/database/database_factory.py

Lines changed: 16 additions & 20 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
@@ -33,25 +34,20 @@ async def get_database():
3334
# Note that you have to assign yourself data plane access to Cosmos in script for this to work locally. See
3435
# https://learn.microsoft.com/en-us/azure/cosmos-db/table/security/how-to-grant-data-plane-role-based-access?tabs=built-in-definition%2Ccsharp&pivots=azure-interface-cli
3536
# Note that your principal id is your entra object id for your user account.
36-
if __name__ == "__main__":
37-
# Example usage
38-
import asyncio
39-
40-
async def main():
41-
database = await DatabaseFactory.get_database()
42-
# Use the database instance...
43-
await database.initialize_cosmos()
44-
await database.create_batch("mark1", "123e4567-e89b-12d3-a456-426614174000")
45-
await database.add_file(
46-
"123e4567-e89b-12d3-a456-426614174000",
47-
"123e4567-e89b-12d3-a456-426614174001",
48-
"q1_informix.sql",
49-
"https://cmsamarktaylstor.blob.core.windows.net/cmsablob",
50-
)
51-
tstbatch = await database.get_batch(
52-
"mark1", "123e4567-e89b-12d3-a456-426614174000"
53-
)
54-
print(tstbatch)
55-
await database.close()
37+
async def main():
38+
database = await DatabaseFactory.get_database()
39+
await database.initialize_cosmos()
40+
await database.create_batch("mark1", "123e4567-e89b-12d3-a456-426614174000")
41+
await database.add_file(
42+
"123e4567-e89b-12d3-a456-426614174000",
43+
"123e4567-e89b-12d3-a456-426614174001",
44+
"q1_informix.sql",
45+
"https://cmsamarktaylstor.blob.core.windows.net/cmsablob",
46+
)
47+
tstbatch = await database.get_batch("mark1", "123e4567-e89b-12d3-a456-426614174000")
48+
print(tstbatch)
49+
await database.close()
5650

51+
52+
if __name__ == "__main__":
5753
asyncio.run(main())
Lines changed: 70 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,70 @@
1-
import unittest
2-
from unittest.mock import patch
3-
4-
# from config import Config
5-
from common.config.config import Config
6-
7-
8-
class TestConfigInitialization(unittest.TestCase):
9-
@patch.dict(
10-
"os.environ",
11-
{
12-
"AZURE_TENANT_ID": "test-tenant-id",
13-
"AZURE_CLIENT_ID": "test-client-id",
14-
"AZURE_CLIENT_SECRET": "test-client-secret",
15-
"COSMOSDB_DATABASE": "test-database",
16-
"COSMOSDB_BATCH_CONTAINER": "test-batch-container",
17-
"COSMOSDB_FILE_CONTAINER": "test-file-container",
18-
"COSMOSDB_LOG_CONTAINER": "test-log-container",
19-
"AZURE_BLOB_CONTAINER_NAME": "test-blob-container-name",
20-
"AZURE_BLOB_ACCOUNT_NAME": "test-blob-account-name",
21-
},
22-
clear=True,
23-
)
24-
def test_config_initialization(self):
25-
"""Test if all attributes are correctly assigned from environment variables."""
26-
config = Config()
27-
28-
# Ensure every attribute is accessed
29-
self.assertEqual(config.azure_tenant_id, "test-tenant-id")
30-
self.assertEqual(config.azure_client_id, "test-client-id")
31-
self.assertEqual(config.azure_client_secret, "test-client-secret")
32-
33-
self.assertEqual(config.cosmosdb_endpoint, "test-cosmosdb-endpoint")
34-
self.assertEqual(config.cosmosdb_database, "test-database")
35-
self.assertEqual(config.cosmosdb_batch_container, "test-batch-container")
36-
self.assertEqual(config.cosmosdb_file_container, "test-file-container")
37-
self.assertEqual(config.cosmosdb_log_container, "test-log-container")
38-
39-
self.assertEqual(config.azure_blob_container_name, "test-blob-container-name")
40-
self.assertEqual(config.azure_blob_account_name, "test-blob-account-name")
41-
42-
@patch.dict(
43-
"os.environ",
44-
{
45-
"COSMOSDB_ENDPOINT": "test-cosmosdb-endpoint",
46-
"COSMOSDB_DATABASE": "test-database",
47-
"COSMOSDB_BATCH_CONTAINER": "test-batch-container",
48-
"COSMOSDB_FILE_CONTAINER": "test-file-container",
49-
"COSMOSDB_LOG_CONTAINER": "test-log-container",
50-
},
51-
)
52-
def test_cosmosdb_config_initialization(self):
53-
config = Config()
54-
self.assertEqual(config.cosmosdb_endpoint, "test-cosmosdb-endpoint")
55-
self.assertEqual(config.cosmosdb_database, "test-database")
56-
self.assertEqual(config.cosmosdb_batch_container, "test-batch-container")
57-
self.assertEqual(config.cosmosdb_file_container, "test-file-container")
58-
self.assertEqual(config.cosmosdb_log_container, "test-log-container")
59-
60-
61-
if __name__ == "__main__":
62-
unittest.main()
1+
import os
2+
import sys
3+
import pytest
4+
5+
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '../../../..', 'backend')))
6+
7+
@pytest.fixture(autouse=True)
8+
def clear_env(monkeypatch):
9+
# Clear environment variables that might affect tests.
10+
keys = [
11+
"AZURE_TENANT_ID",
12+
"AZURE_CLIENT_ID",
13+
"AZURE_CLIENT_SECRET",
14+
"COSMOSDB_ENDPOINT",
15+
"COSMOSDB_DATABASE",
16+
"COSMOSDB_BATCH_CONTAINER",
17+
"COSMOSDB_FILE_CONTAINER",
18+
"COSMOSDB_LOG_CONTAINER",
19+
"AZURE_BLOB_CONTAINER_NAME",
20+
"AZURE_BLOB_ACCOUNT_NAME",
21+
]
22+
for key in keys:
23+
monkeypatch.delenv(key, raising=False)
24+
25+
def test_config_initialization(monkeypatch):
26+
# Set the full configuration environment variables.
27+
monkeypatch.setenv("AZURE_TENANT_ID", "test-tenant-id")
28+
monkeypatch.setenv("AZURE_CLIENT_ID", "test-client-id")
29+
monkeypatch.setenv("AZURE_CLIENT_SECRET", "test-client-secret")
30+
monkeypatch.setenv("COSMOSDB_ENDPOINT", "test-cosmosdb-endpoint")
31+
monkeypatch.setenv("COSMOSDB_DATABASE", "test-database")
32+
monkeypatch.setenv("COSMOSDB_BATCH_CONTAINER", "test-batch-container")
33+
monkeypatch.setenv("COSMOSDB_FILE_CONTAINER", "test-file-container")
34+
monkeypatch.setenv("COSMOSDB_LOG_CONTAINER", "test-log-container")
35+
monkeypatch.setenv("AZURE_BLOB_CONTAINER_NAME", "test-blob-container-name")
36+
monkeypatch.setenv("AZURE_BLOB_ACCOUNT_NAME", "test-blob-account-name")
37+
38+
# Local import to avoid triggering circular imports during module collection.
39+
from common.config.config import Config
40+
config = Config()
41+
42+
assert config.azure_tenant_id == "test-tenant-id"
43+
assert config.azure_client_id == "test-client-id"
44+
assert config.azure_client_secret == "test-client-secret"
45+
assert config.cosmosdb_endpoint == "test-cosmosdb-endpoint"
46+
assert config.cosmosdb_database == "test-database"
47+
assert config.cosmosdb_batch_container == "test-batch-container"
48+
assert config.cosmosdb_file_container == "test-file-container"
49+
assert config.cosmosdb_log_container == "test-log-container"
50+
assert config.azure_blob_container_name == "test-blob-container-name"
51+
assert config.azure_blob_account_name == "test-blob-account-name"
52+
53+
def test_cosmosdb_config_initialization(monkeypatch):
54+
# Set only cosmosdb-related environment variables.
55+
monkeypatch.setenv("COSMOSDB_ENDPOINT", "test-cosmosdb-endpoint")
56+
monkeypatch.setenv("COSMOSDB_DATABASE", "test-database")
57+
monkeypatch.setenv("COSMOSDB_BATCH_CONTAINER", "test-batch-container")
58+
monkeypatch.setenv("COSMOSDB_FILE_CONTAINER", "test-file-container")
59+
monkeypatch.setenv("COSMOSDB_LOG_CONTAINER", "test-log-container")
60+
61+
from common.config.config import Config
62+
config = Config()
63+
64+
assert config.cosmosdb_endpoint == "test-cosmosdb-endpoint"
65+
assert config.cosmosdb_database == "test-database"
66+
assert config.cosmosdb_batch_container == "test-batch-container"
67+
assert config.cosmosdb_file_container == "test-file-container"
68+
assert config.cosmosdb_log_container == "test-log-container"
69+
70+

0 commit comments

Comments
 (0)