Skip to content

Commit 51d32b2

Browse files
removed the container creation from code to avoid permission issue
1 parent 3042b8f commit 51d32b2

File tree

2 files changed

+24
-32
lines changed

2 files changed

+24
-32
lines changed

src/backend/common/database/cosmosdb.py

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -49,32 +49,27 @@ async def initialize_cosmos(self):
4949
try:
5050
self.client = CosmosClient(url=self.endpoint, credential=self.credential)
5151
database = self.client.get_database_client(self.database_name)
52-
53-
self.batch_container = await self._get_or_create_container(
54-
database, self.batch_container_name, "/batch_id"
52+
self.batch_container = await self._get_container(
53+
database, self.batch_container_name
5554
)
56-
self.file_container = await self._get_or_create_container(
57-
database, self.file_container_name, "/file_id"
55+
self.file_container = await self._get_container(
56+
database, self.file_container_name
5857
)
59-
self.log_container = await self._get_or_create_container(
60-
database, self.log_container_name, "/log_id"
58+
self.log_container = await self._get_container(
59+
database, self.log_container_name
6160
)
6261
except Exception as e:
6362
self.logger.error("Failed to initialize Cosmos DB", error=str(e))
6463
raise
6564

66-
async def _get_or_create_container(
67-
self, database: DatabaseProxy, container_name, partition_key
65+
async def _get_container(
66+
self, database: DatabaseProxy, container_name
6867
):
6968
try:
70-
return await database.create_container(
71-
id=container_name, partition_key=PartitionKey(path=partition_key)
72-
)
73-
except exceptions.CosmosResourceExistsError:
7469
return database.get_container_client(container_name)
7570

7671
except Exception as e:
77-
self.logger.error("Failed to Get/Create cosmosdb container", error=str(e))
72+
self.logger.error("Failed to Get cosmosdb container", error=str(e))
7873
raise
7974

8075
async def create_batch(self, user_id: str, batch_id: UUID) -> BatchRecord:

src/tests/backend/common/database/cosmosdb_test.py

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ async def test_initialize_cosmos(cosmos_db_client, mocker):
5959
mock_file_container = mock.MagicMock()
6060
mock_log_container = mock.MagicMock()
6161

62-
# Use AsyncMock to mock asynchronous container creation
63-
mock_database.create_container = AsyncMock(side_effect=[
62+
# Mock get_container_client method (since _get_container uses this)
63+
mock_database.get_container_client = mock.MagicMock(side_effect=[
6464
mock_batch_container,
6565
mock_file_container,
6666
mock_log_container
@@ -69,10 +69,10 @@ async def test_initialize_cosmos(cosmos_db_client, mocker):
6969
# Call the initialize_cosmos method
7070
await cosmos_db_client.initialize_cosmos()
7171

72-
# Assert that the containers were created or fetched successfully
73-
mock_database.create_container.assert_any_call(id=batch_container, partition_key=mock.ANY)
74-
mock_database.create_container.assert_any_call(id=file_container, partition_key=mock.ANY)
75-
mock_database.create_container.assert_any_call(id=log_container, partition_key=mock.ANY)
72+
# Assert that the containers were fetched successfully
73+
mock_database.get_container_client.assert_any_call(batch_container)
74+
mock_database.get_container_client.assert_any_call(file_container)
75+
mock_database.get_container_client.assert_any_call(log_container)
7676

7777
# Check the client and containers were set
7878
assert cosmos_db_client.client is not None
@@ -87,15 +87,15 @@ async def test_initialize_cosmos_with_error(cosmos_db_client, mocker):
8787
mock_client = mocker.patch.object(CosmosClient, 'get_database_client', return_value=mock.MagicMock())
8888
mock_database = mock_client.return_value
8989

90-
# Simulate a general exception during container creation
91-
mock_database.create_container = AsyncMock(side_effect=Exception("Failed to create container"))
90+
# Simulate a general exception during container access
91+
mock_database.get_container_client = mock.MagicMock(side_effect=Exception("Failed to get container"))
9292

9393
# Call the initialize_cosmos method and expect it to raise an error
9494
with pytest.raises(Exception) as exc_info:
9595
await cosmos_db_client.initialize_cosmos()
9696

9797
# Assert that the exception message matches the expected message
98-
assert str(exc_info.value) == "Failed to create container"
98+
assert str(exc_info.value) == "Failed to get container"
9999

100100

101101
@pytest.mark.asyncio
@@ -104,16 +104,13 @@ async def test_initialize_cosmos_container_exists_error(cosmos_db_client, mocker
104104
mock_client = mocker.patch.object(CosmosClient, 'get_database_client', return_value=mock.MagicMock())
105105
mock_database = mock_client.return_value
106106

107-
# Simulating CosmosResourceExistsError for container creation
108-
mock_database.create_container = AsyncMock(side_effect=CosmosResourceExistsError)
109-
110107
# Use AsyncMock for asynchronous methods
111108
mock_batch_container = mock.MagicMock()
112109
mock_file_container = mock.MagicMock()
113110
mock_log_container = mock.MagicMock()
114111

115-
# Use AsyncMock to mock asynchronous container creation
116-
mock_database.create_container = AsyncMock(side_effect=[
112+
# Mock get_container_client method to return existing containers
113+
mock_database.get_container_client = mock.MagicMock(side_effect=[
117114
mock_batch_container,
118115
mock_file_container,
119116
mock_log_container
@@ -122,10 +119,10 @@ async def test_initialize_cosmos_container_exists_error(cosmos_db_client, mocker
122119
# Call the initialize_cosmos method
123120
await cosmos_db_client.initialize_cosmos()
124121

125-
# Assert that the container creation method was called with the correct arguments
126-
mock_database.create_container.assert_any_call(id='batch_container', partition_key=mock.ANY)
127-
mock_database.create_container.assert_any_call(id='file_container', partition_key=mock.ANY)
128-
mock_database.create_container.assert_any_call(id='log_container', partition_key=mock.ANY)
122+
# Assert that the container access method was called with the correct arguments
123+
mock_database.get_container_client.assert_any_call('batch_container')
124+
mock_database.get_container_client.assert_any_call('file_container')
125+
mock_database.get_container_client.assert_any_call('log_container')
129126

130127
# Check that existing containers are returned (mocked containers)
131128
assert cosmos_db_client.batch_container == mock_batch_container

0 commit comments

Comments
 (0)