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

Tim Peters tim.one@comcast.net
Fri, 7 Feb 2003 16:06:40 -0500


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

Modified Files:
	test_datetime.py 
Log Message:
Comparison for timedelta, time, date and datetime objects:  rewrote to
use rich comparisons.  __eq__ and __ne__ no longer complain if they
don't know how to compare to the other thing.  If no meaningful way to
compare is known, saying "not equal" is sensible.  This allows things
like

    if adatetime in some_sequence:
and
    somedict[adatetime] = whatever

to work as expected even if some_sequence contains non-datetime objects,
or somedict non-datetime keys, because they only call __eq__.

It still complains (raises TypeError) for mixed-type comparisons in
contexts that require a total ordering, such as list.sort(), use as a
key in a BTree-based data structure, and cmp().


=== Zope3/src/datetime/tests/test_datetime.py 1.31 => 1.32 ===
--- Zope3/src/datetime/tests/test_datetime.py:1.31	Thu Feb  6 11:41:33 2003
+++ Zope3/src/datetime/tests/test_datetime.py	Fri Feb  7 16:06:39 2003
@@ -134,9 +134,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
@@ -301,15 +343,18 @@
             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)
+        badargs = 10, 10L, 34.5, "abc", {}, [], ()
+        for badarg in badargs:
+            self.assertEqual(t1 == badarg, False)
+            self.assertEqual(t1 != badarg, True)
+            self.assertEqual(badarg == t1, False)
+            self.assertEqual(badarg != t1, True)
+
+        for badarg in badargs:
             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)
@@ -446,7 +491,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.
 
@@ -853,15 +898,17 @@
             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)
+        badargs = 10, 10L, 34.5, "abc", {}, [], ()
+        for badarg in badargs:
+            self.assertEqual(t1 == badarg, False)
+            self.assertEqual(t1 != badarg, True)
+            self.assertEqual(badarg == t1, False)
+            self.assertEqual(badarg != t1, True)
+
+        for badarg in badargs:
             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)
@@ -1353,7 +1400,7 @@
         alsobog = AlsoBogus()
         self.assertRaises(ValueError, dt.astimezone, alsobog) # also naive
 
-class TestTime(unittest.TestCase):
+class TestTime(HarmlessMixedComparison):
 
     theclass = time
 
@@ -1423,15 +1470,18 @@
         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.assertEqual(t1 == badarg, False)
+            self.assertEqual(t1 != badarg, True)
+            self.assertEqual(badarg == t1, False)
+            self.assertEqual(badarg != t1, True)
+
         for badarg in badargs:
-            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: 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)