23 from cubicweb.devtools import TestServerConfiguration, fake |
23 from cubicweb.devtools import TestServerConfiguration, fake |
24 from cubicweb.devtools.testlib import CubicWebTC |
24 from cubicweb.devtools.testlib import CubicWebTC |
25 from cubicweb.server import hook |
25 from cubicweb.server import hook |
26 from cubicweb.hooks import integrity, syncschema |
26 from cubicweb.hooks import integrity, syncschema |
27 |
27 |
28 def clean_session_ops(func): |
|
29 def wrapper(self, *args, **kwargs): |
|
30 try: |
|
31 return func(self, *args, **kwargs) |
|
32 finally: |
|
33 self.session.pending_operations[:] = [] |
|
34 return wrapper |
|
35 |
|
36 class OperationsTC(CubicWebTC): |
28 class OperationsTC(CubicWebTC): |
37 |
29 |
38 def setUp(self): |
30 def setUp(self): |
39 CubicWebTC.setUp(self) |
31 CubicWebTC.setUp(self) |
40 self.hm = self.repo.hm |
32 self.hm = self.repo.hm |
41 |
33 |
42 @clean_session_ops |
|
43 def test_late_operation(self): |
34 def test_late_operation(self): |
44 session = self.session |
35 with self.admin_access.repo_cnx() as cnx: |
45 l1 = hook.LateOperation(session) |
36 l1 = hook.LateOperation(cnx) |
46 l2 = hook.LateOperation(session) |
37 l2 = hook.LateOperation(cnx) |
47 l3 = hook.Operation(session) |
38 l3 = hook.Operation(cnx) |
48 self.assertEqual(session.pending_operations, [l3, l1, l2]) |
39 self.assertEqual(cnx.pending_operations, [l3, l1, l2]) |
49 |
40 |
50 @clean_session_ops |
|
51 def test_single_last_operation(self): |
41 def test_single_last_operation(self): |
52 session = self.session |
42 with self.admin_access.repo_cnx() as cnx: |
53 l0 = hook.SingleLastOperation(session) |
43 l0 = hook.SingleLastOperation(cnx) |
54 l1 = hook.LateOperation(session) |
44 l1 = hook.LateOperation(cnx) |
55 l2 = hook.LateOperation(session) |
45 l2 = hook.LateOperation(cnx) |
56 l3 = hook.Operation(session) |
46 l3 = hook.Operation(cnx) |
57 self.assertEqual(session.pending_operations, [l3, l1, l2, l0]) |
47 self.assertEqual(cnx.pending_operations, [l3, l1, l2, l0]) |
58 l4 = hook.SingleLastOperation(session) |
48 l4 = hook.SingleLastOperation(cnx) |
59 self.assertEqual(session.pending_operations, [l3, l1, l2, l4]) |
49 self.assertEqual(cnx.pending_operations, [l3, l1, l2, l4]) |
60 |
50 |
61 @clean_session_ops |
|
62 def test_global_operation_order(self): |
51 def test_global_operation_order(self): |
63 session = self.session |
52 with self.admin_access.repo_cnx() as cnx: |
64 op1 = syncschema.RDefDelOp(session) |
53 op1 = syncschema.RDefDelOp(cnx) |
65 op2 = integrity._CheckORelationOp(session) |
54 op2 = integrity._CheckORelationOp(cnx) |
66 op3 = syncschema.MemSchemaNotifyChanges(session) |
55 op3 = syncschema.MemSchemaNotifyChanges(cnx) |
67 self.assertEqual([op1, op2, op3], session.pending_operations) |
56 self.assertEqual([op1, op2, op3], cnx.pending_operations) |
68 |
57 |
69 class HookCalled(Exception): pass |
58 class HookCalled(Exception): pass |
70 |
59 |
71 config = TestServerConfiguration('data') |
60 config = TestServerConfiguration('data') |
72 config.bootstrap_cubes() |
61 config.bootstrap_cubes() |
137 self.repo.hm.call_hooks('server_shutdown', repo=self.repo) |
126 self.repo.hm.call_hooks('server_shutdown', repo=self.repo) |
138 self.assertEqual(hooks.CALLED_EVENTS['server_shutdown'], True) |
127 self.assertEqual(hooks.CALLED_EVENTS['server_shutdown'], True) |
139 |
128 |
140 def test_session_open_close(self): |
129 def test_session_open_close(self): |
141 import hooks # cubicweb/server/test/data/hooks.py |
130 import hooks # cubicweb/server/test/data/hooks.py |
142 cnx = self.login('anon') |
131 anonaccess = self.new_access('anon') |
143 self.assertEqual(hooks.CALLED_EVENTS['session_open'], 'anon') |
132 with anonaccess.repo_cnx() as cnx: |
144 cnx.close() |
133 self.assertEqual(hooks.CALLED_EVENTS['session_open'], 'anon') |
|
134 anonaccess.close() |
145 self.assertEqual(hooks.CALLED_EVENTS['session_close'], 'anon') |
135 self.assertEqual(hooks.CALLED_EVENTS['session_close'], 'anon') |
146 |
136 |
147 |
137 |
148 # class RelationHookTC(TestCase): |
138 # class RelationHookTC(TestCase): |
149 # """testcase for relation hooks grouping""" |
139 # """testcase for relation hooks grouping""" |