Exemplo n.º 1
0
def test_wrap_message_prefix():
    with pytest.raises(CustomException) as exc_info:
        with exceptional.wrap(ValueError, CustomException, prefix="foo"):
            raise ValueError("bar")
    assert str(exc_info.value) == "foo: bar"

    with pytest.raises(CustomException) as exc_info:
        with exceptional.wrap(ValueError, CustomException, prefix="{}"):
            raise ValueError("bar")
    assert str(exc_info.value) == "{}: bar"
Exemplo n.º 2
0
def test_wrap_cause():
    with pytest.raises(CustomException) as exc_info:
        with exceptional.wrap(ValueError, CustomException):
            raise ValueError("foo")
    assert isinstance(exc_info.value.__cause__, ValueError)
    assert str(exc_info.value.__cause__) == "foo"

    with pytest.raises(CustomException) as exc_info:
        with exceptional.wrap(ValueError, CustomException, set_cause=False):
            raise ValueError("foo")
    assert exc_info.value.__cause__ is None
Exemplo n.º 3
0
def test_wrap_context():
    with pytest.raises(CustomException) as exc_info:
        with exceptional.wrap(ValueError, CustomException):
            raise ValueError("foo")
    assert isinstance(exc_info.value.__context__, ValueError)
    assert str(exc_info.value.__context__) == "foo"

    with pytest.raises(CustomException) as exc_info:
        with exceptional.wrap(ValueError, CustomException, suppress_context=True):
            raise ValueError
    assert isinstance(exc_info.value.__cause__, ValueError)
    assert isinstance(exc_info.value.__context__, ValueError)
    assert exc_info.value.__suppress_context__
Exemplo n.º 4
0
def test_wrap_repr():
    wrapper = exceptional.wrap(ValueError, CustomException)
    expected = "exceptional.wrap(ValueError, CustomException, ...)"
    assert str(wrapper) == repr(wrapper) == expected

    mapping = {}
    wrapper = exceptional.wrap(mapping)
    expected = "exceptional.wrap({}, ...)"
    assert str(wrapper) == repr(wrapper) == expected

    mapping = {ValueError: CustomException, IndexError: CustomExceptionTwo}
    wrapper = exceptional.wrap(mapping)
    expected = "exceptional.wrap({IndexError: CustomExceptionTwo, ...}, ...)"
    assert str(wrapper) == repr(wrapper) == expected
Exemplo n.º 5
0
def test_wrap_multiple_exceptions():
    wrapper = exceptional.wrap((KeyError, IndexError), CustomException)

    with pytest.raises(CustomException), wrapper:
        raise KeyError

    with pytest.raises(CustomException), wrapper:
        raise IndexError
Exemplo n.º 6
0
def test_wrap_message_propagated_by_default():
    """
    By default, the exception message is propagated as-is.
    """
    with pytest.raises(CustomException) as exc_info:
        with exceptional.wrap(ValueError, CustomException):
            raise ValueError("foo")
    assert str(exc_info.value) == "foo"
Exemplo n.º 7
0
def test_wrap_empty_message():
    """
    The replacement message can be empty.
    """
    with pytest.raises(CustomException) as exc_info:
        with exceptional.wrap(ValueError, CustomException, message=None):
            raise ValueError("foo")
    assert exc_info.value.args == ()
    assert str(exc_info.value) == ""
Exemplo n.º 8
0
def test_wrap_invalid_usage():
    with pytest.raises(TypeError) as exc_info:
        exceptional.wrap({KeyError: CustomException}, CustomException)
    expected = "cannot specify both a mapping and a replacement exception type"
    assert str(exc_info.value) == expected

    with pytest.raises(TypeError) as exc_info:
        exceptional.wrap(KeyError, 123)
    assert str(exc_info.value) == "not an exception class: 123"

    with pytest.raises(TypeError) as exc_info:
        exceptional.wrap(123, CustomException)
    assert str(exc_info.value) == "not an exception class (or tuple): 123"

    with pytest.raises(TypeError) as exc_info:
        exceptional.wrap(ValueError, CustomException, message="foo", format="bar")
    expected = "specify at most one of 'message', 'prefix', or 'format'"
    assert str(exc_info.value) == expected
Exemplo n.º 9
0
def test_wrap_multiple_exceptions_mapping():
    mapping = {
        KeyError: CustomException,
        LookupError: CustomExceptionTwo,
        (ValueError, TypeError): CustomExceptionThree,
    }
    wrapper = exceptional.wrap(mapping)

    with pytest.raises(CustomException), wrapper:
        raise KeyError

    with pytest.raises(CustomExceptionTwo), wrapper:
        assert issubclass(IndexError, LookupError)
        raise IndexError

    with pytest.raises(CustomExceptionThree), wrapper:
        raise ValueError

    with pytest.raises(CustomExceptionThree), wrapper:
        raise TypeError
Exemplo n.º 10
0
def test_wrap_not_handled():
    with pytest.raises(IndexError):
        with exceptional.wrap(ValueError, CustomException):
            raise IndexError
Exemplo n.º 11
0
def test_wrap_no_exception():
    with exceptional.wrap(ValueError, CustomException):
        pass
Exemplo n.º 12
0
def test_wrap():
    with pytest.raises(CustomException):
        with exceptional.wrap(ValueError, CustomException):
            raise ValueError
Exemplo n.º 13
0
def test_wrap_message_format():
    with pytest.raises(CustomException) as exc_info:
        with exceptional.wrap(ValueError, CustomException, format="oops ({})"):
            raise ValueError("foo")
    assert str(exc_info.value) == "oops (foo)"
Exemplo n.º 14
0
def test_wrap_message():
    with pytest.raises(CustomException) as exc_info:
        with exceptional.wrap(ValueError, CustomException, message="foo {}"):
            raise ValueError("bar")
    assert str(exc_info.value) == "foo {}"