test_format.py 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. """
  2. Tests for the parts of jsonschema related to the :kw:`format` keyword.
  3. """
  4. from unittest import TestCase
  5. from jsonschema import FormatChecker, ValidationError
  6. from jsonschema.exceptions import FormatError
  7. from jsonschema.validators import Draft4Validator
  8. BOOM = ValueError("Boom!")
  9. BANG = ZeroDivisionError("Bang!")
  10. def boom(thing):
  11. if thing == "bang":
  12. raise BANG
  13. raise BOOM
  14. class TestFormatChecker(TestCase):
  15. def test_it_can_validate_no_formats(self):
  16. checker = FormatChecker(formats=())
  17. self.assertFalse(checker.checkers)
  18. def test_it_raises_a_key_error_for_unknown_formats(self):
  19. with self.assertRaises(KeyError):
  20. FormatChecker(formats=["o noes"])
  21. def test_it_can_register_cls_checkers(self):
  22. original = dict(FormatChecker.checkers)
  23. self.addCleanup(FormatChecker.checkers.pop, "boom")
  24. with self.assertWarns(DeprecationWarning):
  25. FormatChecker.cls_checks("boom")(boom)
  26. self.assertEqual(
  27. FormatChecker.checkers,
  28. dict(original, boom=(boom, ())),
  29. )
  30. def test_it_can_register_checkers(self):
  31. checker = FormatChecker()
  32. checker.checks("boom")(boom)
  33. self.assertEqual(
  34. checker.checkers,
  35. dict(FormatChecker.checkers, boom=(boom, ())),
  36. )
  37. def test_it_catches_registered_errors(self):
  38. checker = FormatChecker()
  39. checker.checks("boom", raises=type(BOOM))(boom)
  40. with self.assertRaises(FormatError) as cm:
  41. checker.check(instance=12, format="boom")
  42. self.assertIs(cm.exception.cause, BOOM)
  43. self.assertIs(cm.exception.__cause__, BOOM)
  44. self.assertEqual(str(cm.exception), "12 is not a 'boom'")
  45. # Unregistered errors should not be caught
  46. with self.assertRaises(type(BANG)):
  47. checker.check(instance="bang", format="boom")
  48. def test_format_error_causes_become_validation_error_causes(self):
  49. checker = FormatChecker()
  50. checker.checks("boom", raises=ValueError)(boom)
  51. validator = Draft4Validator({"format": "boom"}, format_checker=checker)
  52. with self.assertRaises(ValidationError) as cm:
  53. validator.validate("BOOM")
  54. self.assertIs(cm.exception.cause, BOOM)
  55. self.assertIs(cm.exception.__cause__, BOOM)
  56. def test_format_checkers_come_with_defaults(self):
  57. # This is bad :/ but relied upon.
  58. # The docs for quite awhile recommended people do things like
  59. # validate(..., format_checker=FormatChecker())
  60. # We should change that, but we can't without deprecation...
  61. checker = FormatChecker()
  62. with self.assertRaises(FormatError):
  63. checker.check(instance="not-an-ipv4", format="ipv4")
  64. def test_repr(self):
  65. checker = FormatChecker(formats=())
  66. checker.checks("foo")(lambda thing: True) # pragma: no cover
  67. checker.checks("bar")(lambda thing: True) # pragma: no cover
  68. checker.checks("baz")(lambda thing: True) # pragma: no cover
  69. self.assertEqual(
  70. repr(checker),
  71. "<FormatChecker checkers=['bar', 'baz', 'foo']>",
  72. )