Skip to content

Commit 3992d99

Browse files
committed
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.
1 parent 47c91a0 commit 3992d99

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

jsonschema/tests/test_validators.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -222,9 +222,15 @@ class TestValidationErrorMessages(TestCase):
222222
def message_for(self, instance, schema, *args, **kwargs):
223223
cls = kwargs.pop("cls", validators._LATEST_VERSION)
224224
cls.check_schema(schema)
225-
with self.assertRaises(exceptions.ValidationError) as e:
226-
cls(schema, *args, **kwargs).validate(instance)
227-
return e.exception.message
225+
validator = cls(schema, *args, **kwargs)
226+
errors = list(validator.iter_errors(instance))
227+
self.assertTrue(errors, msg=f"No errors were raised for {instance!r}")
228+
self.assertEqual(
229+
len(errors),
230+
1,
231+
msg=f"Expected exactly one error, found {errors!r}",
232+
)
233+
return errors[0].message
228234

229235
def test_single_type_failure(self):
230236
message = self.message_for(instance=1, schema={"type": "string"})
@@ -484,7 +490,7 @@ def test_contains_too_few_both_constrained(self):
484490

485491
def test_contains_too_many(self):
486492
message = self.message_for(
487-
instance=["foo", "bar", "baz", "quux"],
493+
instance=["foo", "bar", "baz"],
488494
schema={"contains": {"type": "string"}, "maxContains": 2},
489495
)
490496
self.assertEqual(
@@ -494,7 +500,7 @@ def test_contains_too_many(self):
494500

495501
def test_contains_too_many_both_constrained(self):
496502
message = self.message_for(
497-
instance=["foo"] * 7,
503+
instance=["foo"] * 5,
498504
schema={
499505
"contains": {"type": "string"},
500506
"minContains": 2,

0 commit comments

Comments
 (0)