feat: Add endpoint to retrieve currently processing extractions and corresponding tests
This commit is contained in:
@@ -159,3 +159,47 @@ async def get_all_extractions( # noqa: PLR0913
|
||||
) from e
|
||||
else:
|
||||
return result
|
||||
|
||||
|
||||
@router.get("/processing/current")
|
||||
async def get_processing_extractions(
|
||||
current_user: Annotated[User, Depends(get_current_active_user_flexible)], # noqa: ARG001
|
||||
extraction_service: Annotated[ExtractionService, Depends(get_extraction_service)],
|
||||
) -> list[ExtractionInfo]:
|
||||
"""Get all currently processing extractions for showing ongoing toasts."""
|
||||
try:
|
||||
# Get all extractions with processing status
|
||||
processing_extractions = await extraction_service.extraction_repo.get_by_status(
|
||||
"processing"
|
||||
)
|
||||
|
||||
# Convert to ExtractionInfo format
|
||||
result = []
|
||||
for extraction in processing_extractions:
|
||||
# Get user information
|
||||
user = await extraction_service.user_repo.get_by_id(extraction.user_id)
|
||||
user_name = user.name if user else None
|
||||
|
||||
extraction_info: ExtractionInfo = {
|
||||
"id": extraction.id or 0,
|
||||
"url": extraction.url,
|
||||
"service": extraction.service,
|
||||
"service_id": extraction.service_id,
|
||||
"title": extraction.title,
|
||||
"status": extraction.status,
|
||||
"error": extraction.error,
|
||||
"sound_id": extraction.sound_id,
|
||||
"user_id": extraction.user_id,
|
||||
"user_name": user_name,
|
||||
"created_at": extraction.created_at.isoformat(),
|
||||
"updated_at": extraction.updated_at.isoformat(),
|
||||
}
|
||||
result.append(extraction_info)
|
||||
|
||||
return result
|
||||
|
||||
except Exception as e:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||
detail=f"Failed to get processing extractions: {e!s}",
|
||||
) from e
|
||||
|
||||
@@ -184,3 +184,48 @@ class TestExtractionEndpoints:
|
||||
data = response.json()
|
||||
assert "extractions" in data
|
||||
assert isinstance(data["extractions"], list)
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_get_processing_extractions(
|
||||
self,
|
||||
test_client: AsyncClient,
|
||||
auth_cookies: dict[str, str],
|
||||
test_session: AsyncSession,
|
||||
authenticated_user: User,
|
||||
) -> None:
|
||||
"""Test getting currently processing extractions."""
|
||||
# Create a processing extraction
|
||||
processing_extraction = Extraction(
|
||||
url="https://www.youtube.com/watch?v=processing",
|
||||
user_id=authenticated_user.id,
|
||||
service="youtube",
|
||||
service_id="processing123",
|
||||
title="Processing Video",
|
||||
status="processing",
|
||||
)
|
||||
test_session.add(processing_extraction)
|
||||
await test_session.commit()
|
||||
await test_session.refresh(processing_extraction)
|
||||
|
||||
# Set cookies on client instance
|
||||
test_client.cookies.update(auth_cookies)
|
||||
|
||||
response = await test_client.get("/api/v1/extractions/processing/current")
|
||||
|
||||
# Should succeed and return the processing extraction
|
||||
assert response.status_code == 200
|
||||
data = response.json()
|
||||
assert isinstance(data, list)
|
||||
assert len(data) >= 1
|
||||
|
||||
# Find our processing extraction in the results
|
||||
processing_found = False
|
||||
for extraction in data:
|
||||
if extraction["id"] == processing_extraction.id:
|
||||
processing_found = True
|
||||
assert extraction["status"] == "processing"
|
||||
assert extraction["title"] == "Processing Video"
|
||||
assert extraction["url"] == "https://www.youtube.com/watch?v=processing"
|
||||
break
|
||||
|
||||
assert processing_found, "Processing extraction not found in results"
|
||||
|
||||
Reference in New Issue
Block a user