Skip to content

Commit

Permalink
chore: Added proper tests to model
Browse files Browse the repository at this point in the history
  • Loading branch information
rijuma committed Sep 3, 2024
1 parent 180fd69 commit 4b1ee40
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 13 deletions.
13 changes: 8 additions & 5 deletions edx_name_affirmation/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()


Expand Down Expand Up @@ -47,14 +52,12 @@ class Meta:

@property
def verification_attempt_status(self):
if not self.verification_attempt_id:
return None

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
Expand Down
21 changes: 13 additions & 8 deletions edx_name_affirmation/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,13 @@

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()


class VerifiedNameModelTests(TestCase):
"""
Test suite for the VerifiedName models
Expand All @@ -30,15 +25,25 @@ 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'

obj = lambda dict: type('obj', (object,), dict)

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
Expand Down

0 comments on commit 4b1ee40

Please sign in to comment.