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

@@ -48,7 +48,9 @@ class TestCreditService:
mock_repo_class.return_value = mock_repo
mock_repo.get_by_id.return_value = sample_user
result = await credit_service.check_credits(1, CreditActionType.VLC_PLAY_SOUND)
result = await credit_service.check_credits(
1, CreditActionType.VLC_PLAY_SOUND,
)
assert result is True
mock_repo.get_by_id.assert_called_once_with(1)
@@ -72,7 +74,9 @@ class TestCreditService:
mock_repo_class.return_value = mock_repo
mock_repo.get_by_id.return_value = poor_user
result = await credit_service.check_credits(1, CreditActionType.VLC_PLAY_SOUND)
result = await credit_service.check_credits(
1, CreditActionType.VLC_PLAY_SOUND,
)
assert result is False
mock_session.close.assert_called_once()
@@ -87,13 +91,17 @@ class TestCreditService:
mock_repo_class.return_value = mock_repo
mock_repo.get_by_id.return_value = None
result = await credit_service.check_credits(999, CreditActionType.VLC_PLAY_SOUND)
result = await credit_service.check_credits(
999, CreditActionType.VLC_PLAY_SOUND,
)
assert result is False
mock_session.close.assert_called_once()
@pytest.mark.asyncio
async def test_validate_and_reserve_credits_success(self, credit_service, sample_user) -> None:
async def test_validate_and_reserve_credits_success(
self, credit_service, sample_user,
) -> None:
"""Test successful credit validation and reservation."""
mock_session = credit_service.db_session_factory()
@@ -103,7 +111,8 @@ class TestCreditService:
mock_repo.get_by_id.return_value = sample_user
user, action = await credit_service.validate_and_reserve_credits(
1, CreditActionType.VLC_PLAY_SOUND,
1,
CreditActionType.VLC_PLAY_SOUND,
)
assert user == sample_user
@@ -112,7 +121,9 @@ class TestCreditService:
mock_session.close.assert_called_once()
@pytest.mark.asyncio
async def test_validate_and_reserve_credits_insufficient(self, credit_service) -> None:
async def test_validate_and_reserve_credits_insufficient(
self, credit_service,
) -> None:
"""Test credit validation with insufficient credits."""
mock_session = credit_service.db_session_factory()
poor_user = User(
@@ -131,7 +142,8 @@ class TestCreditService:
with pytest.raises(InsufficientCreditsError) as exc_info:
await credit_service.validate_and_reserve_credits(
1, CreditActionType.VLC_PLAY_SOUND,
1,
CreditActionType.VLC_PLAY_SOUND,
)
assert exc_info.value.required == 1
@@ -139,7 +151,9 @@ class TestCreditService:
mock_session.close.assert_called_once()
@pytest.mark.asyncio
async def test_validate_and_reserve_credits_user_not_found(self, credit_service) -> None:
async def test_validate_and_reserve_credits_user_not_found(
self, credit_service,
) -> None:
"""Test credit validation when user is not found."""
mock_session = credit_service.db_session_factory()
@@ -150,7 +164,8 @@ class TestCreditService:
with pytest.raises(ValueError, match="User 999 not found"):
await credit_service.validate_and_reserve_credits(
999, CreditActionType.VLC_PLAY_SOUND,
999,
CreditActionType.VLC_PLAY_SOUND,
)
mock_session.close.assert_called_once()
@@ -160,15 +175,20 @@ class TestCreditService:
"""Test successful credit deduction."""
mock_session = credit_service.db_session_factory()
with patch("app.services.credit.UserRepository") as mock_repo_class, \
patch("app.services.credit.socket_manager") as mock_socket_manager:
with (
patch("app.services.credit.UserRepository") as mock_repo_class,
patch("app.services.credit.socket_manager") as mock_socket_manager,
):
mock_repo = AsyncMock()
mock_repo_class.return_value = mock_repo
mock_repo.get_by_id.return_value = sample_user
mock_socket_manager.send_to_user = AsyncMock()
await credit_service.deduct_credits(
1, CreditActionType.VLC_PLAY_SOUND, success=True, metadata={"test": "data"},
1,
CreditActionType.VLC_PLAY_SOUND,
success=True,
metadata={"test": "data"},
)
# Verify user credits were updated
@@ -180,7 +200,9 @@ class TestCreditService:
# Verify socket event was emitted
mock_socket_manager.send_to_user.assert_called_once_with(
"1", "user_credits_changed", {
"1",
"user_credits_changed",
{
"user_id": "1",
"credits_before": 10,
"credits_after": 9,
@@ -202,19 +224,25 @@ class TestCreditService:
assert json.loads(added_transaction.metadata_json) == {"test": "data"}
@pytest.mark.asyncio
async def test_deduct_credits_failed_action_requires_success(self, credit_service, sample_user) -> None:
async def test_deduct_credits_failed_action_requires_success(
self, credit_service, sample_user,
) -> None:
"""Test credit deduction when action failed but requires success."""
mock_session = credit_service.db_session_factory()
with patch("app.services.credit.UserRepository") as mock_repo_class, \
patch("app.services.credit.socket_manager") as mock_socket_manager:
with (
patch("app.services.credit.UserRepository") as mock_repo_class,
patch("app.services.credit.socket_manager") as mock_socket_manager,
):
mock_repo = AsyncMock()
mock_repo_class.return_value = mock_repo
mock_repo.get_by_id.return_value = sample_user
mock_socket_manager.send_to_user = AsyncMock()
await credit_service.deduct_credits(
1, CreditActionType.VLC_PLAY_SOUND, success=False, # Action failed
1,
CreditActionType.VLC_PLAY_SOUND,
success=False, # Action failed
)
# Verify user credits were NOT updated (action requires success)
@@ -247,8 +275,10 @@ class TestCreditService:
plan_id=1,
)
with patch("app.services.credit.UserRepository") as mock_repo_class, \
patch("app.services.credit.socket_manager") as mock_socket_manager:
with (
patch("app.services.credit.UserRepository") as mock_repo_class,
patch("app.services.credit.socket_manager") as mock_socket_manager,
):
mock_repo = AsyncMock()
mock_repo_class.return_value = mock_repo
mock_repo.get_by_id.return_value = poor_user
@@ -256,7 +286,9 @@ class TestCreditService:
with pytest.raises(InsufficientCreditsError):
await credit_service.deduct_credits(
1, CreditActionType.VLC_PLAY_SOUND, success=True,
1,
CreditActionType.VLC_PLAY_SOUND,
success=True,
)
# Verify no socket event was emitted since credits could not be deducted
@@ -270,15 +302,20 @@ class TestCreditService:
"""Test adding credits to user account."""
mock_session = credit_service.db_session_factory()
with patch("app.services.credit.UserRepository") as mock_repo_class, \
patch("app.services.credit.socket_manager") as mock_socket_manager:
with (
patch("app.services.credit.UserRepository") as mock_repo_class,
patch("app.services.credit.socket_manager") as mock_socket_manager,
):
mock_repo = AsyncMock()
mock_repo_class.return_value = mock_repo
mock_repo.get_by_id.return_value = sample_user
mock_socket_manager.send_to_user = AsyncMock()
await credit_service.add_credits(
1, 5, "Bonus credits", {"reason": "signup"},
1,
5,
"Bonus credits",
{"reason": "signup"},
)
# Verify user credits were updated
@@ -290,7 +327,9 @@ class TestCreditService:
# Verify socket event was emitted
mock_socket_manager.send_to_user.assert_called_once_with(
"1", "user_credits_changed", {
"1",
"user_credits_changed",
{
"user_id": "1",
"credits_before": 10,
"credits_after": 15,