[Zodb-checkins] CVS: Packages/bsddb3Storage - test_minimal.py:1.2

[email protected] [email protected]
Fri, 30 Mar 2001 14:47:38 -0500 (EST)


Update of /cvs-repository/Packages/bsddb3Storage/test
In directory korak:/tmp/cvs-serv32597

Modified Files:
	test_minimal.py 
Log Message:
CommitAndRead.setUp(): Wrap the creation and opening of the
database in a try/except so that we guarantee that tearDown() is
called if there are any errors in database creation.

tearDown(): Added this to make sure that the test directories are
removed when the test is done.

Redesign these tests so they don't need the hackish subclass of
unittest.TestSuite, but so that it still keeps the modular nature of
the individual TestCases.  Each test case method that depends on the
state of an earlier method actually calls that earlier method
explicitly.

Also add some better docstrings.

Added __main__ section so this test can be run standalone.



--- Updated File test_minimal.py in package Packages/bsddb3Storage --
--- test_minimal.py	2001/03/17 00:01:00	1.1
+++ test_minimal.py	2001/03/30 19:47:37	1.2
@@ -1,4 +1,4 @@
-# Test that commits are actually persistent in the Minimal storage.
+# Test the Minimal storage at the ZODB level
 
 import os
 import errno
@@ -9,26 +9,6 @@
 
 
 
-class MasterSetup(unittest.TestSuite):
-    # Let the individual test cases set themselves up by creating the storage
-    # and other higher level objects.  This instance is here simply to ensure
-    # that the support files are deleted when all the tests are done.
-    #
-    # BAW: I think this is a horrible kludge.  What I really want is a mix
-    # between a TestCase and a TestSuite -- essentially a TestSuite that has
-    # setUp() and guaranteed tearDown() semantics.  Must contact Steve Purcell
-    # to ask about the right way to do this.
-    def __call__(self, result):
-        try:
-            unittest.TestSuite.__call__(self, result)
-        finally:
-            dbhome = DBHOME
-            for file in os.listdir(dbhome):
-                os.unlink(os.path.join(dbhome, file))
-            os.removedirs(dbhome)
-            
-
-
 class CommitAndRead(unittest.TestCase):
     # Never tear down the test framework since we want the database support
     # files to persist.  MasterSetup will take care of cleaning things up when
@@ -42,14 +22,23 @@
             os.mkdir(self._dbhome)
         except OSError, e:
             if e.errno <> errno.EEXIST: raise
-
-        self._storage = Minimal.Minimal(self._dbhome)
-        self._db = DB(self._storage)
-        self._conn = self._db.open()
-        self._root = self._conn.root()
 
+        try:
+            self._storage = Minimal.Minimal(self._dbhome)
+            self._db = DB(self._storage)
+            self._conn = self._db.open()
+            self._root = self._conn.root()
+        except:
+            self.tearDown()
+            raise
+
+    def tearDown(self):
+        for file in os.listdir(DBHOME):
+            os.unlink(os.path.join(DBHOME, file))
+        os.removedirs(DBHOME)
+        
     def checkCommit(self):
-        """On an empty database, commit some data"""
+        """Minimal: On an empty database, commit some data"""
         from BTree import BTree
 
         assert not self._root
@@ -59,25 +48,29 @@
         get_transaction().commit()
 
     def checkReadAfterCommit(self):
-        """On a database with data, try to read the data"""
+        """Minimal: On a database with data, try to read the data"""
+        self.checkCommit()
         names = self._root['names']
         assert names['Warsaw'] == 'Barry'
         assert names['Hylton'] == 'Jeremy'
         assert names.get('Drake') is None
 
     def checkAbortAfterRead(self):
-        """On a database with data, abort a transaction"""
+        """Minimal: On a database with data, abort a transaction"""
+        self.checkReadAfterCommit()
         names = self._root['names']
         names['Drake'] = 'Fred'
         get_transaction().abort()
 
     def checkReadAfterAbort(self):
-        """On a database with data, make sure the aborted data is missing"""
+        """Minimal: On a database, make sure the aborted data is missing"""
+        self.checkAbortAfterRead()
         names = self._root['names']
         assert names.get('Drake') is None
 
     def checkChangingCommits(self):
-        """Commit some ever changing data"""
+        """Minimal: Commit some ever changing data"""
+        self.checkReadAfterAbort()
         now = time.time()
         # Make sure the last timestamp was more than 3 seconds ago
         timestamp = self._root.get('timestamp')
@@ -92,13 +85,15 @@
 
 def suite():
     suite = unittest.TestSuite()
-    master = MasterSetup()
-    master.addTest(CommitAndRead('checkCommit'))
-    master.addTest(CommitAndRead('checkReadAfterCommit'))
-    master.addTest(CommitAndRead('checkAbortAfterRead'))
-    master.addTest(CommitAndRead('checkReadAfterCommit'))
+    suite.addTest(CommitAndRead('checkCommit'))
+    suite.addTest(CommitAndRead('checkReadAfterCommit'))
+    suite.addTest(CommitAndRead('checkAbortAfterRead'))
+    suite.addTest(CommitAndRead('checkReadAfterCommit'))
     for i in range(5):
-        master.addTest(CommitAndRead('checkChangingCommits'))
-    suite.addTest(master)
+        suite.addTest(CommitAndRead('checkChangingCommits'))
     return suite
 
+
+
+if __name__ == '__main__':
+    unittest.main(defaultTest='suite')