Esempio n. 1
0
 def test_nested_handler(self):
     """
     This test verifies that a handler that is nested inside another buzz
     catching mechanism properly sanitizes the final error string so that
     format arguments in the outside mechanism don't get caught up in
     curly braces in the final error string
     """
     with pytest.raises(Buzz) as err_info:
         with Buzz.handle_errors("outside handler"):
             with Buzz.handle_errors("inside handler"):
                 raise Exception("this has {curlies}")
     assert 'this has {curlies}' in err_info.value.message
Esempio n. 2
0
    def test_handle_errors__with_do_finally(self):
        check_list = []
        with Buzz.handle_errors(
                'no errors should happen here, but do_finally should be called',
                do_finally=lambda: check_list.append(1)):
            pass
        assert check_list == [1]

        check_list = []
        with pytest.raises(Buzz) as err_info:
            with Buzz.handle_errors('intercepted exception',
                                    do_finally=lambda: check_list.append(1)):
                raise Exception("there was a problem")
        assert 'there was a problem' in str(err_info.value)
        assert 'intercepted exception' in str(err_info.value)
        assert check_list == [1]
Esempio n. 3
0
 def test_handle_errors__with_do_else(self):
     check_list = []
     with Buzz.handle_errors(
             'no errors should happen here, but do_else should be called',
             do_else=lambda: check_list.append(1)):
         pass
     assert check_list == [1]
Esempio n. 4
0
 def test_handle_errors__basic_handling(self):
     with pytest.raises(Buzz) as err_info:
         with Buzz.handle_errors('intercepted exception'):
             raise ValueError("there was a problem")
     assert 'there was a problem' in str(err_info.value)
     assert 'intercepted exception' in str(err_info.value)
     assert 'ValueError' in str(err_info.value)
Esempio n. 5
0
def test_Buzz_handle_errors__basic():
    with pytest.raises(Buzz) as err_info:
        with Buzz.handle_errors("intercepted exception"):
            raise ValueError("there was a problem")

    assert "there was a problem" in str(err_info.value)
    assert "intercepted exception" in str(err_info.value)
    assert "ValueError" in str(err_info.value)
Esempio n. 6
0
    def test_handle_errors__with_specific_exception_class(self):
        class SpecialError(Exception):
            pass

        with pytest.raises(Exception) as err_info:
            with Buzz.handle_errors(
                    'there was a problem',
                    exception_class=SpecialError,
            ):
                raise Exception("there was a problem")
        assert err_info.type == Exception

        with pytest.raises(Exception) as err_info:
            with Buzz.handle_errors(
                    'there was a problem',
                    exception_class=SpecialError,
            ):
                raise SpecialError("there was a problem")
        assert err_info.type == Buzz
Esempio n. 7
0
 def test_handle_errors__without_reraise(self):
     check_list = []
     with Buzz.handle_errors(
             'intercepted exception',
             re_raise=False,
             do_except=lambda e, m, t: check_list.append(m),
     ):
         raise Exception("there was a problem")
     assert len(check_list) == 1
     assert 'there was a problem' in check_list[0]
Esempio n. 8
0
    def test_handle_errors__with_do_except(self):
        check_list = []
        with Buzz.handle_errors(
                'no errors should happen here, so do_except should not be called',
                do_except=lambda e, m, t: check_list.append(m),
        ):
            pass
        assert check_list == []

        check_list = []
        with pytest.raises(Buzz) as err_info:
            with Buzz.handle_errors(
                    'intercepted exception',
                    do_except=lambda e, m, t: check_list.append(m),
            ):
                raise Exception("there was a problem")
        assert 'there was a problem' in str(err_info.value)
        assert 'intercepted exception' in str(err_info.value)
        assert len(check_list) == 1
        assert 'there was a problem' in check_list[0]
Esempio n. 9
0
 def test_handle_errors__no_exceptions(self):
     with Buzz.handle_errors('no errors should happen here'):
         pass
Esempio n. 10
0
 def test_handle_errors__error_formatting_message(self):
     with pytest.raises(Buzz) as err_info:
         with Buzz.handle_errors('{format_arg1}', format_arg2='foo'):
             raise ValueError("there was a problem")
     assert err_info.type == Buzz
     assert 'Failed while formatting message' in str(err_info.value)
Esempio n. 11
0
def test_Buzz_handle_errors__false_re_raise_absorbs_errors():
    with Buzz.handle_errors("intercepted exception", re_raise=False):
        pass
Esempio n. 12
0
def test_Buzz_handle_errors__fails_with_explicitly_passed_raise_exc_class():
    with pytest.raises(ValueError, match="You may not pass"):
        with Buzz.handle_errors("intercepted exception",
                                raise_exc_class=Exception):
            pass