1
1
import re
2
+ from enum import Enum
2
3
3
4
import pytest
4
5
@@ -106,6 +107,7 @@ def test_literal_py_and_json(py_and_json: PyAndJson, kwarg_expected, input_value
106
107
with pytest .raises (ValidationError , match = re .escape (expected .message )) as exc_info :
107
108
v .validate_test (input_value )
108
109
if expected .errors is not None :
110
+ # debug(exc_info.value.errors())
109
111
assert exc_info .value .errors () == expected .errors
110
112
else :
111
113
assert v .validate_test (input_value ) == expected
@@ -123,13 +125,34 @@ def test_literal_py_and_json(py_and_json: PyAndJson, kwarg_expected, input_value
123
125
Err ("Input should be 1 or b'whatever' [kind=literal_error, input_value=3, input_type=int]" ),
124
126
id = 'wrong-general' ,
125
127
),
128
+ ([b'bite' ], b'bite' , b'bite' ),
129
+ pytest .param (
130
+ [b'bite' ],
131
+ 'spoon' ,
132
+ Err (
133
+ "Input should be b'bite' [kind=literal_error, input_value='spoon', input_type=str]" ,
134
+ [
135
+ {
136
+ 'kind' : 'literal_error' ,
137
+ 'loc' : [],
138
+ 'message' : "Input should be 1 or '1'" ,
139
+ 'input_value' : '2' ,
140
+ 'context' : {'expected' : "1 or '1'" },
141
+ }
142
+ ],
143
+ ),
144
+ id = 'single-byte' ,
145
+ ),
126
146
],
127
147
)
128
148
def test_literal_not_json (kwarg_expected , input_value , expected ):
129
149
v = SchemaValidator ({'type' : 'literal' , 'expected' : kwarg_expected })
130
150
if isinstance (expected , Err ):
131
- with pytest .raises (ValidationError , match = re .escape (expected .message )):
151
+ with pytest .raises (ValidationError , match = re .escape (expected .message )) as exc_info :
132
152
v .validate_python (input_value )
153
+ if expected .errors is not None :
154
+ # debug(exc_info.value.errors())
155
+ assert exc_info .value .errors () == expected .errors
133
156
else :
134
157
assert v .validate_python (input_value ) == expected
135
158
@@ -170,3 +193,23 @@ def test_union():
170
193
'input_value' : 'c' ,
171
194
},
172
195
]
196
+
197
+
198
+ def test_enum ():
199
+ class FooEnum (Enum ):
200
+ foo = 'foo_value'
201
+
202
+ v = SchemaValidator (core_schema .literal_schema (FooEnum .foo ))
203
+ assert v .validate_python (FooEnum .foo ) == FooEnum .foo
204
+ with pytest .raises (ValidationError ) as exc_info :
205
+ v .validate_python ('foo_value' )
206
+ # insert_assert(exc_info.value.errors())
207
+ assert exc_info .value .errors () == [
208
+ {
209
+ 'kind' : 'literal_error' ,
210
+ 'loc' : [],
211
+ 'message' : "Input should be <FooEnum.foo: 'foo_value'>" ,
212
+ 'input_value' : 'foo_value' ,
213
+ 'context' : {'expected' : "<FooEnum.foo: 'foo_value'>" },
214
+ }
215
+ ]
0 commit comments