diff options
author | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2023-01-09 09:03:38 +0100 |
---|---|---|
committer | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2023-01-17 11:49:15 +0100 |
commit | e6f82438d4e3750e8d299bfd79dac98eebe9f1e0 (patch) | |
tree | 4ee0cbf2c0be9822416aa3d65105f35a9784fd94 /tests/timezones | |
parent | 8d98f99a4ab5de6f2c730399f53eba8bf6bea470 (diff) | |
download | django-e6f82438d4e3750e8d299bfd79dac98eebe9f1e0.tar.gz |
Refs #32365 -- Removed support for pytz timezones per deprecation timeline.
Diffstat (limited to 'tests/timezones')
-rw-r--r-- | tests/timezones/tests.py | 200 |
1 files changed, 69 insertions, 131 deletions
diff --git a/tests/timezones/tests.py b/tests/timezones/tests.py index f5c8d17a3c..89e87172f1 100644 --- a/tests/timezones/tests.py +++ b/tests/timezones/tests.py @@ -10,11 +10,6 @@ try: except ImportError: from backports import zoneinfo -try: - import pytz -except ImportError: - pytz = None - from django.contrib.auth.models import User from django.core import serializers from django.db import connection @@ -31,7 +26,6 @@ from django.test import ( SimpleTestCase, TestCase, TransactionTestCase, - ignore_warnings, override_settings, skipIfDBFeature, skipUnlessDBFeature, @@ -79,14 +73,6 @@ UTC = datetime.timezone.utc EAT = timezone.get_fixed_timezone(180) # Africa/Nairobi ICT = timezone.get_fixed_timezone(420) # Asia/Bangkok -ZONE_CONSTRUCTORS = (zoneinfo.ZoneInfo,) -if pytz is not None: - ZONE_CONSTRUCTORS += (pytz.timezone,) - - -def get_timezones(key): - return [constructor(key) for constructor in ZONE_CONSTRUCTORS] - class UTCAliasTests(SimpleTestCase): def test_alias_deprecation_warning(self): @@ -413,39 +399,17 @@ class NewDatabaseTests(TestCase): self.assertEqual(Event.objects.filter(dt__gte=dt2).count(), 1) self.assertEqual(Event.objects.filter(dt__gt=dt2).count(), 0) - def test_query_filter_with_pytz_timezones(self): - for tz in get_timezones("Europe/Paris"): - with self.subTest(repr(tz)): - dt = datetime.datetime(2011, 9, 1, 12, 20, 30, tzinfo=tz) - Event.objects.create(dt=dt) - next = dt + datetime.timedelta(seconds=3) - prev = dt - datetime.timedelta(seconds=3) - self.assertEqual(Event.objects.filter(dt__exact=dt).count(), 1) - self.assertEqual(Event.objects.filter(dt__exact=next).count(), 0) - self.assertEqual(Event.objects.filter(dt__in=(prev, next)).count(), 0) - self.assertEqual( - Event.objects.filter(dt__in=(prev, dt, next)).count(), 1 - ) - self.assertEqual( - Event.objects.filter(dt__range=(prev, next)).count(), 1 - ) - - @ignore_warnings(category=RemovedInDjango50Warning) - def test_connection_timezone(self): - tests = [ - (False, None, datetime.timezone), - (False, "Africa/Nairobi", zoneinfo.ZoneInfo), - ] - if pytz is not None: - tests += [ - (True, None, datetime.timezone), - (True, "Africa/Nairobi", pytz.BaseTzInfo), - ] - for use_pytz, connection_tz, expected_type in tests: - with self.subTest(use_pytz=use_pytz, connection_tz=connection_tz): - with self.settings(USE_DEPRECATED_PYTZ=use_pytz): - with override_database_connection_timezone(connection_tz): - self.assertIsInstance(connection.timezone, expected_type) + def test_query_filter_with_timezones(self): + tz = zoneinfo.ZoneInfo("Europe/Paris") + dt = datetime.datetime(2011, 9, 1, 12, 20, 30, tzinfo=tz) + Event.objects.create(dt=dt) + next = dt + datetime.timedelta(seconds=3) + prev = dt - datetime.timedelta(seconds=3) + self.assertEqual(Event.objects.filter(dt__exact=dt).count(), 1) + self.assertEqual(Event.objects.filter(dt__exact=next).count(), 0) + self.assertEqual(Event.objects.filter(dt__in=(prev, next)).count(), 0) + self.assertEqual(Event.objects.filter(dt__in=(prev, dt, next)).count(), 1) + self.assertEqual(Event.objects.filter(dt__range=(prev, next)).count(), 1) def test_query_convert_timezones(self): # Connection timezone is equal to the current timezone, datetime @@ -1075,16 +1039,15 @@ class TemplateTests(SimpleTestCase): ) # Use an IANA timezone as argument - for tz in get_timezones("Europe/Paris"): - with self.subTest(repr(tz)): - tpl = Template("{% load tz %}{{ dt|timezone:tz }}") - ctx = Context( - { - "dt": datetime.datetime(2011, 9, 1, 13, 20, 30), - "tz": tz, - } - ) - self.assertEqual(tpl.render(ctx), "2011-09-01T12:20:30+02:00") + tz = zoneinfo.ZoneInfo("Europe/Paris") + tpl = Template("{% load tz %}{{ dt|timezone:tz }}") + ctx = Context( + { + "dt": datetime.datetime(2011, 9, 1, 13, 20, 30), + "tz": tz, + } + ) + self.assertEqual(tpl.render(ctx), "2011-09-01T12:20:30+02:00") def test_localtime_templatetag_invalid_argument(self): with self.assertRaises(TemplateSyntaxError): @@ -1147,15 +1110,14 @@ class TemplateTests(SimpleTestCase): tpl = Template("{% load tz %}{% timezone tz %}{{ dt }}{% endtimezone %}") # Use a IANA timezone as argument - for tz in get_timezones("Europe/Paris"): - with self.subTest(repr(tz)): - ctx = Context( - { - "dt": datetime.datetime(2011, 9, 1, 13, 20, 30, tzinfo=EAT), - "tz": tz, - } - ) - self.assertEqual(tpl.render(ctx), "2011-09-01T12:20:30+02:00") + tz = zoneinfo.ZoneInfo("Europe/Paris") + ctx = Context( + { + "dt": datetime.datetime(2011, 9, 1, 13, 20, 30, tzinfo=EAT), + "tz": tz, + } + ) + self.assertEqual(tpl.render(ctx), "2011-09-01T12:20:30+02:00") # Use a IANA timezone name as argument ctx = Context( @@ -1166,22 +1128,6 @@ class TemplateTests(SimpleTestCase): ) self.assertEqual(tpl.render(ctx), "2011-09-01T12:20:30+02:00") - @ignore_warnings(category=RemovedInDjango50Warning) - def test_timezone_templatetag_invalid_argument(self): - with self.assertRaises(TemplateSyntaxError): - Template("{% load tz %}{% timezone %}{% endtimezone %}").render() - with self.assertRaises(zoneinfo.ZoneInfoNotFoundError): - Template("{% load tz %}{% timezone tz %}{% endtimezone %}").render( - Context({"tz": "foobar"}) - ) - if pytz is not None: - with override_settings(USE_DEPRECATED_PYTZ=True), self.assertRaises( - pytz.UnknownTimeZoneError - ): - Template("{% load tz %}{% timezone tz %}{% endtimezone %}").render( - Context({"tz": "foobar"}) - ) - @skipIf(sys.platform == "win32", "Windows uses non-standard time zone names") def test_get_current_timezone_templatetag(self): """ @@ -1205,16 +1151,12 @@ class TemplateTests(SimpleTestCase): self.assertEqual(tpl.render(Context({"tz": ICT})), "+0700") def test_get_current_timezone_templatetag_with_iana(self): - """ - Test the {% get_current_timezone %} templatetag with pytz. - """ tpl = Template( "{% load tz %}{% get_current_timezone as time_zone %}{{ time_zone }}" ) - for tz in get_timezones("Europe/Paris"): - with self.subTest(repr(tz)): - with timezone.override(tz): - self.assertEqual(tpl.render(Context()), "Europe/Paris") + tz = zoneinfo.ZoneInfo("Europe/Paris") + with timezone.override(tz): + self.assertEqual(tpl.render(Context()), "Europe/Paris") tpl = Template( "{% load tz %}{% timezone 'Europe/Paris' %}" @@ -1282,27 +1224,25 @@ class LegacyFormsTests(TestCase): def test_form_with_non_existent_time(self): form = EventForm({"dt": "2011-03-27 02:30:00"}) - for tz in get_timezones("Europe/Paris"): - with self.subTest(repr(tz)): - with timezone.override(tz): - # This is a bug. - self.assertTrue(form.is_valid()) - self.assertEqual( - form.cleaned_data["dt"], - datetime.datetime(2011, 3, 27, 2, 30, 0), - ) + tz = zoneinfo.ZoneInfo("Europe/Paris") + with timezone.override(tz): + # This is a bug. + self.assertTrue(form.is_valid()) + self.assertEqual( + form.cleaned_data["dt"], + datetime.datetime(2011, 3, 27, 2, 30, 0), + ) def test_form_with_ambiguous_time(self): form = EventForm({"dt": "2011-10-30 02:30:00"}) - for tz in get_timezones("Europe/Paris"): - with self.subTest(repr(tz)): - with timezone.override(tz): - # This is a bug. - self.assertTrue(form.is_valid()) - self.assertEqual( - form.cleaned_data["dt"], - datetime.datetime(2011, 10, 30, 2, 30, 0), - ) + tz = zoneinfo.ZoneInfo("Europe/Paris") + with timezone.override(tz): + # This is a bug. + self.assertTrue(form.is_valid()) + self.assertEqual( + form.cleaned_data["dt"], + datetime.datetime(2011, 10, 30, 2, 30, 0), + ) def test_split_form(self): form = EventSplitForm({"dt_0": "2011-09-01", "dt_1": "13:20:30"}) @@ -1338,32 +1278,30 @@ class NewFormsTests(TestCase): ) def test_form_with_non_existent_time(self): - for tz in get_timezones("Europe/Paris"): - with self.subTest(repr(tz)): - with timezone.override(tz): - form = EventForm({"dt": "2011-03-27 02:30:00"}) - self.assertFalse(form.is_valid()) - self.assertEqual( - form.errors["dt"], - [ - "2011-03-27 02:30:00 couldn’t be interpreted in time zone " - "Europe/Paris; it may be ambiguous or it may not exist." - ], - ) + tz = zoneinfo.ZoneInfo("Europe/Paris") + with timezone.override(tz): + form = EventForm({"dt": "2011-03-27 02:30:00"}) + self.assertFalse(form.is_valid()) + self.assertEqual( + form.errors["dt"], + [ + "2011-03-27 02:30:00 couldn’t be interpreted in time zone " + "Europe/Paris; it may be ambiguous or it may not exist." + ], + ) def test_form_with_ambiguous_time(self): - for tz in get_timezones("Europe/Paris"): - with self.subTest(repr(tz)): - with timezone.override(tz): - form = EventForm({"dt": "2011-10-30 02:30:00"}) - self.assertFalse(form.is_valid()) - self.assertEqual( - form.errors["dt"], - [ - "2011-10-30 02:30:00 couldn’t be interpreted in time zone " - "Europe/Paris; it may be ambiguous or it may not exist." - ], - ) + tz = zoneinfo.ZoneInfo("Europe/Paris") + with timezone.override(tz): + form = EventForm({"dt": "2011-10-30 02:30:00"}) + self.assertFalse(form.is_valid()) + self.assertEqual( + form.errors["dt"], + [ + "2011-10-30 02:30:00 couldn’t be interpreted in time zone " + "Europe/Paris; it may be ambiguous or it may not exist." + ], + ) @requires_tz_support def test_split_form(self): |