[Checkins] SVN: ZODB/trunk/src/ZODB/Blobs/tests/transaction.txt - restified

Christian Theune ct at gocept.com
Fri Mar 9 12:09:06 EST 2007


Log message for revision 73128:
   - restified
   - added test for custom openDetached() class
  

Changed:
  U   ZODB/trunk/src/ZODB/Blobs/tests/transaction.txt

-=-
Modified: ZODB/trunk/src/ZODB/Blobs/tests/transaction.txt
===================================================================
--- ZODB/trunk/src/ZODB/Blobs/tests/transaction.txt	2007-03-09 17:03:38 UTC (rev 73127)
+++ ZODB/trunk/src/ZODB/Blobs/tests/transaction.txt	2007-03-09 17:09:06 UTC (rev 73128)
@@ -1,6 +1,6 @@
 ##############################################################################
 #
-# Copyright (c) 2005 Zope Corporation and Contributors.
+# Copyright (c) 2005-2007 Zope Corporation and Contributors.
 # All Rights Reserved.
 #
 # This software is subject to the provisions of the Zope Public License,
@@ -15,7 +15,7 @@
 Transaction support for Blobs
 =============================
 
-We need a database with a blob supporting storage:
+We need a database with a blob supporting storage::
 
     >>> from ZODB.MappingStorage import MappingStorage
     >>> from ZODB.Blobs.BlobStorage import BlobStorage
@@ -30,16 +30,16 @@
     >>> connection1 = database.open()
     >>> root1 = connection1.root()
     >>> from ZODB.Blobs.Blob import Blob
-    
-Putting a Blob into a Connection works like any other Persistent object:
 
+Putting a Blob into a Connection works like any other Persistent object::
+
     >>> blob1 = Blob()
     >>> blob1.open('w').write('this is blob 1')
     >>> root1['blob1'] = blob1
     >>> transaction.commit()
 
 Aborting a transaction involving a blob write cleans up uncommitted
-file data:
+file data::
 
     >>> dead_blob = Blob()
     >>> dead_blob.open('w').write('this is a dead blob')
@@ -53,7 +53,7 @@
     False
 
 Opening a blob gives us a filehandle.  Getting data out of the
-resulting filehandle is accomplished via the filehandle's read method:
+resulting filehandle is accomplished via the filehandle's read method::
 
     >>> connection2 = database.open()
     >>> root2 = connection2.root()
@@ -70,7 +70,7 @@
 
 Let's make another filehandle for read only to blob1a, this should bump
 up its refcount by one, and each file handle has a reference to the
-(same) underlying blob:
+(same) underlying blob::
 
     >>> blob1afh2 = blob1a.open("r")
     >>> blob1afh2.blob._p_blob_refcounts()
@@ -81,7 +81,7 @@
     True
 
 Let's close the first filehandle we got from the blob, this should decrease
-its refcount by one:
+its refcount by one::
 
     >>> blob1afh1.close()
     >>> blob1a._p_blob_refcounts()
@@ -89,7 +89,7 @@
 
 Let's abort this transaction, and ensure that the filehandles that we
 opened are now closed and that the filehandle refcounts on the blob
-object are cleared.
+object are cleared::
 
     >>> transaction.abort()
     >>> blob1afh1.blob._p_blob_refcounts()
@@ -106,7 +106,7 @@
 If we open a blob for append, its write refcount should be nonzero.
 Additionally, writing any number of bytes to the blobfile should
 result in the blob being marked "dirty" in the connection (we just
-aborted above, so the object should be "clean" when we start):
+aborted above, so the object should be "clean" when we start)::
 
     >>> bool(blob1a._p_changed)
     False
@@ -120,7 +120,7 @@
     True
 
 We can open more than one blob object during the course of a single
-transaction:
+transaction::
 
     >>> blob2 = Blob()
     >>> blob2.open('w').write('this is blob 3')
@@ -133,7 +133,7 @@
 
 Since we committed the current transaction above, the aggregate
 changes we've made to blob, blob1a (these refer to the same object) and
-blob2 (a different object) should be evident:
+blob2 (a different object) should be evident::
 
     >>> blob1.open('r').read()
     'this is blob 1woot!'
@@ -145,7 +145,7 @@
 We shouldn't be able to persist a blob filehandle at commit time
 (although the exception which is raised when an object cannot be
 pickled appears to be particulary unhelpful for casual users at the
-moment):
+moment)::
 
     >>> root1['wontwork'] = blob1.open('r')
     >>> transaction.commit()
