From e56a00c228cba6c73c5e8e45056c5e2cd062d85e Mon Sep 17 00:00:00 2001 From: Dan Felder Date: Tue, 14 Nov 2023 23:56:41 -0500 Subject: [PATCH] Fixed existing unit test --- training/services/quiz.py | 4 ++-- training/tests/conftest.py | 4 ++-- training/tests/test_quiz_service.py | 12 ++++++++---- 3 files changed, 12 insertions(+), 8 deletions(-) diff --git a/training/services/quiz.py b/training/services/quiz.py index 73bb48a4..1f317408 100644 --- a/training/services/quiz.py +++ b/training/services/quiz.py @@ -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) @@ -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 diff --git a/training/tests/conftest.py b/training/tests/conftest.py index 9e963949..d7402c7d 100644 --- a/training/tests/conftest.py +++ b/training/tests/conftest.py @@ -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) diff --git a/training/tests/test_quiz_service.py b/training/tests/test_quiz_service.py index 925c5b88..3c473b99 100644 --- a/training/tests/test_quiz_service.py +++ b/training/tests/test_quiz_service.py @@ -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 @@ -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, @@ -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)