Skip to content

Commit

Permalink
fix: Updated tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rijuma committed Sep 4, 2024
1 parent 84c4d10 commit a6675f3
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 10 deletions.
4 changes: 3 additions & 1 deletion edx_name_affirmation/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from simple_history.models import HistoricalRecords

from django.contrib.auth import get_user_model
from django.core.exceptions import ObjectDoesNotExist
from django.db import models

from edx_name_affirmation.statuses import VerifiedNameStatus
Expand Down Expand Up @@ -61,9 +62,10 @@ def verification_attempt_status(self):
verification = SoftwareSecurePhotoVerification.objects.get(id=self.verification_attempt_id)
return verification.status if verification else None

except SoftwareSecurePhotoVerification.DoesNotExist:
except ObjectDoesNotExist:
return None


class VerifiedNameConfig(ConfigurationModel):
"""
This model provides various configuration fields for users regarding their
Expand Down
30 changes: 21 additions & 9 deletions edx_name_affirmation/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,38 @@
from unittest.mock import patch

from django.contrib.auth import get_user_model
from django.core.exceptions import ObjectDoesNotExist
from django.test import TestCase

from edx_name_affirmation.models import VerifiedName
from edx_name_affirmation.statuses import VerifiedNameStatus

User = get_user_model()

idv_attempt_id = 34455
idv_attempt_status = 'submitted'

idv_attempt_id_notfound = 404
idv_attempt_id_notfound_status = None


def _obj(dictionary):
"Helper method to turn a dict into an object. Used to mock below."

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


def _mocked_model_get(idv_id):
"Helper method to mock the behavior of SoftwareSecurePhotoVerification model. Used to mock below."
if idv_id == idv_attempt_id_notfound:
raise ObjectDoesNotExist

if idv_id == idv_attempt_id:
return _obj({'status': idv_attempt_status})

return _obj({'status': None})


class VerifiedNameModelTests(TestCase):
"""
Test suite for the VerifiedName models
Expand All @@ -36,7 +54,6 @@ def test_histories(self):
"""
Test the model history is recording records as expected
"""
idv_attempt_id = 34455

verified_name_history = self.verified_name.history.all().order_by('history_date')
assert len(verified_name_history) == 1
Expand All @@ -59,15 +76,10 @@ def test_verification_status(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})
sspv_mock.objects.get = _mocked_model_get

self.verified_name.verification_attempt_id = 12345
assert self.verified_name.verification_attempt_status is None
self.verified_name.verification_attempt_id = idv_attempt_id_notfound
assert self.verified_name.verification_attempt_status is idv_attempt_id_notfound_status

self.verified_name.verification_attempt_id = idv_attempt_id
assert self.verified_name.verification_attempt_status is idv_attempt_status

0 comments on commit a6675f3

Please sign in to comment.