11import re
2+ from enum import Enum
23
34import pytest
45
@@ -106,6 +107,7 @@ def test_literal_py_and_json(py_and_json: PyAndJson, kwarg_expected, input_value
106107 with pytest .raises (ValidationError , match = re .escape (expected .message )) as exc_info :
107108 v .validate_test (input_value )
108109 if expected .errors is not None :
110+ # debug(exc_info.value.errors())
109111 assert exc_info .value .errors () == expected .errors
110112 else :
111113 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
123125 Err ("Input should be 1 or b'whatever' [kind=literal_error, input_value=3, input_type=int]" ),
124126 id = 'wrong-general' ,
125127 ),
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+ ),
126146 ],
127147)
128148def test_literal_not_json (kwarg_expected , input_value , expected ):
129149 v = SchemaValidator ({'type' : 'literal' , 'expected' : kwarg_expected })
130150 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 :
132152 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
133156 else :
134157 assert v .validate_python (input_value ) == expected
135158
@@ -170,3 +193,23 @@ def test_union():
170193 'input_value' : 'c' ,
171194 },
172195 ]
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