Skip to content

Commit 853969c

Browse files
kulikjakulgensbrowniebroke
authored
Fix test with Django 5 when pytz is available (#9715)
* Fix test with Django 5 when pytz is available * fix formatting * remove original condition Co-authored-by: Ülgen Sarıkavak <[email protected]> * remove trailing whitespace * further improvements * let's not skip the pytz test - it should always be executed when testing against Django 4 * add comment to test requirements Co-authored-by: Bruno Alla <[email protected]> * simplify the pytz import as it should always be available * make isort happy --------- Co-authored-by: Ülgen Sarıkavak <[email protected]> Co-authored-by: Bruno Alla <[email protected]>
1 parent 2ae8c11 commit 853969c

File tree

2 files changed

+14
-15
lines changed

2 files changed

+14
-15
lines changed

requirements/requirements-testing.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ pytest-django>=4.5.2,<5.0
55
importlib-metadata<5.0
66
# temporary pin of attrs
77
attrs==22.1.0
8+
pytz # Remove when dropping support for Django<5.0

tests/test_fields.py

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,9 @@
99
from unittest.mock import patch
1010
from zoneinfo import ZoneInfo
1111

12+
import django
1213
import pytest
13-
14-
try:
15-
import pytz
16-
except ImportError:
17-
pytz = None
18-
14+
import pytz
1915
from django.core.exceptions import ValidationError as DjangoValidationError
2016
from django.db.models import IntegerChoices, TextChoices
2117
from django.http import QueryDict
@@ -1624,7 +1620,10 @@ def test_should_render_date_time_in_default_timezone(self):
16241620
assert rendered_date == rendered_date_in_timezone
16251621

16261622

1627-
@pytest.mark.skipif(pytz is None, reason="Django 5.0 has removed pytz; this test should eventually be able to get removed.")
1623+
@pytest.mark.skipif(
1624+
condition=django.VERSION >= (5,),
1625+
reason="Django 5.0 has removed pytz; this test should eventually be able to get removed.",
1626+
)
16281627
class TestPytzNaiveDayLightSavingTimeTimeZoneDateTimeField(FieldValues):
16291628
"""
16301629
Invalid values for `DateTimeField` with datetime in DST shift (non-existing or ambiguous) and timezone with DST.
@@ -1638,16 +1637,15 @@ class TestPytzNaiveDayLightSavingTimeTimeZoneDateTimeField(FieldValues):
16381637
}
16391638
outputs = {}
16401639

1641-
if pytz:
1642-
class MockTimezone(pytz.BaseTzInfo):
1643-
@staticmethod
1644-
def localize(value, is_dst):
1645-
raise pytz.InvalidTimeError()
1640+
class MockTimezone(pytz.BaseTzInfo):
1641+
@staticmethod
1642+
def localize(value, is_dst):
1643+
raise pytz.InvalidTimeError()
16461644

1647-
def __str__(self):
1648-
return 'America/New_York'
1645+
def __str__(self):
1646+
return 'America/New_York'
16491647

1650-
field = serializers.DateTimeField(default_timezone=MockTimezone())
1648+
field = serializers.DateTimeField(default_timezone=MockTimezone())
16511649

16521650

16531651
@patch('rest_framework.utils.timezone.datetime_ambiguous', return_value=True)

0 commit comments

Comments
 (0)