Skip to content

Commit cc53b24

Browse files
committed
update tests
1 parent 9d373ef commit cc53b24

File tree

2 files changed

+27
-17
lines changed

2 files changed

+27
-17
lines changed

tests/validators/test_complex.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,9 +140,9 @@ def test_json_complex_strict():
140140
assert v.validate_json('"1"') == complex(1, 0)
141141

142142
with pytest.raises(ValidationError, match=re.escape(EXPECTED_PARSE_ERROR_MESSAGE)):
143-
assert v.validate_json('1') == complex(1, 0)
143+
v.validate_json('1')
144144
with pytest.raises(ValidationError, match=re.escape(EXPECTED_PARSE_ERROR_MESSAGE)):
145-
assert v.validate_json('1.0') == complex(1, 0)
145+
v.validate_json('1.0')
146146
with pytest.raises(ValidationError, match=re.escape(EXPECTED_TYPE_ERROR_MESSAGE)):
147147
v.validate_json('{"real": 2, "imag": 4}')
148148

tests/validators/test_dict.py

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -46,21 +46,6 @@ def test_dict_cases(input_value, expected):
4646
assert v.validate_python(input_value) == expected
4747

4848

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-
6449
def test_dict_value_error(py_and_json: PyAndJson):
6550
v = py_and_json({'type': 'dict', 'values_schema': {'type': 'int'}})
6651
assert v.validate_test({'a': 2, 'b': '4'}) == {'a': 2, 'b': 4}
@@ -250,3 +235,28 @@ def test_json_dict():
250235
assert exc_info.value.errors(include_url=False) == [
251236
{'type': 'dict_type', 'loc': (), 'msg': 'Input should be an object', 'input': 1}
252237
]
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

Comments
 (0)