[Zope3-checkins] CVS: Zope3/src/datetime/tests - test_datetime.py:1.29.2.1

Sidnei da Silva sidnei@x3ng.com.br
Tue, 11 Feb 2003 09:41:48 -0500


Update of /cvs-repository/Zope3/src/datetime/tests
In directory cvs.zope.org:/tmp/cvs-serv18615/src/datetime/tests

Modified Files:
      Tag: paris-copypasterename-branch
	test_datetime.py 
Log Message:
Updating from HEAD to make sure everything still works before merging

=== Zope3/src/datetime/tests/test_datetime.py 1.29 => 1.29.2.1 ===
--- Zope3/src/datetime/tests/test_datetime.py:1.29	Fri Jan 31 22:24:44 2003
+++ Zope3/src/datetime/tests/test_datetime.py	Tue Feb 11 09:40:47 2003
@@ -14,36 +14,16 @@
 from datetime import time
 from datetime import date, datetime
 
-
-pickle_choices = [
-    (pickle, pickle, 0),
-    (pickle, pickle, 1),
-    (pickle, pickle, 2),
-    (cPickle, cPickle, 0),
-    (cPickle, cPickle, 1),
-##    (cPickle, cPickle, 2),
-    (pickle, cPickle, 0),
-    (pickle, cPickle, 1),
-##    (pickle, cPickle, 2),
-    (cPickle, pickle, 0),
-    (cPickle, pickle, 1),
-##    (cPickle, pickle, 2),
-    ]
-
-
-# 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)
+# Before Python 2.3, proto=2 was taken as a synonym for proto=1.
+pickle_choices = [(pickler, unpickler, proto)
+                  for pickler in pickle, cPickle
+                  for unpickler in pickle, cPickle
+                  for proto in range(3)]
+assert len(pickle_choices) == 2*2*3
+
+# An arbitrary collection of objects of non-datetime types, for testing
+# mixed-type comparisons.
+OTHERSTUFF = (10, 10L, 34.5, "abc", {}, [], ())
 
 
 #############################################################################
@@ -143,9 +123,51 @@
                 self.assertEqual(derived.tzname(None), 'cookie')
 
 #############################################################################
+# Base clase for testing a particular aspect of timedelta, time, date and
+# datetime comparisons.
+
+class HarmlessMixedComparison(unittest.TestCase):
+    # Test that __eq__ and __ne__ don't complain for mixed-type comparisons.
+
+    # Subclasses must define 'theclass', and theclass(1, 1, 1) must be a
+    # legit constructor.
+
+    def test_harmless_mixed_comparison(self):
+        me = self.theclass(1, 1, 1)
+
+        self.failIf(me == ())
+        self.failUnless(me != ())
+        self.failIf(() == me)
+        self.failUnless(() != me)
+
+        self.failUnless(me in [1, 20L, [], me])
+        self.failIf(me not in [1, 20L, [], me])
+
+        self.failUnless([] in [me, 1, 20L, []])
+        self.failIf([] not in [me, 1, 20L, []])
+
+    def test_harmful_mixed_comparison(self):
+        me = self.theclass(1, 1, 1)
+
+        self.assertRaises(TypeError, lambda: me < ())
+        self.assertRaises(TypeError, lambda: me <= ())
+        self.assertRaises(TypeError, lambda: me > ())
+        self.assertRaises(TypeError, lambda: me >= ())
+
+        self.assertRaises(TypeError, lambda: () < me)
+        self.assertRaises(TypeError, lambda: () <= me)
+        self.assertRaises(TypeError, lambda: () > me)
+        self.assertRaises(TypeError, lambda: () >= me)
+
+        self.assertRaises(TypeError, cmp, (), me)
+        self.assertRaises(TypeError, cmp, me, ())
+
+#############################################################################
 # timedelta tests
 
-class TestTimeDelta(unittest.TestCase):
+class TestTimeDelta(HarmlessMixedComparison):
+
+    theclass = timedelta
 
     def test_constructor(self):
         eq = self.assertEqual
