[Zodb-checkins] CVS: ZODB3/ZODB/tests - testZODB.py:1.5

Jeremy Hylton jeremy@zope.com
Sat, 7 Sep 2002 13:22:10 -0400


Update of /cvs-repository/ZODB3/ZODB/tests
In directory cvs.zope.org:/tmp/cvs-serv28166/tests

Modified Files:
	testZODB.py 
Log Message:
Handle empty transactions without touching the storage.

# NB: commit() is responsible for calling tpc_begin() on the storage.
# It uses self._begun to track whether it has been called.  When
# self._begun is None, it has not been called.
 
# This arrangement allows us to handle the special case of a
# transaction with no modified objects.  It is possible for
# registration to be occur unintentionally and for a persistent
# object to compensate by making itself as unchanged.  When this
# happens, it's possible to commit a transaction with no modified
# objects.
 
# Since tpc_begin() may raise a ReadOnlyError, don't call it if there
# are no objects.  This avoids spurious (?) errors when working with
# a read-only storage.

Add code to handle this in Connection's tpc_begin() and commit()
methods.

Add two tests in testZODB.


=== ZODB3/ZODB/tests/testZODB.py 1.4 => 1.5 ===
--- ZODB3/ZODB/tests/testZODB.py:1.4	Wed Aug 14 18:07:09 2002
+++ ZODB3/ZODB/tests/testZODB.py	Sat Sep  7 13:22:09 2002
@@ -94,6 +94,38 @@
         self._storage.close()
         removefs("ZODBTests.fs")
 
+    def checkUnmodifiedObject(self):
+        # Test that a transaction with only unmodified objects works
+        # correctly.  The specific sequence of events is:
+        #     - an object is modified
+        #     - it is registered with the transaction
+        #     - the object is explicitly "unmodified"
+        #     - the transaction commits, but now has no modified objects
+        # We'd like to avoid doing anything with the storage.
+        ltid = self._storage.lastTransaction()
+        _objects = get_transaction()._objects
+        self.assertEqual(len(_objects), 0)
+        r = self._db.open().root()
+        obj = r["test"][0]
+        obj[1] = 1
+        self.assertEqual(obj._p_changed, 1)
+        self.assertEqual(len(_objects), 1)
+        del obj._p_changed
+        self.assertEqual(obj._p_changed, None)
+        self.assertEqual(len(_objects), 1)
+        get_transaction().commit()
+        self.assertEqual(ltid, self._storage.lastTransaction())
+
+    def checkVersionOnly(self):
+        # Make sure the changes to make empty transactions a no-op
+        # still allow things like abortVersion().  This should work
+        # because abortVersion() calls tpc_begin() itself.
+        r = self._db.open("version").root()
+        r[1] = 1
+        get_transaction().commit()
+        self._db.abortVersion("version")
+        get_transaction().commit()
+
 def test_suite():
     return unittest.makeSuite(ZODBTests, 'check')