Factored skip condition when pytz isn't installed.

This commit is contained in:
Aymeric Augustin 2015-05-03 09:08:37 +02:00
parent da2c6959c8
commit 06dc6759d8
2 changed files with 16 additions and 12 deletions

View File

@ -37,6 +37,8 @@ try:
except ImportError: except ImportError:
pytz = None pytz = None
requires_pytz = skipIf(pytz is None, "this test requires pytz")
# These tests use the EAT (Eastern Africa Time) and ICT (Indochina Time) # 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 # who don't have Daylight Saving Time, so we can represent them easily
# with FixedOffset, and use them directly as tzinfo in the constructors. # with FixedOffset, and use them directly as tzinfo in the constructors.
@ -372,7 +374,7 @@ class NewDatabaseTests(TestCase):
self.assertEqual(Event.objects.filter(dt__gte=dt2).count(), 1) self.assertEqual(Event.objects.filter(dt__gte=dt2).count(), 1)
self.assertEqual(Event.objects.filter(dt__gt=dt2).count(), 0) self.assertEqual(Event.objects.filter(dt__gt=dt2).count(), 0)
@skipIf(pytz is None, "this test requires pytz") @requires_pytz
def test_query_filter_with_pytz_timezones(self): def test_query_filter_with_pytz_timezones(self):
tz = pytz.timezone('Europe/Paris') tz = pytz.timezone('Europe/Paris')
dt = datetime.datetime(2011, 9, 1, 12, 20, 30, tzinfo=tz) dt = datetime.datetime(2011, 9, 1, 12, 20, 30, tzinfo=tz)
@ -898,7 +900,7 @@ class TemplateTests(TestCase):
expected = results[k1][k2] expected = results[k1][k2]
self.assertEqual(actual, expected, '%s / %s: %r != %r' % (k1, k2, actual, expected)) self.assertEqual(actual, expected, '%s / %s: %r != %r' % (k1, k2, actual, expected))
@skipIf(pytz is None, "this test requires pytz") @requires_pytz
def test_localtime_filters_with_pytz(self): def test_localtime_filters_with_pytz(self):
""" """
Test the |localtime, |utc, and |timezone filters with pytz. Test the |localtime, |utc, and |timezone filters with pytz.
@ -963,7 +965,7 @@ class TemplateTests(TestCase):
'tz1': ICT, 'tz2': None}) 'tz1': ICT, 'tz2': None})
self.assertEqual(tpl.render(ctx), "2011-09-01T13:20:30+03:00|2011-09-01T17:20:30+07:00|2011-09-01T13:20:30+03:00") self.assertEqual(tpl.render(ctx), "2011-09-01T13:20:30+03:00|2011-09-01T17:20:30+07:00|2011-09-01T13:20:30+03:00")
@skipIf(pytz is None, "this test requires pytz") @requires_pytz
def test_timezone_templatetag_with_pytz(self): def test_timezone_templatetag_with_pytz(self):
""" """
Test the {% timezone %} templatetag with pytz. Test the {% timezone %} templatetag with pytz.
@ -1003,7 +1005,7 @@ class TemplateTests(TestCase):
with timezone.override(UTC): with timezone.override(UTC):
self.assertEqual(tpl.render(Context({'tz': ICT})), "+0700") self.assertEqual(tpl.render(Context({'tz': ICT})), "+0700")
@skipIf(pytz is None, "this test requires pytz") @requires_pytz
def test_get_current_timezone_templatetag_with_pytz(self): def test_get_current_timezone_templatetag_with_pytz(self):
""" """
Test the {% get_current_timezone %} templatetag with pytz. Test the {% get_current_timezone %} templatetag with pytz.
@ -1071,7 +1073,7 @@ class LegacyFormsTests(TestCase):
self.assertTrue(form.is_valid()) self.assertTrue(form.is_valid())
self.assertEqual(form.cleaned_data['dt'], datetime.datetime(2011, 9, 1, 13, 20, 30)) self.assertEqual(form.cleaned_data['dt'], datetime.datetime(2011, 9, 1, 13, 20, 30))
@skipIf(pytz is None, "this test requires pytz") @requires_pytz
def test_form_with_non_existent_time(self): def test_form_with_non_existent_time(self):
form = EventForm({'dt': '2011-03-27 02:30:00'}) form = EventForm({'dt': '2011-03-27 02:30:00'})
with timezone.override(pytz.timezone('Europe/Paris')): with timezone.override(pytz.timezone('Europe/Paris')):
@ -1079,7 +1081,7 @@ class LegacyFormsTests(TestCase):
self.assertTrue(form.is_valid()) self.assertTrue(form.is_valid())
self.assertEqual(form.cleaned_data['dt'], datetime.datetime(2011, 3, 27, 2, 30, 0)) self.assertEqual(form.cleaned_data['dt'], datetime.datetime(2011, 3, 27, 2, 30, 0))
@skipIf(pytz is None, "this test requires pytz") @requires_pytz
def test_form_with_ambiguous_time(self): def test_form_with_ambiguous_time(self):
form = EventForm({'dt': '2011-10-30 02:30:00'}) form = EventForm({'dt': '2011-10-30 02:30:00'})
with timezone.override(pytz.timezone('Europe/Paris')): with timezone.override(pytz.timezone('Europe/Paris')):
@ -1118,7 +1120,7 @@ class NewFormsTests(TestCase):
# Datetime inputs formats don't allow providing a time zone. # Datetime inputs formats don't allow providing a time zone.
self.assertFalse(form.is_valid()) self.assertFalse(form.is_valid())
@skipIf(pytz is None, "this test requires pytz") @requires_pytz
def test_form_with_non_existent_time(self): def test_form_with_non_existent_time(self):
with timezone.override(pytz.timezone('Europe/Paris')): with timezone.override(pytz.timezone('Europe/Paris')):
form = EventForm({'dt': '2011-03-27 02:30:00'}) form = EventForm({'dt': '2011-03-27 02:30:00'})
@ -1127,7 +1129,7 @@ class NewFormsTests(TestCase):
["2011-03-27 02:30:00 couldn't be interpreted in time zone " ["2011-03-27 02:30:00 couldn't be interpreted in time zone "
"Europe/Paris; it may be ambiguous or it may not exist."]) "Europe/Paris; it may be ambiguous or it may not exist."])
@skipIf(pytz is None, "this test requires pytz") @requires_pytz
def test_form_with_ambiguous_time(self): def test_form_with_ambiguous_time(self):
with timezone.override(pytz.timezone('Europe/Paris')): with timezone.override(pytz.timezone('Europe/Paris')):
form = EventForm({'dt': '2011-10-30 02:30:00'}) form = EventForm({'dt': '2011-10-30 02:30:00'})

