Spaces:
Sleeping
Sleeping
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 | |
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 |