From ba3338eb820d865d1ba1f94db9f8dc6b32a148fc Mon Sep 17 00:00:00 2001 From: 2jun0 Date: Mon, 12 Feb 2024 22:59:54 +0900 Subject: [PATCH] =?UTF-8?q?fix:=20greenlet=20=EC=97=90=EB=9F=AC=20?= =?UTF-8?q?=ED=95=B4=EA=B2=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/src/quiz/dependency.py | 17 ++++++++++++++--- backend/src/quiz/repository.py | 8 ++++++-- backend/src/quiz/service.py | 4 +++- .../quiz/test_get_daily_quizzes.py | 13 ++++++++----- backend/tests/utils/quiz.py | 19 ++++++++++++++++++- 5 files changed, 49 insertions(+), 12 deletions(-) diff --git a/backend/src/quiz/dependency.py b/backend/src/quiz/dependency.py index 4160029..7569fb6 100644 --- a/backend/src/quiz/dependency.py +++ b/backend/src/quiz/dependency.py @@ -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: @@ -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)] diff --git a/backend/src/quiz/repository.py b/backend/src/quiz/repository.py index af2c537..7fb1a82 100644 --- a/backend/src/quiz/repository.py +++ b/backend/src/quiz/repository.py @@ -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() diff --git a/backend/src/quiz/service.py b/backend/src/quiz/service.py index d5f284d..c89daf1 100644 --- a/backend/src/quiz/service.py +++ b/backend/src/quiz/service.py @@ -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: diff --git a/backend/tests/integration/quiz/test_get_daily_quizzes.py b/backend/tests/integration/quiz/test_get_daily_quizzes.py index 63169e6..56493c0 100644 --- a/backend/tests/integration/quiz/test_get_daily_quizzes.py +++ b/backend/tests/integration/quiz/test_get_daily_quizzes.py @@ -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 diff --git a/backend/tests/utils/quiz.py b/backend/tests/utils/quiz.py index 06816c8..b6461ea 100644 --- a/backend/tests/utils/quiz.py +++ b/backend/tests/utils/quiz.py @@ -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 @@ -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