Back to index

plone3  3.1.7
tests.py
Go to the documentation of this file.
00001 import unittest
00002 
00003 import plone.portlets
00004 
00005 from zope.testing import doctest
00006 from zope.component.testing import setUp, tearDown
00007 from zope.configuration.xmlconfig import XMLConfig
00008 
00009 optionflags = doctest.NORMALIZE_WHITESPACE | doctest.ELLIPSIS
00010 
00011 def configurationSetUp(test):
00012     setUp()
00013 
00014     import zope.contentprovider
00015     import zope.security
00016     import zope.app.component
00017     import zope.app.container
00018     import zope.app.pagetemplate
00019     import zope.app.security
00020 
00021     XMLConfig('meta.zcml', zope.security)()
00022     XMLConfig('meta.zcml', zope.app.component)()
00023     XMLConfig('meta.zcml', zope.app.pagetemplate)()
00024     XMLConfig('meta.zcml', zope.app.security)()
00025 
00026     # BBB conditional code for loading the utility dispatchers
00027     # In Zope 2.11 they are in zope.component
00028     try:
00029         import zope.component
00030         XMLConfig('configure.zcml', zope.component)()
00031     except IOError:
00032         pass
00033     # In Zope 2.10 they are in zope.app.event
00034     try:
00035         import zope.app.event
00036         XMLConfig('configure.zcml', zope.app.event)()
00037     except (ImportError, IOError):
00038         pass
00039 
00040     XMLConfig('configure.zcml', zope.app.security)()
00041     XMLConfig('configure.zcml', zope.app.container)()
00042     XMLConfig('configure.zcml', zope.contentprovider)()
00043     
00044     XMLConfig('configure.zcml', plone.portlets)()
00045 
00046 
00047 def configurationTearDown(test):
00048     tearDown()
00049 
00050 def test_safe_render():
00051     r"""
00052     Render the portlet safely, so that when an exception
00053     occurs, we log and don't bail out.
00054 
00055       >>> from plone.portlets.manager import PortletManagerRenderer
00056       >>> class PortletRenderer:
00057       ...     def __init__(self, error=False):
00058       ...         self.error = error
00059       ...     def render(self):
00060       ...         if self.error:
00061       ...             raise Exception()
00062       ...         return 'portlet rendered'
00063 
00064     When no error occurs, ``safe_render`` will return the portlet
00065     renderer's ``render()``:
00066     
00067       >>> renderer = PortletManagerRenderer(*(None,) * 4)
00068       >>> renderer.safe_render(PortletRenderer())
00069       'portlet rendered'
00070 
00071     When an error is raised, the ``error_message`` template is
00072     rendered:
00073     
00074       >>> renderer.error_message = lambda: 'error rendered'
00075       >>> renderer.safe_render(PortletRenderer(error=True))
00076       'error rendered'
00077     """
00078 
00079 def test_suite():
00080     return unittest.TestSuite((
00081         doctest.DocFileSuite(
00082             'README.txt',
00083             setUp=configurationSetUp,
00084             tearDown=configurationTearDown,
00085             optionflags=optionflags),
00086         doctest.DocFileSuite(
00087             'uisupport.txt',
00088             setUp=configurationSetUp,
00089             tearDown=configurationTearDown,
00090             optionflags=optionflags),
00091         doctest.DocFileSuite(
00092             'utils.txt',
00093             setUp=configurationSetUp,
00094             tearDown=configurationTearDown,
00095             optionflags=optionflags),
00096         doctest.DocTestSuite()))
00097 
00098 if __name__ == '__main__':
00099     unittest.main(defaultTest='test_suite')