8000 [2.2.x] Fixed timezones tests for PyYAML 5.3+. · django/django@96d6443 · GitHub
[go: up one dir, main page]

Skip to content

Commit 96d6443

Browse files
committed
[2.2.x] Fixed timezones tests for PyYAML 5.3+.
Backport of 8be477b from master
1 parent 813b33e commit 96d6443

File tree

1 file changed

+22
-6
lines changed

1 file changed

+22
-6
lines changed

tests/timezones/tests.py

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@
3333
AllDayEvent, Event, MaybeEvent, Session, SessionEvent, Timestamp,
3434
)
3535

36+
try:
37+
import yaml
38+
HAS_YAML = True
39+
except ImportError:
40+
HAS_YAML = False
41+
3642
# These tests use the EAT (Eastern Africa Time) and ICT (Indochina Time)
3743
# who don't have Daylight Saving Time, so we can represent them easily
3844
# with fixed offset timezones and use them directly as tzinfo in the
@@ -605,9 +611,10 @@ class SerializationTests(SimpleTestCase):
605611

606612
# Backend-specific notes:
607613
# - JSON supports only milliseconds, microseconds will be truncated.
608-
# - PyYAML dumps the UTC offset correctly for timezone-aware datetimes,
609-
# but when it loads this representation, it subtracts the offset and
610-
# returns a naive datetime object in UTC. See ticket #18867.
614+
# - PyYAML dumps the UTC offset correctly for timezone-aware datetimes.
615+
# When PyYAML < 5.3 loads this representation, it subtracts the offset
616+
# and returns a naive datetime object in UTC. PyYAML 5.3+ loads timezones
617+
# correctly.
611618
# Tests are adapted to take these quirks into account.
612619

613620
def assert_python_contains_datetime(self, objects, dt):
@@ -694,7 +701,10 @@ def test_aware_datetime_with_microsecond(self):
694701
data = serializers.serialize('yaml', [Event(dt=dt)], default_flow_style=None)
695702
self.assert_yaml_contains_datetime(data, "2011-09-01 17:20:30.405060+07:00")
696703
obj = next(serializers.deserialize('yaml', data)).object
697-
self.assertEqual(obj.dt.replace(tzinfo=UTC), dt)
704+
if HAS_YAML and yaml.__version__ < '5.3':
705+
self.assertEqual(obj.dt.replace(tzinfo=UTC), dt)
706+
else:
707+
self.assertEqual(obj.dt, dt)
698708

699709
def test_aware_datetime_in_utc(self):
700710
dt = datetime.datetime(2011, 9, 1, 10, 20, 30, tzinfo=UTC)
@@ -742,7 +752,10 @@ def test_aware_datetime_in_local_timezone(self):
742752
data = serializers.serialize('yaml', [Event(dt=dt)], default_flow_style=None)
743753
self.assert_yaml_contains_datetime(data, "2011-09-01 13:20:30+03:00")
744754
obj = next(serializers.deserialize('yaml', data)).object
745-
self.assertEqual(obj.dt.replace(tzinfo=UTC), dt)
755+
if HAS_YAML and yaml.__version__ < '5.3':
756+
self.assertEqual(obj.dt.replace(tzinfo=UTC), dt)
757+
else:
758+
self.assertEqual(obj.dt, dt)
746759

747760
def test_aware_datetime_in_other_timezone(self):
748761
dt = datetime.datetime(2011, 9, 1, 17, 20, 30, tzinfo=ICT)
@@ -766,7 +779,10 @@ def test_aware_datetime_in_other_timezone(self):
766779
data = serializers.serialize('yaml', [Event(dt=dt)], default_flow_style=None)
767780
self.assert_yaml_contains_datetime(data, "2011-09-01 17:20:30+07:00")
768781
obj = next(serializers.deserialize('yaml', data)).object
769-
self.assertEqual(obj.dt.replace(tzinfo=UTC), dt)
782+
if HAS_YAML and yaml.__version__ < '5.3':
783+
self.assertEqual(obj.dt.replace(tzinfo=UTC), dt)
784+
else:
785+
self.assertEqual(obj.dt, dt)
770786

771787

772788
@override_settings(DATETIME_FORMAT='c', TIME_ZONE='Africa/Nairobi', USE_L10N=False, USE_TZ=True)

0 commit comments

Comments
 (0)
0