Skip to content

Commit 7815d5c

Browse files
Enhance CosmosDBClient error logging and improve test assertions for existing batch records
1 parent a6b843a commit 7815d5c

2 files changed

Lines changed: 10 additions & 5 deletions

File tree

src/backend/common/database/cosmosdb.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,13 +95,18 @@ async def create_batch(self, user_id: str, batch_id: UUID) -> BatchRecord:
9595
item=str(batch_id), partition_key=str(batch_id)
9696
)
9797
if batchexists.get("user_id") != user_id:
98+
self.logger.error("Batch belongs to a different user", batch_id=str(batch_id), existing_user=batchexists.get("user_id"), requesting_user=user_id)
9899
raise ValueError("Batch belongs to a different user")
99-
return batchexists
100+
self.logger.info("Returning existing batch record", batch_id=str(batch_id))
101+
return BatchRecord.fromdb(batchexists)
100102
except CosmosResourceNotFoundError:
101103
if attempt < 2:
104+
self.logger.info("Batch read returned 404 after conflict, retrying", batch_id=str(batch_id), attempt=attempt + 1)
102105
await asyncio.sleep(0.5 * (attempt + 1))
103106
else:
104-
return batch
107+
raise RuntimeError(
108+
f"Batch {batch_id} already exists but could not be read after retries"
109+
)
105110

106111
except Exception as e:
107112
self.logger.error("Failed to create batch", error=str(e))

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -179,9 +179,9 @@ async def test_create_batch_exists(cosmos_db_client, mocker):
179179
batch = await cosmos_db_client.create_batch(user_id, batch_id)
180180

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

186186
mock_batch_container.read_item.assert_called_once_with(
187187
item=str(batch_id), partition_key=str(batch_id)

0 commit comments

Comments
 (0)