author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Tue, 18 Aug 2009 17:52:08 +0200 | |
branch | stable |
changeset 2907 | e222fac3cca3 |
parent 1977 | 606923dff11b |
child 2773 | b2530e3e0afb |
permissions | -rw-r--r-- |
0 | 1 |
"""unit tests for the hooks manager |
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
2 |
:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses |
0 | 3 |
""" |
4 |
||
5 |
from logilab.common.testlib import TestCase, unittest_main |
|
6 |
||
7 |
from cubicweb.server.hooksmanager import HooksManager, Hook |
|
8 |
from cubicweb.devtools import TestServerConfiguration |
|
9 |
from cubicweb.devtools.apptest import RepositoryBasedTC |
|
10 |
||
11 |
class HookCalled(Exception): pass |
|
12 |
||
13 |
config = TestServerConfiguration('data') |
|
14 |
config.bootstrap_cubes() |
|
15 |
schema = config.load_schema() |
|
16 |
||
17 |
class HooksManagerTC(TestCase): |
|
18 |
args = (None,) |
|
19 |
kwargs = {'a': 1} |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
20 |
|
0 | 21 |
def setUp(self): |
22 |
""" called before each test from this class """ |
|
23 |
self.o = HooksManager(schema) |
|
24 |
||
25 |
def test_register_hook_raise_keyerror(self): |
|
26 |
self.assertRaises(AssertionError, |
|
27 |
self.o.register_hook, self._hook, 'before_add_entiti') |
|
28 |
self.assertRaises(AssertionError, |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
29 |
self.o.register_hook, self._hook, 'session_login', 'CWEType') |
0 | 30 |
self.assertRaises(AssertionError, |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
31 |
self.o.register_hook, self._hook, 'session_logout', 'CWEType') |
0 | 32 |
self.assertRaises(AssertionError, |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
33 |
self.o.register_hook, self._hook, 'server_startup', 'CWEType') |
0 | 34 |
self.assertRaises(AssertionError, |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
35 |
self.o.register_hook, self._hook, 'server_shutdown', 'CWEType') |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
36 |
|
0 | 37 |
def test_register_hook1(self): |
38 |
self.o.register_hook(self._hook, 'before_add_entity') |
|
39 |
self.o.register_hook(self._hook, 'before_delete_entity', 'Personne') |
|
40 |
self._test_called_hooks() |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
41 |
|
0 | 42 |
def test_register_hook2(self): |
43 |
self.o.register_hook(self._hook, 'before_add_entity', '') |
|
44 |
self.o.register_hook(self._hook, 'before_delete_entity', 'Personne') |
|
45 |
self._test_called_hooks() |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
46 |
|
0 | 47 |
def test_register_hook3(self): |
48 |
self.o.register_hook(self._hook, 'before_add_entity', None) |
|
49 |
self.o.register_hook(self._hook, 'before_delete_entity', 'Personne') |
|
50 |
self._test_called_hooks() |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
51 |
|
0 | 52 |
def test_register_hooks(self): |
53 |
self.o.register_hooks({'before_add_entity' : {'': [self._hook]}, |
|
54 |
'before_delete_entity' : {'Personne': [self._hook]}, |
|
55 |
}) |
|
56 |
self._test_called_hooks() |
|
57 |
||
58 |
def test_unregister_hook(self): |
|
59 |
self.o.register_hook(self._hook, 'after_delete_entity', 'Personne') |
|
60 |
self.assertRaises(HookCalled, |
|
61 |
self.o.call_hooks, 'after_delete_entity', 'Personne', |
|
62 |
*self.args, **self.kwargs) |
|
63 |
self.o.unregister_hook(self._hook, 'after_delete_entity', 'Personne') |
|
64 |
# no hook should be called there |
|
65 |
self.o.call_hooks('after_delete_entity', 'Personne') |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
66 |
|
0 | 67 |
|
68 |
def _test_called_hooks(self): |
|
69 |
self.assertRaises(HookCalled, |
|
70 |
self.o.call_hooks, 'before_add_entity', '', |
|
71 |
*self.args, **self.kwargs) |
|
72 |
self.assertRaises(HookCalled, |
|
73 |
self.o.call_hooks, 'before_add_entity', None, |
|
74 |
*self.args, **self.kwargs) |
|
75 |
self.assertRaises(HookCalled, |
|
76 |
self.o.call_hooks, 'before_add_entity', 'Personne', |
|
77 |
*self.args, **self.kwargs) |
|
78 |
self.assertRaises(HookCalled, |
|
79 |
self.o.call_hooks, 'before_delete_entity', 'Personne', |
|
80 |
*self.args, **self.kwargs) |
|
81 |
# no hook should be called there |
|
82 |
self.o.call_hooks('before_delete_entity', None) |
|
83 |
self.o.call_hooks('before_delete_entity', 'Societe') |
|
84 |
||
85 |
||
86 |
def _hook(self, *args, **kwargs): |
|
87 |
# check arguments |
|
88 |
self.assertEqual(args, self.args) |
|
89 |
self.assertEqual(kwargs, self.kwargs) |
|
90 |
raise HookCalled() |
|
91 |
||
92 |
||
93 |
class RelationHookTC(TestCase): |
|
94 |
"""testcase for relation hooks grouping""" |
|
95 |
def setUp(self): |
|
96 |
""" called before each test from this class """ |
|
97 |
self.o = HooksManager(schema) |
|
98 |
self.called = [] |
|
99 |
||
100 |
def test_before_add_relation(self): |
|
101 |
"""make sure before_xxx_relation hooks are called directly""" |
|
102 |
self.o.register_hook(self._before_relation_hook, |
|
103 |
'before_add_relation', 'concerne') |
|
104 |
self.assertEquals(self.called, []) |
|
105 |
self.o.call_hooks('before_add_relation', 'concerne', 'USER', |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
106 |
1, 'concerne', 2) |
0 | 107 |
self.assertEquals(self.called, [(1, 'concerne', 2)]) |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
108 |
|
0 | 109 |
def test_after_add_relation(self): |
110 |
"""make sure after_xxx_relation hooks are deferred""" |
|
111 |
self.o.register_hook(self._after_relation_hook, |
|
112 |
'after_add_relation', 'concerne') |
|
113 |
self.assertEquals(self.called, []) |
|
114 |
self.o.call_hooks('after_add_relation', 'concerne', 'USER', |
|
115 |
1, 'concerne', 2) |
|
116 |
self.o.call_hooks('after_add_relation', 'concerne', 'USER', |
|
117 |
3, 'concerne', 4) |
|
118 |
self.assertEquals(self.called, [(1, 'concerne', 2), (3, 'concerne', 4)]) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
119 |
|
0 | 120 |
def test_before_delete_relation(self): |
121 |
"""make sure before_xxx_relation hooks are called directly""" |
|
122 |
self.o.register_hook(self._before_relation_hook, |
|
123 |
'before_delete_relation', 'concerne') |
|
124 |
self.assertEquals(self.called, []) |
|
125 |
self.o.call_hooks('before_delete_relation', 'concerne', 'USER', |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
126 |
1, 'concerne', 2) |
0 | 127 |
self.assertEquals(self.called, [(1, 'concerne', 2)]) |
128 |
||
129 |
def test_after_delete_relation(self): |
|
130 |
"""make sure after_xxx_relation hooks are deferred""" |
|
131 |
self.o.register_hook(self._after_relation_hook, |
|
132 |
'after_delete_relation', 'concerne') |
|
133 |
self.o.call_hooks('after_delete_relation', 'concerne', 'USER', |
|
134 |
1, 'concerne', 2) |
|
135 |
self.o.call_hooks('after_delete_relation', 'concerne', 'USER', |
|
136 |
3, 'concerne', 4) |
|
137 |
self.assertEquals(self.called, [(1, 'concerne', 2), (3, 'concerne', 4)]) |
|
138 |
||
139 |
||
140 |
def _before_relation_hook(self, pool, subject, r_type, object): |
|
141 |
self.called.append((subject, r_type, object)) |
|
142 |
||
143 |
def _after_relation_hook(self, pool, subject, r_type, object): |
|
144 |
self.called.append((subject, r_type, object)) |
|
145 |
||
146 |
||
147 |
class SystemHooksTC(RepositoryBasedTC): |
|
148 |
||
149 |
def test_startup_shutdown(self): |
|
150 |
import hooks # cubicweb/server/test/data/hooks.py |
|
151 |
self.assertEquals(hooks.CALLED_EVENTS['server_startup'], True) |
|
152 |
# don't actually call repository.shutdown ! |
|
153 |
self.repo.hm.call_hooks('server_shutdown', repo=None) |
|
154 |
self.assertEquals(hooks.CALLED_EVENTS['server_shutdown'], True) |
|
155 |
||
156 |
def test_session_open_close(self): |
|
157 |
import hooks # cubicweb/server/test/data/hooks.py |
|
158 |
cnx = self.login('anon') |
|
159 |
self.assertEquals(hooks.CALLED_EVENTS['session_open'], 'anon') |
|
160 |
cnx.close() |
|
161 |
self.assertEquals(hooks.CALLED_EVENTS['session_close'], 'anon') |
|
162 |
||
163 |
||
164 |
from itertools import repeat |
|
165 |
||
166 |
class MyHook(Hook): |
|
167 |
schema = schema # set for actual hooks at registration time |
|
168 |
events = ('whatever', 'another') |
|
169 |
accepts = ('Societe', 'Division') |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
170 |
|
0 | 171 |
class HookTC(RepositoryBasedTC): |
172 |
def test_inheritance(self): |
|
173 |
self.assertEquals(list(MyHook.register_to()), |
|
174 |
zip(repeat('whatever'), ('Societe', 'Division', 'SubDivision')) |
|
175 |
+ zip(repeat('another'), ('Societe', 'Division', 'SubDivision'))) |
|
176 |
||
177 |
||
178 |
if __name__ == '__main__': |
|
179 |
unittest_main() |