Bez popisu
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 3.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #
  2. # This file is part of Lodel 2 (https://github.com/OpenEdition)
  3. #
  4. # Copyright (C) 2015-2017 Cléo UMS-3287
  5. #
  6. # This program is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU Affero General Public License as published
  8. # by the Free Software Foundation, either version 3 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU Affero General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU Affero General Public License
  17. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. #
  19. import unittest
  20. from lodel.leapi.datahandlers.datas import FormatString
  21. from lodel.editorial_model.components import EmClass
  22. class FormatStringTestCase(unittest.TestCase):
  23. def test_base_type_is_char(self):
  24. self.assertEqual(FormatString.base_type, 'char')
  25. def test_help_property_str_is_set(self):
  26. self.assertEqual(type(FormatString.help), str)
  27. def test_string_formatting_output(self):
  28. testee = FormatString(self.regex,[self.fone_name, self.ftwo_name])
  29. expected_result = self.regex % (self.field_one_value, self.field_two_value)
  30. actual_result = testee.construct_data(self.dummyEmClass, None, self.field_value_dict, None)
  31. self.assertEqual(actual_result, expected_result)
  32. def test_throws_user_warning_on_exceeding_string(self):
  33. max_length = len(self.regex % (self.field_one_value, self.field_two_value)) / 2
  34. testee = FormatString(self.regex, self.field_list, max_length=self.max_length)
  35. self.assertWarns(
  36. UserWarning,
  37. testee.construct_data,
  38. self.dummyEmClass, None, self.field_value_dict, None
  39. )
  40. def test_exceeding_string_length_is_truncated(self):
  41. max_length = 2
  42. testee = FormatString(self.regex, self.field_list, max_length=self.max_length)
  43. result_string = testee._construct_data(self.dummyEmClass, None, self.field_value_dict, None)
  44. self.assertEqual(self.max_length, len(result_string))
  45. def test_thrown_exceeding_str_warning_message_composition(self):
  46. testee = FormatString(self.regex, self.field_list, max_length=self.max_length)
  47. self.assertWarnsRegex(
  48. UserWarning,
  49. '[T|t]runcat[a-z]+',
  50. testee.construct_data,
  51. self.dummyEmClass, None, self.field_value_dict, None
  52. )
  53. def setUp(self):
  54. self.regex = '%s-%s'
  55. self.dummyEmClass = EmClass('testing')
  56. self.fone_name = 'field1'
  57. self.ftwo_name = 'field2'
  58. self.dummyEmClass.new_field(self.fone_name, 'varchar')
  59. self.dummyEmClass.new_field(self.ftwo_name, 'varchar')
  60. self.field_one_value = 'field one value'
  61. self.field_two_value = 'field two value'
  62. self.field_value_dict = {self.fone_name: self.field_one_value, self.ftwo_name: self.field_two_value}
  63. self.field_list = [self.fone_name, self.ftwo_name]
  64. self.max_length = round(len(self.regex % (self.field_one_value, self.field_two_value)) / 2)