No Description
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 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #
  2. # This file is part of Lodel 2 (https://github.com/OpenEdition)
  3. #
  4. # Copyright (C) 2015-2017 Cléo UMS-3287
  5. #
  6. # This program is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU Affero General Public License as published
  8. # by the Free Software Foundation, either version 3 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU Affero General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU Affero General Public License
  17. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. #
  19. import unittest
  20. from unittest.mock import MagicMock
  21. from lodel.plugin.plugins import Plugin, PluginError
  22. from lodel.plugin.hooks import LodelHook
  23. testhook = 'are_we_sure_that_this_name_is_uniq'
  24. class HookTestCase(unittest.TestCase):
  25. def setUp(self):
  26. LodelHook.__reset__()
  27. def test_registration(self):
  28. """ Testing hook registration using decorator (checking using hook_list() """
  29. hook_list = LodelHook.hook_list(testhook)
  30. self.assertEqual({}, hook_list)
  31. #hook_registration
  32. @LodelHook(testhook,42)
  33. def funny_fun_test_hook(hook_name, caller, payload):
  34. pass
  35. hook_list = LodelHook.hook_list(testhook)
  36. self.assertEqual({testhook: [(funny_fun_test_hook, 42)]}, hook_list)
  37. def test_call(self):
  38. """ Testing LodelHook.call_hook() """
  39. #manual registration using a mock
  40. mmock = MagicMock(return_value = '4242')
  41. mmock.__name__ = 'mmock'
  42. hooking = LodelHook(testhook, 1337)
  43. hooking(mmock)
  44. res = LodelHook.call_hook(testhook, 'Caller', 'payload')
  45. #Check returned value
  46. self.assertEqual(
  47. res,
  48. '4242',
  49. 'Expected value was "4242" but found %s' % res)
  50. #Checks call
  51. mmock.assert_called_once_with(testhook, 'Caller', 'payload')
  52. def test_priority_call(self):
  53. """ Testing priority to ensure a call order """
  54. @LodelHook(testhook, 1)
  55. def stage_1_hook(name, caller, payload):
  56. return '4'+payload
  57. @LodelHook(testhook, 10)
  58. def stage_1_hook(name, caller, payload):
  59. return '3'+payload
  60. @LodelHook(testhook, 20)
  61. def stage_1_hook(name, caller, payload):
  62. return '1'+payload
  63. @LodelHook(testhook, 15)
  64. def stage_1_hook(name, caller, payload):
  65. return '2'+payload
  66. result = LodelHook.call_hook(testhook, 'me', 'WootWoot')
  67. self.assertEqual(result, '1234WootWoot')