-
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.
Merge pull request #18 from HeySlava/test-rm
Test rm service
- Loading branch information
Showing
8 changed files
with
98 additions
and
44 deletions.
There are no files selected for viewing
Empty file.
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 pathlib import Path | ||
|
||
import sqlalchemy as sa | ||
from data.models import Expense | ||
from data.models import Item | ||
from data.models import User | ||
from sqlalchemy import orm | ||
|
||
|
||
base_dir = Path(__file__).parent.parent / 'bot' | ||
|
||
|
||
def fill_in_db( | ||
session: orm.Session, | ||
users_number: int = 1, | ||
expenses_number: int = 1, | ||
items_number: int = 1, | ||
currencies: list = ['unit1'], | ||
commit: bool = True, | ||
): | ||
for u in range(users_number): | ||
user = User(id=u, first_name=f'user-{u}') | ||
for i_num in range(items_number): | ||
item_name = str(i_num) | ||
item = Item(name=item_name) | ||
user.items.append(item) | ||
for unit in currencies: | ||
for e_num in range(expenses_number): | ||
expense = Expense(item_name=item_name, price=100, unit=unit) | ||
user.expenses.append(expense) | ||
session.add(user) | ||
if commit: | ||
session.commit() | ||
return session | ||
|
||
|
||
def get_random_user(session: orm.Session) -> User: | ||
return session.scalars(sa.select(User)).first() |
Empty file.
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,41 @@ | ||
import pytest | ||
from services import rm_service | ||
|
||
from testing.util import fill_in_db | ||
from testing.util import get_random_user | ||
|
||
|
||
def test_raise_wrong_category(db): | ||
fill_in_db(session=db, users_number=2, items_number=0) | ||
user = get_random_user(db) | ||
|
||
with pytest.raises(rm_service.WrongCategoryError): | ||
rm_service.rm_empty_category( | ||
user_id=user.id, | ||
item_name='no such category', | ||
session=db, | ||
) | ||
|
||
|
||
def test_raise_not_empty_category(db): | ||
fill_in_db(session=db) | ||
user = get_random_user(db) | ||
|
||
with pytest.raises(rm_service.NotEmptyCategoryError): | ||
rm_service.rm_empty_category( | ||
user_id=user.id, | ||
item_name=user.items[0].name, | ||
session=db, | ||
) | ||
|
||
|
||
def test_success_delete_empty_cat(db): | ||
fill_in_db(session=db, users_number=10, items_number=1, expenses_number=0) | ||
user = get_random_user(db) | ||
|
||
retval = rm_service.rm_empty_category( | ||
user_id=user.id, | ||
item_name=user.items[0].name, | ||
session=db, | ||
) | ||
assert retval == 0 |
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