Nenhuma descrição
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

tests_hooks.py 2.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #-*- coding: utf-8 -*-
  2. import unittest
  3. from unittest.mock import MagicMock
  4. from lodel.plugin.plugins import Plugin, PluginError
  5. from lodel.plugin.hooks import LodelHook
  6. testhook = 'are_we_sure_that_this_name_is_uniq'
  7. class HookTestCase(unittest.TestCase):
  8. def setUp(self):
  9. LodelHook.__reset__()
  10. def test_registration(self):
  11. """ Testing hook registration using decorator (checking using hook_list() """
  12. hook_list = LodelHook.hook_list(testhook)
  13. self.assertEqual({}, hook_list)
  14. #hook_registration
  15. @LodelHook(testhook,42)
  16. def funny_fun_test_hook(hook_name, caller, payload):
  17. pass
  18. hook_list = LodelHook.hook_list(testhook)
  19. self.assertEqual({testhook: [(funny_fun_test_hook, 42)]}, hook_list)
  20. def test_call(self):
  21. """ Testing LodelHook.call_hook() """
  22. #manual registration using a mock
  23. mmock = MagicMock(return_value = '4242')
  24. mmock.__name__ = 'mmock'
  25. hooking = LodelHook(testhook, 1337)
  26. hooking(mmock)
  27. res = LodelHook.call_hook(testhook, 'Caller', 'payload')
  28. #Check returned value
  29. self.assertEqual(
  30. res,
  31. '4242',
  32. 'Expected value was "4242" but found %s' % res)
  33. #Checks call
  34. mmock.assert_called_once_with(testhook, 'Caller', 'payload')
  35. def test_priority_call(self):
  36. """ Testing priority to ensure a call order """
  37. @LodelHook(testhook, 1)
  38. def stage_1_hook(name, caller, payload):
  39. return '4'+payload
  40. @LodelHook(testhook, 10)
  41. def stage_1_hook(name, caller, payload):
  42. return '3'+payload
  43. @LodelHook(testhook, 20)
  44. def stage_1_hook(name, caller, payload):
  45. return '1'+payload
  46. @LodelHook(testhook, 15)
  47. def stage_1_hook(name, caller, payload):
  48. return '2'+payload
  49. result = LodelHook.call_hook(testhook, 'me', 'WootWoot')
  50. self.assertEqual(result, '1234WootWoot')