Refactor test files for improved readability and consistency
- Removed unnecessary blank lines and adjusted formatting in test files. - Ensured consistent use of commas in function calls and assertions across various test cases. - Updated import statements for better organization and clarity. - Enhanced mock setups in tests for better isolation and reliability. - Improved assertions to follow a consistent style for better readability.
This commit is contained in:
@@ -1,12 +1,16 @@
|
||||
"""Tests for credit decorators."""
|
||||
|
||||
from unittest.mock import AsyncMock, Mock
|
||||
from unittest.mock import AsyncMock
|
||||
|
||||
import pytest
|
||||
|
||||
from app.models.credit_action import CreditActionType
|
||||
from app.services.credit import CreditService, InsufficientCreditsError
|
||||
from app.utils.credit_decorators import CreditManager, requires_credits, validate_credits_only
|
||||
from app.utils.credit_decorators import (
|
||||
CreditManager,
|
||||
requires_credits,
|
||||
validate_credits_only,
|
||||
)
|
||||
|
||||
|
||||
class TestRequiresCreditsDecorator:
|
||||
@@ -32,7 +36,7 @@ class TestRequiresCreditsDecorator:
|
||||
@requires_credits(
|
||||
CreditActionType.VLC_PLAY_SOUND,
|
||||
credit_service_factory,
|
||||
user_id_param="user_id"
|
||||
user_id_param="user_id",
|
||||
)
|
||||
async def test_action(user_id: int, message: str) -> str:
|
||||
return f"Success: {message}"
|
||||
@@ -41,10 +45,10 @@ class TestRequiresCreditsDecorator:
|
||||
|
||||
assert result == "Success: test"
|
||||
mock_credit_service.validate_and_reserve_credits.assert_called_once_with(
|
||||
123, CreditActionType.VLC_PLAY_SOUND, None
|
||||
123, CreditActionType.VLC_PLAY_SOUND, None,
|
||||
)
|
||||
mock_credit_service.deduct_credits.assert_called_once_with(
|
||||
123, CreditActionType.VLC_PLAY_SOUND, True, None
|
||||
123, CreditActionType.VLC_PLAY_SOUND, True, None,
|
||||
)
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@@ -58,7 +62,7 @@ class TestRequiresCreditsDecorator:
|
||||
CreditActionType.VLC_PLAY_SOUND,
|
||||
credit_service_factory,
|
||||
user_id_param="user_id",
|
||||
metadata_extractor=extract_metadata
|
||||
metadata_extractor=extract_metadata,
|
||||
)
|
||||
async def test_action(user_id: int, sound_name: str) -> bool:
|
||||
return True
|
||||
@@ -66,10 +70,10 @@ 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, {"sound_name": "test.mp3"}
|
||||
123, CreditActionType.VLC_PLAY_SOUND, {"sound_name": "test.mp3"},
|
||||
)
|
||||
mock_credit_service.deduct_credits.assert_called_once_with(
|
||||
123, CreditActionType.VLC_PLAY_SOUND, True, {"sound_name": "test.mp3"}
|
||||
123, CreditActionType.VLC_PLAY_SOUND, True, {"sound_name": "test.mp3"},
|
||||
)
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@@ -79,7 +83,7 @@ class TestRequiresCreditsDecorator:
|
||||
@requires_credits(
|
||||
CreditActionType.VLC_PLAY_SOUND,
|
||||
credit_service_factory,
|
||||
user_id_param="user_id"
|
||||
user_id_param="user_id",
|
||||
)
|
||||
async def test_action(user_id: int) -> bool:
|
||||
return False # Action fails
|
||||
@@ -88,7 +92,7 @@ class TestRequiresCreditsDecorator:
|
||||
|
||||
assert result is False
|
||||
mock_credit_service.deduct_credits.assert_called_once_with(
|
||||
123, CreditActionType.VLC_PLAY_SOUND, False, None
|
||||
123, CreditActionType.VLC_PLAY_SOUND, False, None,
|
||||
)
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@@ -98,7 +102,7 @@ class TestRequiresCreditsDecorator:
|
||||
@requires_credits(
|
||||
CreditActionType.VLC_PLAY_SOUND,
|
||||
credit_service_factory,
|
||||
user_id_param="user_id"
|
||||
user_id_param="user_id",
|
||||
)
|
||||
async def test_action(user_id: int) -> str:
|
||||
raise ValueError("Test error")
|
||||
@@ -107,7 +111,7 @@ class TestRequiresCreditsDecorator:
|
||||
await test_action(user_id=123)
|
||||
|
||||
mock_credit_service.deduct_credits.assert_called_once_with(
|
||||
123, CreditActionType.VLC_PLAY_SOUND, False, None
|
||||
123, CreditActionType.VLC_PLAY_SOUND, False, None,
|
||||
)
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@@ -118,7 +122,7 @@ class TestRequiresCreditsDecorator:
|
||||
@requires_credits(
|
||||
CreditActionType.VLC_PLAY_SOUND,
|
||||
credit_service_factory,
|
||||
user_id_param="user_id"
|
||||
user_id_param="user_id",
|
||||
)
|
||||
async def test_action(user_id: int) -> str:
|
||||
return "Should not execute"
|
||||
@@ -136,7 +140,7 @@ class TestRequiresCreditsDecorator:
|
||||
@requires_credits(
|
||||
CreditActionType.VLC_PLAY_SOUND,
|
||||
credit_service_factory,
|
||||
user_id_param="user_id"
|
||||
user_id_param="user_id",
|
||||
)
|
||||
async def test_action(user_id: int, message: str) -> str:
|
||||
return message
|
||||
@@ -145,7 +149,7 @@ class TestRequiresCreditsDecorator:
|
||||
|
||||
assert result == "test"
|
||||
mock_credit_service.validate_and_reserve_credits.assert_called_once_with(
|
||||
123, CreditActionType.VLC_PLAY_SOUND, None
|
||||
123, CreditActionType.VLC_PLAY_SOUND, None,
|
||||
)
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@@ -155,7 +159,7 @@ class TestRequiresCreditsDecorator:
|
||||
@requires_credits(
|
||||
CreditActionType.VLC_PLAY_SOUND,
|
||||
credit_service_factory,
|
||||
user_id_param="user_id"
|
||||
user_id_param="user_id",
|
||||
)
|
||||
async def test_action(other_param: str) -> str:
|
||||
return other_param
|
||||
@@ -186,7 +190,7 @@ class TestValidateCreditsOnlyDecorator:
|
||||
@validate_credits_only(
|
||||
CreditActionType.VLC_PLAY_SOUND,
|
||||
credit_service_factory,
|
||||
user_id_param="user_id"
|
||||
user_id_param="user_id",
|
||||
)
|
||||
async def test_action(user_id: int, message: str) -> str:
|
||||
return f"Validated: {message}"
|
||||
@@ -195,7 +199,7 @@ 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()
|
||||
@@ -219,15 +223,15 @@ class TestCreditManager:
|
||||
mock_credit_service,
|
||||
123,
|
||||
CreditActionType.VLC_PLAY_SOUND,
|
||||
{"test": "data"}
|
||||
{"test": "data"},
|
||||
) as manager:
|
||||
manager.mark_success()
|
||||
|
||||
mock_credit_service.validate_and_reserve_credits.assert_called_once_with(
|
||||
123, CreditActionType.VLC_PLAY_SOUND, {"test": "data"}
|
||||
123, CreditActionType.VLC_PLAY_SOUND, {"test": "data"},
|
||||
)
|
||||
mock_credit_service.deduct_credits.assert_called_once_with(
|
||||
123, CreditActionType.VLC_PLAY_SOUND, True, {"test": "data"}
|
||||
123, CreditActionType.VLC_PLAY_SOUND, True, {"test": "data"},
|
||||
)
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@@ -236,13 +240,13 @@ class TestCreditManager:
|
||||
async with CreditManager(
|
||||
mock_credit_service,
|
||||
123,
|
||||
CreditActionType.VLC_PLAY_SOUND
|
||||
CreditActionType.VLC_PLAY_SOUND,
|
||||
):
|
||||
# Don't mark as success - should be considered failed
|
||||
pass
|
||||
|
||||
mock_credit_service.deduct_credits.assert_called_once_with(
|
||||
123, CreditActionType.VLC_PLAY_SOUND, False, None
|
||||
123, CreditActionType.VLC_PLAY_SOUND, False, None,
|
||||
)
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@@ -252,12 +256,12 @@ class TestCreditManager:
|
||||
async with CreditManager(
|
||||
mock_credit_service,
|
||||
123,
|
||||
CreditActionType.VLC_PLAY_SOUND
|
||||
CreditActionType.VLC_PLAY_SOUND,
|
||||
):
|
||||
raise ValueError("Test error")
|
||||
|
||||
mock_credit_service.deduct_credits.assert_called_once_with(
|
||||
123, CreditActionType.VLC_PLAY_SOUND, False, None
|
||||
123, CreditActionType.VLC_PLAY_SOUND, False, None,
|
||||
)
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@@ -269,9 +273,9 @@ class TestCreditManager:
|
||||
async with CreditManager(
|
||||
mock_credit_service,
|
||||
123,
|
||||
CreditActionType.VLC_PLAY_SOUND
|
||||
CreditActionType.VLC_PLAY_SOUND,
|
||||
):
|
||||
pass
|
||||
|
||||
# Should not call deduct_credits since validation failed
|
||||
mock_credit_service.deduct_credits.assert_not_called()
|
||||
mock_credit_service.deduct_credits.assert_not_called()
|
||||
|
||||
Reference in New Issue
Block a user