Skip to content

Commit a6b843a

Browse files
Refactor test_create_batch_exists to mock read_item instead of get_batch for existing batch records
1 parent 73f054a commit a6b843a

1 file changed

Lines changed: 18 additions & 15 deletions

File tree

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

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -158,31 +158,34 @@ async def test_create_batch_exists(cosmos_db_client, mocker):
158158
user_id = "user_1"
159159
batch_id = uuid4()
160160

161-
# Mock container creation and get_batch
161+
# Mock container creation and read_item
162162
mock_batch_container = mock.MagicMock()
163163
mocker.patch.object(cosmos_db_client, 'batch_container', mock_batch_container)
164164
mock_batch_container.create_item = AsyncMock(side_effect=CosmosResourceExistsError)
165165

166-
# Mock the get_batch method
167-
mock_get_batch = AsyncMock(return_value=BatchRecord(
168-
batch_id=batch_id,
169-
user_id=user_id,
170-
file_count=0,
171-
created_at=datetime.now(timezone.utc),
172-
updated_at=datetime.now(timezone.utc),
173-
status=ProcessStatus.READY_TO_PROCESS
174-
))
175-
mocker.patch.object(cosmos_db_client, 'get_batch', mock_get_batch)
166+
# Mock read_item to return the existing batch record
167+
existing_batch = {
168+
"id": str(batch_id),
169+
"batch_id": str(batch_id),
170+
"user_id": user_id,
171+
"file_count": 0,
172+
"created_at": datetime.now(timezone.utc).isoformat(),
173+
"updated_at": datetime.now(timezone.utc).isoformat(),
174+
"status": ProcessStatus.READY_TO_PROCESS,
175+
}
176+
mock_batch_container.read_item = AsyncMock(return_value=existing_batch)
176177

177178
# Call the method
178179
batch = await cosmos_db_client.create_batch(user_id, batch_id)
179180

180181
# Assert that batch was fetched (not created) due to already existing
181-
assert batch.batch_id == batch_id
182-
assert batch.user_id == user_id
183-
assert batch.status == ProcessStatus.READY_TO_PROCESS
182+
assert batch["batch_id"] == str(batch_id)
183+
assert batch["user_id"] == user_id
184+
assert batch["status"] == ProcessStatus.READY_TO_PROCESS
184185

185-
mock_get_batch.assert_called_once_with(user_id, str(batch_id))
186+
mock_batch_container.read_item.assert_called_once_with(
187+
item=str(batch_id), partition_key=str(batch_id)
188+
)
186189

187190

188191
@pytest.mark.asyncio

0 commit comments

Comments
 (0)