Nessuna descrizione
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

tests_hooks.py 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. #-*- coding: utf-8 -*-
  2. import copy
  3. import unittest
  4. from unittest import mock
  5. from unittest.mock import patch, call, Mock
  6. import leapi.test.utils
  7. from Lodel.hooks import LodelHook
  8. class LodelHookTestCase(unittest.TestCase):
  9. #Dynamic code generation & import
  10. @classmethod
  11. def setUpClass(cls):
  12. """ Write the generated code in a temporary directory and import it """
  13. cls.tmpdir = leapi.test.utils.tmp_load_factory_code()
  14. @classmethod
  15. def tearDownClass(cls):
  16. """ Remove the temporary directory created at class setup """
  17. leapi.test.utils.cleanup(cls.tmpdir)
  18. def test_hook_registration(self):
  19. """ Testing hooks registration """
  20. self.assertEqual(LodelHook.hook_list('test_hook'), dict())
  21. @LodelHook('test_hook', 42)
  22. def test_hook(hook_name, caller, payload):
  23. pass
  24. @LodelHook('test_hook', 1)
  25. def test2_hook(hook_name, caller, payload):
  26. pass
  27. @LodelHook('test_hook')
  28. def test3_hook(hook_name, caller, payload):
  29. pass
  30. self.assertEqual( LodelHook.hook_list('test_hook'),
  31. {
  32. 'test_hook':[
  33. (test2_hook, 1),
  34. (test_hook, 42),
  35. (test3_hook, 0xFFFF),
  36. ]
  37. }
  38. )
  39. def test_hook_call(self):
  40. """ Testing hooks call """
  41. # Registering a mock as hook
  42. mockhook = Mock()
  43. decorator = LodelHook('test_hook_call')
  44. decorator(mockhook)
  45. LodelHook.call_hook('test_hook_call', leapi, [1,2,3,42])
  46. mockhook.assert_called_once_with('test_hook_call', leapi, [1,2,3,42])
  47. mockhook.reset_mock()
  48. LodelHook.call_hook('test_hook_call', None, 'datas')
  49. mockhook.assert_called_once_with('test_hook_call', None, 'datas')
  50. def test_leapi_get_hook_leobject(self):
  51. """ Testing that leapi_get_* hooks get called when calling get on LeObject """
  52. from dyncode import Numero, Article, Publication, Textes, LeRelation, LeObject, LeRelation
  53. call_args = {
  54. 'query_filters': [],
  55. 'offset': 0,
  56. 'limit': None,
  57. 'order': None,
  58. 'group': None,
  59. 'field_list': None
  60. }
  61. for leo in [Numero, Article, Publication, Textes, LeObject]:
  62. call_args_full = copy.copy(call_args)
  63. if 'instanciate' not in call_args_full:
  64. call_args_full['instanciate'] = True
  65. if leo.implements_letype():
  66. call_args_full['query_filters'].append( ('type_id', '=', leo._type_id) )
  67. if leo.implements_leclass():
  68. call_args_full['query_filters'].append( ('class_id', '=', leo._class_id) )
  69. with patch.object(LodelHook, 'call_hook', return_value=call_args_full) as callhook_mock:
  70. foo = leo.get(**call_args)
  71. expected_calls = [
  72. call('leapi_get_pre', leo, call_args_full),
  73. call('leapi_get_post', leo, None)
  74. ]
  75. callhook_mock.assert_has_calls(expected_calls, any_order = False)
  76. def test_leapi_get_hook_lerelation(self):
  77. """ Testing that leapi_get_* hooks get called when calling get on LeRelation """
  78. from dyncode import LeRelation, LeRel2Type, LeHierarch, RelTextesPersonneAuteur
  79. call_args = {
  80. 'query_filters': [],
  81. 'offset': 0,
  82. 'limit': None,
  83. 'order': None,
  84. 'group': None,
  85. 'field_list': None
  86. }
  87. for lerel in [LeRelation, LeRel2Type, LeHierarch, RelTextesPersonneAuteur]:
  88. call_args_full = copy.copy(call_args)
  89. if 'instanciate' not in call_args_full:
  90. call_args_full['instanciate'] = True
  91. with patch.object(LodelHook, 'call_hook', return_value=call_args_full) as callhook_mock:
  92. foo = lerel.get(**call_args)
  93. expected_calls = [
  94. call('leapi_get_pre', lerel, call_args_full),
  95. call('leapi_get_post', lerel, None),
  96. ]
  97. callhook_mock.assert_has_calls(expected_calls, any_order = False)
  98. def test_leapi_update_hook(self):
  99. """ Testing that leapi_update_* hooks get called when calling update on LeCrud child instance"""
  100. from leapi.lecrud import _LeCrud
  101. from dyncode import Numero, Publication, LeRelation, RelTextesPersonneAuteur
  102. call_args = {'datas':dict()}
  103. for leo in [Numero, Publication, RelTextesPersonneAuteur]:
  104. with patch.object(_LeCrud, 'populate', return_value = None) as osef_mock:
  105. with patch.object(LodelHook, 'call_hook', return_value = call_args) as callhook_mock:
  106. inst = leo(42)
  107. inst.update(**call_args)
  108. expected_calls = [
  109. call('leapi_update_pre', inst, call_args),
  110. call('leapi_update_post', inst, False),
  111. ]
  112. callhook_mock.assert_has_calls(expected_calls, any_order = False)
  113. def test_leapi_delete_hooks(self):
  114. """ Testing that leapi_delete_* hooks get called when calling delete on LeCrud child instance"""
  115. from dyncode import Numero, Publication, LeRelation, RelTextesPersonneAuteur
  116. for leo in [Numero, Publication, RelTextesPersonneAuteur]:
  117. with patch.object(LodelHook, 'call_hook', return_value = None) as callhook_mock:
  118. inst = leo(42)
  119. inst.delete()
  120. expected_calls = [
  121. call('leapi_delete_pre', inst, None),
  122. call('leapi_delete_post', inst, None)
  123. ]
  124. callhook_mock.assert_has_calls(expected_calls, any_order = False)
  125. def test_leapi_insert_hooks(self):
  126. """ Testing that leapi_insert_* hooks get called when calling insert on LeCrud child instance """
  127. # Only testing with Article because datas check is anoying
  128. from dyncode import Article
  129. call_args = {
  130. 'datas': {
  131. 'titre': 'test',
  132. 'soustitre': 'avec des mocks',
  133. }
  134. }
  135. full_args = copy.copy(call_args)
  136. full_args['classname'] = None
  137. with patch.object(LodelHook, 'call_hook', return_value = full_args) as callhook_mock:
  138. Article.insert(**call_args)
  139. expected_calls = [
  140. call('leapi_insert_pre', Article, full_args),
  141. call('leapi_insert_post', Article, None),
  142. ]
  143. callhook_mock.assert_has_calls(expected_calls)