@@ -153,12 +153,12 @@
         ...
     TypeError: coercing to Unicode: need string or buffer, BlobFile found
 
-Abort for good measure:
+Abort for good measure::
 
     >>> transaction.abort()
 
 Attempting to change a blob simultaneously from two different
-connections should result in a write conflict error.
+connections should result in a write conflict error::
 
     >>> tm1 = transaction.TransactionManager()
     >>> tm2 = transaction.TransactionManager()
@@ -179,7 +179,7 @@
     ConflictError: database conflict error (oid 0x01, class ZODB.Blobs.Blob.Blob)
 
 After the conflict, the winning transaction's result is visible on both
-connections:
+connections::
 
     >>> root3['blob1'].open('r').read()
     'this is blob 1woot!this is from connection 3'
@@ -188,7 +188,7 @@
     'this is blob 1woot!this is from connection 3'
 
 BlobStorages implementation of getSize() includes the blob data and adds it to
-the underlying storages result of getSize():
+the underlying storages result of getSize()::
 
     >>> underlying_size = base_storage.getSize()
     >>> blob_size = blob_storage.getSize()
@@ -199,7 +199,7 @@
 Savepoints and Blobs
 --------------------
 
-We do support optimistic savepoints :
+We do support optimistic savepoints ::
 
     >>> connection5 = database.open()
     >>> root5 = connection5.root()
@@ -211,7 +211,7 @@
     >>> transaction.commit()
     >>> root5['blob'].open("rb").read()
     "I'm a happy blob."
-    >>> blob_fh = root5['blob'].open("a")       
+    >>> blob_fh = root5['blob'].open("a")
     >>> blob_fh.write(" And I'm singing.")
     >>> blob_fh.close()
     >>> root5['blob'].open("rb").read()
@@ -221,9 +221,9 @@
     "I'm a happy blob. And I'm singing."
     >>> transaction.get().commit()
 
-We do not support non-optimistic savepoints:
+We do not support non-optimistic savepoints::
 
-    >>> blob_fh = root5['blob'].open("a")   
+    >>> blob_fh = root5['blob'].open("a")
     >>> blob_fh.write(" And the weather is beautiful.")
     >>> blob_fh.close()
     >>> root5['blob'].open("rb").read()
@@ -238,7 +238,7 @@
 --------------------------------------
 
 If you want to read from a Blob outside of transaction boundaries (e.g. to
-stream a file to the browser), you can use the openDetached() method:
+stream a file to the browser), you can use the openDetached() method::
 
     >>> connection6 = database.open()
     >>> root6 = connection6.root()
@@ -251,7 +251,7 @@
     >>> blob.openDetached().read()
     "I'm a happy blob."
 
-Of course, that doesn't work for empty blobs
+Of course, that doesn't work for empty blobs::
 
     >>> blob = Blob()
     >>> blob.openDetached()
@@ -259,7 +259,7 @@
         ...
     BlobError: Blob does not exist.
 
-nor when the Blob is already opened for writing:
+nor when the Blob is already opened for writing::
 
     >>> blob = Blob()
     >>> blob_fh = blob.open("wb")
@@ -268,8 +268,25 @@
         ...
     BlobError: Already opened for writing.
 
-It does work when the transaction was aborted, though:
+You can also pass a factory to the openDetached method that will be used to
+instantiate the file. This is used for e.g. creating filestream iterators::
 
+    >>> class customfile(file):
+    ...   pass
+    >>> blob_fh.write('Something')
+    >>> blob_fh.close()
+    >>> fh = blob.openDetached(customfile)
+    >>> fh  # doctest: +ELLIPSIS
+    <open file '...', mode 'rb' at 0x...>
+    >>> isinstance(fh, customfile)
+    True
+
+
+Note: Nasty people could use a factory that opens the file for writing. This
+would be evil.
+
+It does work when the transaction was aborted, though::
+
     >>> blob = Blob()
     >>> blob_fh = blob.open("wb")
     >>> blob_fh.write("I'm a happy blob.")
@@ -288,7 +305,7 @@
 Teardown
 --------
 
-We don't need the storage directory and databases anymore:
+We don't need the storage directory and databases anymore::
 
     >>> import shutil
     >>> shutil.rmtree(blob_dir)



More information about the Checkins mailing list