summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorMariusz Felisiak <felisiak.mariusz@gmail.com>2020-01-07 09:54:22 +0100
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2020-01-07 09:55:41 +0100
commit96d644312106337d714790cbedfcb227f0faa609 (patch)
treeff6c83e5b5b169bb4f7e1de5a941296e5d5f8d80 /tests
parent813b33eec4d3fef8c5c3d4bfbc6ac90a248680c6 (diff)
[2.2.x] Fixed timezones tests for PyYAML 5.3+.
Backport of 8be477be5c1a4afc9ad00bb58a324f637e018c0f from master
Diffstat (limited to 'tests')
-rw-r--r--tests/timezones/tests.py28
1 files changed, 22 insertions, 6 deletions
diff --git a/tests/timezones/tests.py b/tests/timezones/tests.py
index 7a63bac670..b092a16044 100644
--- a/tests/timezones/tests.py
+++ b/tests/timezones/tests.py
@@ -33,6 +33,12 @@ from .models import (
AllDayEvent, Event, MaybeEvent, Session, SessionEvent, Timestamp,
)
+try:
+ import yaml
+ HAS_YAML = True
+except ImportError:
+ HAS_YAML = False
+
# These tests use the EAT (Eastern Africa Time) and ICT (Indochina Time)
# who don't have Daylight Saving Time, so we can represent them easily
# with fixed offset timezones and use them directly as tzinfo in the
@@ -605,9 +611,10 @@ class SerializationTests(SimpleTestCase):
# Backend-specific notes:
# - JSON supports only milliseconds, microseconds will be truncated.
- # - PyYAML dumps the UTC offset correctly for timezone-aware datetimes,
- # but when it loads this representation, it subtracts the offset and
- # returns a naive datetime object in UTC. See ticket #18867.
+ # - PyYAML dumps the UTC offset correctly for timezone-aware datetimes.
+ # When PyYAML < 5.3 loads this representation, it subtracts the offset
+ # and returns a naive datetime object in UTC. PyYAML 5.3+ loads timezones
+ # correctly.
# Tests are adapted to take these quirks into account.
def assert_python_contains_datetime(self, objects, dt):
@@ -694,7 +701,10 @@ class SerializationTests(SimpleTestCase):
data = serializers.serialize('yaml', [Event(dt=dt)], default_flow_style=None)
self.assert_yaml_contains_datetime(data, "2011-09-01 17:20:30.405060+07:00")
obj = next(serializers.deserialize('yaml', data)).object
- self.assertEqual(obj.dt.replace(tzinfo=UTC), dt)
+ if HAS_YAML and yaml.__version__ < '5.3':
+ self.assertEqual(obj.dt.replace(tzinfo=UTC), dt)
+ else:
+ self.assertEqual(obj.dt, dt)
def test_aware_datetime_in_utc(self):
dt = datetime.datetime(2011, 9, 1, 10, 20, 30, tzinfo=UTC)
@@ -742,7 +752,10 @@ class SerializationTests(SimpleTestCase):
data = serializers.serialize('yaml', [Event(dt=dt)], default_flow_style=None)
self.assert_yaml_contains_datetime(data, "2011-09-01 13:20:30+03:00")
obj = next(serializers.deserialize('yaml', data)).object
- self.assertEqual(obj.dt.replace(tzinfo=UTC), dt)
+ if HAS_YAML and yaml.__version__ < '5.3':
+ self.assertEqual(obj.dt.replace(tzinfo=UTC), dt)
+ else:
+ self.assertEqual(obj.dt, dt)
def test_aware_datetime_in_other_timezone(self):
dt = datetime.datetime(2011, 9, 1, 17, 20, 30, tzinfo=ICT)
@@ -766,7 +779,10 @@ class SerializationTests(SimpleTestCase):
data = serializers.serialize('yaml', [Event(dt=dt)], default_flow_style=None)
self.assert_yaml_contains_datetime(data, "2011-09-01 17:20:30+07:00")
obj = next(serializers.deserialize('yaml', data)).object
- self.assertEqual(obj.dt.replace(tzinfo=UTC), dt)
+ if HAS_YAML and yaml.__version__ < '5.3':
+ self.assertEqual(obj.dt.replace(tzinfo=UTC), dt)
+ else:
+ self.assertEqual(obj.dt, dt)
@override_settings(DATETIME_FORMAT='c', TIME_ZONE='Africa/Nairobi', USE_L10N=False, USE_TZ=True)