Spaces:
Sleeping
Sleeping
Praneeth Yerrapragada commited on
Commit ·
cf9085d
1
Parent(s): 6d92adc
test: pytest for transactions
Browse files- app/model/transaction.py +3 -6
- tests/conftest.py +10 -4
- tests/test_transactions.py +109 -0
app/model/transaction.py
CHANGED
|
@@ -31,12 +31,9 @@ class Transaction(Base, BaseModel):
|
|
| 31 |
|
| 32 |
@classmethod
|
| 33 |
async def bulk_create(cls: "type[Transaction]", db: AsyncSession, transactions: List[TransactionCreate]) -> None:
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
amount= transaction.amount,
|
| 38 |
-
name_description=transaction.name_description) for transaction in transactions]
|
| 39 |
-
db.add(transactions_list)
|
| 40 |
await db.commit()
|
| 41 |
|
| 42 |
@classmethod
|
|
|
|
| 31 |
|
| 32 |
@classmethod
|
| 33 |
async def bulk_create(cls: "type[Transaction]", db: AsyncSession, transactions: List[TransactionCreate]) -> None:
|
| 34 |
+
values = [transaction.model_dump() for transaction in transactions]
|
| 35 |
+
query = sql.insert(cls).values(values)
|
| 36 |
+
await db.execute(query)
|
|
|
|
|
|
|
|
|
|
| 37 |
await db.commit()
|
| 38 |
|
| 39 |
@classmethod
|
tests/conftest.py
CHANGED
|
@@ -37,7 +37,7 @@ def client(app):
|
|
| 37 |
yield c
|
| 38 |
|
| 39 |
|
| 40 |
-
@pytest.
|
| 41 |
def event_loop(request):
|
| 42 |
loop = asyncio.get_event_loop_policy().new_event_loop()
|
| 43 |
yield loop
|
|
@@ -52,9 +52,9 @@ async def connection_test(test_db, event_loop):
|
|
| 52 |
pg_db = test_db.dbname
|
| 53 |
pg_password = test_db.password
|
| 54 |
|
| 55 |
-
with DatabaseJanitor(pg_user, pg_host, pg_port, pg_db, test_db.version, pg_password):
|
| 56 |
-
connection_str = f"postgresql+
|
| 57 |
-
sessionmanager.init(connection_str)
|
| 58 |
yield
|
| 59 |
await sessionmanager.close()
|
| 60 |
|
|
@@ -73,3 +73,9 @@ async def session_override(app, connection_test):
|
|
| 73 |
yield session
|
| 74 |
|
| 75 |
app.dependency_overrides[get_db_session] = get_db_session_override
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
yield c
|
| 38 |
|
| 39 |
|
| 40 |
+
@pytest.fixture(scope="session")
|
| 41 |
def event_loop(request):
|
| 42 |
loop = asyncio.get_event_loop_policy().new_event_loop()
|
| 43 |
yield loop
|
|
|
|
| 52 |
pg_db = test_db.dbname
|
| 53 |
pg_password = test_db.password
|
| 54 |
|
| 55 |
+
with DatabaseJanitor(user=pg_user, host=pg_host, port=pg_port, dbname=pg_db, version=test_db.version, password=pg_password):
|
| 56 |
+
connection_str = f"postgresql+psycopg://{pg_user}:@{pg_host}:{pg_port}/{pg_db}"
|
| 57 |
+
sessionmanager.init(connection_str, {"echo": True, "future": True})
|
| 58 |
yield
|
| 59 |
await sessionmanager.close()
|
| 60 |
|
|
|
|
| 73 |
yield session
|
| 74 |
|
| 75 |
app.dependency_overrides[get_db_session] = get_db_session_override
|
| 76 |
+
|
| 77 |
+
|
| 78 |
+
@pytest.fixture(scope="function", autouse=True)
|
| 79 |
+
async def get_db_session_fixture():
|
| 80 |
+
async with sessionmanager.session() as session:
|
| 81 |
+
yield session
|
tests/test_transactions.py
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from datetime import datetime
|
| 2 |
+
from typing import List
|
| 3 |
+
from fastapi import Depends
|
| 4 |
+
from fastapi.testclient import TestClient
|
| 5 |
+
import pytest
|
| 6 |
+
|
| 7 |
+
from app.model.transaction import Transaction
|
| 8 |
+
from app.schema.index import TransactionType, TransactionCreate
|
| 9 |
+
|
| 10 |
+
from sqlalchemy.ext.asyncio import AsyncSession
|
| 11 |
+
from app.engine.postgresdb import get_db_session
|
| 12 |
+
from app.model.user import User
|
| 13 |
+
|
| 14 |
+
def get_fake_transactions(user_id: int) -> List[TransactionCreate]:
|
| 15 |
+
return [
|
| 16 |
+
TransactionCreate(
|
| 17 |
+
user_id=user_id,
|
| 18 |
+
transaction_date=datetime(2022, 1, 1),
|
| 19 |
+
category="category",
|
| 20 |
+
name_description="name_description",
|
| 21 |
+
amount=1.0,
|
| 22 |
+
type=TransactionType.EXPENSE,
|
| 23 |
+
),
|
| 24 |
+
TransactionCreate(
|
| 25 |
+
user_id=user_id,
|
| 26 |
+
transaction_date=datetime(2022, 1, 2),
|
| 27 |
+
category="category",
|
| 28 |
+
name_description="name_description",
|
| 29 |
+
amount=2.0,
|
| 30 |
+
type=TransactionType.EXPENSE,
|
| 31 |
+
),
|
| 32 |
+
TransactionCreate(
|
| 33 |
+
user_id=user_id,
|
| 34 |
+
transaction_date=datetime(2022, 1, 3),
|
| 35 |
+
category="category",
|
| 36 |
+
name_description="name_description",
|
| 37 |
+
amount=3.0,
|
| 38 |
+
type=TransactionType.INCOME,
|
| 39 |
+
),
|
| 40 |
+
TransactionCreate(
|
| 41 |
+
user_id=user_id,
|
| 42 |
+
transaction_date=datetime(2022, 1, 4),
|
| 43 |
+
category="category",
|
| 44 |
+
name_description="name_description",
|
| 45 |
+
amount=4.0,
|
| 46 |
+
type=TransactionType.INCOME,
|
| 47 |
+
),
|
| 48 |
+
TransactionCreate(
|
| 49 |
+
user_id=user_id,
|
| 50 |
+
transaction_date=datetime(2022, 1, 5),
|
| 51 |
+
category="category",
|
| 52 |
+
name_description="name_description",
|
| 53 |
+
amount=5.0,
|
| 54 |
+
type=TransactionType.EXPENSE,
|
| 55 |
+
),
|
| 56 |
+
TransactionCreate(
|
| 57 |
+
user_id=user_id,
|
| 58 |
+
transaction_date=datetime(2022, 1, 6),
|
| 59 |
+
category="category",
|
| 60 |
+
name_description="name_description",
|
| 61 |
+
amount=6.0,
|
| 62 |
+
type=TransactionType.EXPENSE,
|
| 63 |
+
),
|
| 64 |
+
TransactionCreate(
|
| 65 |
+
user_id=user_id,
|
| 66 |
+
transaction_date=datetime(2022, 1, 7),
|
| 67 |
+
category="category",
|
| 68 |
+
name_description="name_description",
|
| 69 |
+
amount=7.0,
|
| 70 |
+
type=TransactionType.INCOME,
|
| 71 |
+
),
|
| 72 |
+
TransactionCreate(
|
| 73 |
+
user_id=user_id,
|
| 74 |
+
transaction_date=datetime(2022, 1, 8),
|
| 75 |
+
category="category",
|
| 76 |
+
name_description="name_description",
|
| 77 |
+
amount=8.0,
|
| 78 |
+
type=TransactionType.INCOME,
|
| 79 |
+
),
|
| 80 |
+
TransactionCreate(
|
| 81 |
+
user_id=user_id,
|
| 82 |
+
transaction_date=datetime(2022, 1, 9),
|
| 83 |
+
category="category",
|
| 84 |
+
name_description="name_description",
|
| 85 |
+
amount=9.0,
|
| 86 |
+
type=TransactionType.EXPENSE,
|
| 87 |
+
),
|
| 88 |
+
TransactionCreate(
|
| 89 |
+
user_id=user_id,
|
| 90 |
+
transaction_date=datetime(2022, 1, 10),
|
| 91 |
+
category="category",
|
| 92 |
+
name_description="name_description",
|
| 93 |
+
amount=10.0,
|
| 94 |
+
type=TransactionType.EXPENSE,
|
| 95 |
+
),
|
| 96 |
+
]
|
| 97 |
+
|
| 98 |
+
@pytest.mark.asyncio
|
| 99 |
+
async def test_transactions(client: TestClient, get_db_session_fixture: AsyncSession) -> None:
|
| 100 |
+
|
| 101 |
+
session_override = get_db_session_fixture
|
| 102 |
+
user = await User.create(session_override, name="user", email="email", hashed_password="password")
|
| 103 |
+
|
| 104 |
+
fake_transactions = get_fake_transactions(user.id)
|
| 105 |
+
await Transaction.bulk_create(session_override, fake_transactions)
|
| 106 |
+
|
| 107 |
+
response = client.get("/api/v1/transactions/1")
|
| 108 |
+
assert response.status_code == 200
|
| 109 |
+
assert len(response.json()) == 10
|