server/test/unittest_hook.py
author Sylvain Thénault <sylvain.thenault@logilab.fr>
Wed, 28 Apr 2010 12:15:52 +0200
brancholdstable
changeset 5424 8ecbcbff9777
parent 5421 8167de96c523
child 5877 0c7b7b76a84f
child 5886 00a78298d30d
permissions -rw-r--r--
replace logilab-common by CubicWeb in disclaimer
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 -*-
5421
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5093
diff changeset
     2
# copyright 2003-2010 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5093
diff changeset
     3
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5093
diff changeset
     4
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5093
diff changeset
     5
# This file is part of CubicWeb.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5093
diff changeset
     6
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5093
diff changeset
     7
# CubicWeb is free software: you can redistribute it and/or modify it under the
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5093
diff changeset
     8
# terms of the GNU Lesser General Public License as published by the Free
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5093
diff changeset
     9
# Software Foundation, either version 2.1 of the License, or (at your option)
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5093
diff changeset
    10
# any later version.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5093
diff changeset
    11
#
5424
8ecbcbff9777 replace logilab-common by CubicWeb in disclaimer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5421
diff changeset
    12
# CubicWeb is distributed in the hope that it will be useful, but WITHOUT
5421
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5093
diff changeset
    13
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5093
diff changeset
    14
# FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5093
diff changeset
    15
# details.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5093
diff changeset
    16
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5093
diff changeset
    17
# You should have received a copy of the GNU Lesser General Public License along
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5093
diff changeset
    18
# with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
3413
5cd27d2cc64f update imports, rename test module
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2968
diff changeset
    19
"""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
    20
606923dff11b big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1787
diff changeset
    21
"""
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    22
3414
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
    23
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
    24
3413
5cd27d2cc64f update imports, rename test module
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2968
diff changeset
    25
3414
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
    26
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
    27
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
    28
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
    29
from cubicweb.server import hook
3413
5cd27d2cc64f update imports, rename test module
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2968
diff changeset
    30
from cubicweb.hooks import integrity, syncschema
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    31
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    32
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2637
diff changeset
    33
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
    34
    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
    35
        try:
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2637
diff changeset
    36
            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
    37
        finally:
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2637
diff changeset
    38
            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
    39
    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
    40
3414
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
    41
class OperationsTC(CubicWebTC):
1787
71c143c0ada3 fix test
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
    42
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    43
    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
    44
        CubicWebTC.setUp(self)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    45
        self.hm = self.repo.hm
