Skip to content

Commit

Permalink
Merge branch 'GDET-50'
Browse files Browse the repository at this point in the history
  • Loading branch information
2jun0 committed Feb 12, 2024
2 parents 34c2f52 + ba3338e commit ac41d95
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 12 deletions.
17 changes: 14 additions & 3 deletions backend/src/quiz/dependency.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,20 @@
from fastapi import Depends

from ..dependency import SessionDep
from .repository import QuizAnswerRepository, QuizRepository
from .repository import DailyQuizRepository, QuizAnswerRepository, QuizRepository
from .service import QuizService


async def get_quiz_service(
quiz_repository: "QuizRepositoryDep", quiz_submit_repository: "QuizAnswerRepositoryDep"
quiz_repository: "QuizRepositoryDep",
quiz_submit_repository: "QuizAnswerRepositoryDep",
daily_quiz_repository: "DailyQuizRepositoryDep",
) -> QuizService:
return QuizService(quiz_repository=quiz_repository, quiz_answer_repository=quiz_submit_repository)
return QuizService(
quiz_repository=quiz_repository,
quiz_answer_repository=quiz_submit_repository,
daily_quiz_repository=daily_quiz_repository,
)


async def get_quiz_repository(session: SessionDep) -> QuizRepository:
Expand All @@ -21,6 +27,11 @@ async def get_quiz_answer_repository(session: SessionDep) -> QuizAnswerRepositor
return QuizAnswerRepository(session)


async def get_daily_quiz_repository(session: SessionDep) -> DailyQuizRepository:
return DailyQuizRepository(session)


QuizRepositoryDep = Annotated[QuizRepository, Depends(get_quiz_repository)]
QuizAnswerRepositoryDep = Annotated[QuizAnswerRepository, Depends(get_quiz_answer_repository)]
DailyQuizRepositoryDep = Annotated[DailyQuizRepository, Depends(get_daily_quiz_repository)]
QuizServiceDep = Annotated[QuizService, Depends(get_quiz_service)]
8 changes: 6 additions & 2 deletions backend/src/quiz/repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,11 @@ class DailyQuizRepository(IRepository[DailyQuiz], CRUDMixin):
def __init__(self, session: AsyncSession) -> None:
self._session = session

async def get_by_target_date_with_quiz(self, *, target_date: date) -> Sequence[DailyQuiz]:
stmt = select(DailyQuiz).where(DailyQuiz.target_date == target_date).options(selectinload(DailyQuiz.quiz))
async def get_by_target_date_with_quiz_and_screenshots(self, *, target_date: date) -> Sequence[DailyQuiz]:
stmt = (
select(DailyQuiz)
.where(DailyQuiz.target_date == target_date)
.options(selectinload(DailyQuiz.quiz).selectinload(Quiz.screenshots)) # type: ignore
)
rs = await self._session.exec(stmt)
return rs.all()
4 changes: 3 additions & 1 deletion backend/src/quiz/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ def _today_quizzes(self, daily_quizzes: Iterable[DailyQuiz]) -> list[schema.Dail
async def get_today_quizzes(self) -> Sequence[schema.DailyQuiz]:
utc_now_date = datetime.utcnow().date()

daily_quizzes = await self._daily_quiz_repo.get_by_target_date_with_quiz(target_date=utc_now_date)
daily_quizzes = await self._daily_quiz_repo.get_by_target_date_with_quiz_and_screenshots(
target_date=utc_now_date
)
return self._today_quizzes(daily_quizzes)

async def submit_answer(self, *, quiz_id: int, user_id: int, answer: str) -> bool:
Expand Down
13 changes: 8 additions & 5 deletions backend/tests/integration/quiz/test_get_daily_quizzes.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
from datetime import datetime

from fastapi import status
from fastapi.testclient import TestClient
from sqlmodel import Session

from tests.utils.quiz import create_random_quiz
from tests.utils.quiz import create_random_daily_quiz


def test_get_daily_quizzes(client: TestClient, session: Session):
saved_quizzes = [create_random_quiz(session) for _ in range(5)]
today = datetime.utcnow().date()
saved_daily_quizzes = [create_random_daily_quiz(session, target_date=today) for _ in range(5)]

res = client.get("/quiz/daily_quizes")
assert res.status_code == status.HTTP_200_OK

res_json = res.json()
assert len(res_json["daily_quizes"]) == 5
for daily_quiz_json, saved_quiz in zip(res_json["daily_quizes"], saved_quizzes):
for daily_quiz_json, saved_daily_quiz in zip(res_json["daily_quizes"], saved_daily_quizzes):
assert len(daily_quiz_json["screenshots"]) == 5
assert daily_quiz_json["screenshots"] == [s.url for s in saved_quiz.screenshots]
assert daily_quiz_json["quiz_id"] == saved_quiz.id
assert daily_quiz_json["screenshots"] == [s.url for s in saved_daily_quiz.quiz.screenshots]
assert daily_quiz_json["quiz_id"] == saved_daily_quiz.quiz_id
19 changes: 18 additions & 1 deletion backend/tests/utils/quiz.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
from collections.abc import Iterable
from datetime import date
from typing import Optional

from sqlmodel import Session, select

from src.game.model import GameScreenshot
from src.quiz.model import Quiz, QuizAnswer
from src.quiz.model import DailyQuiz, Quiz, QuizAnswer

from .auth import create_random_user
from .game import create_random_game
Expand Down Expand Up @@ -68,3 +70,18 @@ def get_quiz_answer(
stmt = stmt.where(QuizAnswer.answer == answer)

return session.exec(stmt).first()


def create_random_daily_quiz(session: Session, *, target_date: date, quiz_id: int | None = None) -> DailyQuiz:
if quiz_id is None:
quiz = create_random_quiz(session)
assert quiz.id is not None
quiz_id = quiz.id

daily_quiz = DailyQuiz(target_date=target_date, quiz_id=quiz_id)

session.add(daily_quiz)
session.commit()
session.refresh(daily_quiz)

return daily_quiz

0 comments on commit ac41d95

Please sign in to comment.