No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_formatstring.py 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import unittest
  2. from lodel.leapi.datahandlers.datas import FormatString
  3. from lodel.editorial_model.components import EmClass
  4. class FormatStringTestCase(unittest.TestCase):
  5. def test_base_type_is_char(self):
  6. self.assertEqual(FormatString.base_type, 'char')
  7. def test_help_property_str_is_set(self):
  8. self.assertEqual(type(FormatString.help), str)
  9. def test_string_formatting_output(self):
  10. testee = FormatString(self.regex,[self.fone_name, self.ftwo_name])
  11. expected_result = self.regex % (self.field_one_value, self.field_two_value)
  12. actual_result = testee.construct_data(self.dummyEmClass, None, self.field_value_dict, None)
  13. self.assertEqual(actual_result, expected_result)
  14. def test_throws_user_warning_on_exceeding_string(self):
  15. max_length = len(self.regex % (self.field_one_value, self.field_two_value)) / 2
  16. testee = FormatString(self.regex, self.field_list, max_length=self.max_length)
  17. self.assertWarns(
  18. UserWarning,
  19. testee.construct_data,
  20. self.dummyEmClass, None, self.field_value_dict, None
  21. )
  22. def test_exceeding_string_length_is_truncated(self):
  23. max_length = 2
  24. testee = FormatString(self.regex, self.field_list, max_length=self.max_length)
  25. result_string = testee._construct_data(self.dummyEmClass, None, self.field_value_dict, None)
  26. self.assertEqual(self.max_length, len(result_string))
  27. def test_thrown_exceeding_str_warning_message_composition(self):
  28. testee = FormatString(self.regex, self.field_list, max_length=self.max_length)
  29. self.assertWarnsRegex(
  30. UserWarning,
  31. '[T|t]runcat[a-z]+',
  32. testee.construct_data,
  33. self.dummyEmClass, None, self.field_value_dict, None
  34. )
  35. def setUp(self):
  36. self.regex = '%s-%s'
  37. self.dummyEmClass = EmClass('testing')
  38. self.fone_name = 'field1'
  39. self.ftwo_name = 'field2'
  40. self.dummyEmClass.new_field(self.fone_name, 'varchar')
  41. self.dummyEmClass.new_field(self.ftwo_name, 'varchar')
  42. self.field_one_value = 'field one value'
  43. self.field_two_value = 'field two value'
  44. self.field_value_dict = {self.fone_name: self.field_one_value, self.ftwo_name: self.field_two_value}
  45. self.field_list = [self.fone_name, self.ftwo_name]
  46. self.max_length = round(len(self.regex % (self.field_one_value, self.field_two_value)) / 2)