1787
71c143c0ada3 fix test
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
    46
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2637
diff changeset
    47
    @clean_session_ops
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    48
    def test_late_operation(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    49
        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
    50
        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
    51
        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
    52
        l3 = hook.Operation(session)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    53
        self.assertEquals(session.pending_operations, [l3, l1, l2])
1787
71c143c0ada3 fix test
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
    54
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2637
diff changeset
    55
    @clean_session_ops
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    56
    def test_single_last_operation(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    57
        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
    58
        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
    59
        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
    60
        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
    61
        l3 = hook.Operation(session)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    62
        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
    63
        l4 = hook.SingleLastOperation(session)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    64
        self.assertEquals(session.pending_operations, [l3, l1, l2, l4])
1787
71c143c0ada3 fix test
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
    65
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2637
diff changeset
    66
    @clean_session_ops
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    67
    def test_global_operation_order(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    68
        session = self.session
3413
5cd27d2cc64f update imports, rename test module
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2968
diff changeset
    69
        op1 = integrity._DelayedDeleteOp(session)
5cd27d2cc64f update imports, rename test module
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2968
diff changeset
    70
        op2 = syncschema.MemSchemaRDefDel(session)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    71
        # 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
    72
        # can check the result...
3413
5cd27d2cc64f update imports, rename test module
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2968
diff changeset
    73
        op3 = syncschema.MemSchemaNotifyChanges(session)
5cd27d2cc64f update imports, rename test module
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2968
diff changeset
    74
        op4 = integrity._DelayedDeleteOp(session)
5cd27d2cc64f update imports, rename test module
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2968
diff changeset
    75
        op5 = integrity._CheckORelationOp(session)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    76
        self.assertEquals(session.pending_operations, [op1, op2, op4, op5, op3])
1787
71c143c0ada3 fix test
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
    77
3414
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
    78
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
    79
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
    80
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
    81
config = TestServerConfiguration('data')
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
    82
config.bootstrap_cubes()
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
    83
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
    84
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
    85
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
    86
    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
    87
    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
    88
3414
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
    89
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
    90
    __regid__ = 'addany'
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
    91
    category = 'cat1'
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
    92
    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
    93
    def __call__(self):
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
    94
        raise HookCalled()
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
    95
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
    96
5093
8d073d2e089d [optimization] improve massive write performance by optimizing hooks selection
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5029
diff changeset
    97
class HooksRegistryTC(TestCase):
3414
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
    98
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
    99
    def setUp(self):
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
   100
        """ 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
   101
        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
   102
        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
   103
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
   104
    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
   105
        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
   106
            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
   107
        ex = self.assertRaises(Exception, self.o.register, _Hook)
5029
f7709d28fb79 [test] fixes when executed under apycot environment (also added ldap1 to test host /etc/hosts)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4835
diff changeset
   108
        self.assertEquals(str(ex), 'bad event before_add_entiti on %s._Hook' % __name__)
3414
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
   109
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
   110
    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
   111
        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
   112
            events = None
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
   113
        ex = self.assertRaises(Exception, self.o.register, _Hook)
5029
f7709d28fb79 [test] fixes when executed under apycot environment (also added ldap1 to test host /etc/hosts)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4835
diff changeset
   114
        self.assertEquals(str(ex), 'bad .events attribute None on %s._Hook' % __name__)
3414
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
   115
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
   116
    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
   117
        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
   118
            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
   119
        ex = self.assertRaises(Exception, self.o.register, _Hook)
5029
f7709d28fb79 [test] fixes when executed under apycot environment (also added ldap1 to test host /etc/hosts)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4835
diff changeset
   120
        self.assertEquals(str(ex), 'bad event b on %s._Hook' % __name__)
3414
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
   121
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
   122
    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
   123
        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
   124
        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
   125
        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
   126
                         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
   127
                         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
   128
                         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
   129
        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
   130
                          self.o.call_hooks, 'before_add_entity', cw)
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
   131
        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
   132
        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
   133
        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
   134
        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
   135
                          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
   136
        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
   137
        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
   138
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
   139
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
   140
class SystemHooksTC(CubicWebTC):
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
   141
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
   142
    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
   143
        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
   144
        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
   145
        # 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
   146
        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
   147
        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
   148
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
   149
    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
   150
        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
   151
        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
   152
        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
   153
        cnx.close()
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
   154
        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
   155
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
   156
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
   157
# class RelationHookTC(TestCase):
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
   158
#     """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
   159
#     def setUp(self):
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
   160
#         """ 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
   161
#         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
   162
#         self.called = []
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
   163
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
   164
#     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
   165
#         """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
   166
#         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
   167
#                              '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
   168
#         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
   169
#         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
   170
#                           1, 'concerne', 2)
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
   171
#         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
   172
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
   173
#     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
   174
#         """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
   175
#         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
   176
#                              '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
   177
#         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
   178
#         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
   179
#                           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
#         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
   181
#                           3, 'concerne', 4)
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
   182
#         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
   183
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
   184
#     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
   185
#         """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
   186
#         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
   187
#                              '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
   188
#         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
   189
#         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
   190
#                           1, 'concerne', 2)
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
   191
#         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
   192
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
   193
#     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
   194
#         """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
   195
#         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
   196
#                         '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
   197
#         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
   198
#                           1, 'concerne', 2)
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
   199
#         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
   200
#                           3, 'concerne', 4)
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
   201
#         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
   202
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
   203
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
   204
#     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
   205
#         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
   206
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
   207
#     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
   208
#         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
   209
fe6c99b96701 no more HooksManager but a HooksRegistry: update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3413
diff changeset
   210
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   211
if __name__ == '__main__':
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   212
    unittest_main()