Skip to content

Commit

Permalink
Merge pull request #4537 from ttys0dev/fix-template-date-tests
Browse files Browse the repository at this point in the history
Convert to naive timezone before formatting with template_date
  • Loading branch information
albertisfu authored Oct 10, 2024
2 parents 076e784 + 5ca6c31 commit 3ba3e9e
Showing 1 changed file with 14 additions and 21 deletions.
35 changes: 14 additions & 21 deletions cl/favorites/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@

import time_machine
from asgiref.sync import sync_to_async
from django.conf import settings
from django.contrib.auth.hashers import make_password
from django.core import mail
from django.template.defaultfilters import date as template_date
from django.test import AsyncClient, override_settings
from django.urls import reverse
from django.utils import timezone
from django.utils.timezone import make_naive, now
from selenium.webdriver.common.by import By
from timeout_decorator import timeout_decorator

Expand Down Expand Up @@ -676,7 +675,7 @@ def setUpTestData(cls) -> None:
async def test_prayer_eligible(self) -> None:
"""Does the prayer_eligible method works properly?"""

current_time = timezone.now()
current_time = now()
with time_machine.travel(current_time, tick=False):
# No user prayers in the last 24 hours yet for this user.
user_is_eligible = await prayer_eligible(self.user)
Expand Down Expand Up @@ -732,7 +731,7 @@ async def test_get_top_prayers_by_number(self) -> None:
"""Does the get_top_prayers method works properly?"""

# Test top documents based on prayers count.
current_time = timezone.now()
current_time = now()
with time_machine.travel(current_time, tick=False):
await create_prayer(self.user, self.rd_2)
await create_prayer(self.user_2, self.rd_2)
Expand Down Expand Up @@ -760,7 +759,7 @@ async def test_get_top_prayers_by_age(self) -> None:
"""Does the get_top_prayers method works properly?"""

# Test top documents based on prayer age.
current_time = timezone.now()
current_time = now()
with time_machine.travel(
current_time - timedelta(minutes=1), tick=False
):
Expand Down Expand Up @@ -791,7 +790,7 @@ async def test_get_top_prayers_by_number_and_age(self) -> None:
"""Does the get_top_prayers method works properly?"""

# Create prayers with different counts and ages
current_time = timezone.now()
current_time = now()
with time_machine.travel(current_time - timedelta(days=5), tick=False):
await create_prayer(self.user, self.rd_5) # 1 prayer, 5 days old

Expand Down Expand Up @@ -861,7 +860,7 @@ async def test_prayers_integration(self) -> None:
description="Dismissing Case",
)

current_time = timezone.now()
current_time = now()
with time_machine.travel(current_time, tick=False):
# Create prayers
prayer_1 = await create_prayer(self.user, rd_6)
Expand Down Expand Up @@ -925,13 +924,10 @@ async def test_prayers_integration(self) -> None:
f"https://www.courtlistener.com{rd_6.get_absolute_url()}",
email_text_content,
)
with timezone.override(settings.TIME_ZONE):
localized_date = timezone.localtime(prayer_1.date_created)
formatted_date = template_date(localized_date, "M j, Y")
self.assertIn(
f"You requested it on {formatted_date}",
email_text_content,
)
self.assertIn(
f"You requested it on {template_date(make_naive(prayer_1.date_created), 'M j, Y')}",
email_text_content,
)
self.assertIn(
f"{len(actual_top_prayers)} people were also waiting for it.",
email_text_content,
Expand All @@ -949,13 +945,10 @@ async def test_prayers_integration(self) -> None:
f"{len(actual_top_prayers)} people were also waiting for it.",
html_content,
)
with timezone.override(settings.TIME_ZONE):
localized_date = timezone.localtime(prayer_1.date_created)
formatted_date = template_date(localized_date, "M j, Y")
self.assertIn(
f"You requested it on {formatted_date}",
html_content,
)
self.assertIn(
f"You requested it on {template_date(make_naive(prayer_1.date_created), 'M j, Y')}",
html_content,
)
self.assertIn(
f"Somebody paid ${price(rd_6)}",
html_content,
Expand Down

0 comments on commit 3ba3e9e

Please sign in to comment.