1+ from typing import Mapping
2+
13import pytest
24from dirty_equals import IsStrictDict
35from inline_snapshot import snapshot
46
5- from generate_self_schema import core_schema
6- from pydantic_core import SchemaValidator , ValidationError
7+ from pydantic_core import SchemaValidator , ValidationError , core_schema
78
89
910def test_list ():
@@ -70,10 +71,25 @@ def test_set_frozenset(collection_type):
7071 v .validate_python ([[1 , 2 ], 'wrong' , 'wrong' ])
7172
7273
74+ class MyMapping (Mapping ):
75+ def __init__ (self , d ):
76+ self ._d = d
77+
78+ def __getitem__ (self , key ):
79+ return self ._d [key ]
80+
81+ def __iter__ (self ):
82+ return iter (self ._d )
83+
84+ def __len__ (self ):
85+ return len (self ._d )
86+
87+
7388def test_dict ():
7489 v = SchemaValidator (core_schema .dict_schema (core_schema .int_schema (), core_schema .int_schema ()))
7590 assert v .validate_python ({'1' : 2 , 3 : '4' }) == snapshot ({1 : 2 , 3 : 4 })
7691 assert v .validate_python ({'1' : 2 , 3 : '4' }, allow_partial = True ) == snapshot ({1 : 2 , 3 : 4 })
92+ assert v .validate_python (MyMapping ({'1' : 2 , 3 : '4' }), allow_partial = True ) == snapshot ({1 : 2 , 3 : 4 })
7793 with pytest .raises (ValidationError ) as exc_info :
7894 v .validate_python ({'1' : 2 , 3 : 'wrong' })
7995 assert exc_info .value .errors (include_url = False ) == snapshot (
@@ -86,7 +102,8 @@ def test_dict():
86102 }
87103 ]
88104 )
89- assert v .validate_python ({'1' : 2 , 3 : 'wrong' }, allow_partial = True ) == snapshot ({1 : 2 })
105+ assert v .validate_python ({'1' : 2 , 3 : 'x' }, allow_partial = True ) == snapshot ({1 : 2 })
106+ assert v .validate_python (MyMapping ({'1' : 2 , 3 : 'x' }), allow_partial = True ) == snapshot ({1 : 2 })
90107 assert v .validate_python ({'1' : 2 , 3 : 4 , 5 : '6' , 7 : 'x' }, allow_partial = True ) == snapshot ({1 : 2 , 3 : 4 , 5 : 6 })
91108 with pytest .raises (ValidationError , match = 'Input should be a valid integer' ):
92109 v .validate_python ({'1' : 2 , 3 : 4 , 5 : 'x' , 7 : '8' })
0 commit comments