Spaces:
Sleeping
Sleeping
File size: 1,847 Bytes
4761dd9 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | from fastapi.testclient import TestClient
import pytest
from sqlalchemy.ext.asyncio import AsyncSession
from app.model.transaction import Transaction
from app.model.user import User
from tests.utils import get_fake_transactions
@pytest.mark.asyncio
async def test_income_statement(client: TestClient, get_db_session_fixture: AsyncSession) -> None:
session_override = get_db_session_fixture
# 1. Create a user
user = await User.create(session_override, name="user", email="email", hashed_password="password")
# 2. Create a bunch of transactions
fake_transactions = get_fake_transactions(user.id)
await Transaction.bulk_create(session_override, fake_transactions)
# 3. Create an income statement
min_date = min(t.transaction_date for t in fake_transactions)
max_date = max(t.transaction_date for t in fake_transactions)
print(f"min_date: {min_date}, max_date: {max_date}")
response = client.post(
"/api/v1/income_statement",
json={
"user_id": 1,
"date_from": str(min_date),
"date_to": str(max_date),
},
)
assert response.status_code == 200
# 4. Verify that the income statement matches the transactions
response = client.get(f"/api/v1/income_statement/user/1")
print(response.json())
assert response.status_code == 200
assert response.json()[0].get("income")
assert response.json()[0].get("expenses")
report_id = response.json()[0].get("id")
# # 5. Verify that the income statement can be retrieved
if report_id is not None:
response = client.get(f"/api/v1/income_statement/report/{report_id}")
assert response.status_code == 200
assert response.json().get("income")
assert response.json().get("expenses")
assert response.json().get("id") == report_id |