Browse Source

Tests for Boolean and Plugin

Quentin Bonaventure 8 years ago
parent
commit
c880c99316
2 changed files with 97 additions and 15 deletions
  1. 1
    1
      tests/datahandlers/test_boolean.py
  2. 96
    14
      tests/plugin/test_plugin.py

+ 1
- 1
tests/datahandlers/test_boolean.py View File

@@ -5,7 +5,7 @@ from lodel.leapi.datahandlers.datas import Boolean, Varchar, Integer
5 5
 
6 6
 
7 7
 test_boolean = Boolean()
8
-class BooleanTestCase(unittest.TestCase):
8
+class BooleanTestCase(unittest.TestCase): 
9 9
 
10 10
     def test_boolean_good_check_data_value(self):
11 11
         for test_value in [True, False]:

+ 96
- 14
tests/plugin/test_plugin.py View File

@@ -2,7 +2,8 @@
2 2
 
3 3
 import unittest
4 4
 
5
-from lodel.plugin.plugins import Plugin, PluginError
5
+from lodel.plugin.plugins import Plugin, PluginError, MetaPlugType,\
6
+    VIRTUAL_PACKAGE_NAME, DEFAULT_PLUGINS_PATH_LIST
6 7
 from lodel.plugin.datasource_plugin import DatasourcePlugin
7 8
 from lodel.plugin.sessionhandler import SessionHandlerPlugin
8 9
 from lodel.plugin.interface import InterfacePlugin
@@ -10,28 +11,109 @@ from lodel.plugin.extensions import Extension
10 11
 from lodel.settings.settings import Settings
11 12
 import tests.loader_utils
12 13
 
14
+from unittest.mock import patch
15
+
13 16
 ##@todo write tests about discovering
17
+##@todo finish tests for plugin_path
18
+##@todo finish tests for check_deps
19
+##@todo write tests for loader_module
20
+##@todo write tests for load_all (ran upon problems as "dummy_datasource"
21
+##      already is "pre-loaded" (I guess), but cannot be found in
22
+##      _plugin_instances (without doing some other work before, I guess)
14 23
 class PluginTestCase(unittest.TestCase):
15
-    """ Test case grouping all tests on Plugin class init procedures """
16 24
 
17 25
     def setUp(self):
18 26
         Plugin.clear()
27
+        self.working_plugins_list = ['dummy', 'dummy_datasource']
28
+    
29
+    
30
+    def test_check_deps_returns_empty_list_if_no_dependencies(self):
31
+        self.assertEqual(list(), Plugin('dummy').check_deps())    
32
+    
33
+    
34
+    def test_loader_module_if_plugin_not_yet_loaded_throws_RuntimeError(self):
35
+        self.assertRaises(RuntimeError, Plugin('dummy').loader_module)
36
+    
19 37
 
20
-    def test_start(self):
21
-        """ Testing plugin registration with a valid list of plugins name """
22
-        Plugin.start(['dummy', 'dummy_datasource'])
38
+    def test_start_calls_register_for_each_plugins_from_array(self):
39
+        plugin_class = Plugin
40
+        with patch.object(Plugin, 'register', wraps=plugin_class.register) as register_wrap:
41
+            Plugin.start(self.working_plugins_list)
42
+            self.assertEqual(len(self.working_plugins_list), register_wrap.call_count)
23 43
 
24
-    def test_double_start(self):
25
-        """ Testing clas behavior when starting it twice """
26
-        Plugin.start(['dummy', 'dummy_datasource'])
27
-        with self.assertRaises(PluginError):
28
-            Plugin.start(['dummy', 'dummy_datasource'])
29 44
 
30
-    def test_clear(self):
31
-        """ Testing that clear allow to start again Plugin """
32
-        Plugin.start(['dummy', 'dummy_datasource'])
45
+    def test_clear_effectively_allow_fresh_new_plugin_reloading(self):
46
+        Plugin.start(self.working_plugins_list)
33 47
         Plugin.clear()
34
-        Plugin.start(['dummy', 'dummy_datasource'])
48
+        Plugin.start(self.working_plugins_list)
49
+        
50
+        
51
+    def test_register_if_plugin_already_registered_throws_PluginError(self):
52
+        Plugin.register('dummy')
53
+        self.assertRaises(PluginError, Plugin.register, 'dummy')
54
+    
55
+    
56
+    def test_register_if_plugin_name_not_in_cache_throws_PluginError(self):
57
+        self.assertRaises(PluginError, Plugin.register, 'azerty')
58
+    
59
+    
60
+    def test_register_if_ptype_not_known_throws_PluginError(self):
61
+        with patch.object(MetaPlugType, 'all_ptype_names', return_value=[]) as mock_method:
62
+            self.assertRaises(PluginError, Plugin.register, 'dummy')
63
+    
64
+        
65
+    def test_register_returns_Plugin_child_object(self):
66
+        self.assertTrue(issubclass(Plugin.register('dummy_datasource').__class__, Plugin))
67
+        
68
+        
69
+    def test_get_if_no_plugin_found_throws_KeyError(self):
70
+        self.assertRaises(PluginError, Plugin.get, 'foo')
71
+        
72
+        
73
+    def test_get_returns_proper_plugin_instance(self):
74
+        Plugin.register('dummy')
75
+        self.assertTrue(Plugin.get('dummy').__class__, Plugin)
76
+        
77
+    
78
+    def test_plugin_path_if_no_plugin_name_found_throws_PluginError(self):
79
+        self.assertRaises(PluginError, Plugin.plugin_path, 'foo')
80
+    
81
+    
82
+    def test_plugin_module_name_correctly_returns_module_name_string_from_plugin_name(self):
83
+        self.assertEqual(Plugin.plugin_module_name('foo'), "%s.%s" % (VIRTUAL_PACKAGE_NAME, 'foo'))
84
+        
85
+        
86
+    def test_discover_if_paths_is_none_default_is_used(self):
87
+        with patch.object(Plugin, '_discover', wraps=Plugin._discover) as _discover_wrap:
88
+            Plugin.discover(DEFAULT_PLUGINS_PATH_LIST)
89
+            _discover_wrap.assert_called_with(DEFAULT_PLUGINS_PATH_LIST[-1])
90
+            
91
+        
92
+    def test_discover_if_paths_is_set_properly_search_in(self):
93
+        paths = ['.', '.']
94
+        with patch.object(Plugin, '_discover', wraps=Plugin._discover) as _discover_wrap:
95
+            Plugin.discover(paths)
96
+            _discover_wrap.assert_called_with(paths[-1])
97
+            
98
+            
99
+    def test_discover_if_no_plugins_found_still_returns_searched_paths(self):
100
+        paths = ['/home/quentin/Pictures']
101
+        dfoi = Plugin.discover(paths)
102
+        self.assertEqual(dfoi['path_list'], paths)
103
+        
104
+            
105
+    def test_discover_if_no_plugins_found_returns_empty_plugin_dict(self):
106
+        dfoi = Plugin.discover(['/home/quentin/Pictures'])
107
+        self.assertEqual(dfoi['plugins'], {})
108
+        
109
+            
110
+    def test_discover_if_no_plugins_found_returns_empty_plugin(self):
111
+        dfoi = Plugin.discover(['./plugins'])
112
+        self.maxDiff = None
113
+        self.assertEqual(dfoi['plugins'], {})
114
+        
115
+
116
+
35 117
 
36 118
 class PluginStartedTestCase(unittest.TestCase):
37 119
     """ Test case grouping all tests on a started Plugin class """

Loading…
Cancel
Save