Completed django.utils.timezone test coverage.

This commit is contained in:
Tim Graham 2016-11-01 14:01:40 -04:00 committed by GitHub
parent b8ae2c16cf
commit 3158695365
1 changed files with 35 additions and 0 deletions

View File

@ -1,4 +1,5 @@
import datetime import datetime
import pickle
import sys import sys
import pytz import pytz
@ -97,6 +98,18 @@ class TimezoneTests(SimpleTestCase):
finally: finally:
timezone.deactivate() timezone.deactivate()
def test_override_string_tz(self):
with timezone.override('Asia/Bangkok'):
self.assertEqual(timezone.get_current_timezone_name(), 'Asia/Bangkok')
def test_override_fixed_offset(self):
with timezone.override(timezone.FixedOffset(0, 'tzname')):
self.assertEqual(timezone.get_current_timezone_name(), 'tzname')
def test_activate_invalid_timezone(self):
with self.assertRaisesMessage(ValueError, 'Invalid timezone: None'):
timezone.activate(None)
def test_is_aware(self): def test_is_aware(self):
self.assertTrue(timezone.is_aware(datetime.datetime(2011, 9, 1, 13, 20, 30, tzinfo=EAT))) 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))) self.assertFalse(timezone.is_aware(datetime.datetime(2011, 9, 1, 13, 20, 30)))
@ -127,6 +140,18 @@ class TimezoneTests(SimpleTestCase):
with self.assertRaisesMessage(ValueError, 'astimezone() cannot be applied to a naive datetime'): with self.assertRaisesMessage(ValueError, 'astimezone() cannot be applied to a naive datetime'):
timezone.make_naive(*args) timezone.make_naive(*args)
def test_make_naive_no_tz(self):
self.assertEqual(
timezone.make_naive(datetime.datetime(2011, 9, 1, 13, 20, 30, tzinfo=EAT)),
datetime.datetime(2011, 9, 1, 5, 20, 30)
)
def test_make_aware_no_tz(self):
self.assertEqual(
timezone.make_aware(datetime.datetime(2011, 9, 1, 13, 20, 30)),
datetime.datetime(2011, 9, 1, 13, 20, 30, tzinfo=timezone.get_fixed_timezone(-300))
)
def test_make_aware2(self): def test_make_aware2(self):
self.assertEqual( self.assertEqual(
timezone.make_aware(datetime.datetime(2011, 9, 1, 12, 20, 30), CET), timezone.make_aware(datetime.datetime(2011, 9, 1, 12, 20, 30), CET),
@ -177,3 +202,13 @@ class TimezoneTests(SimpleTestCase):
self.assertEqual(std - dst, datetime.timedelta(hours=1)) self.assertEqual(std - dst, datetime.timedelta(hours=1))
self.assertEqual(std.tzinfo.utcoffset(std), datetime.timedelta(hours=1)) self.assertEqual(std.tzinfo.utcoffset(std), datetime.timedelta(hours=1))
self.assertEqual(dst.tzinfo.utcoffset(dst), datetime.timedelta(hours=2)) self.assertEqual(dst.tzinfo.utcoffset(dst), datetime.timedelta(hours=2))
def test_get_default_timezone(self):
self.assertEqual(timezone.get_default_timezone_name(), 'America/Chicago')
def test_fixedoffset_timedelta(self):
delta = datetime.timedelta(hours=1)
self.assertEqual(timezone.get_fixed_timezone(delta).utcoffset(''), delta)
def test_fixedoffset_pickle(self):
self.assertEqual(pickle.loads(pickle.dumps(timezone.FixedOffset(0, 'tzname'))).tzname(''), 'tzname')