Browse Source

Added tests for the datahandler abstract class

Roland Haroutiounian 8 years ago
parent
commit
909b9478e3
2 changed files with 21 additions and 4 deletions
  1. 3
    3
      lodel/leapi/datahandlers/base_classes.py
  2. 18
    1
      tests/datahandlers/test_datahandler.py

+ 3
- 3
lodel/leapi/datahandlers/base_classes.py View File

146
     ##@brief given a field type name, returns the associated python class
146
     ##@brief given a field type name, returns the associated python class
147
     # @param fieldtype_name str : A field type name (not case sensitive)
147
     # @param fieldtype_name str : A field type name (not case sensitive)
148
     # @return DataField child class
148
     # @return DataField child class
149
-    # @todo implements custom handlers fetch
150
     # @note To access custom data handlers it can be cool to prefix the handler name by plugin name for example ? (to ensure name unicity)
149
     # @note To access custom data handlers it can be cool to prefix the handler name by plugin name for example ? (to ensure name unicity)
151
     @classmethod
150
     @classmethod
152
     def from_name(cls, name):
151
     def from_name(cls, name):
153
         cls.load_base_handlers()
152
         cls.load_base_handlers()
153
+        all_handlers = dict(cls._base_handlers, **cls.__custom_handlers)
154
         name = name.lower()
154
         name = name.lower()
155
-        if name not in cls._base_handlers:
155
+        if name not in all_handlers:
156
             raise NameError("No data handlers named '%s'" % (name,))
156
             raise NameError("No data handlers named '%s'" % (name,))
157
-        return cls._base_handlers[name]
157
+        return all_handlers[name]
158
  
158
  
159
     ##@brief Return the module name to import in order to use the datahandler
159
     ##@brief Return the module name to import in order to use the datahandler
160
     # @param data_handler_name str : Data handler name
160
     # @param data_handler_name str : Data handler name

+ 18
- 1
tests/datahandlers/test_datahandler.py View File

1
 import unittest
1
 import unittest
2
 
2
 
3
 from lodel.leapi.datahandlers.base_classes import DataHandler
3
 from lodel.leapi.datahandlers.base_classes import DataHandler
4
+from lodel.leapi.datahandlers.datas import Varchar
4
 
5
 
5
 
6
 
6
 class DataHandlerTestCase(unittest.TestCase):
7
 class DataHandlerTestCase(unittest.TestCase):
12
         except NotImplementedError:
13
         except NotImplementedError:
13
             self.assertNotIsInstance(datahandler, DataHandler)
14
             self.assertNotIsInstance(datahandler, DataHandler)
14
             self.assertIsNone(datahandler)
15
             self.assertIsNone(datahandler)
15
-    
16
+
17
+    def test_register_new_handler(self):
18
+        DataHandler.register_new_handler('testvarchar', Varchar)
19
+        self.assertEqual(DataHandler.from_name('testvarchar'), Varchar)
20
+
21
+    def test_register_nonclass_as_handler(self):
22
+        try:
23
+            DataHandler.register_new_handler('testvarchar', 'common string')
24
+        except Exception as err:
25
+            self.assertEqual(ValueError, type(err))
26
+
27
+    def test_register_invalid_class_as_handler(self):
28
+
29
+        try:
30
+            DataHandler.register_new_handler('testvarchar', Exception)
31
+        except Exception as err:
32
+            self.assertEqual(ValueError, type(err))

Loading…
Cancel
Save