Skip to content

Commit 7b85166

Browse files
committed
Add tests for exception re-raise when message lacks attr name
1 parent a125776 commit 7b85166

1 file changed

Lines changed: 41 additions & 0 deletions

File tree

tests/test_validators.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -654,6 +654,19 @@ def test_validators_iterables(self, conv):
654654
assert and_(*member_validator) == v.member_validator
655655
assert and_(*iterable_validator) == v.iterable_validator
656656

657+
def test_member_validator_error_without_attr_name(self):
658+
"""
659+
If a member validator raises an exception whose message does not
660+
contain the attr name, the original exception is re-raised unchanged.
661+
"""
662+
def _validator(inst, attr, value):
663+
raise ValueError("something went wrong")
664+
665+
v = deep_iterable(_validator)
666+
a = simple_attr("test")
667+
668+
with pytest.raises(ValueError, match="something went wrong"):
669+
v(None, a, ["ok", "bad"])
657670

658671
class TestDeepMapping:
659672
"""
@@ -808,6 +821,34 @@ def test_validators_iterables(self, conv):
808821
assert and_(*mapping_validator) == v.mapping_validator
809822

810823

824+
def test_key_validator_error_without_attr_name(self):
825+
"""
826+
If a key validator raises an exception whose message does not
827+
contain the attr name, the original exception is re-raised unchanged.
828+
"""
829+
def _key_validator(inst, attr, value):
830+
raise TypeError("bad key type")
831+
832+
v = deep_mapping(key_validator=_key_validator)
833+
a = simple_attr("test")
834+
835+
with pytest.raises(TypeError, match="bad key type"):
836+
v(None, a, {"bad_key": 1})
837+
838+
def test_value_validator_error_without_attr_name(self):
839+
"""
840+
If a value validator raises an exception whose message does not
841+
contain the attr name, the original exception is re-raised unchanged.
842+
"""
843+
def _value_validator(inst, attr, value):
844+
raise TypeError("bad value type")
845+
846+
v = deep_mapping(value_validator=_value_validator)
847+
a = simple_attr("test")
848+
849+
with pytest.raises(TypeError, match="bad value type"):
850+
v(None, a, {"a": "bad_value"})
851+
811852
class TestIsCallable:
812853
"""
813854
Tests for `is_callable`.

0 commit comments

Comments
 (0)