Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use a dedicated relationship model and enable dataclass support #606

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions app/modules/flappybird/cruds_flappybird.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,28 @@

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())


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()

Expand Down
20 changes: 15 additions & 5 deletions app/modules/flappybird/models_flappybird.py
Original file line number Diff line number Diff line change
@@ -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")
10 changes: 6 additions & 4 deletions tests/test_flappybird.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ""


Expand All @@ -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),
)
Expand All @@ -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),
)
Expand All @@ -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:
Expand All @@ -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:
Expand Down