Refactor test cases for improved readability and consistency
All checks were successful
Backend CI / lint (push) Successful in 9m49s
Backend CI / test (push) Successful in 6m15s

- 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:
JSC
2025-08-01 20:53:30 +02:00
parent d926779fe4
commit 6068599a47
39 changed files with 691 additions and 286 deletions

View File

@@ -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(