[Checkins]
SVN: Sandbox/darrylcousins/mars.contentprovider/src/mars/contentprovider/
Juggling with the tests
Darryl Cousins
darryl at darrylcousins.net.nz
Mon Jul 16 01:22:28 EDT 2007
Log message for revision 78014:
Juggling with the tests
Changed:
A Sandbox/darrylcousins/mars.contentprovider/src/mars/contentprovider/contentprovider.txt
U Sandbox/darrylcousins/mars.contentprovider/src/mars/contentprovider/directive.txt
A Sandbox/darrylcousins/mars.contentprovider/src/mars/contentprovider/ftests/
U Sandbox/darrylcousins/mars.contentprovider/src/mars/contentprovider/ftests/ftesting.zcml
U Sandbox/darrylcousins/mars.contentprovider/src/mars/contentprovider/ftests/test_all.py
D Sandbox/darrylcousins/mars.contentprovider/src/mars/contentprovider/tests/
A Sandbox/darrylcousins/mars.contentprovider/src/mars/contentprovider/tests.py
-=-
Added: Sandbox/darrylcousins/mars.contentprovider/src/mars/contentprovider/contentprovider.txt
===================================================================
--- Sandbox/darrylcousins/mars.contentprovider/src/mars/contentprovider/contentprovider.txt (rev 0)
+++ Sandbox/darrylcousins/mars.contentprovider/src/mars/contentprovider/contentprovider.txt 2007-07-16 05:22:27 UTC (rev 78014)
@@ -0,0 +1,139 @@
+====================
+Mars ContentProvider
+====================
+
+The mars.contentprovider package provides the means of creating and configuring
+``contentproviders`` for an application using Zope3.
+
+For more detailed information about Zope 3 content providers please refer to the
+zope.contentprovider package. (A snippet of the README follows).
+
+The Zope 3 concept of content providers is that they are multi-adapters that are
+looked up by the context, request (and thus the layer/skin), and view they are
+displayed in.
+
+The second important concept of content providers are their two-phase
+rendering design. In the first phase the state of the content provider is
+prepared and, if applicable, any data, the provider is responsible for, is
+updated.
+
+Set up
+------
+
+First a few imports then we define a content provider using this package.
+
+ >>> import zope.interface
+ >>> import zope.component
+ >>> import grok
+ >>> import mars.contentprovider
+
+Define the content provider, note that none of the possible directives have been
+used. The default name that the provider is registered as is
+factory.__name__.lower(), in this case: 'title'.
+
+ >>> class Title(mars.contentprovider.ContentProvider):
+ ...
+ ... def render(self):
+ ... return self.context.title
+
+We need also to set up a fake module info class to pass to the content provider
+grokker.
+
+ >>> from martian.interfaces import IModuleInfo
+ >>> class ModuleInfo(object):
+ ... zope.interface.implements(IModuleInfo)
+ ... def getAnnotation(self, name, default):
+ ... return default
+
+In the test we manually ``grok`` the factory, normally this happens when a
+module is ``grokked`` on start up.
+
+ >>> from mars.contentprovider.meta import ContentProviderGrokker
+ >>> ContentProviderGrokker().grok('title', Title, zope.interface.Interface,
+ ... ModuleInfo(), None)
+ True
+
+Testing the content provider
+----------------------------
+
+Let's create a content object that will act as the context to the content
+provider.
+
+ >>> class Content(object):
+ ... zope.interface.implements(zope.interface.Interface)
+ ... title = u'My Title'
+
+ >>> content = Content()
+
+Now we can test the content provider in a minimal fashion.
+
+ >>> from zope.publisher.browser import TestRequest
+ >>> request = TestRequest()
+
+ >>> title = Title(content, request, None)
+ >>> title.render()
+ u'My Title'
+
+Provider lookup
+---------------
+
+Content providers are looked up as named multiadapters to a context, request and
+a view. This is usually done through the use of the tales expression
+``provider`` from within a page template. Other mars packages could be used
+here: mars.layer, mars.template and mars.view but we will use the `vanilla` zope
+3 architecture.
+
+ >>> import os, tempfile
+ >>> temp_dir = tempfile.mkdtemp()
+ >>> templateFileName = os.path.join(temp_dir, 'template.pt')
+ >>> open(templateFileName, 'w').write('''
+ ... <html>
+ ... <body>
+ ... <h1 tal:content="provider:title">Title</h1>
+ ... </body>
+ ... </html>
+ ... ''')
+
+As you can see, we expect the ``provider`` expression to simply look up the
+content provider and insert the HTML content at this place.
+
+Next we register the template as a view (browser page) for all objects:
+
+ >>> from zope.app.pagetemplate.simpleviewclass import SimpleViewClass
+ >>> FrontPage = SimpleViewClass(templateFileName, name='main.html')
+
+ >>> from zope.publisher.interfaces.browser import IDefaultBrowserLayer
+ >>> zope.component.provideAdapter(
+ ... FrontPage,
+ ... (zope.interface.Interface, IDefaultBrowserLayer),
+ ... zope.interface.Interface,
+ ... name='main.html')
+
+Finally we look up the view and render it:
+
+ >>> from zope.publisher.browser import TestRequest
+ >>> request = TestRequest()
+
+ >>> view = zope.component.getMultiAdapter((content, request),
+ ... name='main.html')
+ >>> print view().strip()
+ <html>
+ <body>
+ <h1>My Title</h1>
+ </body>
+ </html>
+
+With the view we could also directly look up the content provider as a multi
+adapter:
+
+ >>> from zope.contentprovider.interfaces import IContentProvider
+ >>> title= zope.component.queryMultiAdapter((content, request, view),
+ ... IContentProvider, name='title')
+ >>> print title.render()
+ My Title
+
+The other directives
+--------------------
+
+The use and testing of the available directives is done in the tests directory
+of this package.
Property changes on: Sandbox/darrylcousins/mars.contentprovider/src/mars/contentprovider/contentprovider.txt
___________________________________________________________________
Name: svn:keywords
+ Date Author
Modified: Sandbox/darrylcousins/mars.contentprovider/src/mars/contentprovider/directive.txt
===================================================================
--- Sandbox/darrylcousins/mars.contentprovider/src/mars/contentprovider/directive.txt 2007-07-16 03:52:44 UTC (rev 78013)
+++ Sandbox/darrylcousins/mars.contentprovider/src/mars/contentprovider/directive.txt 2007-07-16 05:22:27 UTC (rev 78014)
@@ -28,4 +28,4 @@
* grok.provides(class_or_interface):
Interface the class is looked up as, probably wouldn't be used.
- Default: zope.interface.Interface
+ Default: zope.contentprovider.interfaces.IContentProvider
Copied: Sandbox/darrylcousins/mars.contentprovider/src/mars/contentprovider/ftests (from rev 77855, Sandbox/darrylcousins/mars.contentprovider/src/mars/contentprovider/tests)
Modified: Sandbox/darrylcousins/mars.contentprovider/src/mars/contentprovider/ftests/ftesting.zcml
===================================================================
--- Sandbox/darrylcousins/mars.contentprovider/src/mars/contentprovider/tests/ftesting.zcml 2007-07-13 11:38:53 UTC (rev 77855)
+++ Sandbox/darrylcousins/mars.contentprovider/src/mars/contentprovider/ftests/ftesting.zcml 2007-07-16 05:22:27 UTC (rev 78014)
@@ -12,7 +12,6 @@
<include package="z3c.layer.minimal.tests" file="ftesting.zcml" />
- <include package="grok" />
<include package="zope.contentprovider" />
</configure>
Modified: Sandbox/darrylcousins/mars.contentprovider/src/mars/contentprovider/ftests/test_all.py
===================================================================
--- Sandbox/darrylcousins/mars.contentprovider/src/mars/contentprovider/tests/test_all.py 2007-07-13 11:38:53 UTC (rev 77855)
+++ Sandbox/darrylcousins/mars.contentprovider/src/mars/contentprovider/ftests/test_all.py 2007-07-16 05:22:27 UTC (rev 78014)
@@ -1,22 +1,30 @@
import unittest
-from pkg_resources import resource_listdir
+from zope.testing import doctest
-from grok.ftests.test_grok_functional import FunctionalDocTestSuite
-
+from zope.app.testing.functional import FunctionalTestSetup
from zope.app.testing import functional
functional.defineLayer('TestLayer', 'ftesting.zcml')
+optionflags = doctest.NORMALIZE_WHITESPACE + doctest.ELLIPSIS
+
+def setUp(test):
+ FunctionalTestSetup().setUp()
+
+def tearDown(test):
+ FunctionalTestSetup().tearDown()
+
def test_suite():
suite = unittest.TestSuite()
- dottedname = 'mars.contentprovider.tests.%s'
- for name in ['contentprovider']:
- test = FunctionalDocTestSuite(dottedname % name)
- test.layer = TestLayer
- suite.addTest(test)
-
+ dottedname = 'mars.contentprovider.ftests.contentprovider'
+ test = doctest.DocTestSuite(
+ dottedname, setUp=setUp,
+ tearDown=tearDown, optionflags=optionflags)
+ test.layer = TestLayer
+ suite.addTest(test)
return suite
+
if __name__ == '__main__':
unittest.main(defaultTest='test_suite')
Added: Sandbox/darrylcousins/mars.contentprovider/src/mars/contentprovider/tests.py
===================================================================
--- Sandbox/darrylcousins/mars.contentprovider/src/mars/contentprovider/tests.py (rev 0)
+++ Sandbox/darrylcousins/mars.contentprovider/src/mars/contentprovider/tests.py 2007-07-16 05:22:27 UTC (rev 78014)
@@ -0,0 +1,25 @@
+import unittest
+from zope.testing import doctest
+
+optionflags = doctest.NORMALIZE_WHITESPACE + doctest.ELLIPSIS
+
+def setUp(test):
+ from zope.app.pagetemplate import metaconfigure
+ from zope.contentprovider import tales
+ metaconfigure.registerType('provider', tales.TALESProviderExpression)
+
+def test_suite():
+ suite = unittest.TestSuite()
+ suite.addTests([doctest.DocFileSuite('./contentprovider.txt',
+ setUp=setUp,
+ optionflags=optionflags),
+ ])
+
+ return suite
+
+
+
+if __name__ == '__main__':
+ unittest.main(defaultTest='test_suite')
+
+
Property changes on: Sandbox/darrylcousins/mars.contentprovider/src/mars/contentprovider/tests.py
___________________________________________________________________
Name: svn:keywords
+ Id
More information about the Checkins
mailing list