summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJulian Berman <Julian@GrayVines.com>2021-08-25 10:20:26 +0100
committerJulian Berman <Julian@GrayVines.com>2021-08-25 10:20:26 +0100
commit3992d99a74099c99cc02338ecffa5647d0a2bdc4 (patch)
tree7424776ef7c3a3fd07be663858772c1644ee21f3
parent47c91a029ae43786fdc7ef333676d2b897d54b64 (diff)
downloadjsonschema-3992d99a74099c99cc02338ecffa5647d0a2bdc4.tar.gz
Ensure message_for allows only one error.
This prevents some nondeterminism (if there were multiple errors that came out in different orders). These tests don't really deal with multiple errors, so this seems 'safe' from a convenience perspective to require.
-rw-r--r--jsonschema/tests/test_validators.py16
1 files changed, 11 insertions, 5 deletions
diff --git a/jsonschema/tests/test_validators.py b/jsonschema/tests/test_validators.py
index e3e4bbc..91d3819 100644
--- a/jsonschema/tests/test_validators.py
+++ b/jsonschema/tests/test_validators.py
@@ -222,9 +222,15 @@ class TestValidationErrorMessages(TestCase):
def message_for(self, instance, schema, *args, **kwargs):
cls = kwargs.pop("cls", validators._LATEST_VERSION)
cls.check_schema(schema)
- with self.assertRaises(exceptions.ValidationError) as e:
- cls(schema, *args, **kwargs).validate(instance)
- return e.exception.message
+ validator = cls(schema, *args, **kwargs)
+ errors = list(validator.iter_errors(instance))
+ self.assertTrue(errors, msg=f"No errors were raised for {instance!r}")
+ self.assertEqual(
+ len(errors),
+ 1,
+ msg=f"Expected exactly one error, found {errors!r}",
+ )
+ return errors[0].message
def test_single_type_failure(self):
message = self.message_for(instance=1, schema={"type": "string"})
@@ -484,7 +490,7 @@ class TestValidationErrorMessages(TestCase):
def test_contains_too_many(self):
message = self.message_for(
- instance=["foo", "bar", "baz", "quux"],
+ instance=["foo", "bar", "baz"],
schema={"contains": {"type": "string"}, "maxContains": 2},
)
self.assertEqual(
@@ -494,7 +500,7 @@ class TestValidationErrorMessages(TestCase):
def test_contains_too_many_both_constrained(self):
message = self.message_for(
- instance=["foo"] * 7,
+ instance=["foo"] * 5,
schema={
"contains": {"type": "string"},
"minContains": 2,