@@ -310,15 +332,16 @@
             self.assertEqual(cmp(t1, t2), -1)
             self.assertEqual(cmp(t2, t1), 1)
 
-        for badarg in 10, 10L, 34.5, "abc", {}, [], ():
-            self.assertRaises(TypeError, lambda: t1 == badarg)
-            self.assertRaises(TypeError, lambda: t1 != badarg)
+        for badarg in OTHERSTUFF:
+            self.assertEqual(t1 == badarg, False)
+            self.assertEqual(t1 != badarg, True)
+            self.assertEqual(badarg == t1, False)
+            self.assertEqual(badarg != t1, True)
+
             self.assertRaises(TypeError, lambda: t1 <= badarg)
             self.assertRaises(TypeError, lambda: t1 < badarg)
             self.assertRaises(TypeError, lambda: t1 > badarg)
             self.assertRaises(TypeError, lambda: t1 >= badarg)
-            self.assertRaises(TypeError, lambda: badarg == t1)
-            self.assertRaises(TypeError, lambda: badarg != t1)
             self.assertRaises(TypeError, lambda: badarg <= t1)
             self.assertRaises(TypeError, lambda: badarg < t1)
             self.assertRaises(TypeError, lambda: badarg > t1)
@@ -455,7 +478,7 @@
         dt2 = dt - delta
         self.assertEqual(dt2, dt - days)
 
-class TestDate(unittest.TestCase):
+class TestDate(HarmlessMixedComparison):
     # Tests here should pass for both dates and datetimes, except for a
     # few tests that TestDateTime overrides.
 
@@ -862,15 +885,15 @@
             self.assertEqual(cmp(t1, t2), -1)
             self.assertEqual(cmp(t2, t1), 1)
 
-        for badarg in 10, 10L, 34.5, "abc", {}, [], ():
-            self.assertRaises(TypeError, lambda: t1 == badarg)
-            self.assertRaises(TypeError, lambda: t1 != badarg)
-            self.assertRaises(TypeError, lambda: t1 <= badarg)
+        for badarg in OTHERSTUFF:
+            self.assertEqual(t1 == badarg, False)
+            self.assertEqual(t1 != badarg, True)
+            self.assertEqual(badarg == t1, False)
+            self.assertEqual(badarg != t1, True)
+
             self.assertRaises(TypeError, lambda: t1 < badarg)
             self.assertRaises(TypeError, lambda: t1 > badarg)
             self.assertRaises(TypeError, lambda: t1 >= badarg)
-            self.assertRaises(TypeError, lambda: badarg == t1)
-            self.assertRaises(TypeError, lambda: badarg != t1)
             self.assertRaises(TypeError, lambda: badarg <= t1)
             self.assertRaises(TypeError, lambda: badarg < t1)
             self.assertRaises(TypeError, lambda: badarg > t1)
@@ -1185,6 +1208,14 @@
             derived = unpickler.loads(green)
             self.assertEqual(orig, derived)
 
+    def test_more_pickling(self):
+        a = self.theclass(2003, 2, 7, 16, 48, 37, 444116)
+        s = pickle.dumps(a)
+        b = pickle.loads(s)
+        self.assertEqual(b.year, 2003)
+        self.assertEqual(b.month, 2)
+        self.assertEqual(b.day, 7)
+
     def test_more_compare(self):
         # The test_compare() inherited from TestDate covers the error cases.
         # We just want to test lexicographic ordering on the members datetime
@@ -1362,7 +1393,7 @@
         alsobog = AlsoBogus()
         self.assertRaises(ValueError, dt.astimezone, alsobog) # also naive
 
-class TestTime(unittest.TestCase):
+class TestTime(HarmlessMixedComparison):
 
     theclass = time
 
@@ -1429,18 +1460,16 @@
             self.assertEqual(cmp(t1, t2), -1)
             self.assertEqual(cmp(t2, t1), 1)
 
