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:
@@ -4,6 +4,7 @@ from typing import Annotated
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException, Query, status
|
||||
|
||||
from app.core.database import get_session_factory
|
||||
from app.core.dependencies import get_current_active_user
|
||||
from app.models.user import User
|
||||
from app.schemas.common import MessageResponse
|
||||
@@ -19,12 +20,10 @@ router = APIRouter(prefix="/favorites", tags=["favorites"])
|
||||
|
||||
def get_favorite_service() -> FavoriteService:
|
||||
"""Get the favorite service."""
|
||||
from app.core.database import get_session_factory
|
||||
|
||||
return FavoriteService(get_session_factory())
|
||||
|
||||
|
||||
@router.get("/", response_model=FavoritesListResponse)
|
||||
@router.get("/")
|
||||
async def get_user_favorites(
|
||||
current_user: Annotated[User, Depends(get_current_active_user)],
|
||||
favorite_service: Annotated[FavoriteService, Depends(get_favorite_service)],
|
||||
@@ -33,12 +32,14 @@ async def get_user_favorites(
|
||||
) -> FavoritesListResponse:
|
||||
"""Get all favorites for the current user."""
|
||||
favorites = await favorite_service.get_user_favorites(
|
||||
current_user.id, limit, offset,
|
||||
current_user.id,
|
||||
limit,
|
||||
offset,
|
||||
)
|
||||
return FavoritesListResponse(favorites=favorites)
|
||||
|
||||
|
||||
@router.get("/sounds", response_model=FavoritesListResponse)
|
||||
@router.get("/sounds")
|
||||
async def get_user_sound_favorites(
|
||||
current_user: Annotated[User, Depends(get_current_active_user)],
|
||||
favorite_service: Annotated[FavoriteService, Depends(get_favorite_service)],
|
||||
@@ -47,12 +48,14 @@ async def get_user_sound_favorites(
|
||||
) -> FavoritesListResponse:
|
||||
"""Get sound favorites for the current user."""
|
||||
favorites = await favorite_service.get_user_sound_favorites(
|
||||
current_user.id, limit, offset,
|
||||
current_user.id,
|
||||
limit,
|
||||
offset,
|
||||
)
|
||||
return FavoritesListResponse(favorites=favorites)
|
||||
|
||||
|
||||
@router.get("/playlists", response_model=FavoritesListResponse)
|
||||
@router.get("/playlists")
|
||||
async def get_user_playlist_favorites(
|
||||
current_user: Annotated[User, Depends(get_current_active_user)],
|
||||
favorite_service: Annotated[FavoriteService, Depends(get_favorite_service)],
|
||||
@@ -61,12 +64,14 @@ async def get_user_playlist_favorites(
|
||||
) -> FavoritesListResponse:
|
||||
"""Get playlist favorites for the current user."""
|
||||
favorites = await favorite_service.get_user_playlist_favorites(
|
||||
current_user.id, limit, offset,
|
||||
current_user.id,
|
||||
limit,
|
||||
offset,
|
||||
)
|
||||
return FavoritesListResponse(favorites=favorites)
|
||||
|
||||
|
||||
@router.get("/counts", response_model=FavoriteCountsResponse)
|
||||
@router.get("/counts")
|
||||
async def get_favorite_counts(
|
||||
current_user: Annotated[User, Depends(get_current_active_user)],
|
||||
favorite_service: Annotated[FavoriteService, Depends(get_favorite_service)],
|
||||
@@ -76,7 +81,7 @@ async def get_favorite_counts(
|
||||
return FavoriteCountsResponse(**counts)
|
||||
|
||||
|
||||
@router.post("/sounds/{sound_id}", response_model=FavoriteResponse)
|
||||
@router.post("/sounds/{sound_id}")
|
||||
async def add_sound_favorite(
|
||||
sound_id: int,
|
||||
current_user: Annotated[User, Depends(get_current_active_user)],
|
||||
@@ -103,7 +108,7 @@ async def add_sound_favorite(
|
||||
) from e
|
||||
|
||||
|
||||
@router.post("/playlists/{playlist_id}", response_model=FavoriteResponse)
|
||||
@router.post("/playlists/{playlist_id}")
|
||||
async def add_playlist_favorite(
|
||||
playlist_id: int,
|
||||
current_user: Annotated[User, Depends(get_current_active_user)],
|
||||
@@ -112,7 +117,8 @@ async def add_playlist_favorite(
|
||||
"""Add a playlist to favorites."""
|
||||
try:
|
||||
favorite = await favorite_service.add_playlist_favorite(
|
||||
current_user.id, playlist_id,
|
||||
current_user.id,
|
||||
playlist_id,
|
||||
)
|
||||
return FavoriteResponse.model_validate(favorite)
|
||||
except ValueError as e:
|
||||
@@ -132,7 +138,7 @@ async def add_playlist_favorite(
|
||||
) from e
|
||||
|
||||
|
||||
@router.delete("/sounds/{sound_id}", response_model=MessageResponse)
|
||||
@router.delete("/sounds/{sound_id}")
|
||||
async def remove_sound_favorite(
|
||||
sound_id: int,
|
||||
current_user: Annotated[User, Depends(get_current_active_user)],
|
||||
@@ -149,7 +155,7 @@ async def remove_sound_favorite(
|
||||
) from e
|
||||
|
||||
|
||||
@router.delete("/playlists/{playlist_id}", response_model=MessageResponse)
|
||||
@router.delete("/playlists/{playlist_id}")
|
||||
async def remove_playlist_favorite(
|
||||
playlist_id: int,
|
||||
current_user: Annotated[User, Depends(get_current_active_user)],
|
||||
@@ -185,6 +191,7 @@ async def check_playlist_favorited(
|
||||
) -> dict[str, bool]:
|
||||
"""Check if a playlist is favorited by the current user."""
|
||||
is_favorited = await favorite_service.is_playlist_favorited(
|
||||
current_user.id, playlist_id,
|
||||
current_user.id,
|
||||
playlist_id,
|
||||
)
|
||||
return {"is_favorited": is_favorited}
|
||||
|
||||
Reference in New Issue
Block a user