Browse Source

Writing tests on hooks

Yann Weber 8 years ago
parent
commit
196d959188
2 changed files with 44 additions and 0 deletions
  1. 1
    0
      tests/plugin/test_plugin.py
  2. 43
    0
      tests/plugin/tests_hooks.py

+ 1
- 0
tests/plugin/test_plugin.py View File

@@ -10,6 +10,7 @@ from lodel.plugin.extensions import Extension
10 10
 from lodel.settings.settings import Settings
11 11
 import tests.loader_utils
12 12
 
13
+##@todo write tests about discovering
13 14
 class PluginTestCase(unittest.TestCase):
14 15
     """ Test case grouping all tests on Plugin class init procedures """
15 16
 

+ 43
- 0
tests/plugin/tests_hooks.py View File

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

Loading…
Cancel
Save