[Zodb-checkins] CVS: Zope3/lib/python/Persistence/BTrees/tests - testBTrees.py:1.1.2.4

Jeremy Hylton jeremy@zope.com
Wed, 5 Jun 2002 17:36:09 -0400


Update of /cvs-repository/Zope3/lib/python/Persistence/BTrees/tests
In directory cvs.zope.org:/tmp/cvs-serv3409/tests

Modified Files:
      Tag: Zope-3x-branch
	testBTrees.py 
Log Message:
Refactor the BTrees test to avoid boilerplate.

Use make_test() function to create a test class specialized for a
particular base class and BTree object type.  This replaces lots of
top-level classes that had to be managed explicitly.


=== Zope3/lib/python/Persistence/BTrees/tests/testBTrees.py 1.1.2.3 => 1.1.2.4 ===
 from Persistence.BTrees.IIBTree import IIBTree, IIBucket, IISet, IITreeSet
 from Persistence.BTrees.OIBTree import OIBTree, OIBucket, OISet, OITreeSet
+
 from unittest import TestCase, TestSuite, TextTestRunner, makeSuite
 
 from glob import glob
 
-class Base:
+class Base(TestCase):
     """ Tests common to all types: sets, buckets, and BTrees """
     def tearDown(self):
         self.t = None
@@ -33,7 +34,7 @@
         from ZODB.DB import DB
         n = 'fs_tmp__%s' % os.getpid()
         s = FileStorage(n)
-        db = DB(s)
+        db = DB(s, cache_size=1)
         root = db.open().root()
         return root
 
@@ -151,7 +152,6 @@
             self.assertEqual(v[i],i*i , (i*i,i))
 
     def testValuesWorks1(self):
-
         for x in range(100):
             self.t[99-x] = x 
 
@@ -159,7 +159,6 @@
             lst = list(self.t.values(0+x,99-x))
             lst.sort()
             self.assertEqual(lst,range(0+x,99-x+1))
-
             
     def testKeysWorks(self):
         for x in range(100):
@@ -253,12 +252,10 @@
 class NormalSetTests(Base):
     """ Test common to all set types """
 
-
     def _populate(self, t, l): 
         # Make some data
         t.update(range(l))
 
-
     def testInsertReturnsValue(self):
         t = self.t
         self.assertEqual(t.insert(5) , 1)
@@ -365,10 +362,6 @@
         for x in r:
             self.assertEqual(t[x] , x)
         
-class BucketTests(MappingBase):
-    """ Tests common to all buckets """
-    pass
-
 class BTreeTests(MappingBase):
     """ Tests common to all BTrees """
     def testDeleteNoChildrenWorks(self):
@@ -584,17 +577,19 @@
         self.assertEqual(t.insert(1, 1) , 1)
         self.assertEqual(lsubtract(list(t.keys()), [0,1]) , [])
 
-## BTree tests
+# tests of various type errors
 
-class TestIOBTrees(BTreeTests, TestCase):
-    def setUp(self):
-        self.t = IOBTree()
+class TypeTest(TestCase):
 
-    def nonIntegerKeyRaises(self):
+    def testBadTypeRaises(self):
         self.assertRaises(TypeError, self._stringraises)
         self.assertRaises(TypeError, self._floatraises)
         self.assertRaises(TypeError, self._noneraises)
 
+class TestIOBTrees(TypeTest):
+    def setUp(self):
+        self.t = IOBTree()
+
     def _stringraises(self):
         self.t['c'] = 1
 
@@ -604,19 +599,10 @@
     def _noneraises(self):
         self.t[None] = 1
 
-class TestOOBTrees(BTreeTests, TestCase):
-    def setUp(self):
-        self.t = OOBTree()
-
-class TestOIBTrees(BTreeTests, TestCase):
+class TestOIBTrees(TypeTest):
     def setUp(self):
         self.t = OIBTree()
 
-    def testNonIntegerValueRaises(self):
-        self.assertRaises(TypeError, self._stringraises)
-        self.assertRaises(TypeError, self._floatraises)
-        self.assertRaises(TypeError, self._noneraises)
-
     def _stringraises(self):
         self.t[1] = 'c'
 
@@ -626,7 +612,7 @@
     def _noneraises(self):
         self.t[1] = None
 
-class TestIIBTrees(BTreeTests, TestCase):
+class TestIIBTrees(TestCase):
     def setUp(self):
         self.t = IIBTree()
 
@@ -658,9 +644,7 @@
     def _noneraisesvalue(self):
         self.t[1] = None
 
-## Set tests
-
-class TestIOSets(ExtendedSetTests, TestCase):
+class TestIOSets(TestCase):
     def setUp(self):
         self.t = IOSet()
 
@@ -678,94 +662,48 @@
     def _insertnoneraises(self):
         self.t.insert(None)
 
