Refactor test cases for improved readability and consistency
- Adjusted function signatures in various test files to enhance clarity by aligning parameters. - Updated patching syntax for better readability across test cases. - Improved formatting and spacing in test assertions and mock setups. - Ensured consistent use of async/await patterns in async test functions. - Enhanced comments for better understanding of test intentions.
This commit is contained in:
@@ -19,8 +19,8 @@ from app.utils.audio import (
|
||||
SHA256_HASH_LENGTH = 64
|
||||
BINARY_FILE_SIZE = 700
|
||||
EXPECTED_DURATION_MS_1 = 123456 # 123.456 seconds * 1000
|
||||
EXPECTED_DURATION_MS_2 = 60000 # 60 seconds * 1000
|
||||
EXPECTED_DURATION_MS_3 = 45123 # 45.123 seconds * 1000
|
||||
EXPECTED_DURATION_MS_2 = 60000 # 60 seconds * 1000
|
||||
EXPECTED_DURATION_MS_3 = 45123 # 45.123 seconds * 1000
|
||||
|
||||
|
||||
class TestAudioUtils:
|
||||
@@ -220,7 +220,8 @@ class TestAudioUtils:
|
||||
|
||||
@patch("app.utils.audio.ffmpeg.probe")
|
||||
def test_get_audio_duration_fractional_duration(
|
||||
self, mock_probe: MagicMock,
|
||||
self,
|
||||
mock_probe: MagicMock,
|
||||
) -> None:
|
||||
"""Test audio duration extraction with fractional seconds."""
|
||||
# Mock ffmpeg.probe to return fractional duration
|
||||
|
||||
@@ -27,13 +27,17 @@ class TestRequiresCreditsDecorator:
|
||||
return service
|
||||
|
||||
@pytest.fixture
|
||||
def credit_service_factory(self, mock_credit_service: AsyncMock) -> Callable[[], AsyncMock]:
|
||||
def credit_service_factory(
|
||||
self, mock_credit_service: AsyncMock,
|
||||
) -> Callable[[], AsyncMock]:
|
||||
"""Create a credit service factory."""
|
||||
return lambda: mock_credit_service
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_decorator_success(
|
||||
self, credit_service_factory: Callable[[], AsyncMock], mock_credit_service: AsyncMock,
|
||||
self,
|
||||
credit_service_factory: Callable[[], AsyncMock],
|
||||
mock_credit_service: AsyncMock,
|
||||
) -> None:
|
||||
"""Test decorator with successful action."""
|
||||
|
||||
@@ -49,15 +53,21 @@ class TestRequiresCreditsDecorator:
|
||||
|
||||
assert result == "Success: test"
|
||||
mock_credit_service.validate_and_reserve_credits.assert_called_once_with(
|
||||
123, CreditActionType.VLC_PLAY_SOUND,
|
||||
123,
|
||||
CreditActionType.VLC_PLAY_SOUND,
|
||||
)
|
||||
mock_credit_service.deduct_credits.assert_called_once_with(
|
||||
123, CreditActionType.VLC_PLAY_SOUND, success=True, metadata=None,
|
||||
123,
|
||||
CreditActionType.VLC_PLAY_SOUND,
|
||||
success=True,
|
||||
metadata=None,
|
||||
)
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_decorator_with_metadata(
|
||||
self, credit_service_factory: Callable[[], AsyncMock], mock_credit_service: AsyncMock,
|
||||
self,
|
||||
credit_service_factory: Callable[[], AsyncMock],
|
||||
mock_credit_service: AsyncMock,
|
||||
) -> None:
|
||||
"""Test decorator with metadata extraction."""
|
||||
|
||||
@@ -76,14 +86,20 @@ class TestRequiresCreditsDecorator:
|
||||
await test_action(user_id=123, sound_name="test.mp3")
|
||||
|
||||
mock_credit_service.validate_and_reserve_credits.assert_called_once_with(
|
||||
123, CreditActionType.VLC_PLAY_SOUND,
|
||||
123,
|
||||
CreditActionType.VLC_PLAY_SOUND,
|
||||
)
|
||||
mock_credit_service.deduct_credits.assert_called_once_with(
|
||||
123, CreditActionType.VLC_PLAY_SOUND, success=True, metadata={"sound_name": "test.mp3"},
|
||||
123,
|
||||
CreditActionType.VLC_PLAY_SOUND,
|
||||
success=True,
|
||||
metadata={"sound_name": "test.mp3"},
|
||||
)
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_decorator_failed_action(self, credit_service_factory, mock_credit_service) -> None:
|
||||
async def test_decorator_failed_action(
|
||||
self, credit_service_factory, mock_credit_service,
|
||||
) -> None:
|
||||
"""Test decorator with failed action."""
|
||||
|
||||
@requires_credits(
|
||||
@@ -98,11 +114,16 @@ class TestRequiresCreditsDecorator:
|
||||
|
||||
assert result is False
|
||||
mock_credit_service.deduct_credits.assert_called_once_with(
|
||||
123, CreditActionType.VLC_PLAY_SOUND, success=False, metadata=None,
|
||||
123,
|
||||
CreditActionType.VLC_PLAY_SOUND,
|
||||
success=False,
|
||||
metadata=None,
|
||||
)
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_decorator_exception_in_action(self, credit_service_factory, mock_credit_service) -> None:
|
||||
async def test_decorator_exception_in_action(
|
||||
self, credit_service_factory, mock_credit_service,
|
||||
) -> None:
|
||||
"""Test decorator when action raises exception."""
|
||||
|
||||
@requires_credits(
|
||||
@@ -118,13 +139,20 @@ class TestRequiresCreditsDecorator:
|
||||
await test_action(user_id=123)
|
||||
|
||||
mock_credit_service.deduct_credits.assert_called_once_with(
|
||||
123, CreditActionType.VLC_PLAY_SOUND, success=False, metadata=None,
|
||||
123,
|
||||
CreditActionType.VLC_PLAY_SOUND,
|
||||
success=False,
|
||||
metadata=None,
|
||||
)
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_decorator_insufficient_credits(self, credit_service_factory, mock_credit_service) -> None:
|
||||
async def test_decorator_insufficient_credits(
|
||||
self, credit_service_factory, mock_credit_service,
|
||||
) -> None:
|
||||
"""Test decorator with insufficient credits."""
|
||||
mock_credit_service.validate_and_reserve_credits.side_effect = InsufficientCreditsError(1, 0)
|
||||
mock_credit_service.validate_and_reserve_credits.side_effect = (
|
||||
InsufficientCreditsError(1, 0)
|
||||
)
|
||||
|
||||
@requires_credits(
|
||||
CreditActionType.VLC_PLAY_SOUND,
|
||||
@@ -141,7 +169,9 @@ class TestRequiresCreditsDecorator:
|
||||
mock_credit_service.deduct_credits.assert_not_called()
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_decorator_user_id_in_args(self, credit_service_factory, mock_credit_service) -> None:
|
||||
async def test_decorator_user_id_in_args(
|
||||
self, credit_service_factory, mock_credit_service,
|
||||
) -> None:
|
||||
"""Test decorator extracting user_id from positional args."""
|
||||
|
||||
@requires_credits(
|
||||
@@ -156,7 +186,8 @@ class TestRequiresCreditsDecorator:
|
||||
|
||||
assert result == "test"
|
||||
mock_credit_service.validate_and_reserve_credits.assert_called_once_with(
|
||||
123, CreditActionType.VLC_PLAY_SOUND,
|
||||
123,
|
||||
CreditActionType.VLC_PLAY_SOUND,
|
||||
)
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@@ -186,12 +217,16 @@ class TestValidateCreditsOnlyDecorator:
|
||||
return service
|
||||
|
||||
@pytest.fixture
|
||||
def credit_service_factory(self, mock_credit_service: AsyncMock) -> Callable[[], AsyncMock]:
|
||||
def credit_service_factory(
|
||||
self, mock_credit_service: AsyncMock,
|
||||
) -> Callable[[], AsyncMock]:
|
||||
"""Create a credit service factory."""
|
||||
return lambda: mock_credit_service
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_validate_only_decorator(self, credit_service_factory, mock_credit_service) -> None:
|
||||
async def test_validate_only_decorator(
|
||||
self, credit_service_factory, mock_credit_service,
|
||||
) -> None:
|
||||
"""Test validate_credits_only decorator."""
|
||||
|
||||
@validate_credits_only(
|
||||
@@ -206,7 +241,8 @@ class TestValidateCreditsOnlyDecorator:
|
||||
|
||||
assert result == "Validated: test"
|
||||
mock_credit_service.validate_and_reserve_credits.assert_called_once_with(
|
||||
123, CreditActionType.VLC_PLAY_SOUND,
|
||||
123,
|
||||
CreditActionType.VLC_PLAY_SOUND,
|
||||
)
|
||||
# Should not deduct credits, only validate
|
||||
mock_credit_service.deduct_credits.assert_not_called()
|
||||
@@ -235,10 +271,14 @@ class TestCreditManager:
|
||||
manager.mark_success()
|
||||
|
||||
mock_credit_service.validate_and_reserve_credits.assert_called_once_with(
|
||||
123, CreditActionType.VLC_PLAY_SOUND,
|
||||
123,
|
||||
CreditActionType.VLC_PLAY_SOUND,
|
||||
)
|
||||
mock_credit_service.deduct_credits.assert_called_once_with(
|
||||
123, CreditActionType.VLC_PLAY_SOUND, success=True, metadata={"test": "data"},
|
||||
123,
|
||||
CreditActionType.VLC_PLAY_SOUND,
|
||||
success=True,
|
||||
metadata={"test": "data"},
|
||||
)
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@@ -253,7 +293,10 @@ class TestCreditManager:
|
||||
pass
|
||||
|
||||
mock_credit_service.deduct_credits.assert_called_once_with(
|
||||
123, CreditActionType.VLC_PLAY_SOUND, success=False, metadata=None,
|
||||
123,
|
||||
CreditActionType.VLC_PLAY_SOUND,
|
||||
success=False,
|
||||
metadata=None,
|
||||
)
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@@ -269,13 +312,18 @@ class TestCreditManager:
|
||||
raise ValueError(msg)
|
||||
|
||||
mock_credit_service.deduct_credits.assert_called_once_with(
|
||||
123, CreditActionType.VLC_PLAY_SOUND, success=False, metadata=None,
|
||||
123,
|
||||
CreditActionType.VLC_PLAY_SOUND,
|
||||
success=False,
|
||||
metadata=None,
|
||||
)
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_credit_manager_validation_failure(self, mock_credit_service) -> None:
|
||||
"""Test CreditManager when validation fails."""
|
||||
mock_credit_service.validate_and_reserve_credits.side_effect = InsufficientCreditsError(1, 0)
|
||||
mock_credit_service.validate_and_reserve_credits.side_effect = (
|
||||
InsufficientCreditsError(1, 0)
|
||||
)
|
||||
|
||||
with pytest.raises(InsufficientCreditsError):
|
||||
async with CreditManager(
|
||||
|
||||
Reference in New Issue
Block a user