|
@@ -1,28 +1,66 @@
|
1
|
1
|
import unittest
|
2
|
2
|
|
3
|
|
-from lodel.leapi.datahandlers.datas import FormatString
|
|
3
|
+from lodel.leapi.datahandlers.datas import FormatString as Testee
|
4
|
4
|
from lodel.editorial_model.components import EmClass
|
5
|
5
|
|
|
6
|
+class TesteeTestCase(unittest.TestCase):
|
|
7
|
+ regex = '%s-%s'
|
|
8
|
+ dummyEmClass = EmClass('testing')
|
|
9
|
+ fone_name = 'field1'
|
|
10
|
+ ftwo_name = 'field2'
|
|
11
|
+ dummyEmClass.new_field(fone_name, 'varchar')
|
|
12
|
+ dummyEmClass.new_field(ftwo_name, 'varchar')
|
|
13
|
+ field_one_value = 'field one value'
|
|
14
|
+ field_two_value = 'field two value'
|
|
15
|
+ field_value_dict = {fone_name: field_one_value, ftwo_name: field_two_value}
|
|
16
|
+ field_list = [fone_name, ftwo_name]
|
|
17
|
+
|
|
18
|
+ test_Testee = Testee(regex,[fone_name, ftwo_name])
|
|
19
|
+
|
|
20
|
+ max_length = round(len(regex % (field_one_value, field_two_value)) / 2)
|
6
|
21
|
|
7
|
|
-class FormatStringTestCase(unittest.TestCase):
|
8
|
22
|
|
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')
|
|
23
|
+ def test_has_base_type_property(self):
|
|
24
|
+ self.assertTrue(hasattr(Testee, 'base_type'))
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+ def test_base_type_is_char(self):
|
|
28
|
+ self.assertEqual(Testee.base_type, 'char')
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+ def test_has_help_property(self):
|
|
32
|
+ self.assertTrue(hasattr(Testee, 'help'))
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+ def test_help_property_str_is_set(self):
|
|
36
|
+ self.assertEqual(type(Testee.help), str)
|
13
|
37
|
|
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
|
38
|
|
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])
|
|
39
|
+ def test_string_formatting_output(self):
|
|
40
|
+ expected_result = self.regex % (self.field_one_value, self.field_two_value)
|
|
41
|
+ actual_result = self.test_Testee.construct_data(self.dummyEmClass, None, self.field_value_dict, None)
|
|
42
|
+
|
|
43
|
+ self.assertEqual(actual_result, expected_result)
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+ def test_throws_user_warning_on_exceeding_string(self):
|
|
47
|
+ max_length = len(self.regex % (self.field_one_value, self.field_two_value)) / 2
|
|
48
|
+ testee = Testee(self.regex, self.field_list, max_length=self.max_length)
|
|
49
|
+
|
|
50
|
+ with self.assertWarns(UserWarning):
|
|
51
|
+ testee.construct_data(self.dummyEmClass, None, self.field_value_dict, None)
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+ def test_exceeding_string_length_is_truncated(self):
|
|
55
|
+ max_length = 2
|
|
56
|
+ testee = Testee(self.regex, self.field_list, max_length=self.max_length)
|
|
57
|
+ result_string = testee._construct_data(self.dummyEmClass, None, self.field_value_dict, None)
|
|
58
|
+
|
|
59
|
+ self.assertEqual(self.max_length, len(result_string))
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+ def test_thrown_exceeding_str_warning_message_composition(self):
|
|
63
|
+ testee = Testee(self.regex, self.field_list, max_length=self.max_length)
|
|
64
|
+
|
|
65
|
+ with self.assertWarnsRegex(UserWarning, '[T|t]runcat[a-z]+'):
|
|
66
|
+ testee.construct_data(self.dummyEmClass, None, self.field_value_dict, None)
|