暫無描述
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_regex.py 2.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import unittest
  2. from lodel.exceptions import *
  3. from lodel.leapi.datahandlers.datas import Regex, Varchar, Integer, UniqID
  4. from lodel.leapi.datahandlers.base_classes import DataHandler
  5. from unittest.mock import Mock
  6. import re
  7. class RegexTestCase(unittest.TestCase):
  8. def test_invalid_regex_throws_TypeError(self):
  9. self.assertRaises(TypeError, Regex().__init__, object)
  10. def test_compiled_re_property_is_set(self):
  11. self.assertIs(self.testee.compiled_re, re.compile(self.testee.regex))
  12. def test_exceeding_value_length_throws_FieldValidationError(self):
  13. self.assertRaises(
  14. FieldValidationError,
  15. self.testee._check_data_value,
  16. self.valid_value*self.max_length
  17. )
  18. def test_invalid_field_value_throws_FieldValidationError(self):
  19. self.assertRaises(
  20. FieldValidationError,
  21. self.testee._check_data_value,
  22. ''
  23. )
  24. def test_valid_field_value_is_returned(self):
  25. self.assertEqual(self.valid_value, self.testee._check_data_value(self.valid_value))
  26. def test_can_override_returns_false_if_different_datahandler_base_type(self):
  27. mock = self._get_datahandler_mock()
  28. mock.__class__.base_type = self.testee.base_type*2
  29. self.assertFalse(self.testee.can_override(mock))
  30. def test_can_override_returns_false_if_different_datahandler_maxlen(self):
  31. mock = self._get_datahandler_mock()
  32. mock.__class__.max_length = self.max_length*2
  33. self.assertFalse(self.testee.can_override(mock))
  34. def test_can_override_returns_true_if_overridable(self):
  35. self.assertTrue(self.testee.can_override(self._get_datahandler_mock()))
  36. def _get_datahandler_mock(self):
  37. dataHandlerMock = Mock()
  38. dataHandlerMock.__class__.base_type = self.testee.base_type
  39. dataHandlerMock.__class__.max_length = self.testee.max_length
  40. return dataHandlerMock
  41. def setUp(self):
  42. self.max_length = 15
  43. self.regex = '^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$'
  44. self.valid_value = '126.205.255.12'
  45. self.testee = Regex(self.regex, self.max_length)