server/test/unittest_hook.py
author Sylvain Thénault <sylvain.thenault@logilab.fr>
Tue, 09 Mar 2010 08:59:43 +0100
changeset 4835 13b0b96d7982
parent 4834 b718626a0e60
child 5029 f7709d28fb79
permissions -rw-r--r--
[repo] enhanced security handling: deprecates unsafe_execute, in favor of explicit read/write security control using the `enabled_security` context manager. Also code executed on the repository side is now unsafe by default.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     1
# -*- coding: utf-8 -*-
3413
5cd27d2cc64f update imports, rename test module
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2968
diff changeset
     2
"""unit/functional tests for cubicweb.server.hook
1977
606923dff11b big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1787
diff changeset
     3
606923dff11b big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1787
diff changeset
     4
:organization: Logilab
4212
ab6573088b4a update copyright: welcome 2010
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2920
diff changeset
     5
:copyright: 2001-2010 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2.
1977
606923dff11b big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1787
diff changeset
     6
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr
606923dff11b big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1787
diff changeset
     7
:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses
606923dff11b big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1787
diff changeset
     8
"""
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     9
3414
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
    10
from logilab.common.testlib import TestCase, unittest_main, mock_object
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
    11
3413
5cd27d2cc64f update imports, rename test module
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2968
diff changeset
    12
3414
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
    13
from cubicweb.devtools import TestServerConfiguration
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2637
diff changeset
    14
from cubicweb.devtools.testlib import CubicWebTC
3414
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
    15
from cubicweb.selectors import implements
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
    16
from cubicweb.server import hook
3413
5cd27d2cc64f update imports, rename test module
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2968
diff changeset
    17
from cubicweb.hooks import integrity, syncschema
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    18
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    19
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2637
diff changeset
    20
def clean_session_ops(func):
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2637
diff changeset
    21
    def wrapper(self, *args, **kwargs):
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2637
diff changeset
    22
        try:
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2637
diff changeset
    23
            return func(self, *args, **kwargs)
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2637
diff changeset
    24
        finally:
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2637
diff changeset
    25
            self.session.pending_operations[:] = []
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2637
diff changeset
    26
    return wrapper
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2637
diff changeset
    27
3414
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
    28
class OperationsTC(CubicWebTC):
1787
71c143c0ada3 fix test
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
    29
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    30
    def setUp(self):
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2637
diff changeset
    31
        CubicWebTC.setUp(self)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    32
        self.hm = self.repo.hm
