"""Test exception.""" from http.client import BAD_REQUEST from http.client import CREATED from http.client import INTERNAL_SERVER_ERROR from http.client import MULTIPLE_CHOICES from http.client import OK from http.client import UNAUTHORIZED import json from unittest.mock import call, NonCallableMagicMock import pytest from video.constants import exceptions from video.utils import exception as exception_utils class TestException(Exception): """TestException.""" def __init__(self, arg0, arg1, kwarg0=None, kwarg1=None): """Init TestException.""" super(TestException, self).__init__(json.dumps({ 'arg0': arg0, 'arg1': arg1, 'kwarg0': kwarg0, 'kwarg1': kwarg1, })) @pytest.mark.parametrize( ('exception_class', 'exception_args', 'exception_kwargs', 'expected_message'), [ (exceptions.NotCurrentlyAcceptingWork, [], {}, ''), (Exception, ['some message'], {}, 'some message'), (TestException, ['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_request_failed.""" 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) @pytest.mark.parametrize(('status', 'expected_raise_exception_calls'), [ ( 199, [call( Exception, json.dumps({ 'request_params': {'some': 'param'}, 'response_status': 199, 'response_text': 'some text' }))], ), (OK, []), (CREATED, []), (299, []), ( MULTIPLE_CHOICES, [call( Exception, json.dumps({ 'request_params': {'some': 'param'}, 'response_status': MULTIPLE_CHOICES, 'response_text': 'some text' }))], ), ( 399, [call( Exception, json.dumps({ 'request_params': {'some': 'param'}, 'response_status': 399, 'response_text': 'some text' }))], ), ( BAD_REQUEST, [call( Exception, json.dumps({ 'request_params': {'some': 'param'}, 'response_status': BAD_REQUEST, 'response_text': 'some text' }))], ), ( UNAUTHORIZED, [call( exception_utils.RetryableError, json.dumps({ 'request_params': {'some': 'param'}, 'response_status': UNAUTHORIZED, 'response_text': 'some text' }))], ), ( 499, [call( Exception, json.dumps({ 'request_params': {'some': 'param'}, 'response_status': 499, 'response_text': 'some text' }))], ), ( INTERNAL_SERVER_ERROR, [call( exception_utils.RetryableError, json.dumps({ 'request_params': {'some': 'param'}, 'response_status': INTERNAL_SERVER_ERROR, 'response_text': 'some text' }))], ), ( 599, [call( exception_utils.RetryableError, json.dumps({ 'request_params': {'some': 'param'}, 'response_status': 599, 'response_text': 'some text' }))], ), ( 600, [call( Exception, json.dumps({ 'request_params': {'some': 'param'}, 'response_status': 600, 'response_text': 'some text' }))], ), ]) def test_raise_exception_if_request_failed( mocker, status, expected_raise_exception_calls): """Test raise_exception_if_request_failed.""" mocker.patch.object(exception_utils, 'raise_exception', autospec=True) exception_utils.raise_exception_if_request_failed( {'some': 'param'}, status, 'some text') exception_utils.raise_exception_if_request_failed( {'some': 'param'}, response=NonCallableMagicMock(status_code=status, text='some text')) exception_utils.raise_exception.assert_has_calls( 2 * expected_raise_exception_calls) assert exception_utils.raise_exception.call_count == len( 2 * expected_raise_exception_calls)