diff options
| author | Tim Graham <timograham@gmail.com> | 2016-10-07 21:06:49 -0400 |
|---|---|---|
| committer | Tim Graham <timograham@gmail.com> | 2016-10-27 08:53:20 -0400 |
| commit | 414ad25b090a63eaaf297b1164c8f7d814a710a2 (patch) | |
| tree | e3d0e6fdffee0793baad41a339c2d63db742c33a /tests/utils_tests | |
| parent | d84ffcc22b2a0dbc0a44a67d56da7e3647e2f87a (diff) | |
Fixed #27327 -- Simplified time zone handling by requiring pytz.
Diffstat (limited to 'tests/utils_tests')
| -rw-r--r-- | tests/utils_tests/test_dateformat.py | 21 | ||||
| -rw-r--r-- | tests/utils_tests/test_timezone.py | 64 |
2 files changed, 9 insertions, 76 deletions
diff --git a/tests/utils_tests/test_dateformat.py b/tests/utils_tests/test_dateformat.py index bda69fb4cd..bd99b31a42 100644 --- a/tests/utils_tests/test_dateformat.py +++ b/tests/utils_tests/test_dateformat.py @@ -1,8 +1,6 @@ from __future__ import unicode_literals -import sys from datetime import date, datetime -from unittest import skipIf from django.test import SimpleTestCase, override_settings from django.test.utils import TZ_SUPPORT, requires_tz_support @@ -12,11 +10,6 @@ from django.utils.timezone import ( get_default_timezone, get_fixed_timezone, make_aware, utc, ) -try: - import pytz -except ImportError: - pytz = None - @override_settings(TIME_ZONE='Europe/Copenhagen') class DateFormatTests(SimpleTestCase): @@ -36,18 +29,16 @@ class DateFormatTests(SimpleTestCase): dt = datetime(2009, 5, 16, 5, 30, 30) self.assertEqual(datetime.fromtimestamp(int(format(dt, 'U'))), dt) - @skipIf(sys.platform.startswith('win') and not pytz, "Test requires pytz on Windows") def test_naive_ambiguous_datetime(self): - # dt is ambiguous in Europe/Copenhagen. LocalTimezone guesses the - # offset (and gets it wrong 50% of the time) while pytz refuses the - # temptation to guess. In any case, this shouldn't crash. + # dt is ambiguous in Europe/Copenhagen. pytz raises an exception for + # the ambiguity, which results in an empty string. dt = datetime(2015, 10, 25, 2, 30, 0) # Try all formatters that involve self.timezone. - self.assertEqual(format(dt, 'I'), '0' if pytz is None else '') - self.assertEqual(format(dt, 'O'), '+0100' if pytz is None else '') - self.assertEqual(format(dt, 'T'), 'CET' if pytz is None else '') - self.assertEqual(format(dt, 'Z'), '3600' if pytz is None else '') + self.assertEqual(format(dt, 'I'), '') + self.assertEqual(format(dt, 'O'), '') + self.assertEqual(format(dt, 'T'), '') + self.assertEqual(format(dt, 'Z'), '') @requires_tz_support def test_datetime_with_local_tzinfo(self): diff --git a/tests/utils_tests/test_timezone.py b/tests/utils_tests/test_timezone.py index 1be1d63b2c..2fe5445c48 100644 --- a/tests/utils_tests/test_timezone.py +++ b/tests/utils_tests/test_timezone.py @@ -1,21 +1,12 @@ -import copy import datetime -import pickle import sys -import unittest + +import pytz from django.test import SimpleTestCase, mock, override_settings from django.utils import timezone -try: - import pytz -except ImportError: - pytz = None - -requires_pytz = unittest.skipIf(pytz is None, "this test requires pytz") - -if pytz is not None: - CET = pytz.timezone("Europe/Paris") +CET = pytz.timezone("Europe/Paris") EAT = timezone.get_fixed_timezone(180) # Africa/Nairobi ICT = timezone.get_fixed_timezone(420) # Asia/Bangkok @@ -24,33 +15,6 @@ PY36 = sys.version_info >= (3, 6) class TimezoneTests(SimpleTestCase): - def test_localtime(self): - now = datetime.datetime.utcnow().replace(tzinfo=timezone.utc) - local_tz = timezone.LocalTimezone() - with timezone.override(local_tz): - local_now = timezone.localtime(now) - self.assertEqual(local_now.tzinfo, local_tz) - local_now = timezone.localtime(now, timezone=local_tz) - self.assertEqual(local_now.tzinfo, local_tz) - - def test_localtime_naive(self): - now = datetime.datetime.now() - if PY36: - self.assertEqual(timezone.localtime(now), now.replace(tzinfo=timezone.LocalTimezone())) - else: - with self.assertRaisesMessage(ValueError, 'astimezone() cannot be applied to a naive datetime'): - timezone.localtime(now) - - def test_localtime_out_of_range(self): - local_tz = timezone.LocalTimezone() - long_ago = datetime.datetime(1900, 1, 1, tzinfo=timezone.utc) - try: - timezone.localtime(long_ago, local_tz) - except (OverflowError, ValueError) as exc: - self.assertIn("install pytz", exc.args[0]) - else: - self.skipTest("Failed to trigger an OverflowError or ValueError") - def test_now(self): with override_settings(USE_TZ=True): self.assertTrue(timezone.is_aware(timezone.now())) @@ -133,18 +97,6 @@ class TimezoneTests(SimpleTestCase): finally: timezone.deactivate() - def test_copy(self): - self.assertIsInstance(copy.copy(timezone.UTC()), timezone.UTC) - self.assertIsInstance(copy.copy(timezone.LocalTimezone()), timezone.LocalTimezone) - - def test_deepcopy(self): - self.assertIsInstance(copy.deepcopy(timezone.UTC()), timezone.UTC) - self.assertIsInstance(copy.deepcopy(timezone.LocalTimezone()), timezone.LocalTimezone) - - def test_pickling_unpickling(self): - self.assertIsInstance(pickle.loads(pickle.dumps(timezone.UTC())), timezone.UTC) - self.assertIsInstance(pickle.loads(pickle.dumps(timezone.LocalTimezone())), timezone.LocalTimezone) - def test_is_aware(self): self.assertTrue(timezone.is_aware(datetime.datetime(2011, 9, 1, 13, 20, 30, tzinfo=EAT))) self.assertFalse(timezone.is_aware(datetime.datetime(2011, 9, 1, 13, 20, 30))) @@ -175,7 +127,6 @@ class TimezoneTests(SimpleTestCase): with self.assertRaisesMessage(ValueError, 'astimezone() cannot be applied to a naive datetime'): timezone.make_naive(*args) - @requires_pytz def test_make_aware2(self): self.assertEqual( timezone.make_aware(datetime.datetime(2011, 9, 1, 12, 20, 30), CET), @@ -183,7 +134,6 @@ class TimezoneTests(SimpleTestCase): with self.assertRaises(ValueError): timezone.make_aware(CET.localize(datetime.datetime(2011, 9, 1, 12, 20, 30)), CET) - @requires_pytz def test_make_aware_pytz(self): self.assertEqual( timezone.make_naive(CET.localize(datetime.datetime(2011, 9, 1, 12, 20, 30)), CET), @@ -202,7 +152,6 @@ class TimezoneTests(SimpleTestCase): with self.assertRaises(ValueError): timezone.make_naive(datetime.datetime(2011, 9, 1, 12, 20, 30), CET) - @requires_pytz def test_make_aware_pytz_ambiguous(self): # 2:30 happens twice, once before DST ends and once after ambiguous = datetime.datetime(2015, 10, 25, 2, 30) @@ -216,7 +165,6 @@ class TimezoneTests(SimpleTestCase): self.assertEqual(std.tzinfo.utcoffset(std), datetime.timedelta(hours=1)) self.assertEqual(dst.tzinfo.utcoffset(dst), datetime.timedelta(hours=2)) - @requires_pytz def test_make_aware_pytz_non_existent(self): # 2:30 never happened due to DST non_existent = datetime.datetime(2015, 3, 29, 2, 30) @@ -229,9 +177,3 @@ class TimezoneTests(SimpleTestCase): self.assertEqual(std - dst, datetime.timedelta(hours=1)) self.assertEqual(std.tzinfo.utcoffset(std), datetime.timedelta(hours=1)) self.assertEqual(dst.tzinfo.utcoffset(dst), datetime.timedelta(hours=2)) - - # round trip to UTC then back to CET - std = timezone.localtime(timezone.localtime(std, timezone.UTC()), CET) - dst = timezone.localtime(timezone.localtime(dst, timezone.UTC()), CET) - self.assertEqual((std.hour, std.minute), (3, 30)) - self.assertEqual((dst.hour, dst.minute), (1, 30)) |
