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 9bd4e7d
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 13 deletions.
14 changes: 9 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,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
Expand Down
26 changes: 18 additions & 8 deletions edx_name_affirmation/tests/test_models.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
"""
Tests for Name Affirmation models
"""

from unittest.mock import patch
from django.contrib.auth import get_user_model
from django.test import TestCase

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(dictionary):
"Helper method to turn a dict into an object. Used to mock below."

return type('obj', (object,), dictionary)


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

0 comments on commit 9bd4e7d

Please sign in to comment.