Browse Source

Added two unit tests for the format string datahandler + fix on a slicing end value

Roland Haroutiounian 8 years ago
parent
commit
49e893aa37
2 changed files with 29 additions and 1 deletions
  1. 1
    1
      lodel/leapi/datahandlers/datas.py
  2. 28
    0
      tests/datahandlers/test_formatstring.py

+ 1
- 1
lodel/leapi/datahandlers/datas.py View File

@@ -26,7 +26,7 @@ build its content'
26 26
             datas[fname] for fname in self._field_list)
27 27
         if len(ret) > self.max_length:
28 28
             warnings.warn("Format field overflow. Truncating value")
29
-            ret = ret[:self.max_length-1]
29
+            ret = ret[:self.max_length]
30 30
         return ret
31 31
     
32 32
 ##@brief Varchar validated by a regex

+ 28
- 0
tests/datahandlers/test_formatstring.py View File

@@ -0,0 +1,28 @@
1
+import unittest
2
+
3
+from lodel.leapi.datahandlers.datas import FormatString
4
+from lodel.editorial_model.components import EmClass
5
+
6
+
7
+class FormatStringTestCase(unittest.TestCase):
8
+
9
+    def test_construct_data(self):
10
+        test_class = EmClass('testing',display_name='testing class')
11
+        test_class.new_field('field1', 'varchar')
12
+        test_class.new_field('field2', 'varchar')
13
+
14
+        test_formatstring = FormatString('%s_%s',['field1', 'field2'], max_length=10)
15
+        formatted_string_value = test_formatstring.construct_data(test_class, 'field', {'field1': 'o'*5, 'field2': 'k'*4}, '')
16
+        self.assertEqual('%s_%s' % ('o'*5, 'k'*4), formatted_string_value)
17
+
18
+    def test_construct_too_long_data(self):
19
+        test_class = EmClass('testing', display_name='testing class')
20
+        test_class.new_field('field1', 'varchar')
21
+        test_class.new_field('field2', 'varchar')
22
+        test_formatstring = FormatString('%s-%s', ['field2', 'field1'], max_length=10)
23
+        formatted_string_value = test_formatstring.construct_data(test_class, 'field', {'field1': 'o'*300, 'field2': 'k'*500},'')
24
+        test_value = '%s-%s' % ('k'*500, 'o'*300)
25
+        self.assertNotEqual(test_value, formatted_string_value)
26
+        self.assertTrue(formatted_string_value in test_value)
27
+        self.assertTrue(len(formatted_string_value) == test_formatstring.max_length)
28
+        self.assertEqual(formatted_string_value, test_value[:test_formatstring.max_length])

Loading…
Cancel
Save