Browse Source

Created and added varcharlist DH to lodelsites EM

Quentin Bonaventure 7 years ago
parent
commit
c4289764ec
3 changed files with 83 additions and 4 deletions
  1. 19
    0
      lodel/leapi/datahandlers/datas.py
  2. 3
    4
      lodelsites_em.py
  3. 61
    0
      tests/datahandlers/test_varcharlist.py

+ 19
- 0
lodel/leapi/datahandlers/datas.py View File

@@ -121,7 +121,26 @@ class Concat(FormatString):
121 121
         super().__init__(
122 122
             format_string = format_string, field_list = field_list, **kwargs)
123 123
 
124
+
125
+
124 126
 class Password(Varchar):
125 127
     help = 'Handle passwords'
126 128
     base_type = 'password'
127 129
     pass
130
+
131
+
132
+
133
+class VarcharList(Varchar):
134
+    help = 'DataHandler designed to make a list out of a string.'
135
+    base_type = 'varchar'
136
+    
137
+    def __init__(self, delimiter=' ', **kwargs):
138
+        if not isinstance(delimiter, str):
139
+            raise LodelException("The delimiter has to be a string, %s given" % type(delimiter))
140
+        self.delimiter = str(delimiter)
141
+        super().__init__(**kwargs)
142
+
143
+
144
+    def construct_data(self, emcomponent, fname, datas, cur_value):
145
+        result = cur_value.split(self.delimiter)
146
+        return result

+ 3
- 4
lodelsites_em.py View File

@@ -22,8 +22,7 @@ base_group = em.new_group(
22 22
 
23 23
 em_lodel_site = em.new_class(
24 24
     'LodelSite',
25
-    group = base_group,
26
-    abstract=True
25
+    group = base_group
27 26
 )
28 27
 
29 28
 em_lodel_site.new_field(
@@ -49,8 +48,8 @@ em_lodel_site.new_field(
49 48
     display_text = 'lodeSiteExtensions',
50 49
     help_text = 'Lodel site extensions',
51 50
     group = base_group,
52
-    data_handler = 'list',
53
-    back_reference = ('Plugin', 'plugin_name') 
51
+    data_handler = 'varcharlist',
52
+    delimiter = ' '
54 53
 )
55 54
 
56 55
 em_lodel_site.new_field(

+ 61
- 0
tests/datahandlers/test_varcharlist.py View File

@@ -0,0 +1,61 @@
1
+import unittest
2
+
3
+from lodel.leapi.datahandlers.datas import VarcharList as Testee
4
+from lodel.leapi.datahandlers.exceptions import LodelDataHandlerException
5
+from lodel.exceptions import LodelException
6
+
7
+class VarcharListTestCase(unittest.TestCase):
8
+    
9
+    
10
+    string = 'a simple string for testing purpose'
11
+    expected_result = string.split(' ')
12
+    
13
+    
14
+    def test_base_type_is_varchar(self):
15
+        '''base_type property has to be equal to \'varchar\''''
16
+        self.assertEqual(Testee.base_type, 'varchar')
17
+    
18
+    
19
+    def test_help_property_str_is_set(self):
20
+        '''Help property must be set and be a string'''
21
+        self.assertEqual(type(Testee.help), str)
22
+        
23
+        
24
+    def test_default_delimiter_is_set(self):
25
+        '''The DataHandler has to provide a default delimiter if not provided'''
26
+        testee = Testee()
27
+
28
+        self.assertTrue(hasattr(testee, 'delimiter'))
29
+    
30
+    
31
+    def test_set_custom_delimiter_success(self):
32
+        '''The custom delimiter isn't set as expected'''
33
+        delimiter = ';'
34
+        testee = Testee(delimiter=delimiter)
35
+        
36
+        self.assertEqual(delimiter, testee.delimiter)
37
+        
38
+    
39
+    def test_custom_delimiter_not_string_raises_error(self):
40
+        '''DataHandler\'s init should raise an exception on trying to pass incorrect type delimiter'''
41
+
42
+        self.assertRaises(LodelException, Testee, 123456789)
43
+        
44
+        
45
+    def test_construct_datas_success(self):
46
+        '''Basic usage of VarcharList doesn't behave as expected'''
47
+        string = 'a b c'
48
+        testee = Testee()
49
+        
50
+        actual_result = testee.construct_data(None, None, None, self.string)
51
+        self.assertEqual(self.expected_result, actual_result)
52
+        
53
+
54
+    def test_constuct_datas_with_custom_delimiter_success(self):
55
+        '''The use of custom delimiter doesn't work as supposed to !'''
56
+        delimiter = ';'
57
+        string = self.string.replace(' ', delimiter)
58
+        testee = Testee(delimiter=delimiter)
59
+        
60
+        actual_result = testee.construct_data(None, None, None, string)
61
+        self.assertEqual(self.expected_result, actual_result)

Loading…
Cancel
Save