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:
@@ -42,7 +42,7 @@ class TestCreditTransactionRepository:
|
||||
"""Create test credit transactions."""
|
||||
transactions = []
|
||||
user_id = test_user_id
|
||||
|
||||
|
||||
# Create various types of transactions
|
||||
transaction_data = [
|
||||
{
|
||||
@@ -105,9 +105,8 @@ class TestCreditTransactionRepository:
|
||||
ensure_plans: tuple[Any, ...], # noqa: ARG002
|
||||
) -> AsyncGenerator[CreditTransaction, None]:
|
||||
"""Create a transaction for a different user."""
|
||||
from app.models.plan import Plan
|
||||
from app.repositories.user import UserRepository
|
||||
|
||||
|
||||
# Create another user
|
||||
user_repo = UserRepository(test_session)
|
||||
other_user_data = {
|
||||
@@ -134,7 +133,7 @@ class TestCreditTransactionRepository:
|
||||
test_session.add(transaction)
|
||||
await test_session.commit()
|
||||
await test_session.refresh(transaction)
|
||||
|
||||
|
||||
yield transaction
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@@ -178,7 +177,7 @@ class TestCreditTransactionRepository:
|
||||
assert len(transactions) == 4
|
||||
# Should be ordered by created_at desc (newest first)
|
||||
assert all(t.user_id == test_user_id for t in transactions)
|
||||
|
||||
|
||||
# Should not include other user's transaction
|
||||
other_user_ids = [t.user_id for t in transactions]
|
||||
assert other_user_transaction.user_id not in other_user_ids
|
||||
@@ -193,13 +192,13 @@ class TestCreditTransactionRepository:
|
||||
"""Test getting transactions by user ID with pagination."""
|
||||
# Get first 2 transactions
|
||||
first_page = await credit_transaction_repository.get_by_user_id(
|
||||
test_user_id, limit=2, offset=0
|
||||
test_user_id, limit=2, offset=0,
|
||||
)
|
||||
assert len(first_page) == 2
|
||||
|
||||
# Get next 2 transactions
|
||||
second_page = await credit_transaction_repository.get_by_user_id(
|
||||
test_user_id, limit=2, offset=2
|
||||
test_user_id, limit=2, offset=2,
|
||||
)
|
||||
assert len(second_page) == 2
|
||||
|
||||
@@ -216,17 +215,17 @@ class TestCreditTransactionRepository:
|
||||
) -> None:
|
||||
"""Test getting transactions by action type."""
|
||||
vlc_transactions = await credit_transaction_repository.get_by_action_type(
|
||||
"vlc_play_sound"
|
||||
"vlc_play_sound",
|
||||
)
|
||||
|
||||
|
||||
# Should return 2 VLC transactions (1 successful, 1 failed)
|
||||
assert len(vlc_transactions) >= 2
|
||||
assert all(t.action_type == "vlc_play_sound" for t in vlc_transactions)
|
||||
|
||||
extraction_transactions = await credit_transaction_repository.get_by_action_type(
|
||||
"audio_extraction"
|
||||
"audio_extraction",
|
||||
)
|
||||
|
||||
|
||||
# Should return 1 extraction transaction
|
||||
assert len(extraction_transactions) >= 1
|
||||
assert all(t.action_type == "audio_extraction" for t in extraction_transactions)
|
||||
@@ -240,14 +239,14 @@ class TestCreditTransactionRepository:
|
||||
"""Test getting transactions by action type with pagination."""
|
||||
# Test with limit
|
||||
transactions = await credit_transaction_repository.get_by_action_type(
|
||||
"vlc_play_sound", limit=1
|
||||
"vlc_play_sound", limit=1,
|
||||
)
|
||||
assert len(transactions) == 1
|
||||
assert transactions[0].action_type == "vlc_play_sound"
|
||||
|
||||
# Test with offset
|
||||
transactions = await credit_transaction_repository.get_by_action_type(
|
||||
"vlc_play_sound", limit=1, offset=1
|
||||
"vlc_play_sound", limit=1, offset=1,
|
||||
)
|
||||
assert len(transactions) <= 1 # Might be 0 if only 1 VLC transaction in total
|
||||
|
||||
@@ -275,7 +274,7 @@ class TestCreditTransactionRepository:
|
||||
) -> None:
|
||||
"""Test getting successful transactions filtered by user."""
|
||||
successful_transactions = await credit_transaction_repository.get_successful_transactions(
|
||||
user_id=test_user_id
|
||||
user_id=test_user_id,
|
||||
)
|
||||
|
||||
# Should only return successful transactions for test_user
|
||||
@@ -294,14 +293,14 @@ class TestCreditTransactionRepository:
|
||||
"""Test getting successful transactions with pagination."""
|
||||
# Get first 2 successful transactions
|
||||
first_page = await credit_transaction_repository.get_successful_transactions(
|
||||
user_id=test_user_id, limit=2, offset=0
|
||||
user_id=test_user_id, limit=2, offset=0,
|
||||
)
|
||||
assert len(first_page) == 2
|
||||
assert all(t.success is True for t in first_page)
|
||||
|
||||
# Get next successful transaction
|
||||
second_page = await credit_transaction_repository.get_successful_transactions(
|
||||
user_id=test_user_id, limit=2, offset=2
|
||||
user_id=test_user_id, limit=2, offset=2,
|
||||
)
|
||||
assert len(second_page) == 1 # Should be 1 remaining
|
||||
assert all(t.success is True for t in second_page)
|
||||
@@ -363,7 +362,7 @@ class TestCreditTransactionRepository:
|
||||
}
|
||||
|
||||
updated_transaction = await credit_transaction_repository.update(
|
||||
transaction, update_data
|
||||
transaction, update_data,
|
||||
)
|
||||
|
||||
assert updated_transaction.id == transaction.id
|
||||
@@ -413,7 +412,7 @@ class TestCreditTransactionRepository:
|
||||
) -> None:
|
||||
"""Test that transactions are ordered by created_at desc."""
|
||||
transactions = await credit_transaction_repository.get_by_user_id(test_user_id)
|
||||
|
||||
|
||||
# Should be ordered by created_at desc (newest first)
|
||||
for i in range(len(transactions) - 1):
|
||||
assert transactions[i].created_at >= transactions[i + 1].created_at
|
||||
assert transactions[i].created_at >= transactions[i + 1].created_at
|
||||
|
||||
Reference in New Issue
Block a user