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:
@@ -202,13 +202,17 @@ 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) == PAGE_SIZE
|
||||
|
||||
# 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) == PAGE_SIZE
|
||||
|
||||
@@ -251,14 +255,17 @@ 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
|
||||
|
||||
@@ -269,7 +276,9 @@ class TestCreditTransactionRepository:
|
||||
test_transactions: list[CreditTransaction],
|
||||
) -> None:
|
||||
"""Test getting only successful transactions."""
|
||||
successful_transactions = await credit_transaction_repository.get_successful_transactions()
|
||||
successful_transactions = (
|
||||
await credit_transaction_repository.get_successful_transactions()
|
||||
)
|
||||
|
||||
# Should only return successful transactions
|
||||
assert all(t.success is True for t in successful_transactions)
|
||||
@@ -285,8 +294,10 @@ class TestCreditTransactionRepository:
|
||||
test_user_id: int,
|
||||
) -> None:
|
||||
"""Test getting successful transactions filtered by user."""
|
||||
successful_transactions = await credit_transaction_repository.get_successful_transactions(
|
||||
user_id=test_user_id,
|
||||
successful_transactions = (
|
||||
await credit_transaction_repository.get_successful_transactions(
|
||||
user_id=test_user_id,
|
||||
)
|
||||
)
|
||||
|
||||
# Should only return successful transactions for test_user
|
||||
@@ -305,14 +316,18 @@ 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) == PAGE_SIZE
|
||||
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)
|
||||
@@ -328,7 +343,9 @@ class TestCreditTransactionRepository:
|
||||
all_transactions = await credit_transaction_repository.get_all()
|
||||
|
||||
# Should return all transactions
|
||||
assert len(all_transactions) >= MIN_ALL_TRANSACTIONS # 4 from test_transactions + 1 other_user_transaction
|
||||
assert (
|
||||
len(all_transactions) >= MIN_ALL_TRANSACTIONS
|
||||
) # 4 from test_transactions + 1 other_user_transaction
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_create_transaction(
|
||||
@@ -374,7 +391,8 @@ class TestCreditTransactionRepository:
|
||||
}
|
||||
|
||||
updated_transaction = await credit_transaction_repository.update(
|
||||
transaction, update_data,
|
||||
transaction,
|
||||
update_data,
|
||||
)
|
||||
|
||||
assert updated_transaction.id == transaction.id
|
||||
@@ -412,7 +430,9 @@ class TestCreditTransactionRepository:
|
||||
|
||||
# Verify transaction is deleted
|
||||
assert transaction_id is not None
|
||||
deleted_transaction = await credit_transaction_repository.get_by_id(transaction_id)
|
||||
deleted_transaction = await credit_transaction_repository.get_by_id(
|
||||
transaction_id,
|
||||
)
|
||||
assert deleted_transaction is None
|
||||
|
||||
@pytest.mark.asyncio
|
||||
|
||||
@@ -407,7 +407,8 @@ class TestPlaylistRepository:
|
||||
|
||||
# Test the repository method
|
||||
playlist_sound = await playlist_repository.add_sound_to_playlist(
|
||||
playlist_id, sound_id,
|
||||
playlist_id,
|
||||
sound_id,
|
||||
)
|
||||
|
||||
assert playlist_sound.playlist_id == playlist_id
|
||||
@@ -472,7 +473,9 @@ class TestPlaylistRepository:
|
||||
|
||||
# Test the repository method
|
||||
playlist_sound = await playlist_repository.add_sound_to_playlist(
|
||||
playlist_id, sound_id, position=5,
|
||||
playlist_id,
|
||||
sound_id,
|
||||
position=5,
|
||||
)
|
||||
|
||||
assert playlist_sound.position == TEST_POSITION
|
||||
@@ -535,17 +538,20 @@ class TestPlaylistRepository:
|
||||
|
||||
# Verify it was added
|
||||
assert await playlist_repository.is_sound_in_playlist(
|
||||
playlist_id, sound_id,
|
||||
playlist_id,
|
||||
sound_id,
|
||||
)
|
||||
|
||||
# Remove the sound
|
||||
await playlist_repository.remove_sound_from_playlist(
|
||||
playlist_id, sound_id,
|
||||
playlist_id,
|
||||
sound_id,
|
||||
)
|
||||
|
||||
# Verify it was removed
|
||||
assert not await playlist_repository.is_sound_in_playlist(
|
||||
playlist_id, sound_id,
|
||||
playlist_id,
|
||||
sound_id,
|
||||
)
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@@ -732,7 +738,8 @@ class TestPlaylistRepository:
|
||||
|
||||
# Initially not in playlist
|
||||
assert not await playlist_repository.is_sound_in_playlist(
|
||||
playlist_id, sound_id,
|
||||
playlist_id,
|
||||
sound_id,
|
||||
)
|
||||
|
||||
# Add sound
|
||||
@@ -740,7 +747,8 @@ class TestPlaylistRepository:
|
||||
|
||||
# Now in playlist
|
||||
assert await playlist_repository.is_sound_in_playlist(
|
||||
playlist_id, sound_id,
|
||||
playlist_id,
|
||||
sound_id,
|
||||
)
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@@ -794,16 +802,21 @@ class TestPlaylistRepository:
|
||||
|
||||
# Add sounds to playlist
|
||||
await playlist_repository.add_sound_to_playlist(
|
||||
playlist_id, sound1_id, position=0,
|
||||
playlist_id,
|
||||
sound1_id,
|
||||
position=0,
|
||||
)
|
||||
await playlist_repository.add_sound_to_playlist(
|
||||
playlist_id, sound2_id, position=1,
|
||||
playlist_id,
|
||||
sound2_id,
|
||||
position=1,
|
||||
)
|
||||
|
||||
# Reorder sounds - use different positions to avoid constraint issues
|
||||
sound_positions = [(sound1_id, 10), (sound2_id, 5)]
|
||||
await playlist_repository.reorder_playlist_sounds(
|
||||
playlist_id, sound_positions,
|
||||
playlist_id,
|
||||
sound_positions,
|
||||
)
|
||||
|
||||
# Verify new order
|
||||
@@ -863,10 +876,14 @@ class TestPlaylistRepository:
|
||||
|
||||
# Add sounds to playlist at positions 0 and 1
|
||||
await playlist_repository.add_sound_to_playlist(
|
||||
playlist_id, sound1_id, position=0,
|
||||
playlist_id,
|
||||
sound1_id,
|
||||
position=0,
|
||||
)
|
||||
await playlist_repository.add_sound_to_playlist(
|
||||
playlist_id, sound2_id, position=1,
|
||||
playlist_id,
|
||||
sound2_id,
|
||||
position=1,
|
||||
)
|
||||
|
||||
# Verify initial order
|
||||
@@ -878,7 +895,8 @@ class TestPlaylistRepository:
|
||||
# Swap positions - this used to cause unique constraint violation
|
||||
sound_positions = [(sound1_id, 1), (sound2_id, 0)]
|
||||
await playlist_repository.reorder_playlist_sounds(
|
||||
playlist_id, sound_positions,
|
||||
playlist_id,
|
||||
sound_positions,
|
||||
)
|
||||
|
||||
# Verify swapped order
|
||||
|
||||
@@ -61,7 +61,8 @@ class TestUserOauthRepository:
|
||||
) -> None:
|
||||
"""Test getting OAuth by provider user ID when it exists."""
|
||||
oauth = await user_oauth_repository.get_by_provider_user_id(
|
||||
"google", "google_123456",
|
||||
"google",
|
||||
"google_123456",
|
||||
)
|
||||
|
||||
assert oauth is not None
|
||||
@@ -77,7 +78,8 @@ class TestUserOauthRepository:
|
||||
) -> None:
|
||||
"""Test getting OAuth by provider user ID when it doesn't exist."""
|
||||
oauth = await user_oauth_repository.get_by_provider_user_id(
|
||||
"google", "nonexistent_id",
|
||||
"google",
|
||||
"nonexistent_id",
|
||||
)
|
||||
|
||||
assert oauth is None
|
||||
@@ -91,7 +93,8 @@ class TestUserOauthRepository:
|
||||
) -> None:
|
||||
"""Test getting OAuth by user ID and provider when it exists."""
|
||||
oauth = await user_oauth_repository.get_by_user_id_and_provider(
|
||||
test_user_id, "google",
|
||||
test_user_id,
|
||||
"google",
|
||||
)
|
||||
|
||||
assert oauth is not None
|
||||
@@ -107,7 +110,8 @@ class TestUserOauthRepository:
|
||||
) -> None:
|
||||
"""Test getting OAuth by user ID and provider when it doesn't exist."""
|
||||
oauth = await user_oauth_repository.get_by_user_id_and_provider(
|
||||
test_user_id, "github",
|
||||
test_user_id,
|
||||
"github",
|
||||
)
|
||||
|
||||
assert oauth is None
|
||||
@@ -186,7 +190,8 @@ class TestUserOauthRepository:
|
||||
|
||||
# Verify it's deleted by trying to find it
|
||||
deleted_oauth = await user_oauth_repository.get_by_provider_user_id(
|
||||
"twitter", "twitter_456",
|
||||
"twitter",
|
||||
"twitter_456",
|
||||
)
|
||||
assert deleted_oauth is None
|
||||
|
||||
@@ -243,10 +248,12 @@ class TestUserOauthRepository:
|
||||
|
||||
# Verify both exist by querying back from database
|
||||
found_google = await user_oauth_repository.get_by_user_id_and_provider(
|
||||
test_user_id, "google",
|
||||
test_user_id,
|
||||
"google",
|
||||
)
|
||||
found_github = await user_oauth_repository.get_by_user_id_and_provider(
|
||||
test_user_id, "github",
|
||||
test_user_id,
|
||||
"github",
|
||||
)
|
||||
|
||||
assert found_google is not None
|
||||
@@ -260,10 +267,12 @@ class TestUserOauthRepository:
|
||||
|
||||
# Verify we can also find them by provider_user_id
|
||||
found_google_by_provider = await user_oauth_repository.get_by_provider_user_id(
|
||||
"google", "google_user_1",
|
||||
"google",
|
||||
"google_user_1",
|
||||
)
|
||||
found_github_by_provider = await user_oauth_repository.get_by_provider_user_id(
|
||||
"github", "github_user_1",
|
||||
"github",
|
||||
"github_user_1",
|
||||
)
|
||||
|
||||
assert found_google_by_provider is not None
|
||||
|
||||
Reference in New Issue
Block a user