|
| 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) |
0 commit comments