[Zope3-checkins] SVN: Zope3/trunk/src/zope/app/ Added the ability to:

Jim Fulton jim at zope.com
Sun Aug 22 22:58:28 EDT 2004


Log message for revision 27222:
  Added the ability to:
  
  - Get access to teh object system from functional doctests
  
  - Make changes in regular Python code and have the changes
    reflected sanely.
  
  In particular, making an http request implicitly commits any
    Python changes and syncs the connection used for objects accessed
    via Python.
  


Changed:
  U   Zope3/trunk/src/zope/app/ftests/doctest.txt
  U   Zope3/trunk/src/zope/app/tests/functional.py


-=-
Modified: Zope3/trunk/src/zope/app/ftests/doctest.txt
===================================================================
--- Zope3/trunk/src/zope/app/ftests/doctest.txt	2004-08-22 19:12:27 UTC (rev 27221)
+++ Zope3/trunk/src/zope/app/ftests/doctest.txt	2004-08-23 02:58:28 UTC (rev 27222)
@@ -4,6 +4,9 @@
 This file documents and tests doctest-based functional tests and basic
 Zope web-application functionality.
 
+Request/Response Functional Tests
+---------------------------------
+
 You can create Functional tests as doctests.  Typically, this is done
 by using a script such as zope.app.tests.dochttp.py to convert
 tcpwatch recorded output to a doctest, which is then edited to provide
@@ -44,6 +47,10 @@
   <BLANKLINE>
   <!DOCTYPE html PUBLIC ...
 
+Important note: you must use the user named "mgr" with a password
+"mgrpw". This means that when you record requests with tcpwatch, you
+need to use that user login name and password.
+
 And we get a normal output.
 
 Next we'll try accessing site management. Since we used "/manage", 
@@ -100,3 +107,36 @@
   <BLANKLINE>
   <!DOCTYPE html PUBLIC ...
 
+Access to the object system
+---------------------------
+
+You can use the getRootFolder function:
+
+  >>> root = getRootFolder()
+  >>> root
+  <zope.app.folder.folder.Folder object at ...>
+
+You can intermic http requests with regular Python calls.  Note,
+however, that making an http call implied a transaction commit.
+If you want to throw away changes made in Python code, abort the
+transaction before the http request.
+
+  >>> print http(r"""
+  ... POST /@@contents.html HTTP/1.1
+  ... Authorization: Basic bWdyOm1ncnB3
+  ... Content-Length: 54
+  ... Content-Type: application/x-www-form-urlencoded
+  ... 
+  ... type_name=zope.app.browser.add.Folder.f10&new_value=f1""")
+  HTTP/1.1 303 See Other
+  Content-Length: ...
+  Content-Type: text/html;charset=utf-8
+  Location: http://localhost/@@contents.html
+  <BLANKLINE>
+  <!DOCTYPE html ...
+
+Now we can see that the new folder was added:
+
+  >>> list(root.keys())
+  [u'f1']
+

Modified: Zope3/trunk/src/zope/app/tests/functional.py
===================================================================
--- Zope3/trunk/src/zope/app/tests/functional.py	2004-08-22 19:12:27 UTC (rev 27221)
+++ Zope3/trunk/src/zope/app/tests/functional.py	2004-08-23 02:58:28 UTC (rev 27222)
@@ -27,7 +27,7 @@
 from StringIO import StringIO
 from Cookie import SimpleCookie
 
-from transaction import get_transaction
+from transaction import abort, commit
 from ZODB.DB import DB
 from ZODB.DemoStorage import DemoStorage
 import zope.interface
@@ -123,7 +123,7 @@
 
     def tearDown(self):
         """Cleans up after a functional test case."""
-        get_transaction().abort()
+        abort()
         if self.connection:
             self.connection.close()
             self.connection = None
@@ -160,10 +160,10 @@
         return FunctionalTestSetup().getRootFolder()
 
     def commit(self):
-        get_transaction().commit()
+        commit()
 
     def abort(self):
-        get_transaction().abort()
+        abort()
 
 class BrowserTestCase(FunctionalTestCase):
     """Functional test case for Browser requests."""
@@ -435,6 +435,9 @@
 
     This is used for HTTP doc tests.
     """
+    # Commit work done by previous python code.
+    commit()
+
     # Discard leading white space to make call layout simpler
     request_string = request_string.lstrip()
 
@@ -448,6 +451,7 @@
     instream = StringIO(request_string)
     environment = {"HTTP_HOST": 'localhost',
                    "HTTP_REFERER": 'localhost',
+                   "REQUEST_METHOD": method,
                    "SERVER_PROTOCOL": protocol,
                    }
 
@@ -458,6 +462,8 @@
         environment[name] = value.rstrip()
 
     outstream = HTTPTaskStub()
+
+
     old_site = getSite()
     setSite(None)
     app = FunctionalTestSetup().getApplication()
@@ -466,7 +472,7 @@
 
     if method in ('GET', 'POST', 'HEAD'):
         if (method == 'POST' and
-            env.get('CONTENT_TYPE', '').startswith('text/xml')
+            environment.get('CONTENT_TYPE', '').startswith('text/xml')
             ):
             request_cls = XMLRPCRequest
             publication_cls = XMLRPCPublication
@@ -486,13 +492,22 @@
     
     publish(request)
     setSite(old_site)
+
+    # sync Python connection:
+    getRootFolder()._p_jar.sync()
+    
     return response
 
 headerre = re.compile('(\S+): (.+)$')
 def split_header(header):
     return headerre.match(header).group(1, 2)
 
+def getRootFolder():
+    return FunctionalTestSetup().getRootFolder()
 
+def sync():
+    getRootFolder()._p_jar.sync()
+
 #
 # Sample functional test case
 #
@@ -525,21 +540,24 @@
 def FunctionalDocFileSuite(*paths, **kw):
     globs = kw.setdefault('globs', {})
     globs['http'] = http
+    globs['getRootFolder'] = getRootFolder
+    globs['sync'] = sync
 
     kw['package'] = doctest._normalize_module(kw.get('package'))
 
     kwsetUp = kw.get('setUp')
     def setUp():
         FunctionalTestSetup().setUp()
+        
         if kwsetUp is not None:
             kwsetUp()
     kw['setUp'] = setUp
 
     kwtearDown = kw.get('tearDown')
     def tearDown():
-        FunctionalTestSetup().tearDown()
         if kwtearDown is not None:
             kwtearDown()
+        FunctionalTestSetup().tearDown()
     kw['tearDown'] = tearDown
 
     kw['optionflags'] = doctest.ELLIPSIS | doctest.CONTEXT_DIFF



More information about the Zope3-Checkins mailing list