1
1
from unittest import TestCase
2
2
import textwrap
3
3
4
- from jsonschema import Draft4Validator , exceptions
4
+ from jsonschema import exceptions
5
+ from jsonschema .validators import _LATEST_VERSION
5
6
6
7
7
8
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 ) )
11
12
reversed_best = exceptions .best_match (reversed (errors ))
12
- msg = "Didn't return a consistent best match!\n Got: {0}\n \n Then: {1}"
13
13
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!\n Got: { best } \n \n Then: { reversed_best } " ,
16
17
)
17
18
return best
18
19
19
20
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" }},
27
26
},
28
27
},
29
- )
30
- best = self .best_match ( validator . iter_errors ( {"foo" : {"bar" : []}}) )
28
+ }
29
+ best = self .best_match_of ( instance = {"foo" : {"bar" : []}}, schema = schema )
31
30
self .assertEqual (best .validator , "minProperties" )
32
31
33
32
def test_oneOf_and_anyOf_are_weak_matches (self ):
@@ -36,14 +35,12 @@ def test_oneOf_and_anyOf_are_weak_matches(self):
36
35
match a part of.
37
36
"""
38
37
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 )
47
44
self .assertEqual (best .validator , "minProperties" )
48
45
49
46
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):
56
53
relevant one.
57
54
"""
58
55
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
+ ],
68
63
},
69
64
},
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 )
72
67
self .assertEqual (best .validator_value , "array" )
73
68
74
69
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):
81
76
relevant one.
82
77
"""
83
78
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
+ ],
93
86
},
94
87
},
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 )
97
90
self .assertEqual (best .validator_value , "array" )
98
91
99
92
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):
102
95
error from the context, because all schemas here must match anyways.
103
96
"""
104
97
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
+ ],
114
105
},
115
106
},
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 )
118
109
self .assertEqual (best .validator_value , "string" )
119
110
120
111
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" },
134
123
},
135
- ] ,
136
- } ,
137
- ] ,
138
- } ,
124
+ } ,
125
+ ] ,
126
+ } ,
127
+ ] ,
139
128
},
140
129
},
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 )
143
132
self .assertEqual (best .validator_value , "array" )
144
133
145
134
def test_one_error (self ):
146
- validator = Draft4Validator ({"minProperties" : 2 })
135
+ validator = _LATEST_VERSION ({"minProperties" : 2 })
147
136
error , = validator .iter_errors ({})
148
137
self .assertEqual (
149
138
exceptions .best_match (validator .iter_errors ({})).validator ,
150
139
"minProperties" ,
151
140
)
152
141
153
142
def test_no_errors (self ):
154
- validator = Draft4Validator ({})
143
+ validator = _LATEST_VERSION ({})
155
144
self .assertIsNone (exceptions .best_match (validator .iter_errors ({})))
156
145
157
146
0 commit comments