[Checkins] SVN: Sandbox/shane/republish/zope.pipeline/src/zope/pipeline/apps/t added txnctl tests
Shane Hathaway
shane at hathawaymix.org
Wed Feb 18 02:01:16 EST 2009
Log message for revision 96673:
added txnctl tests
Changed:
U Sandbox/shane/republish/zope.pipeline/src/zope/pipeline/apps/tests/openroot.txt
U Sandbox/shane/republish/zope.pipeline/src/zope/pipeline/apps/tests/tests.py
A Sandbox/shane/republish/zope.pipeline/src/zope/pipeline/apps/tests/txnctl.txt
U Sandbox/shane/republish/zope.pipeline/src/zope/pipeline/apps/txnctl.py
-=-
Modified: Sandbox/shane/republish/zope.pipeline/src/zope/pipeline/apps/tests/openroot.txt
===================================================================
--- Sandbox/shane/republish/zope.pipeline/src/zope/pipeline/apps/tests/openroot.txt 2009-02-18 06:12:48 UTC (rev 96672)
+++ Sandbox/shane/republish/zope.pipeline/src/zope/pipeline/apps/tests/openroot.txt 2009-02-18 07:01:15 UTC (rev 96673)
@@ -65,7 +65,7 @@
If the traversal stack contains ++etc++process, the root opener uses
a utility by that name as the root instead of opening the database.
- >>> from zope.interface import Interface, implements
+ >>> from zope.interface import Interface
>>> from zope.component import provideUtility
>>> class ProcessRoot(object):
... pass
Modified: Sandbox/shane/republish/zope.pipeline/src/zope/pipeline/apps/tests/tests.py
===================================================================
--- Sandbox/shane/republish/zope.pipeline/src/zope/pipeline/apps/tests/tests.py 2009-02-18 06:12:48 UTC (rev 96672)
+++ Sandbox/shane/republish/zope.pipeline/src/zope/pipeline/apps/tests/tests.py 2009-02-18 07:01:15 UTC (rev 96673)
@@ -35,6 +35,8 @@
doctest.DocFileSuite('requestsetup.txt', optionflags=flags,
setUp=setUp, tearDown=tearDown),
doctest.DocFileSuite('retry.txt', optionflags=flags),
+ doctest.DocFileSuite('txnctl.txt', optionflags=flags,
+ setUp=setUp, tearDown=tearDown),
])
if __name__ == '__main__':
Added: Sandbox/shane/republish/zope.pipeline/src/zope/pipeline/apps/tests/txnctl.txt
===================================================================
--- Sandbox/shane/republish/zope.pipeline/src/zope/pipeline/apps/tests/txnctl.txt (rev 0)
+++ Sandbox/shane/republish/zope.pipeline/src/zope/pipeline/apps/tests/txnctl.txt 2009-02-18 07:01:15 UTC (rev 96673)
@@ -0,0 +1,130 @@
+
+TransactionController Tests
+---------------------------
+
+The TransactionController commits or aborts the current transaction after
+the pipeline processing.
+
+Create a data manager object and an application that registers that data
+manager with the transaction in progress.
+
+ >>> import transaction
+ >>> class TestDataManager(object):
+ ... def __init__(self):
+ ... self.phase = ''
+ ... self.committed = []
+ ... self.aborted = []
+ ... def tpc_begin(self, txn):
+ ... self.phase = 'begin'
+ ... def tpc_vote(self, txn):
+ ... self.phase = 'vote'
+ ... def tpc_finish(self, txn):
+ ... self.phase = 'finish'
+ ... def tpc_abort(self, txn):
+ ... self.phase = 'abort'
+ ... def commit(self, obj, txn):
+ ... self.committed.append(obj)
+ ... def abort(self, obj, txn):
+ ... self.aborted.append(obj)
+ >>> dm = TestDataManager()
+ >>> def my_app(environ, start_response):
+ ... transaction.get().register(dm)
+ ... return ['done']
+ >>> from zope.pipeline.apps.txnctl import TransactionController
+ >>> app = TransactionController(my_app)
+
+Run the app.
+
+ >>> environ = {}
+ >>> app(environ, None)
+ ['done']
+ >>> dm.phase
+ 'finish'
+ >>> dm.committed
+ [<TestDataManager object at ...>]
+ >>> dm.aborted
+ []
+
+Make the app raise an exception.
+
+ >>> dm = TestDataManager()
+ >>> def my_app(environ, start_response):
+ ... transaction.get().register(dm)
+ ... raise ValueError('no way')
+ >>> app = TransactionController(my_app)
+ >>> app(environ, None)
+ Traceback (most recent call last):
+ ...
+ ValueError: no way
+ >>> dm.phase
+ ''
+ >>> dm.committed
+ []
+ >>> dm.aborted
+ [<TestDataManager object at ...>]
+
+Doom a transaction. When a transaction is doomed, no data can be written,
+but the pipeline can return a normal (non-exception) response.
+
+ >>> dm = TestDataManager()
+ >>> def doom_app(environ, start_response):
+ ... transaction.get().register(dm)
+ ... transaction.doom()
+ ... return ['done but doomed']
+ >>> from zope.pipeline.apps.txnctl import TransactionController
+ >>> app = TransactionController(doom_app)
+ >>> app(environ, None)
+ ['done but doomed']
+ >>> dm.phase
+ ''
+ >>> dm.committed
+ []
+ >>> dm.aborted
+ [<TestDataManager object at ...>]
+
+
+TransactionAnnotator Tests
+--------------------------
+
+The TransactionAnnotator application adds information to the
+zope.request after the rest of the pipeline has finished.
+
+Set up enough of a request to make the TransactionAnnotator work.
+
+ >>> from zope.interface import classImplements
+ >>> from zope.location.interfaces import IRoot
+ >>> from zope.location.traversing import RootPhysicallyLocatable
+ >>> from zope.component import provideAdapter
+ >>> provideAdapter(RootPhysicallyLocatable)
+
+ >>> class TestRoot(object):
+ ... def mymethod(self):
+ ... pass
+ >>> classImplements(TestRoot, IRoot)
+ >>> class TestRequest(object):
+ ... pass
+ >>> request = TestRequest()
+ >>> request.traversed = [('xyz', TestRoot().mymethod)]
+ >>> class TestPrincipal(object):
+ ... pass
+ >>> request.principal = TestPrincipal()
+ >>> request.principal.id = 'testuser'
+
+Create and run an app with a TransactionAnnotator.
+
+ >>> def my_app(environ, start_response):
+ ... return ['done annotating']
+ >>> from zope.pipeline.apps.txnctl import TransactionAnnotator
+ >>> app = TransactionAnnotator(my_app)
+ >>> environ = {'zope.request': request}
+ >>> app(environ, None)
+ ['done annotating']
+
+Examine the annotations.
+
+ >>> transaction.get().user
+ '/ testuser'
+ >>> from pprint import pprint
+ >>> pprint(transaction.get()._extension)
+ {'location': u'/', 'request_type': 'zope.publisher.interfaces.base.IRequest'}
+ >>> transaction.abort()
Modified: Sandbox/shane/republish/zope.pipeline/src/zope/pipeline/apps/txnctl.py
===================================================================
--- Sandbox/shane/republish/zope.pipeline/src/zope/pipeline/apps/txnctl.py 2009-02-18 06:12:48 UTC (rev 96672)
+++ Sandbox/shane/republish/zope.pipeline/src/zope/pipeline/apps/txnctl.py 2009-02-18 07:01:15 UTC (rev 96673)
@@ -12,6 +12,7 @@
#
##############################################################################
+from new import instancemethod
import transaction
from zope.location.interfaces import ILocationInfo
@@ -56,7 +57,7 @@
self.next_app = next_app
def __call__(self, environ, start_response):
- res = self.app(environ, start_response)
+ res = self.next_app(environ, start_response)
txn = transaction.get()
if not txn.isDoomed():
request = environ['zope.request']
More information about the Checkins
mailing list