fix: Lint fixes of core and repositories tests
All checks were successful
Backend CI / lint (push) Successful in 9m26s
Backend CI / test (push) Successful in 4m24s

This commit is contained in:
JSC
2025-08-01 09:17:20 +02:00
parent 389cfe2d6a
commit dc29915fbc
8 changed files with 135 additions and 88 deletions

View File

@@ -1,4 +1,5 @@
"""Tests for credit transaction repository."""
# ruff: noqa: ARG002, E501
import json
from collections.abc import AsyncGenerator
@@ -11,6 +12,18 @@ from sqlmodel.ext.asyncio.session import AsyncSession
from app.models.credit_transaction import CreditTransaction
from app.models.user import User
from app.repositories.credit_transaction import CreditTransactionRepository
from app.repositories.user import UserRepository
# Constants
EXPECTED_TRANSACTION_COUNT = 4
PAGE_SIZE = 2
MIN_VLC_TRANSACTIONS = 2
MIN_SUCCESSFUL_TRANSACTIONS = 3
SUCCESSFUL_TRANSACTION_COUNT = 3
MIN_ALL_TRANSACTIONS = 5
TEST_AMOUNT = -10
TEST_BALANCE_BEFORE = 100
TEST_BALANCE_AFTER = 90
class TestCreditTransactionRepository:
@@ -102,11 +115,9 @@ class TestCreditTransactionRepository:
async def other_user_transaction(
self,
test_session: AsyncSession,
ensure_plans: tuple[Any, ...], # noqa: ARG002
ensure_plans: tuple[Any, ...],
) -> AsyncGenerator[CreditTransaction, None]:
"""Create a transaction for a different user."""
from app.repositories.user import UserRepository
# Create another user
user_repo = UserRepository(test_session)
other_user_data = {
@@ -174,7 +185,7 @@ class TestCreditTransactionRepository:
transactions = await credit_transaction_repository.get_by_user_id(test_user_id)
# Should return all transactions for test_user
assert len(transactions) == 4
assert len(transactions) == EXPECTED_TRANSACTION_COUNT
# Should be ordered by created_at desc (newest first)
assert all(t.user_id == test_user_id for t in transactions)
@@ -194,13 +205,13 @@ class TestCreditTransactionRepository:
first_page = await credit_transaction_repository.get_by_user_id(
test_user_id, limit=2, offset=0,
)
assert len(first_page) == 2
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,
)
assert len(second_page) == 2
assert len(second_page) == PAGE_SIZE
# Should not overlap
first_page_ids = {t.id for t in first_page}
@@ -219,11 +230,13 @@ class TestCreditTransactionRepository:
)
# Should return 2 VLC transactions (1 successful, 1 failed)
assert len(vlc_transactions) >= 2
assert len(vlc_transactions) >= MIN_VLC_TRANSACTIONS
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",
extraction_transactions = (
await credit_transaction_repository.get_by_action_type(
"audio_extraction",
)
)
# Should return 1 extraction transaction
@@ -262,7 +275,7 @@ class TestCreditTransactionRepository:
# Should only return successful transactions
assert all(t.success is True for t in successful_transactions)
# Should be at least 3 (vlc_play_sound, audio_extraction, credit_addition)
assert len(successful_transactions) >= 3
assert len(successful_transactions) >= MIN_SUCCESSFUL_TRANSACTIONS
@pytest.mark.asyncio
async def test_get_successful_transactions_by_user(
@@ -281,7 +294,7 @@ class TestCreditTransactionRepository:
assert all(t.success is True for t in successful_transactions)
assert all(t.user_id == test_user_id for t in successful_transactions)
# Should be 3 successful transactions for test_user
assert len(successful_transactions) == 3
assert len(successful_transactions) == SUCCESSFUL_TRANSACTION_COUNT
@pytest.mark.asyncio
async def test_get_successful_transactions_with_pagination(
@@ -295,7 +308,7 @@ class TestCreditTransactionRepository:
first_page = await credit_transaction_repository.get_successful_transactions(
user_id=test_user_id, limit=2, offset=0,
)
assert len(first_page) == 2
assert len(first_page) == PAGE_SIZE
assert all(t.success is True for t in first_page)
# Get next successful transaction
@@ -316,7 +329,7 @@ class TestCreditTransactionRepository:
all_transactions = await credit_transaction_repository.get_all()
# Should return all transactions
assert len(all_transactions) >= 5 # 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(
@@ -341,9 +354,9 @@ class TestCreditTransactionRepository:
assert transaction.id is not None
assert transaction.user_id == test_user_id
assert transaction.action_type == "test_action"
assert transaction.amount == -10
assert transaction.balance_before == 100
assert transaction.balance_after == 90
assert transaction.amount == TEST_AMOUNT
assert transaction.balance_before == TEST_BALANCE_BEFORE
assert transaction.balance_after == TEST_BALANCE_AFTER
assert transaction.success is True
assert transaction.metadata_json is not None
assert json.loads(transaction.metadata_json) == {"test": "data"}