diff --git a/app/modules/flappybird/cruds_flappybird.py b/app/modules/flappybird/cruds_flappybird.py index 1f7cb76b8..34f553d8e 100644 --- a/app/modules/flappybird/cruds_flappybird.py +++ b/app/modules/flappybird/cruds_flappybird.py @@ -8,12 +8,12 @@ async def get_flappybird_score_leaderboard( db: AsyncSession, -) -> list[models_flappybird.FlappyBirdBestScore]: +) -> list[models_flappybird.RFlappyBirdBestScore]: """Return the flappybird leaderboard scores""" result = await db.execute( - select(models_flappybird.FlappyBirdBestScore) - .order_by(models_flappybird.FlappyBirdBestScore.value.desc()) - .options(selectinload(models_flappybird.FlappyBirdBestScore.user)), + select(models_flappybird.RFlappyBirdBestScore) + .order_by(models_flappybird.RFlappyBirdBestScore.value.desc()) + .options(selectinload(models_flappybird.RFlappyBirdBestScore.user)), ) return list(result.scalars().all()) @@ -21,13 +21,15 @@ async def get_flappybird_score_leaderboard( async def get_flappybird_personal_best_by_user_id( db: AsyncSession, user_id: str, -) -> models_flappybird.FlappyBirdBestScore | None: +) -> models_flappybird.RFlappyBirdBestScore | None: """Return the flappybird PB in the leaderboard by user_id""" personal_best_result = await db.execute( - select(models_flappybird.FlappyBirdBestScore).where( - models_flappybird.FlappyBirdBestScore.user_id == user_id, - ), + select(models_flappybird.RFlappyBirdBestScore) + .where( + models_flappybird.RFlappyBirdBestScore.user_id == user_id, + ) + .options(selectinload(models_flappybird.RFlappyBirdBestScore.user)), ) return personal_best_result.scalar() diff --git a/app/modules/flappybird/models_flappybird.py b/app/modules/flappybird/models_flappybird.py index bef0f79b0..616491248 100644 --- a/app/modules/flappybird/models_flappybird.py +++ b/app/modules/flappybird/models_flappybird.py @@ -1,27 +1,37 @@ from datetime import datetime from sqlalchemy import ForeignKey -from sqlalchemy.orm import Mapped, mapped_column, relationship +from sqlalchemy.orm import Mapped, MappedAsDataclass, mapped_column, relationship from app.core.models_core import CoreUser from app.types.sqlalchemy import Base, PrimaryKey -class FlappyBirdScore(Base): +class FlappyBirdScore(MappedAsDataclass, Base): __tablename__ = "flappy-bird_score" id: Mapped[PrimaryKey] user_id: Mapped[str] = mapped_column(ForeignKey("core_user.id")) - user: Mapped[CoreUser] = relationship("CoreUser") value: Mapped[int] creation_time: Mapped[datetime] -class FlappyBirdBestScore(Base): +class RFlappyBirdScore(FlappyBirdScore): + __tablename__ = "flappy-bird_score" + + user: Mapped[CoreUser] = relationship("CoreUser") + + +class FlappyBirdBestScore(MappedAsDataclass, Base): __tablename__ = "flappy-bird_best_score" id: Mapped[PrimaryKey] user_id: Mapped[str] = mapped_column(ForeignKey("core_user.id")) - user: Mapped[CoreUser] = relationship("CoreUser") value: Mapped[int] creation_time: Mapped[datetime] + + +class RFlappyBirdBestScore(FlappyBirdBestScore): + __tablename__ = "flappy-bird_best_score" + + user: Mapped[CoreUser] = relationship("CoreUser") diff --git a/tests/test_flappybird.py b/tests/test_flappybird.py index e808ebae2..913002da7 100644 --- a/tests/test_flappybird.py +++ b/tests/test_flappybird.py @@ -13,8 +13,8 @@ create_user_with_groups, ) -flappybird_score: models_flappybird.FlappyBirdScore | None = None -user: models_core.CoreUser | None = None +flappybird_score: models_flappybird.FlappyBirdScore +user: models_core.CoreUser token: str = "" @@ -30,7 +30,6 @@ async def init_objects() -> None: flappybird_score = models_flappybird.FlappyBirdScore( id=uuid.uuid4(), user_id=user.id, - user=user, value=25, creation_time=datetime.now(UTC), ) @@ -40,7 +39,6 @@ async def init_objects() -> None: flappybird_best_score = models_flappybird.FlappyBirdBestScore( id=uuid.uuid4(), user_id=user.id, - user=user, value=25, creation_time=datetime.now(UTC), ) @@ -54,6 +52,8 @@ def test_get_flappybird_score(client: TestClient) -> None: headers={"Authorization": f"Bearer {token}"}, ) assert response.status_code == 200 + body = response.json() + assert body[0]["user"]["firstname"] == user.firstname def test_get_current_user_flappybird_personal_best(client: TestClient) -> None: @@ -62,6 +62,8 @@ def test_get_current_user_flappybird_personal_best(client: TestClient) -> None: headers={"Authorization": f"Bearer {token}"}, ) assert response.status_code == 200 + body = response.json() + assert body["user"]["firstname"] == user.firstname def test_create_flappybird_score(client: TestClient) -> None: