From 95354e543d656da9ac9ab565bf0fc4c12e970570 Mon Sep 17 00:00:00 2001 From: Marcos Date: Mon, 9 Sep 2024 15:07:18 -0300 Subject: [PATCH] chore: Moved test variables to be more localized --- edx_name_affirmation/models.py | 2 +- edx_name_affirmation/tests/test_models.py | 50 ++++++++++++----------- 2 files changed, 27 insertions(+), 25 deletions(-) diff --git a/edx_name_affirmation/models.py b/edx_name_affirmation/models.py index bd61db7..08e109b 100644 --- a/edx_name_affirmation/models.py +++ b/edx_name_affirmation/models.py @@ -60,7 +60,7 @@ def verification_attempt_status(self): try: verification = SoftwareSecurePhotoVerification.objects.get(id=self.verification_attempt_id) - return verification.status if verification else None + return verification.status except ObjectDoesNotExist: return None diff --git a/edx_name_affirmation/tests/test_models.py b/edx_name_affirmation/tests/test_models.py index 5a030f1..471d80c 100644 --- a/edx_name_affirmation/tests/test_models.py +++ b/edx_name_affirmation/tests/test_models.py @@ -13,27 +13,6 @@ 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(id): # pylint: disable=redefined-builtin - "Helper method to mock the behavior of SoftwareSecurePhotoVerification model. Used to mock below." - if id == idv_attempt_id_notfound: - raise ObjectDoesNotExist - - if id == idv_attempt_id: - return _obj({'status': idv_attempt_status}) - - return _obj({'status': None}) class VerifiedNameModelTests(TestCase): @@ -41,6 +20,9 @@ class VerifiedNameModelTests(TestCase): Test suite for the VerifiedName models """ def setUp(self): + self.idv_attempt_id = 34455 + self.idv_attempt_status = 'submitted' + self.idv_attempt_id_notfound = 404 self.verified_name = 'Test Tester' self.user = User.objects.create(username='modelTester', email='model@tester.com') self.verified_name = VerifiedName.objects.create( @@ -76,10 +58,30 @@ def test_verification_status(self, sspv_mock): """ Test the model history is recording records as expected """ - sspv_mock.objects.get = _mocked_model_get - self.verified_name.verification_attempt_id = idv_attempt_id_notfound + idv_attempt_id_notfound_status = None + + sspv_mock.objects.get = self._mocked_model_get + + self.verified_name.verification_attempt_id = self.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 + assert self.verified_name.verification_attempt_status is self.idv_attempt_status + + # Helper methods + + def _obj(self, dictionary): + "Helper method to turn a dict into an object. Used to mock below." + + return type('obj', (object,), dictionary) + + def _mocked_model_get(self, id): # pylint: disable=redefined-builtin + "Helper method to mock the behavior of SoftwareSecurePhotoVerification model. Used to mock below." + if id == self.idv_attempt_id_notfound: + raise ObjectDoesNotExist + + if id == idv_attempt_id: + return self._obj({'status': self.idv_attempt_status}) + + return self._obj({'status': None})