Refactor test cases for improved readability and consistency
All checks were successful
Backend CI / lint (push) Successful in 9m49s
Backend CI / test (push) Successful in 6m15s

- Adjusted function signatures in various test files to enhance clarity by aligning parameters.
- Updated patching syntax for better readability across test cases.
- Improved formatting and spacing in test assertions and mock setups.
- Ensured consistent use of async/await patterns in async test functions.
- Enhanced comments for better understanding of test intentions.
This commit is contained in:
JSC
2025-08-01 20:53:30 +02:00
parent d926779fe4
commit 6068599a47
39 changed files with 691 additions and 286 deletions

View File

@@ -43,7 +43,9 @@ class BaseRepository[ModelType]:
return result.first()
except Exception:
logger.exception(
"Failed to get %s by ID: %s", self.model.__name__, entity_id,
"Failed to get %s by ID: %s",
self.model.__name__,
entity_id,
)
raise

View File

@@ -91,8 +91,7 @@ class CreditTransactionRepository(BaseRepository[CreditTransaction]):
"""
stmt = (
select(CreditTransaction)
.where(CreditTransaction.success == True) # noqa: E712
select(CreditTransaction).where(CreditTransaction.success == True) # noqa: E712
)
if user_id is not None:

View File

@@ -1,6 +1,5 @@
"""Extraction repository for database operations."""
from sqlalchemy import desc
from sqlmodel import select
from sqlmodel.ext.asyncio.session import AsyncSession
@@ -17,12 +16,15 @@ class ExtractionRepository(BaseRepository[Extraction]):
super().__init__(Extraction, session)
async def get_by_service_and_id(
self, service: str, service_id: str,
self,
service: str,
service_id: str,
) -> Extraction | None:
"""Get an extraction by service and service_id."""
result = await self.session.exec(
select(Extraction).where(
Extraction.service == service, Extraction.service_id == service_id,
Extraction.service == service,
Extraction.service_id == service_id,
),
)
return result.first()

View File

@@ -1,6 +1,5 @@
"""Playlist repository for database operations."""
from sqlalchemy import func
from sqlmodel import select
from sqlmodel.ext.asyncio.session import AsyncSession
@@ -66,7 +65,9 @@ class PlaylistRepository(BaseRepository[Playlist]):
raise
async def search_by_name(
self, query: str, user_id: int | None = None,
self,
query: str,
user_id: int | None = None,
) -> list[Playlist]:
"""Search playlists by name (case-insensitive)."""
try:
@@ -98,7 +99,10 @@ class PlaylistRepository(BaseRepository[Playlist]):
raise
async def add_sound_to_playlist(
self, playlist_id: int, sound_id: int, position: int | None = None,
self,
playlist_id: int,
sound_id: int,
position: int | None = None,
) -> PlaylistSound:
"""Add a sound to a playlist."""
try:
@@ -121,7 +125,9 @@ class PlaylistRepository(BaseRepository[Playlist]):
except Exception:
await self.session.rollback()
logger.exception(
"Failed to add sound %s to playlist %s", sound_id, playlist_id,
"Failed to add sound %s to playlist %s",
sound_id,
playlist_id,
)
raise
else:
@@ -150,12 +156,16 @@ class PlaylistRepository(BaseRepository[Playlist]):
except Exception:
await self.session.rollback()
logger.exception(
"Failed to remove sound %s from playlist %s", sound_id, playlist_id,
"Failed to remove sound %s from playlist %s",
sound_id,
playlist_id,
)
raise
async def reorder_playlist_sounds(
self, playlist_id: int, sound_positions: list[tuple[int, int]],
self,
playlist_id: int,
sound_positions: list[tuple[int, int]],
) -> None:
"""Reorder sounds in a playlist.
@@ -220,6 +230,8 @@ class PlaylistRepository(BaseRepository[Playlist]):
return result.first() is not None
except Exception:
logger.exception(
"Failed to check if sound %s is in playlist %s", sound_id, playlist_id,
"Failed to check if sound %s is in playlist %s",
sound_id,
playlist_id,
)
raise

View File

@@ -91,6 +91,7 @@ class SoundRepository(BaseRepository[Sound]):
return list(result.all())
except Exception:
logger.exception(
"Failed to get unnormalized sounds by type: %s", sound_type,
"Failed to get unnormalized sounds by type: %s",
sound_type,
)
raise

View File

@@ -1,6 +1,5 @@
"""Repository for user OAuth operations."""
from sqlmodel import select
from sqlmodel.ext.asyncio.session import AsyncSession
@@ -60,4 +59,3 @@ class UserOauthRepository(BaseRepository[UserOauth]):
raise
else:
return result.first()