Permalink
Browse files

ported most doctests to unittests (added some extra tests while I was…

… at it)
  • Loading branch information...
1 parent 24cc54f commit 9ab81a80f4c456295e6b8f57300cc96c66110402 @brosner committed Mar 20, 2010
Showing with 77 additions and 29 deletions.
  1. +77 −29 timezones/tests.py
View
@@ -1,37 +1,88 @@
+from datetime import datetime
-__test__ = {"API_TESTS": r"""
->>> from datetime import datetime
+from django import forms
+from django.conf import settings
+from django.test import TestCase
->>> from django.conf import settings
->>> ORIGINAL_TIME_ZONE = settings.TIME_ZONE
->>> settings.TIME_ZONE = "UTC"
+import timezones.forms
->>> from timezones import forms, decorators
->>> from timezones.utils import localtime_for_timezone, adjust_datetime_to_timezone
+from timezones.utils import localtime_for_timezone, adjust_datetime_to_timezone
->>> localtime_for_timezone(datetime(2008, 6, 25, 18, 0, 0), "America/Denver").strftime("%m/%d/%Y %H:%I:%S")
-'06/25/2008 12:12:00'
-# the default case where no timezone is given explicitly. uses settings.TIME_ZONE.
->>> f = forms.LocalizedDateTimeField()
->>> f.clean("2008-05-30 14:30:00")
-datetime.datetime(2008, 5, 30, 14, 30, tzinfo=<UTC>)
-# specify a timezone explicity. this may come from a UserProfile for example.
->>> f = forms.LocalizedDateTimeField(timezone="America/Denver")
->>> f.clean("2008-05-30 14:30:00")
-datetime.datetime(2008, 5, 30, 20, 30, tzinfo=<UTC>)
+class TimeZoneTestCase(TestCase):
+
+ def setUp(self):
+ # ensure UTC
+ self.ORIGINAL_TIME_ZONE = settings.TIME_ZONE
+ settings.TIME_ZONE = "UTC"
+
+ def tearDown(self):
+ settings.TIME_ZONE = self.ORIGINAL_TIME_ZONE
->>> f = forms.TimeZoneField()
->>> f.clean('US/Eastern')
-<DstTzInfo 'US/Eastern' EST-1 day, 19:00:00 STD>
->>> f = forms.TimeZoneField(required=False)
->>> f.clean('')
-u''
->>> f.clean('US/Eastern')
-<DstTzInfo 'US/Eastern' EST-1 day, 19:00:00 STD>
+class UtilsTestCase(TimeZoneTestCase):
+
+ def test_localtime_for_timezone(self):
+ self.assertEqual(
+ localtime_for_timezone(
+ datetime(2008, 6, 25, 18, 0, 0), "America/Denver"
+ ).strftime("%m/%d/%Y %H:%M:%S"),
+ "06/25/2008 12:00:00"
+ )
+
+ def test_adjust_datetime_to_timezone(self):
+ self.assertEqual(
+ adjust_datetime_to_timezone(
+ datetime(2008, 6, 25, 18, 0, 0), "UTC"
+ ).strftime("%m/%d/%Y %H:%M:%S"),
+ "06/25/2008 18:00:00"
+ )
+
+class TimeZoneFieldTestCase(TimeZoneTestCase):
+
+ def test_clean_required(self):
+ f = timezones.forms.TimeZoneField()
+ self.assertEqual(
+ repr(f.clean("US/Eastern")),
+ "<DstTzInfo 'US/Eastern' EST-1 day, 19:00:00 STD>"
+ )
+ self.assertRaises(forms.ValidationError, f.clean, "")
+
+ def test_clean_not_required(self):
+ f = timezones.forms.TimeZoneField(required=False)
+ self.assertEqual(
+ repr(f.clean("US/Eastern")),
+ "<DstTzInfo 'US/Eastern' EST-1 day, 19:00:00 STD>"
+ )
+ self.assertEqual(f.clean(""), "")
+
+
+class LocalizedDateTimeFieldTestCase(TimeZoneTestCase):
+
+ def test_clean_required(self):
+ # the default case where no timezone is given explicitly. uses settings.TIME_ZONE.
+ f = timezones.forms.LocalizedDateTimeField()
+ self.assertEqual(
+ repr(f.clean("2008-05-30 14:30:00")),
+ "datetime.datetime(2008, 5, 30, 14, 30, tzinfo=<UTC>)"
+ )
+ self.assertRaises(forms.ValidationError, f.clean, "")
+
+ def test_clean_required(self):
+ # the default case where no timezone is given explicitly. uses settings.TIME_ZONE.
+ f = timezones.forms.LocalizedDateTimeField(required=False)
+ self.assertEqual(
+ repr(f.clean("2008-05-30 14:30:00")),
+ "datetime.datetime(2008, 5, 30, 14, 30, tzinfo=<UTC>)"
+ )
+ self.assertEqual(f.clean(""), None)
+
+
+# @@@ old doctests that have not been finished (largely due to needing to
+# better understand how these bits were created and use-cases)
+NOT_USED = {"API_TESTS": r"""
>>> class Foo(object):
... datetime = datetime(2008, 6, 20, 23, 58, 17)
... @decorators.localdatetime('datetime')
@@ -48,7 +99,4 @@
datetime.datetime(2008, 6, 12, 13, 50, tzinfo=<UTC>)
>>> foo.localdatetime
datetime.datetime(2008, 6, 12, 23, 50, tzinfo=<DstTzInfo 'Australia/Lindeman' EST+10:00:00 STD>)
-
-
->>> settings.TIME_ZONE = ORIGINAL_TIME_ZONE
-"""}
+"""}

0 comments on commit 9ab81a8

Please sign in to comment.