summaryrefslogtreecommitdiff
path: root/jsonschema/tests/test_format.py
diff options
context:
space:
mode:
authorJulian Berman <Julian@GrayVines.com>2018-08-09 13:35:11 +0300
committerJulian Berman <Julian@GrayVines.com>2018-08-09 15:22:59 +0300
commit09aed744194a57449d60cd74ef92580282032ed3 (patch)
treebdacebc1e2f3015a5fe8a2a9dc988a04b312eab5 /jsonschema/tests/test_format.py
parentc9d4944c2946d13686bf8218925547b94517e737 (diff)
downloadjsonschema-09aed744194a57449d60cd74ef92580282032ed3.tar.gz
More dead mocks.
Diffstat (limited to 'jsonschema/tests/test_format.py')
-rw-r--r--jsonschema/tests/test_format.py53
1 files changed, 30 insertions, 23 deletions
diff --git a/jsonschema/tests/test_format.py b/jsonschema/tests/test_format.py
index c8d2634..e5250f4 100644
--- a/jsonschema/tests/test_format.py
+++ b/jsonschema/tests/test_format.py
@@ -6,14 +6,20 @@ Tests for the parts of jsonschema related to the :validator:`format` property.
from unittest import TestCase
from jsonschema import FormatError, ValidationError, FormatChecker
-from jsonschema.tests.compat import mock
from jsonschema.validators import Draft4Validator
-class TestFormatChecker(TestCase):
- def setUp(self):
- self.fn = mock.Mock()
+BOOM = ValueError("Boom!")
+BANG = ZeroDivisionError("Bang!")
+
+
+def boom(thing):
+ if thing == "bang":
+ raise BANG
+ raise BOOM
+
+class TestFormatChecker(TestCase):
def test_it_can_validate_no_formats(self):
checker = FormatChecker(formats=())
self.assertFalse(checker.checkers)
@@ -23,42 +29,43 @@ class TestFormatChecker(TestCase):
FormatChecker(formats=["o noes"])
def test_it_can_register_cls_checkers(self):
- with mock.patch.dict(FormatChecker.checkers, clear=True):
- FormatChecker.cls_checks("new")(self.fn)
- self.assertEqual(FormatChecker.checkers, {"new": (self.fn, ())})
+ original = dict(FormatChecker.checkers)
+ self.addCleanup(FormatChecker.checkers.pop, "boom")
+ FormatChecker.cls_checks("boom")(boom)
+ self.assertEqual(
+ FormatChecker.checkers,
+ dict(original, boom=(boom, ())),
+ )
def test_it_can_register_checkers(self):
checker = FormatChecker()
- checker.checks("new")(self.fn)
+ checker.checks("boom")(boom)
self.assertEqual(
checker.checkers,
- dict(FormatChecker.checkers, new=(self.fn, ()))
+ dict(FormatChecker.checkers, boom=(boom, ()))
)
def test_it_catches_registered_errors(self):
checker = FormatChecker()
- cause = self.fn.side_effect = ValueError()
-
- checker.checks("foo", raises=ValueError)(self.fn)
+ checker.checks("boom", raises=type(BOOM))(boom)
with self.assertRaises(FormatError) as cm:
- checker.check("bar", "foo")
+ checker.check(instance=12, format="boom")
- self.assertIs(cm.exception.cause, cause)
- self.assertIs(cm.exception.__cause__, cause)
+ self.assertIs(cm.exception.cause, BOOM)
+ self.assertIs(cm.exception.__cause__, BOOM)
# Unregistered errors should not be caught
- self.fn.side_effect = AttributeError
- with self.assertRaises(AttributeError):
- checker.check("bar", "foo")
+ with self.assertRaises(type(BANG)):
+ checker.check(instance="bang", format="boom")
def test_format_error_causes_become_validation_error_causes(self):
checker = FormatChecker()
- checker.checks("foo", raises=ValueError)(self.fn)
- cause = self.fn.side_effect = ValueError()
- validator = Draft4Validator({"format": "foo"}, format_checker=checker)
+ checker.checks("boom", raises=ValueError)(boom)
+ validator = Draft4Validator({"format": "boom"}, format_checker=checker)
with self.assertRaises(ValidationError) as cm:
- validator.validate("bar")
+ validator.validate("BOOM")
- self.assertIs(cm.exception.__cause__, cause)
+ self.assertIs(cm.exception.cause, BOOM)
+ self.assertIs(cm.exception.__cause__, BOOM)