Skip to content

Commit 28a7598

Browse files
committed
Run best match tests on the latest validator.
1 parent 32b277f commit 28a7598

File tree

1 file changed

+67
-78
lines changed

1 file changed

+67
-78
lines changed

jsonschema/tests/test_exceptions.py

Lines changed: 67 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,32 @@
11
from unittest import TestCase
22
import textwrap
33

4-
from jsonschema import Draft4Validator, exceptions
4+
from jsonschema import exceptions
5+
from jsonschema.validators import _LATEST_VERSION
56

67

78
class TestBestMatch(TestCase):
8-
def best_match(self, errors):
9-
errors = list(errors)
10-
best = exceptions.best_match(errors)
9+
def best_match_of(self, instance, schema):
10+
errors = list(_LATEST_VERSION(schema).iter_errors(instance))
11+
best = exceptions.best_match(iter(errors))
1112
reversed_best = exceptions.best_match(reversed(errors))
12-
msg = "Didn't return a consistent best match!\nGot: {0}\n\nThen: {1}"
1313
self.assertEqual(
14-
best._contents(), reversed_best._contents(),
15-
msg=msg.format(best, reversed_best),
14+
best._contents(),
15+
reversed_best._contents(),
16+
f"No consistent best match!\nGot: {best}\n\nThen: {reversed_best}",
1617
)
1718
return best
1819

1920
def test_shallower_errors_are_better_matches(self):
20-
validator = Draft4Validator(
21-
{
22-
"properties": {
23-
"foo": {
24-
"minProperties": 2,
25-
"properties": {"bar": {"type": "object"}},
26-
},
21+
schema = {
22+
"properties": {
23+
"foo": {
24+
"minProperties": 2,
25+
"properties": {"bar": {"type": "object"}},
2726
},
2827
},
29-
)
30-
best = self.best_match(validator.iter_errors({"foo": {"bar": []}}))
28+
}
29+
best = self.best_match_of(instance={"foo": {"bar": []}}, schema=schema)
3130
self.assertEqual(best.validator, "minProperties")
3231

3332
def test_oneOf_and_anyOf_are_weak_matches(self):
@@ -36,14 +35,12 @@ def test_oneOf_and_anyOf_are_weak_matches(self):
3635
match a part of.
3736
"""
3837

39-
validator = Draft4Validator(
40-
{
41-
"minProperties": 2,
42-
"anyOf": [{"type": "string"}, {"type": "number"}],
43-
"oneOf": [{"type": "string"}, {"type": "number"}],
44-
},
45-
)
46-
best = self.best_match(validator.iter_errors({}))
38+
schema = {
39+
"minProperties": 2,
40+
"anyOf": [{"type": "string"}, {"type": "number"}],
41+
"oneOf": [{"type": "string"}, {"type": "number"}],
42+
}
43+
best = self.best_match_of(instance={}, schema=schema)
4744
self.assertEqual(best.validator, "minProperties")
4845

4946
def test_if_the_most_relevant_error_is_anyOf_it_is_traversed(self):
@@ -56,19 +53,17 @@ def test_if_the_most_relevant_error_is_anyOf_it_is_traversed(self):
5653
relevant one.
5754
"""
5855

59-
validator = Draft4Validator(
60-
{
61-
"properties": {
62-
"foo": {
63-
"anyOf": [
64-
{"type": "string"},
65-
{"properties": {"bar": {"type": "array"}}},
66-
],
67-
},
56+
schema = {
57+
"properties": {
58+
"foo": {
59+
"anyOf": [
60+
{"type": "string"},
61+
{"properties": {"bar": {"type": "array"}}},
62+
],
6863
},
6964
},
70-
)
71-
best = self.best_match(validator.iter_errors({"foo": {"bar": 12}}))
65+
}
66+
best = self.best_match_of(instance={"foo": {"bar": 12}}, schema=schema)
7267
self.assertEqual(best.validator_value, "array")
7368

7469
def test_if_the_most_relevant_error_is_oneOf_it_is_traversed(self):
@@ -81,19 +76,17 @@ def test_if_the_most_relevant_error_is_oneOf_it_is_traversed(self):
8176
relevant one.
8277
"""
8378

84-
validator = Draft4Validator(
85-
{
86-
"properties": {
87-
"foo": {
88-
"oneOf": [
89-
{"type": "string"},
90-
{"properties": {"bar": {"type": "array"}}},
91-
],
92-
},
79+
schema = {
80+
"properties": {
81+
"foo": {
82+
"oneOf": [
83+
{"type": "string"},
84+
{"properties": {"bar": {"type": "array"}}},
85+
],
9386
},
9487
},
95-
)
96-
best = self.best_match(validator.iter_errors({"foo": {"bar": 12}}))
88+
}
89+
best = self.best_match_of(instance={"foo": {"bar": 12}}, schema=schema)
9790
self.assertEqual(best.validator_value, "array")
9891

9992
def test_if_the_most_relevant_error_is_allOf_it_is_traversed(self):
@@ -102,56 +95,52 @@ def test_if_the_most_relevant_error_is_allOf_it_is_traversed(self):
10295
error from the context, because all schemas here must match anyways.
10396
"""
10497

105-
validator = Draft4Validator(
106-
{
107-
"properties": {
108-
"foo": {
109-
"allOf": [
110-
{"type": "string"},
111-
{"properties": {"bar": {"type": "array"}}},
112-
],
113-
},
98+
schema = {
99+
"properties": {
100+
"foo": {
101+
"allOf": [
102+
{"type": "string"},
103+
{"properties": {"bar": {"type": "array"}}},
104+
],
114105
},
115106
},
116-
)
117-
best = self.best_match(validator.iter_errors({"foo": {"bar": 12}}))
107+
}
108+
best = self.best_match_of(instance={"foo": {"bar": 12}}, schema=schema)
118109
self.assertEqual(best.validator_value, "string")
119110

120111
def test_nested_context_for_oneOf(self):
121-
validator = Draft4Validator(
122-
{
123-
"properties": {
124-
"foo": {
125-
"oneOf": [
126-
{"type": "string"},
127-
{
128-
"oneOf": [
129-
{"type": "string"},
130-
{
131-
"properties": {
132-
"bar": {"type": "array"},
133-
},
112+
schema = {
113+
"properties": {
114+
"foo": {
115+
"oneOf": [
116+
{"type": "string"},
117+
{
118+
"oneOf": [
119+
{"type": "string"},
120+
{
121+
"properties": {
122+
"bar": {"type": "array"},
134123
},
135-
],
136-
},
137-
],
138-
},
124+
},
125+
],
126+
},
127+
],
139128
},
140129
},
141-
)
142-
best = self.best_match(validator.iter_errors({"foo": {"bar": 12}}))
130+
}
131+
best = self.best_match_of(instance={"foo": {"bar": 12}}, schema=schema)
143132
self.assertEqual(best.validator_value, "array")
144133

145134
def test_one_error(self):
146-
validator = Draft4Validator({"minProperties": 2})
135+
validator = _LATEST_VERSION({"minProperties": 2})
147136
error, = validator.iter_errors({})
148137
self.assertEqual(
149138
exceptions.best_match(validator.iter_errors({})).validator,
150139
"minProperties",
151140
)
152141

153142
def test_no_errors(self):
154-
validator = Draft4Validator({})
143+
validator = _LATEST_VERSION({})
155144
self.assertIsNone(exceptions.best_match(validator.iter_errors({})))
156145

157146

0 commit comments

Comments
 (0)