"""Test exception.""" import json import pytest from account.utils import exception as exception_util class MockException(Exception): """MockException.""" def __init__(self, arg0, arg1, kwarg0=None, kwarg1=None): """Init MockException.""" super(MockException, self).__init__( json.dumps( { 'arg0': arg0, 'arg1': arg1, 'kwarg0': kwarg0, 'kwarg1': kwarg1, } ) ) @pytest.mark.parametrize( ('exception_class', 'exception_args', 'exception_kwargs', 'expected_message'), [ (Exception, ['some message'], {}, 'some message'), ( MockException, ['abc', 'def'], {'kwarg0': 'jkl', 'kwarg1': 'pqr'}, '{"arg0": "abc", "arg1": "def", "kwarg0": "jkl", "kwarg1": "pqr"}', ), ], ) def test_raise_exception(exception_class, exception_args, exception_kwargs, expected_message): """Test raise_exception_if_necessary.""" with pytest.raises(exception_class) as excinfo: exception_util.raise_exception(exception_class, *exception_args, **exception_kwargs) assert expected_message == str(excinfo.value) def test_reraise_exception(): """Test reraise_exception.""" with pytest.raises(Exception) as excinfo: exception_util.reraise_exception(Exception('test exception')) assert 'test exception' == str(excinfo.value)