import json import pytest from src.backend import exception_handlers as exh from src.backend.constants import ResponseAttributes as RespAttr class CustomException(Exception): """Custom exception for testing.""" @pytest.mark.asyncio @pytest.mark.parametrize( "exception,expected_status_code, expected_json", [ ( exh.youtube_apis.exceptions.NotFound, 404, {RespAttr.ERROR: exh.ErrorMessages.YOUTUBE_NOT_FOUND}, ), ], ) async def test_handle_exceptions_defined_exc( exception, expected_status_code, expected_json ): """Test handle_exceptions decorator with explicitly defined exceptions.""" @exh.handle_exceptions async def func(): raise exception result = await func() assert result.status_code == expected_status_code body = json.loads(result.body) assert body[RespAttr.ERROR] == expected_json[RespAttr.ERROR] assert body[RespAttr.SUCCESS] == (200 <= expected_status_code < 300) @pytest.mark.asyncio async def test_handle_exceptions_other_exc(): """Test handle_exceptions decorator with other, non-defined exception.""" @exh.handle_exceptions async def func(): raise CustomException with pytest.raises(CustomException): await func()