1787
71c143c0ada3 fix test
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
    33
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2637
diff changeset
    34
    @clean_session_ops
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    35
    def test_late_operation(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    36
        session = self.session
3414
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
    37
        l1 = hook.LateOperation(session)
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
    38
        l2 = hook.LateOperation(session)
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
    39
        l3 = hook.Operation(session)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    40
        self.assertEquals(session.pending_operations, [l3, l1, l2])
1787
71c143c0ada3 fix test
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
    41
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2637
diff changeset
    42
    @clean_session_ops
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    43
    def test_single_last_operation(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    44
        session = self.session
3414
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
    45
        l0 = hook.SingleLastOperation(session)
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
    46
        l1 = hook.LateOperation(session)
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
    47
        l2 = hook.LateOperation(session)
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
    48
        l3 = hook.Operation(session)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    49
        self.assertEquals(session.pending_operations, [l3, l1, l2, l0])
3414
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
    50
        l4 = hook.SingleLastOperation(session)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    51
        self.assertEquals(session.pending_operations, [l3, l1, l2, l4])
1787
71c143c0ada3 fix test
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
    52
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2637
diff changeset
    53
    @clean_session_ops
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    54
    def test_global_operation_order(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    55
        session = self.session
3413
5cd27d2cc64f update imports, rename test module
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2968
diff changeset
    56
        op1 = integrity._DelayedDeleteOp(session)
5cd27d2cc64f update imports, rename test module
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2968
diff changeset
    57
        op2 = syncschema.MemSchemaRDefDel(session)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    58
        # equivalent operation generated by op2 but replace it here by op3 so we
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    59
        # can check the result...
3413
5cd27d2cc64f update imports, rename test module
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2968
diff changeset
    60
        op3 = syncschema.MemSchemaNotifyChanges(session)
5cd27d2cc64f update imports, rename test module
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2968
diff changeset
    61
        op4 = integrity._DelayedDeleteOp(session)
5cd27d2cc64f update imports, rename test module
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2968
diff changeset
    62
        op5 = integrity._CheckORelationOp(session)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    63
        self.assertEquals(session.pending_operations, [op1, op2, op4, op5, op3])
1787
71c143c0ada3 fix test
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
    64
3414
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
    65
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
    66
class HookCalled(Exception): pass
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
    67
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
    68
config = TestServerConfiguration('data')
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
    69
config.bootstrap_cubes()
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
    70
schema = config.load_schema()
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
    71
4766
162b2b127b15 [test] get a chance to get proper garbage collection when running pytest on whole cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
    72
def teardown_module(*args):
162b2b127b15 [test] get a chance to get proper garbage collection when running pytest on whole cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
    73
    global config, schema
162b2b127b15 [test] get a chance to get proper garbage collection when running pytest on whole cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
    74
    del config, schema
162b2b127b15 [test] get a chance to get proper garbage collection when running pytest on whole cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
    75
3414
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
    76
class AddAnyHook(hook.Hook):
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
    77
    __regid__ = 'addany'
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
    78
    category = 'cat1'
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
    79
    events = ('before_add_entity',)
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
    80
    def __call__(self):
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
    81
        raise HookCalled()
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
    82
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
    83
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
    84
class HooksManagerTC(TestCase):
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
    85
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
    86
    def setUp(self):
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
    87
        """ called before each test from this class """
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
    88
        self.vreg = mock_object(config=config, schema=schema)
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
    89
        self.o = hook.HooksRegistry(self.vreg)
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
    90
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
    91
    def test_register_bad_hook1(self):
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
    92
        class _Hook(hook.Hook):
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
    93
            events = ('before_add_entiti',)
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
    94
        ex = self.assertRaises(Exception, self.o.register, _Hook)
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
    95
        self.assertEquals(str(ex), 'bad event before_add_entiti on unittest_hook._Hook')
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
    96
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
    97
    def test_register_bad_hook2(self):
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
    98
        class _Hook(hook.Hook):
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
    99
            events = None
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
   100
        ex = self.assertRaises(Exception, self.o.register, _Hook)
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
   101
        self.assertEquals(str(ex), 'bad .events attribute None on unittest_hook._Hook')
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
   102
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
   103
    def test_register_bad_hook3(self):
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
   104
        class _Hook(hook.Hook):
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
   105
            events = 'before_add_entity'
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
   106
        ex = self.assertRaises(Exception, self.o.register, _Hook)
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
   107
        self.assertEquals(str(ex), 'bad event b on unittest_hook._Hook')
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
   108
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
   109
    def test_call_hook(self):
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
   110
        self.o.register(AddAnyHook)
4834
b718626a0e60 move hooks activation control on session object, so we can have a per transaction control. Added a new `hooks_control` context manager for usual modification of hooks activation.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4766
diff changeset
   111
        dis = set()
b718626a0e60 move hooks activation control on session object, so we can have a per transaction control. Added a new `hooks_control` context manager for usual modification of hooks activation.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4766
diff changeset
   112
        cw = mock_object(vreg=self.vreg,
4835
13b0b96d7982 [repo] enhanced security handling: deprecates unsafe_execute, in favor of explicit read/write security control using the `enabled_security` context manager. Also code executed on the repository side is now unsafe by default.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4834
diff changeset
   113
                         set_read_security=lambda *a,**k: None,
13b0b96d7982 [repo] enhanced security handling: deprecates unsafe_execute, in favor of explicit read/write security control using the `enabled_security` context manager. Also code executed on the repository side is now unsafe by default.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4834
diff changeset
   114
                         set_write_security=lambda *a,**k: None,
4834
b718626a0e60 move hooks activation control on session object, so we can have a per transaction control. Added a new `hooks_control` context manager for usual modification of hooks activation.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4766
diff changeset
   115
                         is_hook_activated=lambda x, cls: cls.category not in dis)
b718626a0e60 move hooks activation control on session object, so we can have a per transaction control. Added a new `hooks_control` context manager for usual modification of hooks activation.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4766
diff changeset
   116
        self.assertRaises(HookCalled,
b718626a0e60 move hooks activation control on session object, so we can have a per transaction control. Added a new `hooks_control` context manager for usual modification of hooks activation.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4766
diff changeset
   117
                          self.o.call_hooks, 'before_add_entity', cw)
3414
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
   118
        self.o.call_hooks('before_delete_entity', cw) # nothing to call
4834
b718626a0e60 move hooks activation control on session object, so we can have a per transaction control. Added a new `hooks_control` context manager for usual modification of hooks activation.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4766
diff changeset
   119
        dis.add('cat1')
3414
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
   120
        self.o.call_hooks('before_add_entity', cw) # disabled hooks category, not called
4834
b718626a0e60 move hooks activation control on session object, so we can have a per transaction control. Added a new `hooks_control` context manager for usual modification of hooks activation.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4766
diff changeset
   121
        dis.remove('cat1')
b718626a0e60 move hooks activation control on session object, so we can have a per transaction control. Added a new `hooks_control` context manager for usual modification of hooks activation.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4766
diff changeset
   122
        self.assertRaises(HookCalled,
b718626a0e60 move hooks activation control on session object, so we can have a per transaction control. Added a new `hooks_control` context manager for usual modification of hooks activation.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4766
diff changeset
   123
                          self.o.call_hooks, 'before_add_entity', cw)
3414
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
   124
        self.o.unregister(AddAnyHook)
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
   125
        self.o.call_hooks('before_add_entity', cw) # nothing to call
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
   126
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
   127
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
   128
class SystemHooksTC(CubicWebTC):
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
   129
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
   130
    def test_startup_shutdown(self):
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
   131
        import hooks # cubicweb/server/test/data/hooks.py
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
   132
        self.assertEquals(hooks.CALLED_EVENTS['server_startup'], True)
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
   133
        # don't actually call repository.shutdown !
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
   134
        self.repo.hm.call_hooks('server_shutdown', repo=self.repo)
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
   135
        self.assertEquals(hooks.CALLED_EVENTS['server_shutdown'], True)
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
   136
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
   137
    def test_session_open_close(self):
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
   138
        import hooks # cubicweb/server/test/data/hooks.py
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
   139
        cnx = self.login('anon')
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
   140
        self.assertEquals(hooks.CALLED_EVENTS['session_open'], 'anon')
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
   141
        cnx.close()
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
   142
        self.assertEquals(hooks.CALLED_EVENTS['session_close'], 'anon')
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
   143
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
   144
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
   145
# class RelationHookTC(TestCase):
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
   146
#     """testcase for relation hooks grouping"""
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
   147
#     def setUp(self):
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
   148
#         """ called before each test from this class """
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
   149
#         self.o = HooksManager(schema)
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
   150
#         self.called = []
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
   151
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
   152
#     def test_before_add_relation(self):
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
   153
#         """make sure before_xxx_relation hooks are called directly"""
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
   154
#         self.o.register(self._before_relation_hook,
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
   155
#                              'before_add_relation', 'concerne')
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
   156
#         self.assertEquals(self.called, [])
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
   157
#         self.o.call_hooks('before_add_relation', 'concerne', 'USER',
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
   158
#                           1, 'concerne', 2)
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
   159
#         self.assertEquals(self.called, [(1, 'concerne', 2)])
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
   160
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
   161
#     def test_after_add_relation(self):
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
   162
#         """make sure after_xxx_relation hooks are deferred"""
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
   163
#         self.o.register(self._after_relation_hook,
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
   164
#                              'after_add_relation', 'concerne')
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
   165
#         self.assertEquals(self.called, [])
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
   166
#         self.o.call_hooks('after_add_relation', 'concerne', 'USER',
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
   167
#                           1, 'concerne', 2)
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
   168
#         self.o.call_hooks('after_add_relation', 'concerne', 'USER',
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
   169
#                           3, 'concerne', 4)
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
   170
#         self.assertEquals(self.called, [(1, 'concerne', 2), (3, 'concerne', 4)])
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
   171
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
   172
#     def test_before_delete_relation(self):
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
   173
#         """make sure before_xxx_relation hooks are called directly"""
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
   174
#         self.o.register(self._before_relation_hook,
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
   175
#                              'before_delete_relation', 'concerne')
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
   176
#         self.assertEquals(self.called, [])
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
   177
#         self.o.call_hooks('before_delete_relation', 'concerne', 'USER',
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
   178
#                           1, 'concerne', 2)
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
   179
#         self.assertEquals(self.called, [(1, 'concerne', 2)])
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
   180
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
   181
#     def test_after_delete_relation(self):
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
   182
#         """make sure after_xxx_relation hooks are deferred"""
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
   183
#         self.o.register(self._after_relation_hook,
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
   184
#                         'after_delete_relation', 'concerne')
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
   185
#         self.o.call_hooks('after_delete_relation', 'concerne', 'USER',
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
   186
#                           1, 'concerne', 2)
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
   187
#         self.o.call_hooks('after_delete_relation', 'concerne', 'USER',
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
   188
#                           3, 'concerne', 4)
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
   189
#         self.assertEquals(self.called, [(1, 'concerne', 2), (3, 'concerne', 4)])
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
   190
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
   191
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
   192
#     def _before_relation_hook(self, pool, subject, r_type, object):
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
   193
#         self.called.append((subject, r_type, object))
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
   194
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
   195
#     def _after_relation_hook(self, pool, subject, r_type, object):
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
   196
#         self.called.append((subject, r_type, object))
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
   197
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
   198
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   199
if __name__ == '__main__':
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   200
    unittest_main()