[5780] | 1 | """Testing support for :mod:`waeup.sirp`. |
---|
| 2 | """ |
---|
[6238] | 3 | import grok |
---|
| 4 | import doctest |
---|
[3521] | 5 | import os.path |
---|
[6238] | 6 | import re |
---|
[5796] | 7 | import warnings |
---|
[5780] | 8 | import zope.component |
---|
[4920] | 9 | import waeup.sirp |
---|
[6238] | 10 | from zope.app.testing.functional import ( |
---|
| 11 | ZCMLLayer, FunctionalTestSetup, getRootFolder, sync) |
---|
[5780] | 12 | from zope.component import getGlobalSiteManager |
---|
[5865] | 13 | from zope.security.testing import addCheckerPublic |
---|
[6238] | 14 | from zope.testing import renormalizing |
---|
[5796] | 15 | from zope.testing.cleanup import cleanUp |
---|
[3521] | 16 | |
---|
| 17 | ftesting_zcml = os.path.join( |
---|
[4920] | 18 | os.path.dirname(waeup.sirp.__file__), 'ftesting.zcml') |
---|
[4789] | 19 | FunctionalLayer = ZCMLLayer(ftesting_zcml, __name__, 'FunctionalLayer', |
---|
| 20 | allow_teardown=True) |
---|
[5139] | 21 | |
---|
[5780] | 22 | def setUpZope(test=None): |
---|
| 23 | """Initialize a Zope-compatible environment. |
---|
| 24 | |
---|
| 25 | Currently, we only initialize the event machinery. |
---|
| 26 | """ |
---|
| 27 | zope.component.eventtesting.setUp(test) |
---|
| 28 | |
---|
| 29 | def cleanUpZope(test=None): |
---|
| 30 | """Clean up Zope-related registrations. |
---|
| 31 | |
---|
| 32 | Cleans up all registrations and the like. |
---|
| 33 | """ |
---|
| 34 | cleanUp() |
---|
| 35 | |
---|
| 36 | def maybe_grok(): |
---|
| 37 | """Try to grok the :mod:`waeup.sirp` package. |
---|
| 38 | |
---|
| 39 | For many tests, even simple ones, we want the components defined |
---|
| 40 | somewhere in the :mod:`waeup.sirp` package being registered. While |
---|
| 41 | grokking the complete package can become expensive when done many |
---|
| 42 | times, we only want to grok if it did not happen |
---|
| 43 | before. Furthermore regrokking the whole package makes no sense if |
---|
| 44 | done already. |
---|
| 45 | |
---|
| 46 | :func:`maybe_grok` checks whether any eventhandlers are already |
---|
| 47 | registered and does nothing in that case. |
---|
| 48 | |
---|
| 49 | The grokking of :mod:`waeup.sirp` is done with warnings disabled. |
---|
| 50 | |
---|
| 51 | Returns ``True`` if grokking was done, ``False`` else. |
---|
| 52 | |
---|
[5796] | 53 | .. The following samples should go into Sphinx docs directly.... |
---|
[6237] | 54 | |
---|
[5780] | 55 | Sample |
---|
| 56 | ****** |
---|
[5796] | 57 | |
---|
| 58 | Usage with plain Python testrunners |
---|
| 59 | ----------------------------------- |
---|
[6237] | 60 | |
---|
[5780] | 61 | Together with the :func:`setUpZope` and :func:`cleanUpZope` |
---|
| 62 | functions we then can do unittests with all components registered |
---|
[5796] | 63 | and the event dispatcher in place like this:: |
---|
[5780] | 64 | |
---|
| 65 | import unittest2 as unittest # Want Python 2.7 features |
---|
| 66 | from waeup.sirp.testing import ( |
---|
| 67 | maybe_grok, setUpZope, cleanUpZope, |
---|
| 68 | ) |
---|
| 69 | from waeup.sirp.app import University |
---|
| 70 | |
---|
| 71 | class MyTestCase(unittest.TestCase): |
---|
| 72 | |
---|
| 73 | @classmethod |
---|
| 74 | def setUpClass(cls): |
---|
| 75 | grokked = maybe_grok() |
---|
| 76 | if grokked: |
---|
| 77 | setUpZope(None) |
---|
| 78 | return |
---|
| 79 | |
---|
| 80 | @classmethod |
---|
| 81 | def tearDownClass(cls): |
---|
| 82 | cleanUpZope(None) |
---|
| 83 | |
---|
| 84 | def setUp(self): |
---|
| 85 | pass |
---|
[6237] | 86 | |
---|
[5780] | 87 | def tearDown(self): |
---|
| 88 | pass |
---|
| 89 | |
---|
| 90 | def test_jambdata_in_site(self): |
---|
| 91 | u = University() |
---|
| 92 | self.assertTrue('jambdata' in u.keys()) |
---|
| 93 | return |
---|
| 94 | |
---|
| 95 | Here the component registration is done only once for the whole |
---|
| 96 | :class:`unittest.TestCase` and no ZODB is needed. That means |
---|
| 97 | inside the tests you can expect to have all :mod:`waeup.sirp` |
---|
| 98 | components (utilities, adapter, events) registered but as objects |
---|
| 99 | have here still have no place inside a ZODB things like 'browsing' |
---|
| 100 | won't work out of the box. The benefit is the faster test |
---|
| 101 | setup/teardown. |
---|
[5796] | 102 | |
---|
| 103 | .. note:: This works only with the default Python testrunners. |
---|
[6237] | 104 | |
---|
[5796] | 105 | If you use the Zope testrunner (from :mod:`zope.testing`) |
---|
| 106 | then you have to use appropriate layers like the |
---|
| 107 | :class:`waeup.sirp.testing.WAeUPSIRPUnitTestLayer`. |
---|
| 108 | |
---|
| 109 | Usage with :mod:`zope.testing` testrunners |
---|
| 110 | ------------------------------------------ |
---|
| 111 | |
---|
| 112 | If you use the standard Zope testrunner, classmethods like |
---|
| 113 | `setUpClass` are not executed. Instead you have to use a layer |
---|
| 114 | like the one defined in this module. |
---|
| 115 | |
---|
| 116 | .. seealso:: :class:`waeup.sirp.testing.WAeUPSIRPUnitTestLayer` |
---|
[6237] | 117 | |
---|
[5780] | 118 | """ |
---|
| 119 | gsm = getGlobalSiteManager() |
---|
| 120 | # If there are any event handlers registered already, we assume |
---|
| 121 | # that waeup.sirp was grokked already. There might be a batter |
---|
| 122 | # check, though. |
---|
| 123 | if len(list(gsm.registeredHandlers())) > 0: |
---|
| 124 | return False |
---|
[5865] | 125 | # Register the zope.Public permission, normally done via ZCML setup. |
---|
| 126 | addCheckerPublic() |
---|
[5780] | 127 | warnings.simplefilter('ignore') # disable (erraneous) warnings |
---|
| 128 | grok.testing.grok('waeup.sirp') |
---|
| 129 | warnings.simplefilter('default') # reenable warnings |
---|
| 130 | return True |
---|
[5796] | 131 | |
---|
| 132 | |
---|
| 133 | class WAeUPSIRPUnitTestLayer(object): |
---|
| 134 | """A layer for tests that groks `waeup.sirp`. |
---|
| 135 | |
---|
| 136 | A Zope test layer that registers all :mod:`waeup.sirp` components |
---|
| 137 | before attached tests are run and cleans this registrations up |
---|
| 138 | afterwards. Also basic (non-waeup.sirp) components like the event |
---|
| 139 | dispatcher machinery are registered, set up and cleaned up. |
---|
| 140 | |
---|
| 141 | This layer does not provide a complete ZODB setup (and is |
---|
| 142 | therefore much faster than complete functional setups) but does |
---|
| 143 | only the registrations (which also takes some time, so running |
---|
| 144 | this layer is slower than test cases that need none or only a |
---|
| 145 | few registrations). |
---|
| 146 | |
---|
| 147 | The registrations are done for all tests the layer is attached to |
---|
| 148 | once before all these tests are run (and torn down once |
---|
| 149 | afterwards). |
---|
[6237] | 150 | |
---|
[5796] | 151 | To make use of this layer, you have to write a |
---|
| 152 | :mod:`unittest.TestCase` class that provides an attribute called |
---|
| 153 | ``layer`` with this class as value like this:: |
---|
| 154 | |
---|
| 155 | import unittest |
---|
| 156 | from waeup.sirp.testing import WAeUPSIRPUnitTestLayer |
---|
[6237] | 157 | |
---|
[5796] | 158 | class MyTestCase(unittest.TestCase): |
---|
| 159 | |
---|
| 160 | layer = WAeUPSIRPUnitTestLayer |
---|
| 161 | |
---|
| 162 | # per-test setups and real tests go here... |
---|
| 163 | def test_foo(self): |
---|
| 164 | self.assertEqual(1, 1) |
---|
| 165 | return |
---|
| 166 | |
---|
| 167 | """ |
---|
| 168 | @classmethod |
---|
| 169 | def setUp(cls): |
---|
[5865] | 170 | #setUpZope(None) |
---|
[5796] | 171 | grokked = maybe_grok() |
---|
| 172 | if grokked: |
---|
[5865] | 173 | pass |
---|
| 174 | #setUpZope(None) |
---|
[5796] | 175 | return |
---|
| 176 | |
---|
| 177 | @classmethod |
---|
| 178 | def tearDown(cls): |
---|
| 179 | cleanUpZope(None) |
---|
[6238] | 180 | |
---|
| 181 | checker = renormalizing.RENormalizing([ |
---|
| 182 | # Relevant normalizers from zope.testing.testrunner.tests: |
---|
| 183 | (re.compile(r'\d+[.]\d\d\d seconds'), 'N.NNN seconds'), |
---|
| 184 | # Our own one to work around |
---|
| 185 | # http://reinout.vanrees.org/weblog/2009/07/16/invisible-test-diff.html: |
---|
| 186 | (re.compile(r'.*1034h'), ''), |
---|
| 187 | (re.compile(r'httperror_seek_wrapper:'), 'HTTPError:' ) |
---|
| 188 | ]) |
---|
| 189 | |
---|
| 190 | def setUp(test): |
---|
| 191 | FunctionalTestSetup().setUp() |
---|
| 192 | |
---|
| 193 | def tearDown(test): |
---|
| 194 | FunctionalTestSetup().tearDown() |
---|
| 195 | |
---|
| 196 | def doctestsuite_for_module(dotted_path): |
---|
| 197 | """Create a doctest suite for the module at `dotted_path`. |
---|
| 198 | """ |
---|
| 199 | test = doctest.DocTestSuite( |
---|
| 200 | dotted_path, |
---|
| 201 | setUp = setUp, |
---|
| 202 | tearDown = tearDown, |
---|
| 203 | checker = checker, |
---|
| 204 | extraglobs = dict( |
---|
| 205 | getRootFolder=getRootFolder, |
---|
| 206 | sync=sync,), |
---|
| 207 | optionflags = (doctest.ELLIPSIS + |
---|
| 208 | doctest.NORMALIZE_WHITESPACE + |
---|
| 209 | doctest.REPORT_NDIFF), |
---|
| 210 | ) |
---|
| 211 | test.layer = FunctionalLayer |
---|
| 212 | return test |
---|