[Checkins] SVN: zope.app.appsetup/trunk/ - Added stacking of storages for layer/test level setup separation in derived

Christian Theune ct at gocept.com
Thu Jan 27 09:12:40 EST 2011


Log message for revision 119977:
  - Added stacking of storages for layer/test level setup separation in derived
    ZODBLayers.
  
  

Changed:
  U   zope.app.appsetup/trunk/CHANGES.txt
  U   zope.app.appsetup/trunk/setup.py
  U   zope.app.appsetup/trunk/src/zope/app/appsetup/testlayer.py
  U   zope.app.appsetup/trunk/src/zope/app/appsetup/testlayer.txt

-=-
Modified: zope.app.appsetup/trunk/CHANGES.txt
===================================================================
--- zope.app.appsetup/trunk/CHANGES.txt	2011-01-27 11:11:26 UTC (rev 119976)
+++ zope.app.appsetup/trunk/CHANGES.txt	2011-01-27 14:12:39 UTC (rev 119977)
@@ -1,10 +1,11 @@
 Changelog
 =========
 
-3.15.1 (unreleased)
+3.16.0 (unreleased)
 -------------------
 
-- Nothing changed yet.
+- Added stacking of storages for layer/test level setup separation in derived
+  ZODBLayers.
 
 
 3.15.0 (2010-09-25)

Modified: zope.app.appsetup/trunk/setup.py
===================================================================
--- zope.app.appsetup/trunk/setup.py	2011-01-27 11:11:26 UTC (rev 119976)
+++ zope.app.appsetup/trunk/setup.py	2011-01-27 14:12:39 UTC (rev 119977)
@@ -30,7 +30,7 @@
 
 setup(
     name='zope.app.appsetup',
-    version='3.15.1dev',
+    version='3.16.0dev',
     author='Zope Corporation and Contributors',
     author_email='zope-dev at zope.org',
     description="Zope app setup helper",

Modified: zope.app.appsetup/trunk/src/zope/app/appsetup/testlayer.py
===================================================================
--- zope.app.appsetup/trunk/src/zope/app/appsetup/testlayer.py	2011-01-27 11:11:26 UTC (rev 119976)
+++ zope.app.appsetup/trunk/src/zope/app/appsetup/testlayer.py	2011-01-27 14:12:39 UTC (rev 119977)
@@ -25,10 +25,10 @@
 import zope.processlifetime
 
 
-def createTestDB(name='main'):
+def createTestDB(name='main', base=None):
     """This create a test storage and register it.
     """
-    storage = DemoStorage(name)
+    storage = DemoStorage(name, base=base)
     db = DB(storage, database_name=name)
     db.setActivityMonitor(ZODB.ActivityMonitor.ActivityMonitor())
 
@@ -59,11 +59,7 @@
             self.connection = self.db.open()
         return self.connection.root()[ZopePublication.root_name]
 
-    def testSetUp(self):
-        super(ZODBLayer, self).testSetUp()
-        self.db = createTestDB(self.db_name)
-
-    def testTearDown(self):
+    def _close_db(self):
         # Close any opened connections
         if self.connection is not None:
             transaction.abort()
@@ -79,4 +75,22 @@
             self.db.close()
             self.db = None
 
+    def setUp(self):
+        super(ZODBLayer, self).setUp()
+        self.db = createTestDB(self.db_name)
+        self.base_storage = self.db._storage
+        self._base_db_open = True
+
+    def testSetUp(self):
+        if self._base_db_open:
+            # This is the first time testSetUp is called.
+            # We need to close down the database configuration
+            # that we needed for performing global setup now.
+            self._close_db()
+            self._base_db_open = False
+        super(ZODBLayer, self).testSetUp()
+        self.db = createTestDB(self.db_name, self.base_storage)
+
+    def testTearDown(self):
+        self._close_db()
         super(ZODBLayer, self).testTearDown()

Modified: zope.app.appsetup/trunk/src/zope/app/appsetup/testlayer.txt
===================================================================
--- zope.app.appsetup/trunk/src/zope/app/appsetup/testlayer.txt	2011-01-27 11:11:26 UTC (rev 119976)
+++ zope.app.appsetup/trunk/src/zope/app/appsetup/testlayer.txt	2011-01-27 14:12:39 UTC (rev 119977)
@@ -60,3 +60,56 @@
     Ran 4 tests with 0 failures and 0 errors in ... seconds.
   Tearing down left over layers:
     Tear down zope.app.appsetup.testpackage.ZODBLayer in ... seconds.
+
+Database stacking with ZODBlayer
+--------------------------------
+
+When deriving from ZODBLayer we can start populating a test database at layer
+setup time. Each test will be run with a DemoStorage wrapped around that
+baseline and unwrapped afterwards. This way you can save a significant amount
+of time if tests require shared setup.
+
+We start with a derived layer, based on ZODBLayer that has some setup code:
+
+  >>> class CustomZODBLayer(ZODBLayer):
+  ...     def setUp(self):
+  ...         super(CustomZODBLayer, self).setUp()
+  ...         root = self.getRootFolder()
+  ...         root['foo'] = 1
+  ...         transaction.commit()
+
+  >>> layer = CustomZODBLayer(testpackage)
+  >>> layer
+  <CustomZODBLayer object at ...>
+
+Now, when running tests in this layer, each test sees the prepopulated
+database but is still isolated from other tests' changes:
+
+  >>> class CustomZODBLayerTests(unittest.TestCase):
+  ...     layer = layer
+  ...     def test_test1(self):
+  ...         root = self.layer.getRootFolder()
+  ...         self.assertEquals(1, len(root))
+  ...         root['bar'] = 1
+  ...         transaction.commit()
+  ...     def test_test2(self):
+  ...         root = self.layer.getRootFolder()
+  ...         self.assertEquals(1, len(root))
+  ...         root['baz'] = 1
+  ...         transaction.commit()
+
+Run the tests to prove that above statements are correct::
+
+  >>> suite = unittest.TestSuite()
+  >>> suite.addTest(unittest.makeSuite(CustomZODBLayerTests))
+
+And run that suite:
+
+  >>> from zope.testrunner.runner import Runner
+  >>> runner = Runner(args=[], found_suites=[suite])
+  >>> succeeded = runner.run()
+  Running zope.app.appsetup.testpackage.CustomZODBLayer tests:
+    Set up zope.app.appsetup.testpackage.CustomZODBLayer in ... seconds.
+    Ran 2 tests with 0 failures and 0 errors in ... seconds.
+  Tearing down left over layers:
+    Tear down zope.app.appsetup.testpackage.CustomZODBLayer in ... seconds.



More information about the checkins mailing list