Skip to content

Commit

Permalink
Fixed existing unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
felder101 committed Nov 15, 2023
1 parent fa5ee6b commit e56a00c
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 8 deletions.
4 changes: 2 additions & 2 deletions training/services/quiz.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ def grade(self, quiz_id: int, user_id: int, submission: QuizSubmission) -> QuizG
db_user_certificate.agency,
db_user_certificate.completion_date
)
self.emailCertificate(user.name, quiz.name, user.email, pdf_bytes)
self.email_certificate(user.name, quiz.name, user.email, pdf_bytes)
logging.info(f"Sent confirmation email to {user.email} for passing training quiz")
except Exception as e:
logging.error("Error sending quiz confirmation mail", e)
Expand All @@ -129,7 +129,7 @@ def grade(self, quiz_id: int, user_id: int, submission: QuizSubmission) -> QuizG

return grade

def emailCertificate(self, user_name: str, course_name: str, to_email: str, certificate: bytes) -> None:
def email_certificate(self, user_name: str, course_name: str, to_email: str, certificate: bytes) -> None:
"""
Sends congratulatory email to user with certificate attached.
:param user_name: User's Name
Expand Down
4 changes: 2 additions & 2 deletions training/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -346,8 +346,8 @@ def valid_user_certificate() -> Generator[schemas.UserCertificate, None, None]:
'user_id': 2,
'user_name': "Molly",
'agency': 'Freeman Journal',
'quiz_id': 100,
'quiz_name': "Dublin History",
'quiz_id': 123,
'quiz_name': "Travel Training for Agency/Organization Program Coordinators",
'completion_date': '2023-08-21T22:59:36'
}
yield UserCertificate.model_validate(testdata)
12 changes: 8 additions & 4 deletions training/tests/test_quiz_service.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
from datetime import datetime

import pytest
from unittest.mock import MagicMock, patch
from training import models, schemas
from training.errors import IncompleteQuizResponseError
from training.services import QuizService
from training.services import QuizService, Certificate
from training.repositories import QuizRepository, QuizCompletionRepository, CertificateRepository
from sqlalchemy.orm import Session
from .factories import QuizCompletionFactory
Expand All @@ -13,10 +11,14 @@
@patch.object(QuizRepository, "find_by_id")
@patch.object(QuizCompletionRepository, "create")
@patch.object(CertificateRepository, "get_certificate_by_id")
@patch.object(Certificate, "generate_pdf")
@patch.object(QuizService, "email_certificate")
def test_grade_passing(
mock_quiz_service_email_certificate: MagicMock,
mock_certificate_service_generate_pdf: MagicMock,
mock_certificate_repo_get_certificate_by_id: MagicMock,
mock_quiz_completion_repo_create: MagicMock,
mock_quiz_repo_find_by_id: MagicMock,
mock_certificate_repo_get_certificate_by_id: MagicMock,
db_with_data: Session,
valid_passing_submission: schemas.QuizSubmission,
valid_quiz: models.Quiz,
Expand All @@ -26,6 +28,8 @@ def test_grade_passing(
mock_quiz_repo_find_by_id.return_value = valid_quiz
mock_quiz_completion_repo_create.return_value = QuizCompletionFactory.build()
mock_certificate_repo_get_certificate_by_id.return_value = valid_user_certificate
mock_certificate_service_generate_pdf.return_value = bytearray()
mock_quiz_service_email_certificate.return_value = None

result = quiz_service.grade(quiz_id=123, user_id=123, submission=valid_passing_submission)

Expand Down

0 comments on commit e56a00c

Please sign in to comment.