Skip to content

Commit df7a4e4

Browse files
committed
Test iter_errors independent of a draft validator.
Removes some testing redundancy as well.
1 parent 3cf1d13 commit df7a4e4

File tree

1 file changed

+31
-47
lines changed

1 file changed

+31
-47
lines changed

jsonschema/tests/test_validators.py

Lines changed: 31 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,10 @@
1717
from jsonschema.tests._helpers import bug
1818

1919

20-
def startswith(validator, startswith, instance, schema):
21-
if not instance.startswith(startswith):
22-
yield exceptions.ValidationError("Whoops!")
20+
def fail(validator, errors, instance, schema):
21+
for each in errors:
22+
each.setdefault("message", "You told me to fail!")
23+
yield exceptions.ValidationError(**each)
2324

2425

2526
class TestCreateAndExtend(SynchronousTestCase):
@@ -31,7 +32,7 @@ def setUp(self):
3132
)
3233

3334
self.meta_schema = {"$id": "some://meta/schema"}
34-
self.validators = {"startswith": startswith}
35+
self.validators = {"fail": fail}
3536
self.type_checker = TypeChecker()
3637
self.Validator = validators.create(
3738
meta_schema=self.meta_schema,
@@ -53,29 +54,46 @@ def test_attrs(self):
5354
)
5455

5556
def test_init(self):
56-
schema = {"startswith": "foo"}
57+
schema = {"fail": []}
5758
self.assertEqual(self.Validator(schema).schema, schema)
5859

59-
def test_iter_errors(self):
60-
schema = {"startswith": "hel"}
61-
iter_errors = self.Validator(schema).iter_errors
60+
def test_iter_errors_successful(self):
61+
schema = {"fail": []}
62+
validator = self.Validator(schema)
6263

63-
errors = list(iter_errors("hello"))
64+
errors = list(validator.iter_errors("hello"))
6465
self.assertEqual(errors, [])
6566

67+
def test_iter_errors_one_error(self):
68+
schema = {"fail": [{"message": "Whoops!"}]}
69+
validator = self.Validator(schema)
70+
6671
expected_error = exceptions.ValidationError(
6772
"Whoops!",
6873
instance="goodbye",
6974
schema=schema,
70-
validator="startswith",
71-
validator_value="hel",
72-
schema_path=deque(["startswith"]),
75+
validator="fail",
76+
validator_value=[{"message": "Whoops!"}],
77+
schema_path=deque(["fail"]),
7378
)
7479

75-
errors = list(iter_errors("goodbye"))
80+
errors = list(validator.iter_errors("goodbye"))
7681
self.assertEqual(len(errors), 1)
7782
self.assertEqual(errors[0]._contents(), expected_error._contents())
7883

84+
def test_iter_errors_multiple_errors(self):
85+
schema = {
86+
"fail": [
87+
{"message": "First"},
88+
{"message": "Second!", "validator": "asdf"},
89+
{"message": "Third"},
90+
],
91+
}
92+
validator = self.Validator(schema)
93+
94+
errors = list(validator.iter_errors("goodbye"))
95+
self.assertEqual(len(errors), 3)
96+
7997
def test_if_a_version_is_provided_it_is_registered(self):
8098
Validator = validators.create(
8199
meta_schema={"$id": "something"},
@@ -216,40 +234,6 @@ def id_of(schema):
216234
self.assertEqual(Derived.ID_OF(Derived.META_SCHEMA), correct_id)
217235

218236

219-
class TestIterErrors(TestCase):
220-
def setUp(self):
221-
self.validator = validators.Draft3Validator({})
222-
223-
def test_iter_errors(self):
224-
instance = [1, 2]
225-
schema = {
226-
"disallow": "array",
227-
"enum": [["a", "b", "c"], ["d", "e", "f"]],
228-
"minItems": 3,
229-
}
230-
231-
got = (e.message for e in self.validator.iter_errors(instance, schema))
232-
expected = [
233-
f"{schema['disallow']!r} is disallowed for [1, 2]",
234-
"[1, 2] is too short",
235-
f"[1, 2] is not one of {schema['enum']}",
236-
]
237-
self.assertEqual(sorted(got), sorted(expected))
238-
239-
def test_iter_errors_multiple_failures_one_validator(self):
240-
instance = {"foo": 2, "bar": [1], "baz": 15, "quux": "spam"}
241-
schema = {
242-
"properties": {
243-
"foo": {"type": "string"},
244-
"bar": {"minItems": 2},
245-
"baz": {"maximum": 10, "enum": [2, 4, 6, 8]},
246-
},
247-
}
248-
249-
errors = list(self.validator.iter_errors(instance, schema))
250-
self.assertEqual(len(errors), 4)
251-
252-
253237
class TestValidationErrorMessages(TestCase):
254238
def message_for(self, instance, schema, *args, **kwargs):
255239
cls = kwargs.pop("cls", validators._LATEST_VERSION)

0 commit comments

Comments
 (0)