From 470dddb7759a9a236ef8be943932fcbbf6febef1 Mon Sep 17 00:00:00 2001 From: Marcos Date: Tue, 3 Sep 2024 12:29:53 -0300 Subject: [PATCH] chore: Added proper tests to model --- edx_name_affirmation/models.py | 14 +++++++++----- edx_name_affirmation/tests/test_models.py | 22 +++++++++++++++------- 2 files changed, 24 insertions(+), 12 deletions(-) diff --git a/edx_name_affirmation/models.py b/edx_name_affirmation/models.py index 0dc2056..f0e0728 100644 --- a/edx_name_affirmation/models.py +++ b/edx_name_affirmation/models.py @@ -11,6 +11,11 @@ from edx_name_affirmation.statuses import VerifiedNameStatus +try: + from lms.djangoapps.verify_student.models import SoftwareSecurePhotoVerification +except ImportError: + SoftwareSecurePhotoVerification = None + User = get_user_model() @@ -47,14 +52,13 @@ class Meta: @property def verification_attempt_status(self): - if not self.verification_attempt_id: - return None + "Returns the status associated with its SoftwareSecurePhotoVerification with verification_attempt_id if any." - try: - from lms.djangoapps.verify_student.models import SoftwareSecurePhotoVerification - except ImportError: + if not self.verification_attempt_id or not SoftwareSecurePhotoVerification: return None + # breakpoint() + verification = SoftwareSecurePhotoVerification.objects.get(id=self.verification_attempt_id) return verification.status if verification else None diff --git a/edx_name_affirmation/tests/test_models.py b/edx_name_affirmation/tests/test_models.py index 043c1bf..5b58132 100644 --- a/edx_name_affirmation/tests/test_models.py +++ b/edx_name_affirmation/tests/test_models.py @@ -4,17 +4,17 @@ from django.contrib.auth import get_user_model from django.test import TestCase +from unittest.mock import patch, MagicMock from edx_name_affirmation.models import VerifiedName from edx_name_affirmation.statuses import VerifiedNameStatus -try: - from lms.djangoapps.verify_student.models import SoftwareSecurePhotoVerification -except ImportError: - pass - User = get_user_model() +def _obj(dict): + "Helper method to turn a dict into an object. Used to mock below." + + return type('obj', (object,), dict) class VerifiedNameModelTests(TestCase): """ @@ -30,15 +30,23 @@ def setUp(self): ) return super().setUp() - def test_histories(self): + @patch('edx_name_affirmation.models.SoftwareSecurePhotoVerification') + def test_histories(self, sspv_mock): """ Test the model history is recording records as expected """ + idv_attempt_id = 34455 + idv_attempt_status = 'submitted' + + sspv_mock.objects.get = lambda id : _obj({ 'status': idv_attempt_status }) if id == idv_attempt_id else _obj({ 'status': None }) + verified_name_history = self.verified_name.history.all().order_by('history_date') assert len(verified_name_history) == 1 - idv_attempt_id = 34455 self.verified_name.status = VerifiedNameStatus.APPROVED self.verified_name.verification_attempt_id = idv_attempt_id + + + assert self.verified_name.verification_attempt_status is idv_attempt_status self.verified_name.save() verified_name_history = self.verified_name.history.all().order_by('history_date') assert len(verified_name_history) == 2