Refactor sound and extraction services to include user and timestamp fields
All checks were successful
Backend CI / lint (push) Successful in 18m8s
Backend CI / test (push) Successful in 53m35s

- Updated ExtractionInfo to include user_id, created_at, and updated_at fields.
- Modified ExtractionService to return user and timestamp information in extraction responses.
- Enhanced sound serialization in PlayerState to include extraction URL if available.
- Adjusted PlaylistRepository to load sound extractions when retrieving playlist sounds.
- Added tests for new fields in extraction and sound endpoints, ensuring proper response structure.
- Created new test file endpoints for sound downloads and thumbnail retrievals, including success and error cases.
- Refactored various test cases for consistency and clarity, ensuring proper mocking and assertions.
This commit is contained in:
JSC
2025-08-03 20:54:14 +02:00
parent 77446cb5a8
commit b4f0f54516
20 changed files with 780 additions and 73 deletions

View File

@@ -1,34 +1,134 @@
"""Tests for extraction API endpoints."""
from unittest.mock import patch
import pytest
import pytest_asyncio
from httpx import AsyncClient
from sqlmodel.ext.asyncio.session import AsyncSession
from app.models.extraction import Extraction
from app.models.user import User
class TestExtractionEndpoints:
"""Test extraction API endpoints."""
@pytest_asyncio.fixture
async def test_extraction(
self,
test_session: AsyncSession,
authenticated_user: User,
) -> Extraction:
"""Create a test extraction."""
extraction = Extraction(
url="https://www.youtube.com/watch?v=test",
user_id=authenticated_user.id,
service="youtube",
service_id="test",
title="Test Video",
status="completed",
)
test_session.add(extraction)
await test_session.commit()
await test_session.refresh(extraction)
return extraction
@pytest.mark.asyncio
async def test_create_extraction_success(
self,
test_client: AsyncClient,
auth_cookies: dict[str, str],
authenticated_client: AsyncClient,
authenticated_user: User,
) -> None:
"""Test successful extraction creation."""
# Set cookies on client instance to avoid deprecation warning
test_client.cookies.update(auth_cookies)
"""Test successful extraction creation with proper response format."""
# Store user ID to avoid session issues
user_id = authenticated_user.id
response = await test_client.post(
"/api/v1/extractions/",
params={"url": "https://www.youtube.com/watch?v=test"},
with patch(
"app.services.extraction_processor.extraction_processor.queue_extraction",
):
response = await authenticated_client.post(
"/api/v1/extractions/",
params={"url": "https://www.youtube.com/watch?v=test"},
)
assert response.status_code == 200
data = response.json()
# Verify response structure
assert "message" in data
assert "extraction" in data
extraction_data = data["extraction"]
# Verify all required fields including timestamps
assert "id" in extraction_data
assert "url" in extraction_data
assert "user_id" in extraction_data
assert "status" in extraction_data
assert "created_at" in extraction_data
assert "updated_at" in extraction_data
assert extraction_data["url"] == "https://www.youtube.com/watch?v=test"
assert extraction_data["user_id"] == user_id
assert extraction_data["status"] == "pending"
@pytest.mark.asyncio
async def test_get_extraction_success(
self,
authenticated_client: AsyncClient,
authenticated_user: User,
test_extraction: Extraction,
) -> None:
"""Test successful extraction retrieval with timestamp fields."""
response = await authenticated_client.get(
f"/api/v1/extractions/{test_extraction.id}",
)
# This will fail because we don't have actual extraction service mocked
# But at least we'll get past authentication
assert response.status_code in [200, 400, 500] # Allow any non-auth error
assert response.status_code == 200
data = response.json()
# Verify all fields including timestamps
assert data["id"] == test_extraction.id
assert data["url"] == test_extraction.url
assert data["user_id"] == test_extraction.user_id
assert data["service"] == test_extraction.service
assert data["service_id"] == test_extraction.service_id
assert data["title"] == test_extraction.title
assert data["status"] == test_extraction.status
assert "created_at" in data
assert "updated_at" in data
@pytest.mark.asyncio
async def test_get_user_extractions_success(
self,
authenticated_client: AsyncClient,
authenticated_user: User,
test_extraction: Extraction,
) -> None:
"""Test successful user extractions retrieval with timestamp fields."""
response = await authenticated_client.get(
"/api/v1/extractions/",
)
assert response.status_code == 200
data = response.json()
# Verify response structure
assert "extractions" in data
assert len(data["extractions"]) >= 1
extraction_data = data["extractions"][0]
# Verify all fields including timestamps
assert "id" in extraction_data
assert "url" in extraction_data
assert "user_id" in extraction_data
assert "status" in extraction_data
assert "created_at" in extraction_data
assert "updated_at" in extraction_data
@pytest.mark.asyncio
async def test_create_extraction_unauthenticated(
self, test_client: AsyncClient,
self,
test_client: AsyncClient,
) -> None:
"""Test extraction creation without authentication."""
response = await test_client.post(
@@ -41,7 +141,8 @@ class TestExtractionEndpoints:
@pytest.mark.asyncio
async def test_get_extraction_unauthenticated(
self, test_client: AsyncClient,
self,
test_client: AsyncClient,
) -> None:
"""Test extraction retrieval without authentication."""
response = await test_client.get("/api/v1/extractions/1")