digest tests: Improve Tuesday tests.

If we're mocking time, we should do it consistently.
This commit is contained in:
Steve Howell 2020-11-12 13:42:46 +00:00 committed by Tim Abbott
parent 2f4d7a6171
commit 0624833af6
1 changed files with 22 additions and 20 deletions

View File

@ -345,30 +345,32 @@ class TestDigestEmailMessages(ZulipTestCase):
self.assertEqual(queue_mock.call_count, len(users)) self.assertEqual(queue_mock.call_count, len(users))
@mock.patch('zerver.lib.digest.queue_digest_recipient') def tuesday(self) -> datetime.datetime:
@mock.patch('zerver.lib.digest.timezone_now') return datetime.datetime(year=2016, month=1, day=5, tzinfo=datetime.timezone.utc)
def test_disabled(self, mock_django_timezone: mock.MagicMock,
mock_queue_digest_recipient: mock.MagicMock) -> None: @override_settings(SEND_DIGEST_EMAILS=False)
RealmAuditLog.objects.all().delete() def test_disabled(self) -> None:
cutoff = timezone_now() RealmAuditLog.objects.all().delete()
# A Tuesday
mock_django_timezone.return_value = datetime.datetime(year=2016, month=1, day=5, tzinfo=datetime.timezone.utc) tuesday = self.tuesday()
enqueue_emails(cutoff) cutoff = tuesday - datetime.timedelta(days=5)
mock_queue_digest_recipient.assert_not_called()
with mock.patch("zerver.lib.digest.timezone_now", return_value=tuesday):
with mock.patch("zerver.lib.digest.queue_digest_recipient") as queue_mock:
enqueue_emails(cutoff)
queue_mock.assert_not_called()
@mock.patch('zerver.lib.digest.queue_digest_recipient')
@mock.patch('zerver.lib.digest.timezone_now')
@override_settings(SEND_DIGEST_EMAILS=True) @override_settings(SEND_DIGEST_EMAILS=True)
def test_only_enqueue_on_valid_day(self, mock_django_timezone: mock.MagicMock, def test_only_enqueue_on_valid_day(self) -> None:
mock_queue_digest_recipient: mock.MagicMock) -> None:
RealmAuditLog.objects.all().delete() RealmAuditLog.objects.all().delete()
# Not a Tuesday
mock_django_timezone.return_value = datetime.datetime(year=2016, month=1, day=6, tzinfo=datetime.timezone.utc)
# Check that digests are not sent on days other than Tuesday. not_tuesday = datetime.datetime(year=2016, month=1, day=6, tzinfo=datetime.timezone.utc)
cutoff = timezone_now() cutoff = not_tuesday - datetime.timedelta(days=5)
with mock.patch("zerver.lib.digest.timezone_now", return_value=not_tuesday):
with mock.patch("zerver.lib.digest.queue_digest_recipient") as queue_mock:
enqueue_emails(cutoff) enqueue_emails(cutoff)
self.assertEqual(mock_queue_digest_recipient.call_count, 0) queue_mock.assert_not_called()
@mock.patch('zerver.lib.digest.queue_digest_recipient') @mock.patch('zerver.lib.digest.queue_digest_recipient')
@mock.patch('zerver.lib.digest.timezone_now') @mock.patch('zerver.lib.digest.timezone_now')