-
-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#2540] Add tests for common functions used in the receivers
- Loading branch information
1 parent
1fe9179
commit a1ed19a
Showing
1 changed file
with
132 additions
and
0 deletions.
There are no files selected for viewing
132 changes: 132 additions & 0 deletions
132
amy/emails/tests/actions/test_instructor_training_completed_not_badged_common_functions.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
from datetime import UTC, datetime | ||
from unittest.mock import MagicMock, patch | ||
|
||
from django.test import RequestFactory, TestCase | ||
|
||
from emails.actions.instructor_training_completed_not_badged import ( | ||
get_context, | ||
get_generic_relation_object, | ||
get_recipients, | ||
get_scheduled_at, | ||
) | ||
from emails.types import InstructorTrainingCompletedNotBadgedContext | ||
from workshops.models import Person, TrainingProgress, TrainingRequirement | ||
|
||
|
||
class TestInstructorTrainingCompletedNotBadgedCommonFunctions(TestCase): | ||
def setUpTrainingProgresses(self, person: Person) -> None: | ||
requirements = TrainingRequirement.objects.all() # should be 4 of them | ||
self.progresses = [ | ||
TrainingProgress(state="p", trainee=person, requirement=requirements[0]), | ||
TrainingProgress(state="f", trainee=person, requirement=requirements[1]), | ||
TrainingProgress(state="f", trainee=person, requirement=requirements[2]), | ||
TrainingProgress(state="p", trainee=person, requirement=requirements[3]), | ||
] | ||
TrainingProgress.objects.bulk_create(self.progresses) | ||
|
||
def setUpContext( | ||
self, person: Person, training_completed_date: datetime | ||
) -> InstructorTrainingCompletedNotBadgedContext: | ||
return { | ||
"person": person, | ||
"passed_requirements": [self.progresses[0], self.progresses[3]], | ||
"missing_requirements": [self.progresses[1], self.progresses[2]], | ||
"training_completed_date": training_completed_date, | ||
} | ||
|
||
@patch("emails.utils.datetime", wraps=datetime) | ||
def test_get_scheduled_at(self, mock_datetime: MagicMock) -> None: | ||
# Arrange | ||
request = RequestFactory().get("/") | ||
person = Person() | ||
training_completed_date = datetime(2023, 1, 1) | ||
mock_datetime.now.return_value = datetime(2023, 1, 1, 12, 0, 0, tzinfo=UTC) | ||
|
||
# Act | ||
scheduled_at = get_scheduled_at( | ||
request=request, | ||
person=person, | ||
training_completed_date=training_completed_date, | ||
) | ||
|
||
# Assert | ||
self.assertEqual(scheduled_at, datetime(2023, 3, 2, 12, 0, 0, tzinfo=UTC)) | ||
|
||
def test_get_context(self) -> None: | ||
# Arrange | ||
request = RequestFactory().get("/") | ||
person = Person.objects.create(username="test") | ||
training_completed_date = datetime.now() | ||
self.setUpTrainingProgresses(person) | ||
|
||
# Act | ||
context = get_context( | ||
request=request, | ||
person=person, | ||
training_completed_date=training_completed_date, | ||
) | ||
|
||
# Assert | ||
self.assertEqual( | ||
context, | ||
{ | ||
"person": person, | ||
"passed_requirements": [self.progresses[0], self.progresses[3]], | ||
"missing_requirements": [self.progresses[1], self.progresses[2]], | ||
"training_completed_date": training_completed_date, | ||
}, | ||
) | ||
|
||
def test_get_generic_relation_object(self) -> None: | ||
# Arrange | ||
request = RequestFactory().get("/") | ||
person = Person.objects.create(username="test") | ||
training_completed_date = datetime.now() | ||
self.setUpTrainingProgresses(person) | ||
|
||
# Act | ||
obj = get_generic_relation_object( | ||
context=self.setUpContext(person, training_completed_date), | ||
request=request, | ||
person=person, | ||
training_completed_date=training_completed_date, | ||
) | ||
|
||
# Assert | ||
self.assertEqual(obj, person) | ||
|
||
def test_get_recipients(self) -> None: | ||
# Arrange | ||
request = RequestFactory().get("/") | ||
person = Person.objects.create(username="test", email="[email protected]") | ||
training_completed_date = datetime.now() | ||
self.setUpTrainingProgresses(person) | ||
|
||
# Act | ||
obj = get_recipients( | ||
context=self.setUpContext(person, training_completed_date), | ||
request=request, | ||
person=person, | ||
training_completed_date=training_completed_date, | ||
) | ||
|
||
# Assert | ||
self.assertEqual(obj, [person.email]) | ||
|
||
def test_get_recipients__no_email(self) -> None: | ||
# Arrange | ||
request = RequestFactory().get("/") | ||
person = Person.objects.create(username="test") # no email for this person | ||
training_completed_date = datetime.now() | ||
self.setUpTrainingProgresses(person) | ||
|
||
# Act | ||
obj = get_recipients( | ||
context=self.setUpContext(person, training_completed_date), | ||
request=request, | ||
person=person, | ||
training_completed_date=training_completed_date, | ||
) | ||
|
||
# Assert | ||
self.assertEqual(obj, []) |