-class TestOOSets(ExtendedSetTests, TestCase):
-    def setUp(self):
-        self.t = OOSet()
-
-class TestIISets(ExtendedSetTests, TestCase):
-    def setUp(self):
-        self.t = IISet()
-
-class TestOISets(ExtendedSetTests, TestCase):
-    def setUp(self):
-        self.t = OISet()
-
-class TestIOTreeSets(NormalSetTests, TestCase):
-    def setUp(self):
-        self.t = IOTreeSet()
-        
-class TestOOTreeSets(NormalSetTests, TestCase):
-    def setUp(self):
-        self.t = OOTreeSet()
-
-class TestIITreeSets(NormalSetTests, TestCase):
-    def setUp(self):
-        self.t = IITreeSet()
-
-class TestOITreeSets(NormalSetTests, TestCase):
-    def setUp(self):
-        self.t = OITreeSet()
-        
-## Bucket tests
-
-class TestIOBuckets(BucketTests, TestCase):
-    def setUp(self):
-        self.t = IOBucket()
-
-class TestOOBuckets(BucketTests, TestCase):
-    def setUp(self):
-        self.t = OOBucket()
-
-class TestIIBuckets(BucketTests, TestCase):
-    def setUp(self):
-        self.t = IIBucket()
-
-class TestOIBuckets(BucketTests, TestCase):
-    def setUp(self):
-        self.t = OIBucket()
+def make_test(klass, base):
+    class Test(base):
+        def setUp(self):
+            self.t = klass()
+    # Give the test an artificial name so that unittest output
+    # includes the name of the BTree object being tested.  Include a
+    # '!' in the name so that it is obvious that it doesn't occur in
+    # the source code anywhere.
+    Test.__name__ = "!%sTest" % klass.__name__
+    return makeSuite(Test)
 
 def test_suite():
-    TIOBTree = makeSuite(TestIOBTrees, 'test')
-    TOOBTree = makeSuite(TestOOBTrees, 'test')
-    TOIBTree = makeSuite(TestOIBTrees, 'test')
-    TIIBTree = makeSuite(TestIIBTrees, 'test')
-
-    TIOSet = makeSuite(TestIOSets, 'test')
-    TOOSet = makeSuite(TestOOSets, 'test')
-    TOISet = makeSuite(TestIOSets, 'test')
-    TIISet = makeSuite(TestOOSets, 'test')
-
-    TIOTreeSet = makeSuite(TestIOTreeSets, 'test')
-    TOOTreeSet = makeSuite(TestOOTreeSets, 'test')
-    TOITreeSet = makeSuite(TestIOTreeSets, 'test')
-    TIITreeSet = makeSuite(TestOOTreeSets, 'test')
-
-    TIOBucket = makeSuite(TestIOBuckets, 'test')
-    TOOBucket = makeSuite(TestOOBuckets, 'test')
-    TOIBucket = makeSuite(TestOIBuckets, 'test')
-    TIIBucket = makeSuite(TestIIBuckets, 'test')
+    # Handle the basic test cases for each type of object via make_test().
+    template = [(MappingBase, (IIBucket, IOBucket, OIBucket, OOBucket)),
+                (NormalSetTests, (IITreeSet, IOTreeSet, OITreeSet, OOTreeSet)),
+                (ExtendedSetTests, (IISet, IOSet, OISet, OOSet)),
+                (BTreeTests, (IIBTree, IOBTree,  OIBTree, OOBTree)),
+                ]
     
-    alltests = TestSuite((TIOSet, TOOSet, TOISet, TIISet,
-                          TIOTreeSet, TOOTreeSet, TOITreeSet, TIITreeSet,
-                          TIOBucket, TOOBucket, TOIBucket, TIIBucket,
-                          TOOBTree, TIOBTree, TOIBTree, TIIBTree))
-
-    return alltests
-
+    s = TestSuite()
+    for base, classes in template:
+        for klass in classes:
+            s.addTest(make_test(klass, base))
+
+    # Handle the odd assortment of other tests, which appear to be
+    # specific to whether keys and values are Is or Os.
+    for klass in TestIOBTrees, TestOIBTrees, TestIIBTrees, TestIOSets:
+        s.addTest(makeSuite(klass))
 
+    return s
 
 ## utility functions
 
 def lsubtract(l1, l2):
-   l1=list(l1)
-   l2=list(l2)
+   l1 = list(l1)
+   l2 = list(l2)
    l = filter(lambda x, l1=l1: x not in l1, l2)
    l = l + filter(lambda x, l2=l2: x not in l2, l1)
    return l
 
 def realseq(itemsob):
-    return map(lambda x: x, itemsob)
-
+    return [x for x in itemsob]
 
 def main():
     TextTestRunner().run(test_suite())