@@ -46,21 +46,6 @@ def test_dict_cases(input_value, expected):
46
46
assert v .validate_python (input_value ) == expected
47
47
48
48
49
- def test_dict_complex_key ():
50
- v = SchemaValidator (
51
- {'type' : 'dict' , 'keys_schema' : {'type' : 'complex' , 'strict' : True }, 'values_schema' : {'type' : 'str' }}
52
- )
53
- assert v .validate_python ({complex (1 , 2 ): '1' }) == {complex (1 , 2 ): '1' }
54
- with pytest .raises (ValidationError , match = 'Input should be a valid Python complex object' ):
55
- assert v .validate_python ({'1+2j' : b'1' }) == {complex (1 , 2 ): '1' }
56
-
57
- v = SchemaValidator ({'type' : 'dict' , 'keys_schema' : {'type' : 'complex' }, 'values_schema' : {'type' : 'str' }})
58
- with pytest .raises (
59
- ValidationError , match = 'Input should be a valid python complex object, a number, or a valid complex string'
60
- ):
61
- v .validate_python ({'1+2ja' : b'1' })
62
-
63
-
64
49
def test_dict_value_error (py_and_json : PyAndJson ):
65
50
v = py_and_json ({'type' : 'dict' , 'values_schema' : {'type' : 'int' }})
66
51
assert v .validate_test ({'a' : 2 , 'b' : '4' }) == {'a' : 2 , 'b' : 4 }
@@ -250,3 +235,28 @@ def test_json_dict():
250
235
assert exc_info .value .errors (include_url = False ) == [
251
236
{'type' : 'dict_type' , 'loc' : (), 'msg' : 'Input should be an object' , 'input' : 1 }
252
237
]
238
+
239
+
240
+ def test_dict_complex_key ():
241
+ v = SchemaValidator (
242
+ {'type' : 'dict' , 'keys_schema' : {'type' : 'complex' , 'strict' : True }, 'values_schema' : {'type' : 'str' }}
243
+ )
244
+ assert v .validate_python ({complex (1 , 2 ): '1' }) == {complex (1 , 2 ): '1' }
245
+ with pytest .raises (ValidationError , match = 'Input should be a valid Python complex object' ):
246
+ assert v .validate_python ({'1+2j' : b'1' }) == {complex (1 , 2 ): '1' }
247
+
248
+ v = SchemaValidator ({'type' : 'dict' , 'keys_schema' : {'type' : 'complex' }, 'values_schema' : {'type' : 'str' }})
249
+ with pytest .raises (
250
+ ValidationError , match = 'Input should be a valid python complex object, a number, or a valid complex string'
251
+ ):
252
+ v .validate_python ({'1+2ja' : b'1' })
253
+
254
+
255
+ def test_json_dict_complex_key ():
256
+ v = SchemaValidator (
257
+ {'type' : 'dict' , 'keys_schema' : {'type' : 'complex' }, 'values_schema' : {'type' : 'int' }}
258
+ )
259
+ assert v .validate_json ('{"1+2j": 2, "-3": 4}' ) == {complex (1 , 2 ): 2 , complex (- 3 , 0 ): 4 }
260
+ assert v .validate_json ('{"1+2j": 2, "infj": 4}' ) == {complex (1 , 2 ): 2 , complex (0 , float ('inf' )): 4 }
261
+ with pytest .raises (ValidationError , match = 'Input should be a valid complex string' ):
262
+ v .validate_json ('{"1+2j": 2, "": 4}' ) == {complex (1 , 2 ): 2 , complex (0 , float ('inf' )): 4 }
0 commit comments