View File

@ -11,6 +11,8 @@ try:
except ImportError: except ImportError:
pytz = None pytz = None
requires_pytz = unittest.skipIf(pytz is None, "this test requires pytz")
if pytz is not None: if pytz is not None:
CET = pytz.timezone("Europe/Paris") CET = pytz.timezone("Europe/Paris")
EAT = timezone.get_fixed_timezone(180) # Africa/Nairobi EAT = timezone.get_fixed_timezone(180) # Africa/Nairobi
@ -137,7 +139,7 @@ class TimezoneTests(unittest.TestCase):
with self.assertRaises(ValueError): with self.assertRaises(ValueError):
timezone.make_naive(datetime.datetime(2011, 9, 1, 13, 20, 30), EAT) timezone.make_naive(datetime.datetime(2011, 9, 1, 13, 20, 30), EAT)
@unittest.skipIf(pytz is None, "this test requires pytz") @requires_pytz
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),
@ -145,7 +147,7 @@ class TimezoneTests(unittest.TestCase):
with self.assertRaises(ValueError): with self.assertRaises(ValueError):
timezone.make_aware(CET.localize(datetime.datetime(2011, 9, 1, 12, 20, 30)), CET) timezone.make_aware(CET.localize(datetime.datetime(2011, 9, 1, 12, 20, 30)), CET)
@unittest.skipIf(pytz is None, "this test requires pytz") @requires_pytz
def test_make_aware_pytz(self): def test_make_aware_pytz(self):
self.assertEqual( self.assertEqual(
timezone.make_naive(CET.localize(datetime.datetime(2011, 9, 1, 12, 20, 30)), CET), timezone.make_naive(CET.localize(datetime.datetime(2011, 9, 1, 12, 20, 30)), CET),
@ -156,7 +158,7 @@ class TimezoneTests(unittest.TestCase):
with self.assertRaises(ValueError): with self.assertRaises(ValueError):
timezone.make_naive(datetime.datetime(2011, 9, 1, 12, 20, 30), CET) timezone.make_naive(datetime.datetime(2011, 9, 1, 12, 20, 30), CET)
@unittest.skipIf(pytz is None, "this test requires pytz") @requires_pytz
def test_make_aware_pytz_ambiguous(self): def test_make_aware_pytz_ambiguous(self):
# 2:30 happens twice, once before DST ends and once after # 2:30 happens twice, once before DST ends and once after
ambiguous = datetime.datetime(2015, 10, 25, 2, 30) ambiguous = datetime.datetime(2015, 10, 25, 2, 30)
@ -170,7 +172,7 @@ class TimezoneTests(unittest.TestCase):
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))
@unittest.skipIf(pytz is None, "this test requires pytz") @requires_pytz
def test_make_aware_pytz_non_existent(self): def test_make_aware_pytz_non_existent(self):
# 2:30 never happened due to DST # 2:30 never happened due to DST
non_existent = datetime.datetime(2015, 3, 29, 2, 30) non_existent = datetime.datetime(2015, 3, 29, 2, 30)