-        badargs = (10, 10L, 34.5, "abc", {}, [], ())
-        if CMP_BUG_FIXED:
-            badargs += (date(1, 1, 1), datetime(1, 1, 1, 1, 1), timedelta(9))
-        for badarg in badargs:
-            self.assertRaises(TypeError, lambda: t1 == badarg)
-            self.assertRaises(TypeError, lambda: t1 != badarg)
+        for badarg in OTHERSTUFF:
+            self.assertEqual(t1 == badarg, False)
+            self.assertEqual(t1 != badarg, True)
+            self.assertEqual(badarg == t1, False)
+            self.assertEqual(badarg != t1, True)
+
             self.assertRaises(TypeError, lambda: t1 <= badarg)
             self.assertRaises(TypeError, lambda: t1 < badarg)
             self.assertRaises(TypeError, lambda: t1 > badarg)
             self.assertRaises(TypeError, lambda: t1 >= badarg)
-            self.assertRaises(TypeError, lambda: badarg == t1)
-            self.assertRaises(TypeError, lambda: badarg != t1)
             self.assertRaises(TypeError, lambda: badarg <= t1)
             self.assertRaises(TypeError, lambda: badarg < t1)
             self.assertRaises(TypeError, lambda: badarg > t1)
@@ -1958,9 +1987,8 @@
         self.assertEqual(t1, t2)
         t2 = t2.replace(tzinfo=FixedOffset(None, ""))
         self.assertEqual(t1, t2)
-        if CMP_BUG_FIXED:
-            t2 = t2.replace(tzinfo=FixedOffset(0, ""))
-            self.assertRaises(TypeError, lambda: t1 == t2)
+        t2 = t2.replace(tzinfo=FixedOffset(0, ""))
+        self.assertRaises(TypeError, lambda: t1 == t2)
 
         # In time w/ identical tzinfo objects, utcoffset is ignored.
         class Varies(tzinfo):
@@ -2266,18 +2294,17 @@
         self.assertRaises(TypeError, meth)
 
         # Try to make sure tz= actually does some conversion.
-        timestamp = 1000000000  #  2001-09-09 01:46:40 UTC, give or take
-        utc = FixedOffset(0, "utc", 0)
-        expected = datetime(2001, 9, 9, 1, 46, 40)
-        got = datetime.utcfromtimestamp(timestamp)
-        # We don't support leap seconds, but maybe the platfrom insists
-        # on using them, so don't demand exact equality).
-        self.failUnless(abs(got - expected) < timedelta(minutes=1))
-
-        est = FixedOffset(-5*60, "est", 0)
-        expected -= timedelta(hours=5)
-        got = datetime.fromtimestamp(timestamp, est).replace(tzinfo=None)
-        self.failUnless(abs(got - expected) < timedelta(minutes=1))
+        timestamp = 1000000000
+        utcdatetime = datetime.utcfromtimestamp(timestamp)
+        # In POSIX (epoch 1970), that's 2001-09-09 01:46:40 UTC, give or take.
+        # But on some flavor of Mac, it's nowhere near that.  So we can't have
+        # any idea here what time that actually is, we can only test that
+        # relative changes match.
+        utcoffset = timedelta(hours=-15, minutes=39) # arbitrary, but not zero
+        tz = FixedOffset(utcoffset, "tz", 0)
+        expected = utcdatetime + utcoffset
+        got = datetime.fromtimestamp(timestamp, tz)
+        self.assertEqual(expected, got.replace(tzinfo=None))
 
     def test_tzinfo_utcnow(self):
         meth = self.theclass.utcnow
@@ -2543,9 +2570,8 @@
         self.assertEqual(t1, t2)
         t2 = t2.replace(tzinfo=FixedOffset(None, ""))
         self.assertEqual(t1, t2)
-        if CMP_BUG_FIXED:
-            t2 = t2.replace(tzinfo=FixedOffset(0, ""))
-            self.assertRaises(TypeError, lambda: t1 == t2)
+        t2 = t2.replace(tzinfo=FixedOffset(0, ""))
+        self.assertRaises(TypeError, lambda: t1 == t2)
 
         # In datetime w/ identical tzinfo objects, utcoffset is ignored.
         class Varies(tzinfo):