33
33
AllDayEvent , Event , MaybeEvent , Session , SessionEvent , Timestamp ,
34
34
)
35
35
36
+ try :
37
+ import yaml
38
+ HAS_YAML = True
39
+ except ImportError :
40
+ HAS_YAML = False
41
+
36
42
# These tests use the EAT (Eastern Africa Time) and ICT (Indochina Time)
37
43
# who don't have Daylight Saving Time, so we can represent them easily
38
44
# with fixed offset timezones and use them directly as tzinfo in the
@@ -605,9 +611,10 @@ class SerializationTests(SimpleTestCase):
605
611
606
612
# Backend-specific notes:
607
613
# - 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.
611
618
# Tests are adapted to take these quirks into account.
612
619
613
620
def assert_python_contains_datetime (self , objects , dt ):
@@ -694,7 +701,10 @@ def test_aware_datetime_with_microsecond(self):
694
701
data = serializers .serialize ('yaml' , [Event (dt = dt )], default_flow_style = None )
695
702
self .assert_yaml_contains_datetime (data , "2011-09-01 17:20:30.405060+07:00" )
696
703
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 )
698
708
699
709
def test_aware_datetime_in_utc (self ):
700
710
dt = datetime .datetime (2011 , 9 , 1 , 10 , 20 , 30 , tzinfo = UTC )
@@ -742,7 +752,10 @@ def test_aware_datetime_in_local_timezone(self):
742
752
data = serializers .serialize ('yaml' , [Event (dt = dt )], default_flow_style = None )
743
753
self .assert_yaml_contains_datetime (data , "2011-09-01 13:20:30+03:00" )
744
754
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 )
746
759
747
760
def test_aware_datetime_in_other_timezone (self ):
748
761
dt = datetime .datetime (2011 , 9 , 1 , 17 , 20 , 30 , tzinfo = ICT )
@@ -766,7 +779,10 @@ def test_aware_datetime_in_other_timezone(self):
766
779
data = serializers .serialize ('yaml' , [Event (dt = dt )], default_flow_style = None )
767
780
self .assert_yaml_contains_datetime (data , "2011-09-01 17:20:30+07:00" )
768
781
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 )
770
786
771
787
772
788
@override_settings (DATETIME_FORMAT = 'c' , TIME_ZONE = 'Africa/Nairobi' , USE_L10N = False , USE_TZ = True )
0 commit comments