cubicweb/server/test/unittest_hook.py
changeset 11767 432f87a63057
parent 11269 73ac69970047
child 11768 b8b71dd09a2c
equal deleted inserted replaced
11766:d8de1ac21f36 11767:432f87a63057
     1 # -*- coding: utf-8 -*-
     1 # -*- coding: utf-8 -*-
     2 # copyright 2003-2014 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
     2 # copyright 2003-2016 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
     3 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
     3 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
     4 #
     4 #
     5 # This file is part of CubicWeb.
     5 # This file is part of CubicWeb.
     6 #
     6 #
     7 # CubicWeb is free software: you can redistribute it and/or modify it under the
     7 # CubicWeb is free software: you can redistribute it and/or modify it under the
    22 
    22 
    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 
    28 class OperationsTC(CubicWebTC):
    29 class OperationsTC(CubicWebTC):
    29 
    30 
    30     def setUp(self):
    31     def setUp(self):
    31         CubicWebTC.setUp(self)
    32         CubicWebTC.setUp(self)
    53             op1 = syncschema.RDefDelOp(cnx)
    54             op1 = syncschema.RDefDelOp(cnx)
    54             op2 = integrity._CheckORelationOp(cnx)
    55             op2 = integrity._CheckORelationOp(cnx)
    55             op3 = syncschema.MemSchemaNotifyChanges(cnx)
    56             op3 = syncschema.MemSchemaNotifyChanges(cnx)
    56             self.assertEqual([op1, op2, op3], cnx.pending_operations)
    57             self.assertEqual([op1, op2, op3], cnx.pending_operations)
    57 
    58 
    58 class HookCalled(Exception): pass
    59 
       
    60 class HookCalled(Exception):
       
    61     pass
       
    62 
    59 
    63 
    60 config = TestServerConfiguration('data', __file__)
    64 config = TestServerConfiguration('data', __file__)
    61 config.bootstrap_cubes()
    65 config.bootstrap_cubes()
    62 schema = config.load_schema()
    66 schema = config.load_schema()
    63 
    67 
    64 def tearDownModule(*args):
    68 def tearDownModule(*args):
    65     global config, schema
    69     global config, schema
    66     del config, schema
    70     del config, schema
    67 
    71 
       
    72 
    68 class AddAnyHook(hook.Hook):
    73 class AddAnyHook(hook.Hook):
    69     __regid__ = 'addany'
    74     __regid__ = 'addany'
    70     category = 'cat1'
    75     category = 'cat1'
    71     events = ('before_add_entity',)
    76     events = ('before_add_entity',)
       
    77 
    72     def __call__(self):
    78     def __call__(self):
    73         raise HookCalled()
    79         raise HookCalled()
    74 
    80 
    75 
    81 
    76 class HooksRegistryTC(TestCase):
    82 class HooksRegistryTC(TestCase):
   133             self.assertEqual(hooks.CALLED_EVENTS['session_open'], 'anon')
   139             self.assertEqual(hooks.CALLED_EVENTS['session_open'], 'anon')
   134         anonaccess.close()
   140         anonaccess.close()
   135         self.assertEqual(hooks.CALLED_EVENTS['session_close'], 'anon')
   141         self.assertEqual(hooks.CALLED_EVENTS['session_close'], 'anon')
   136 
   142 
   137 
   143 
   138 # class RelationHookTC(TestCase):
       
   139 #     """testcase for relation hooks grouping"""
       
   140 #     def setUp(self):
       
   141 #         """ called before each test from this class """
       
   142 #         self.o = HooksManager(schema)
       
   143 #         self.called = []
       
   144 
       
   145 #     def test_before_add_relation(self):
       
   146 #         """make sure before_xxx_relation hooks are called directly"""
       
   147 #         self.o.register(self._before_relation_hook,
       
   148 #                              'before_add_relation', 'concerne')
       
   149 #         self.assertEqual(self.called, [])
       
   150 #         self.o.call_hooks('before_add_relation', 'concerne', 'USER',
       
   151 #                           1, 'concerne', 2)
       
   152 #         self.assertEqual(self.called, [(1, 'concerne', 2)])
       
   153 
       
   154 #     def test_after_add_relation(self):
       
   155 #         """make sure after_xxx_relation hooks are deferred"""
       
   156 #         self.o.register(self._after_relation_hook,
       
   157 #                              'after_add_relation', 'concerne')
       
   158 #         self.assertEqual(self.called, [])
       
   159 #         self.o.call_hooks('after_add_relation', 'concerne', 'USER',
       
   160 #                           1, 'concerne', 2)
       
   161 #         self.o.call_hooks('after_add_relation', 'concerne', 'USER',
       
   162 #                           3, 'concerne', 4)
       
   163 #         self.assertEqual(self.called, [(1, 'concerne', 2), (3, 'concerne', 4)])
       
   164 
       
   165 #     def test_before_delete_relation(self):
       
   166 #         """make sure before_xxx_relation hooks are called directly"""
       
   167 #         self.o.register(self._before_relation_hook,
       
   168 #                              'before_delete_relation', 'concerne')
       
   169 #         self.assertEqual(self.called, [])
       
   170 #         self.o.call_hooks('before_delete_relation', 'concerne', 'USER',
       
   171 #                           1, 'concerne', 2)
       
   172 #         self.assertEqual(self.called, [(1, 'concerne', 2)])
       
   173 
       
   174 #     def test_after_delete_relation(self):
       
   175 #         """make sure after_xxx_relation hooks are deferred"""
       
   176 #         self.o.register(self._after_relation_hook,
       
   177 #                         'after_delete_relation', 'concerne')
       
   178 #         self.o.call_hooks('after_delete_relation', 'concerne', 'USER',
       
   179 #                           1, 'concerne', 2)
       
   180 #         self.o.call_hooks('after_delete_relation', 'concerne', 'USER',
       
   181 #                           3, 'concerne', 4)
       
   182 #         self.assertEqual(self.called, [(1, 'concerne', 2), (3, 'concerne', 4)])
       
   183 
       
   184 
       
   185 #     def _before_relation_hook(self, cnxset, subject, r_type, object):
       
   186 #         self.called.append((subject, r_type, object))
       
   187 
       
   188 #     def _after_relation_hook(self, cnxset, subject, r_type, object):
       
   189 #         self.called.append((subject, r_type, object))
       
   190 
       
   191 
       
   192 if __name__ == '__main__':
   144 if __name__ == '__main__':
   193     unittest_main()
   145     unittest_main()