[Zope3-checkins] CVS: Zope3/lib/python/datetime/tests - test_datetime.py:1.2
Tim Peters
tim.one@comcast.net
Fri, 20 Dec 2002 17:30:38 -0500
Update of /cvs-repository/Zope3/lib/python/datetime/tests
In directory cvs.zope.org:/tmp/cvs-serv2135/lib/python/datetime/tests
Modified Files:
test_datetime.py
Log Message:
Synch with the current version of datetime.py. This includes a beefed-
up test suite.
Note that tzinfo objects must now derive from datetime.tzinfo.
Note that repr() strings now include the module name, such as
datetime.date or datetime.timetz. This is for compatibility with the
Python 2.3's C implementation (which, alas, doesn't compile under
Python 2.2.2).
Note that there's a bug in Python 2.2.2 (fixed in CVS) that the test
suite worms around.
=== Zope3/lib/python/datetime/tests/test_datetime.py 1.1 => 1.2 === (2057/2157 lines abridged)
--- Zope3/lib/python/datetime/tests/test_datetime.py:1.1 Fri Dec 20 13:53:04 2002
+++ Zope3/lib/python/datetime/tests/test_datetime.py Fri Dec 20 17:30:38 2002
@@ -1,56 +1,1248 @@
-"""Test date/time type.
-
-See http://www.zope.org/Members/fdrake/DateTimeWiki/TestCases
-"""
+"""Test date/time type."""
import sys
import unittest
-from datetime import date, time, timetz, datetime, datetimetz, timedelta
from datetime import MINYEAR, MAXYEAR
+from datetime import timedelta
+from datetime import tzinfo
+from datetime import time, timetz
+from datetime import date, datetime, datetimetz
+
+
+# XXX The test suite uncovered a bug in Python 2.2.2: if x and y are
+# XXX instances of new-style classes (like date and time) that both
+# XXX define __cmp__, and x is compared to y, and one of the __cmp__
+# XXX implementations raises an exception, the exception can get dropped
+# XXX on the floor when it occurs, and pop up again at some "random" time
+# XXX later (it depends on when the next opcode gets executed that
+# XXX bothers to check). There isn't a workaround for this, so instead
+# XXX we disable the parts of the tests that trigger it unless
+# XXX CMP_BUG_FIXED is true. The bug is still there, we simply avoid
+# XXX provoking it here.
+# XXX Guido checked into a fix that will go into 2.2.3. The bug was
+# XXX already fixed in 2.3 CVS via a different means.
+CMP_BUG_FIXED = sys.version_info >= (2, 2, 3)
+
+#############################################################################
+# module tests
+
+class TestModule(unittest.TestCase):
+
+ def test_constants(self):
+ import datetime
+ self.assertEqual(datetime.MINYEAR, 1)
+ self.assertEqual(datetime.MAXYEAR, 9999)
+
+#############################################################################
+# tzinfo tests
+
+class FixedOffset(tzinfo):
+ def __init__(self, offset, name, dstoffset=42):
+ self.__offset = offset
[-=- -=- -=- 2057 lines omitted -=- -=- -=-]
+ self.assertEqual(t.tm_hour, 23)
+ self.assertEqual(t.tm_min, 58)
+ self.assertEqual(t.tm_sec, 37)
+ self.assertEqual(t.tm_yday, 1)
+ self.assertEqual(t.tm_isdst, 0)
+
+ def test_tzinfo_isoformat(self):
+ zero = FixedOffset(0, "+00:00")
+ plus = FixedOffset(220, "+03:40")
+ minus = FixedOffset(-231, "-03:51")
+ unknown = FixedOffset(None, "")
+
+ cls = self.theclass
+ datestr = '0001-02-03'
+ for ofs in None, zero, plus, minus, unknown:
+ for us in 0, 987001:
+ d = cls(1, 2, 3, 4, 5, 59, us, tzinfo=ofs)
+ timestr = '04:05:59' + (us and '.987001' or '')
+ ofsstr = ofs is not None and d.tzname() or ''
+ tailstr = timestr + ofsstr
+ iso = d.isoformat()
+ self.assertEqual(iso, datestr + 'T' + tailstr)
+ self.assertEqual(iso, d.isoformat('T'))
+ self.assertEqual(d.isoformat('k'), datestr + 'k' + tailstr)
+ self.assertEqual(str(d), datestr + ' ' + tailstr)
+
def test_suite():
- s2 = unittest.makeSuite(TestDate, 'test')
- s3 = unittest.makeSuite(TestTime, 'test')
- s4 = unittest.makeSuite(TestTimeTZ, 'test')
- s5 = unittest.makeSuite(TestDateTime, 'test')
- s6 = unittest.makeSuite(TestDateTimeTZ, 'test')
- return unittest.TestSuite([s2, s3, s4, s5, s6])
+ allsuites = [unittest.makeSuite(klass, 'test')
+ for klass in (TestModule,
+ TestTZInfo,
+ TestTimeDelta,
+ TestDateOnly,
+ TestDate,
+ TestDateTime,
+ TestTime,
+ TestTimeTZ,
+ TestDateTimeTZ,
+ )
+ ]
+ return unittest.TestSuite(allsuites)
def test_main():
r = unittest.TextTestRunner(stream=sys.stdout, verbosity=2)