Refactor user endpoint tests to include pagination and response structure validation

- Updated tests for listing users to validate pagination and response format.
- Changed mock return values to include total count and pagination details.
- Refactored user creation mocks for clarity and consistency.
- Enhanced assertions to check for presence of pagination fields in responses.
- Adjusted test cases for user retrieval and updates to ensure proper handling of user data.
- Improved readability by restructuring mock definitions and assertions across various test files.
This commit is contained in:
JSC
2025-08-17 12:36:52 +02:00
parent e6f796a3c9
commit 6b55ff0e81
35 changed files with 863 additions and 503 deletions

View File

@@ -9,7 +9,7 @@ from httpx import AsyncClient
from app.models.user import User
if TYPE_CHECKING:
from app.services.extraction import ExtractionInfo
from app.services.extraction import ExtractionInfo, PaginatedExtractionsResponse
class TestSoundEndpoints:
@@ -32,6 +32,7 @@ class TestSoundEndpoints:
"error": None,
"sound_id": None,
"user_id": authenticated_user.id,
"user_name": authenticated_user.name,
"created_at": "2025-08-03T12:00:00Z",
"updated_at": "2025-08-03T12:00:00Z",
}
@@ -111,6 +112,7 @@ class TestSoundEndpoints:
"error": None,
"sound_id": 42,
"user_id": authenticated_user.id,
"user_name": authenticated_user.name,
"created_at": "2025-08-03T12:00:00Z",
"updated_at": "2025-08-03T12:00:00Z",
}
@@ -154,41 +156,49 @@ class TestSoundEndpoints:
authenticated_user: User,
) -> None:
"""Test getting user extractions."""
mock_extractions: list[ExtractionInfo] = [
{
"id": 1,
"url": "https://www.youtube.com/watch?v=dQw4w9WgXcQ",
"title": "Never Gonna Give You Up",
"service": "youtube",
"service_id": "dQw4w9WgXcQ",
"status": "completed",
"error": None,
"sound_id": 42,
"user_id": authenticated_user.id,
"created_at": "2025-08-03T12:00:00Z",
"updated_at": "2025-08-03T12:00:00Z",
},
{
"id": 2,
"url": "https://soundcloud.com/example/track",
"title": "Example Track",
"service": "soundcloud",
"service_id": "example-track",
"status": "pending",
"error": None,
"sound_id": None,
"user_id": authenticated_user.id,
"created_at": "2025-08-03T12:00:00Z",
"updated_at": "2025-08-03T12:00:00Z",
},
]
mock_extractions: PaginatedExtractionsResponse = {
"extractions": [
{
"id": 1,
"url": "https://www.youtube.com/watch?v=dQw4w9WgXcQ",
"title": "Never Gonna Give You Up",
"service": "youtube",
"service_id": "dQw4w9WgXcQ",
"status": "completed",
"error": None,
"sound_id": 42,
"user_id": authenticated_user.id,
"user_name": authenticated_user.name,
"created_at": "2025-08-03T12:00:00Z",
"updated_at": "2025-08-03T12:00:00Z",
},
{
"id": 2,
"url": "https://soundcloud.com/example/track",
"title": "Example Track",
"service": "soundcloud",
"service_id": "example-track",
"status": "pending",
"error": None,
"sound_id": None,
"user_id": authenticated_user.id,
"user_name": authenticated_user.name,
"created_at": "2025-08-03T12:00:00Z",
"updated_at": "2025-08-03T12:00:00Z",
},
],
"total": 2,
"page": 1,
"limit": 50,
"total_pages": 1,
}
with patch(
"app.services.extraction.ExtractionService.get_user_extractions",
) as mock_get:
mock_get.return_value = mock_extractions
response = await authenticated_client.get("/api/v1/extractions/")
response = await authenticated_client.get("/api/v1/extractions/user")
assert response.status_code == 200
data = response.json()
@@ -337,7 +347,9 @@ class TestSoundEndpoints:
"""Test getting sounds with authentication."""
from app.models.sound import Sound
with patch("app.repositories.sound.SoundRepository.search_and_sort") as mock_get:
with patch(
"app.repositories.sound.SoundRepository.search_and_sort",
) as mock_get:
# Create mock sounds with all required fields
mock_sound_1 = Sound(
id=1,
@@ -383,7 +395,9 @@ class TestSoundEndpoints:
"""Test getting sounds with type filtering."""
from app.models.sound import Sound
with patch("app.repositories.sound.SoundRepository.search_and_sort") as mock_get:
with patch(
"app.repositories.sound.SoundRepository.search_and_sort",
) as mock_get:
# Create mock sound with all required fields
mock_sound = Sound(
id=1,