1 | """Testing support for :mod:`waeup.sirp`. |
---|
2 | """ |
---|
3 | import os.path |
---|
4 | import grok |
---|
5 | import zope.component |
---|
6 | import waeup.sirp |
---|
7 | from zope.app.testing.functional import ZCMLLayer |
---|
8 | from zope.component import getGlobalSiteManager |
---|
9 | |
---|
10 | ftesting_zcml = os.path.join( |
---|
11 | os.path.dirname(waeup.sirp.__file__), 'ftesting.zcml') |
---|
12 | FunctionalLayer = ZCMLLayer(ftesting_zcml, __name__, 'FunctionalLayer', |
---|
13 | allow_teardown=True) |
---|
14 | |
---|
15 | class WAeUPSIRPUnitTestLayer(object): |
---|
16 | """A layer for doctests that groks `waeup.sirp`. |
---|
17 | """ |
---|
18 | @classmethod |
---|
19 | def setUp(self): |
---|
20 | import grok |
---|
21 | grok.testing.grok('waeup.sirp') |
---|
22 | |
---|
23 | @classmethod |
---|
24 | def tearDown(self): |
---|
25 | pass |
---|
26 | |
---|
27 | |
---|
28 | def setUpZope(test=None): |
---|
29 | """Initialize a Zope-compatible environment. |
---|
30 | |
---|
31 | Currently, we only initialize the event machinery. |
---|
32 | """ |
---|
33 | zope.component.eventtesting.setUp(test) |
---|
34 | |
---|
35 | def cleanUpZope(test=None): |
---|
36 | """Clean up Zope-related registrations. |
---|
37 | |
---|
38 | Cleans up all registrations and the like. |
---|
39 | """ |
---|
40 | cleanUp() |
---|
41 | |
---|
42 | def maybe_grok(): |
---|
43 | """Try to grok the :mod:`waeup.sirp` package. |
---|
44 | |
---|
45 | For many tests, even simple ones, we want the components defined |
---|
46 | somewhere in the :mod:`waeup.sirp` package being registered. While |
---|
47 | grokking the complete package can become expensive when done many |
---|
48 | times, we only want to grok if it did not happen |
---|
49 | before. Furthermore regrokking the whole package makes no sense if |
---|
50 | done already. |
---|
51 | |
---|
52 | :func:`maybe_grok` checks whether any eventhandlers are already |
---|
53 | registered and does nothing in that case. |
---|
54 | |
---|
55 | The grokking of :mod:`waeup.sirp` is done with warnings disabled. |
---|
56 | |
---|
57 | Returns ``True`` if grokking was done, ``False`` else. |
---|
58 | |
---|
59 | Sample |
---|
60 | ****** |
---|
61 | |
---|
62 | Together with the :func:`setUpZope` and :func:`cleanUpZope` |
---|
63 | functions we then can do unittests with all components registered |
---|
64 | like this:: |
---|
65 | |
---|
66 | import unittest2 as unittest # Want Python 2.7 features |
---|
67 | from waeup.sirp.testing import ( |
---|
68 | maybe_grok, setUpZope, cleanUpZope, |
---|
69 | ) |
---|
70 | from waeup.sirp.app import University |
---|
71 | |
---|
72 | class MyTestCase(unittest.TestCase): |
---|
73 | |
---|
74 | @classmethod |
---|
75 | def setUpClass(cls): |
---|
76 | grokked = maybe_grok() |
---|
77 | if grokked: |
---|
78 | setUpZope(None) |
---|
79 | return |
---|
80 | |
---|
81 | @classmethod |
---|
82 | def tearDownClass(cls): |
---|
83 | cleanUpZope(None) |
---|
84 | |
---|
85 | def setUp(self): |
---|
86 | pass |
---|
87 | |
---|
88 | def tearDown(self): |
---|
89 | pass |
---|
90 | |
---|
91 | def test_jambdata_in_site(self): |
---|
92 | u = University() |
---|
93 | self.assertTrue('jambdata' in u.keys()) |
---|
94 | return |
---|
95 | |
---|
96 | Here the component registration is done only once for the whole |
---|
97 | :class:`unittest.TestCase` and no ZODB is needed. That means |
---|
98 | inside the tests you can expect to have all :mod:`waeup.sirp` |
---|
99 | components (utilities, adapter, events) registered but as objects |
---|
100 | have here still have no place inside a ZODB things like 'browsing' |
---|
101 | won't work out of the box. The benefit is the faster test |
---|
102 | setup/teardown. |
---|
103 | """ |
---|
104 | gsm = getGlobalSiteManager() |
---|
105 | # If there are any event handlers registered already, we assume |
---|
106 | # that waeup.sirp was grokked already. There might be a batter |
---|
107 | # check, though. |
---|
108 | if len(list(gsm.registeredHandlers())) > 0: |
---|
109 | return False |
---|
110 | warnings.simplefilter('ignore') # disable (erraneous) warnings |
---|
111 | grok.testing.grok('waeup.sirp') |
---|
112 | warnings.simplefilter('default') # reenable warnings |
---|
113 | return True |
---|