Skip to content

Commit

Permalink
[#2540] Add tests for new utils
Browse files Browse the repository at this point in the history
  • Loading branch information
pbanaszkiewicz committed Oct 28, 2023
1 parent aab583d commit 7cf3acf
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 1 deletion.
57 changes: 57 additions & 0 deletions amy/emails/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from emails.models import ScheduledEmail
from emails.signals import Signal
from emails.utils import (
combine_date_with_current_utc_time,
find_signal_by_name,
immediate_action,
messages_action_cancelled,
Expand All @@ -21,6 +22,8 @@
one_month_before,
person_from_request,
session_condition,
shift_date_and_apply_current_utc_time,
two_months_after,
)
from workshops.models import Person

Expand Down Expand Up @@ -51,6 +54,42 @@ def test_immediate_action(self) -> None:
self.assertTrue(immediate - timedelta(hours=1) - now < timedelta(seconds=1))


class TestCombineDateWithCurrentUtcTime(TestCase):
@patch("emails.utils.datetime", wraps=datetime)
def test_combine_date_with_current_utc_time(self, mock_datetime) -> None:
# Arrange
mock_datetime.now.return_value = datetime(
2020, 1, 31, 12, 1, 2, tzinfo=pytz.UTC
)
date_to_combine = date(1999, 12, 31)

# Act
calculated = combine_date_with_current_utc_time(date_to_combine)

# Assert
self.assertEqual(calculated.tzinfo, pytz.UTC)
self.assertEqual(calculated.date(), date(1999, 12, 31))
self.assertEqual(calculated.timetz(), time(12, 1, 2, tzinfo=pytz.UTC))


class TestShiftDateAndApplyCurrentUtcTime(TestCase):
@patch("emails.utils.datetime", wraps=datetime)
def test_shift_date_and_apply_current_utc_time(self, mock_datetime) -> None:
# Arrange
mocked_datetime = datetime(2020, 1, 31, 12, 0, 0, tzinfo=pytz.UTC)
mock_datetime.now.return_value = mocked_datetime
date_to_shift = date(1999, 12, 31)
offset = timedelta(days=1, hours=1, minutes=2, seconds=3)

# Act
shifted = shift_date_and_apply_current_utc_time(date_to_shift, offset)

# Assert
self.assertEqual(shifted.tzinfo, pytz.UTC)
self.assertEqual(shifted.date(), date(2000, 1, 1))
self.assertEqual(shifted.timetz(), time(12, 0, 0, tzinfo=pytz.UTC))


class TestOneMonthBefore(TestCase):
@patch("emails.utils.datetime", wraps=datetime)
def test_one_month_before(self, mock_datetime) -> None:
Expand All @@ -69,6 +108,24 @@ def test_one_month_before(self, mock_datetime) -> None:
self.assertEqual(calculated.timetz(), time(12, 0, 0, tzinfo=pytz.UTC))


class TestTwoMonthsAfter(TestCase):
@patch("emails.utils.datetime", wraps=datetime)
def test_two_months_after(self, mock_datetime) -> None:
# Arrange
mock_datetime.now.return_value = datetime(
2020, 1, 31, 12, 0, 0, tzinfo=pytz.UTC
)
start_date = date(2020, 1, 31)

# Act
calculated = two_months_after(start_date)

# Assert
self.assertEqual(calculated.tzinfo, pytz.UTC)
self.assertEqual(calculated.date(), date(2020, 3, 31))
self.assertEqual(calculated.timetz(), time(12, 0, 0, tzinfo=pytz.UTC))


class TestMessagesMissingRecipients(TestCase):
@patch("emails.utils.messages.warning")
def test_messages_missing_recipients(self, mock_messages_warning) -> None:
Expand Down
3 changes: 2 additions & 1 deletion amy/emails/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ def combine_date_with_current_utc_time(date: date) -> datetime:

def shift_date_and_apply_current_utc_time(date: date, offset: timedelta) -> datetime:
"""Return timezone-aware datetime object combining current time in UTC
with a given date shifted by offset (timedelta)."""
with a given date shifted by offset (timedelta).
Time component of the offset is discarded."""
date_shifted = date + offset
return combine_date_with_current_utc_time(date_shifted)

Expand Down

0 comments on commit 7cf3acf

Please sign in to comment.