-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Gritsuk Maksim
committed
Mar 13, 2024
1 parent
51e7a8f
commit 09340bc
Showing
7 changed files
with
170 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |