Skip to content

Commit b7b9967

Browse files
authored
Feat: faq api 테스트 코드 구현 (#102)
- faq 테스트 코드를 작성했습니다 - faq api에서 QuestionNowFoundException를 QuestionNotFoundException로 바꿨습니다 - faq 생성 api에서 Service 레이어에 request를 넘겨주는 것으로 수정했습니다 <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * Bug Fixes * Ensured FAQ “question not found” responses consistently return 404 with a clear message. * Refactor * Streamlined FAQ creation flow to improve consistency and maintainability. * Tests * Added comprehensive tests covering FAQ create, read, update, and delete scenarios. * Updated sponsor tests to support and verify partial updates. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
1 parent 566fe49 commit b7b9967

File tree

6 files changed

+137
-10
lines changed

6 files changed

+137
-10
lines changed

wacruit/src/apps/faq/exceptions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from wacruit.src.apps.common.exceptions import WacruitException
22

33

4-
class QuestionNowFoundException(WacruitException):
4+
class QuestionNotFoundException(WacruitException):
55
def __init__(self):
66
super().__init__(status_code=404, detail="존재하지 않는 질문입니다.")

wacruit/src/apps/faq/services.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22

33
from fastapi import Depends
44

5-
from wacruit.src.apps.faq.exceptions import QuestionNowFoundException
5+
from wacruit.src.apps.faq.exceptions import QuestionNotFoundException
66
from wacruit.src.apps.faq.models import FAQ
77
from wacruit.src.apps.faq.repositories import QuestionRepository
8+
from wacruit.src.apps.faq.schemas import CreateQuestionRequest
89
from wacruit.src.apps.faq.schemas import UpdateQuestionRequest
910

1011

@@ -15,10 +16,11 @@ def __init__(self, question_repository: Annotated[QuestionRepository, Depends()]
1516
def get_question_by_id(self, questions_id: int) -> FAQ:
1617
question = self.question_repository.get_question_by_id(questions_id)
1718
if question is None:
18-
raise QuestionNowFoundException
19+
raise QuestionNotFoundException
1920
return question
2021

21-
def create_question(self, question: FAQ) -> FAQ:
22+
def create_question(self, request: CreateQuestionRequest) -> FAQ:
23+
question = FAQ(question=request.question, answer=request.answer)
2224
return self.question_repository.create_questions(question)
2325

2426
def get_questions(self) -> list[FAQ]:

wacruit/src/apps/faq/views.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,7 @@ def create_question(
3434
request: CreateQuestionRequest,
3535
question_service: Annotated[QuestionService, Depends()],
3636
) -> QuestionResponse:
37-
res = question_service.create_question(
38-
FAQ(question=request.question, answer=request.answer)
39-
)
37+
res = question_service.create_question(request)
4038

4139
return QuestionResponse.from_orm(res)
4240

wacruit/src/tests/faq/conftest.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
from typing import List
2+
3+
import pytest
4+
from sqlalchemy.orm import Session
5+
6+
from wacruit.src.apps.faq.models import FAQ
7+
from wacruit.src.apps.faq.repositories import QuestionRepository
8+
from wacruit.src.apps.faq.schemas import CreateQuestionRequest
9+
from wacruit.src.apps.faq.schemas import UpdateQuestionRequest
10+
from wacruit.src.apps.faq.services import QuestionService
11+
from wacruit.src.database.connection import Transaction
12+
13+
14+
@pytest.fixture
15+
def created_question(db_session: Session) -> FAQ:
16+
question = FAQ(
17+
question="개발 경험이 없는데 지원 가능한가요?",
18+
answer="개발을 처음 접하시는 분들도 물론 준회원(Rookies)으로 지원 가능합니다! \
19+
와플스튜디오에서 제공하는 세미나를 통해 지식과 협업 경험을 쌓으실 수 있습니다.",
20+
)
21+
db_session.add(question)
22+
db_session.commit()
23+
return question
24+
25+
26+
@pytest.fixture
27+
def created_questions(db_session: Session) -> List[FAQ]:
28+
question1 = FAQ(
29+
question="개발 경험이 없는데 지원 가능한가요?",
30+
answer="개발을 처음 접하시는 분들도 물론 준회원(Rookies)으로 지원 가능합니다! \
31+
와플스튜디오에서 제공하는 세미나를 통해 지식과 협업 경험을 쌓으실 수 있습니다.",
32+
)
33+
question2 = FAQ(
34+
question="저학번/고학번/졸업생인데 지원해도 되나요?",
35+
answer="와플스튜디오는 서울대학교에 재학중이거나 졸업했던 누구나, 나이 제한 없이 가입 가능합니다. \
36+
다양한 학번으로 구성되어 있어 학번 및 졸업 여부와 상관 없이 지원 가능하니 부담 가지지 말고 지원해주세요!",
37+
)
38+
db_session.add_all([question1, question2])
39+
db_session.commit()
40+
return [question1, question2]
41+
42+
43+
@pytest.fixture
44+
def create_question_dto() -> CreateQuestionRequest:
45+
return CreateQuestionRequest(
46+
question="개발 경험이 없는데 지원 가능한가요?",
47+
answer="개발을 처음 접하시는 분들도 물론 준회원(Rookies)으로 지원 가능합니다! \
48+
와플스튜디오에서 제공하는 세미나를 통해 지식과 협업 경험을 쌓으실 수 있습니다.",
49+
)
50+
51+
52+
@pytest.fixture
53+
def update_question_dto() -> UpdateQuestionRequest:
54+
return UpdateQuestionRequest(answer="답변 수정")
55+
56+
57+
@pytest.fixture
58+
def question_service(question_repository: QuestionRepository) -> QuestionService:
59+
return QuestionService(question_repository=question_repository)
60+
61+
62+
@pytest.fixture
63+
def question_repository(db_session: Session) -> QuestionRepository:
64+
return QuestionRepository(session=db_session, transaction=Transaction(db_session))
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
from typing import List
2+
3+
import pytest
4+
5+
from wacruit.src.apps.faq.exceptions import QuestionNotFoundException
6+
from wacruit.src.apps.faq.models import FAQ
7+
from wacruit.src.apps.faq.schemas import CreateQuestionRequest
8+
from wacruit.src.apps.faq.schemas import UpdateQuestionRequest
9+
from wacruit.src.apps.faq.services import QuestionService
10+
11+
12+
def test_create_question(
13+
question_service: QuestionService, create_question_dto: CreateQuestionRequest
14+
):
15+
created_question = question_service.create_question(create_question_dto)
16+
assert created_question.id is not None
17+
assert created_question.question == create_question_dto.question
18+
assert created_question.answer == create_question_dto.answer
19+
20+
21+
def test_update_question(
22+
question_service: QuestionService,
23+
created_question: FAQ,
24+
update_question_dto: UpdateQuestionRequest,
25+
):
26+
question_id = created_question.id
27+
updated_question = question_service.update_question(
28+
question_id, update_question_dto
29+
)
30+
assert updated_question.id == question_id
31+
if update_question_dto.question is not None:
32+
assert updated_question.question == update_question_dto.question
33+
if update_question_dto.answer is not None:
34+
assert updated_question.answer == update_question_dto.answer
35+
36+
37+
def test_get_questions(question_service: QuestionService, created_questions: list[FAQ]):
38+
questions = question_service.get_questions()
39+
assert len(questions) == len(created_questions)
40+
41+
for got, exp in zip(questions, created_questions):
42+
assert got.id == exp.id
43+
assert got.question == exp.question
44+
assert got.answer == exp.answer
45+
46+
47+
def test_delete_question(question_service: QuestionService, created_question: FAQ):
48+
question_id = created_question.id
49+
question_service.delete_question(question_id)
50+
with pytest.raises(QuestionNotFoundException):
51+
question_service.get_question_by_id(question_id)
52+
53+
54+
def test_delete_question_twice(
55+
question_service: QuestionService, created_question: FAQ
56+
):
57+
question_id = created_question.id
58+
question_service.delete_question(question_id)
59+
with pytest.raises(QuestionNotFoundException):
60+
question_service.delete_question(question_id)

wacruit/src/tests/sponsor/test_sponsor_service.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ def test_update_sponsor(
6464
sponsor = sponsor_service.create_sponsor(sponsor_create_dto)
6565
assert sponsor.id is not None
6666
updated_sponsor = sponsor_service.update_sponsor(sponsor.id, sponsor_update_dto)
67-
assert updated_sponsor.amount == sponsor_update_dto.amount
68-
assert updated_sponsor.email == sponsor_update_dto.email
69-
assert updated_sponsor.phone_number == sponsor_update_dto.phone_number
67+
if sponsor_update_dto.email is not None:
68+
assert updated_sponsor.email == sponsor_update_dto.email
69+
if sponsor_update_dto.amount is not None:
70+
assert updated_sponsor.amount == sponsor_update_dto.amount
71+
if sponsor_update_dto.phone_number is not None:
72+
assert updated_sponsor.phone_number == sponsor_update_dto.phone_number

0 commit comments

Comments
 (0)