Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Gritsuk Maksim committed Mar 13, 2024
1 parent 51e7a8f commit 09340bc
Show file tree
Hide file tree
Showing 7 changed files with 170 additions and 7 deletions.
8 changes: 4 additions & 4 deletions app/api/endpoints/account.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,9 @@ def get_account_with_transactions(


@router.post(
"/", dependencies=[Depends(get_current_user)], response_model=AccountSchema
"/", response_model=AccountSchema
)
def create_account(*, session: SessionDep, account_in: AccountCreate):
def create_account(*, session: SessionDep, account_in: AccountCreate, current_user: CurrentUser):
"""
**Создает новый счет для текущего пользователя.**
Expand All @@ -117,8 +117,8 @@ def create_account(*, session: SessionDep, account_in: AccountCreate):
Returns:
AccountSchema: Созданный счет.
"""
user = crud.account.create(db=session, obj_in=account_in)
return user
account = crud.account.create(db=session, obj_in=account_in, user_id=current_user.id)
return account


@router.put("/{account_id}", response_model=AccountSchema)
Expand Down
6 changes: 3 additions & 3 deletions app/api/endpoints/budget.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ def get_budgets(*, session: SessionDep, current_user: CurrentUser):
return budgets


@router.post("/", dependencies=[Depends(get_current_user)], response_model=BudgetSchema)
def create_budget(*, session: SessionDep, budget_in: BudgetCreate):
budget = crud.budget.create(db=session, obj_in=budget_in)
@router.post("/", response_model=BudgetSchema)
def create_budget(*, session: SessionDep, budget_in: BudgetCreate, current_user: CurrentUser):
budget = crud.budget.create(db=session, obj_in=budget_in, user_id=current_user.id)
return budget


Expand Down
13 changes: 13 additions & 0 deletions app/tests/api/test_login.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from typing import Dict

from fastapi.testclient import TestClient
from sqlalchemy.orm import Session

from app.core.config import settings
from app.crud.auth import get_user_by_email


def test_get_access_token(client: TestClient):
Expand All @@ -15,3 +17,14 @@ def test_get_access_token(client: TestClient):
assert auth.status_code == 200
assert "access_token" in tokens
assert tokens["access_token"]


def test_get_me(client: TestClient, user_token_headers: dict, db: Session):
user = get_user_by_email(session=db, email=settings.EMAIL_TEST_USER)
response = client.get("/auth/me", headers=user_token_headers)
assert response.status_code == 200
content = response.json()
assert content["name"] == user.name
assert content["email"] == user.email
assert content["phone_number"] == user.phone_number
assert content["region"] == user.region
38 changes: 38 additions & 0 deletions app/tests/api/test_transaction.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
from decimal import Decimal

from fastapi.testclient import TestClient
from sqlalchemy.orm import Session

from app.tests.utils.category import create_random_category
from app.tests.utils.account import create_random_account


def test_create_transaction(client: TestClient, db: Session, user_token_headers: dict):
category = create_random_category(db=db)
account = create_random_account(db=db)
data = {
"amount": 50.0,
"description": "Test Transaction",
"category_id": category.id,
"account_id": account.id,
}

response = client.post("/transaction/", headers=user_token_headers, json=data)
assert response.status_code == 200
content = response.json()

assert Decimal(content["amount"]) == Decimal(data["amount"])
assert content["description"] == data["description"]
assert content["category_id"] == data["category_id"]
assert content["account_id"] == data["account_id"]
assert "date" in content

def test_get_account_transactions(client: TestClient, db: Session, user_token_headers: dict):
account = create_random_account(db=db)

response = client.get(f"/transaction/account_transactions/{account.id}", headers=user_token_headers)

assert response.status_code == 200



44 changes: 44 additions & 0 deletions app/tests/api/test_user.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
from fastapi.testclient import TestClient
from sqlalchemy.orm import Session
from faker import Faker

from app.tests.utils.user import create_random_user
fake = Faker()


def test_create_user(client: TestClient, user_token_headers: dict):
user_data = {
"name": "New User",
"email": "newuser@example.com",
"phone_number": fake.phone_number(),
"password": "newpassword",
"region": "RU",
}

response = client.post("/user/", headers=user_token_headers, json=user_data)
assert response.status_code == 200

content = response.json()
assert content["name"] == user_data["name"]
assert content["phone_number"] == user_data["phone_number"]
assert content["email"] == user_data["email"]
assert content["region"] == user_data["region"]
assert "id" in content


def test_get_users(client: TestClient, db: Session, user_token_headers: dict):
create_random_user(db=db)
create_random_user(db=db)

response = client.get("/user/", headers=user_token_headers)
assert response.status_code == 200
content = response.json()
assert len(content) >= 2


def test_delete_user(client: TestClient, user_token_headers: dict, db: Session):
user = create_random_user(db=db)
response = client.delete(f"/user/{user.id}", headers=user_token_headers)
assert response.status_code == 200
content = response.text
assert f"Пользователь: {user.name} удалён" in content
33 changes: 33 additions & 0 deletions app/tests/utils/account.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from typing import Optional
from faker import Faker
from sqlalchemy.orm import Session

from app.schemas.account import AccountCreate, CurrencyEnum
from app import crud
from app.core.config import settings
from app.models import Account

fake = Faker()


def create_random_account(db: Session, user_id: Optional[int] = None) -> Account:
if not user_id:
user = crud.auth.get_user_by_email(session=db, email=settings.EMAIL_TEST_USER)
user_id = user.id

name = fake.word()
balance = fake.pydecimal(min_value=0, max_value=10000, right_digits=2)
currency = fake.random_element(elements=[c.value for c in CurrencyEnum])
account_type = fake.word()
is_active = fake.boolean()

account_in = AccountCreate(
name=name,
balance=balance,
currency=currency,
type=account_type,
is_active=is_active,
user_id=user_id,
)

return crud.account.create(db=db, obj_in=account_in, user_id=user_id)
35 changes: 35 additions & 0 deletions app/tests/utils/transaction.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
from typing import Optional
from faker import Faker
from sqlalchemy.orm import Session

from app.schemas.transaction import TransactionCreate
from app import crud
from app.core.config import settings
from app.models import Transaction
from app.tests.utils.category import create_random_category
from app.tests.utils.account import create_random_account

fake = Faker()


def create_random_transaction(
db: Session, user_id: Optional[int] = None
) -> Transaction:
if not user_id:
user = crud.auth.get_user_by_email(session=db, email=settings.EMAIL_TEST_USER)
user_id = user.id

amount = fake.pydecimal(min_value=-1000, max_value=1000, right_digits=2)
description = fake.word()

category = create_random_category(db=db, user_id=user_id)
account = create_random_account(db=db, user_id=user_id)

transaction_in = TransactionCreate(
amount=amount,
description=description,
category_id=category.id,
account_id=account.id,
)

return crud.transaction.create(db=db, obj_in=transaction_in, user_id=user_id)

0 comments on commit 09340bc

Please sign in to comment.