"""Test exception.""" import json import pytest from artwork.utils import exception as exception_utils class ExampleException(Exception): """TestException.""" def __init__(self, arg0, arg1, kwarg0=None, kwarg1=None): """Init TestException.""" super(ExampleException, 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'), ( ExampleException, ['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_utils.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_utils.reraise_exception(Exception('test exception')) assert 'test exception' == str(excinfo.value)