From 22c83defab4516beb64f04981293765a3726feba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=BC=D0=B8=D1=82=D1=80=D0=BE=20=D0=A0=D1=94=D0=B7?= =?UTF-8?q?=D1=94=D0=BD=D0=BA=D0=BE=D0=B2?= <108422398+RezenkovD@users.noreply.github.com> Date: Wed, 6 Sep 2023 16:52:52 +0300 Subject: [PATCH] refactor: improve code comprehension * flake8: f-string is missing placeholders * rename: services and routers * ref: improve expenses and replenishments fucn * fix: eternal pending in invitations * ref: improve queries to db * ref: update queries and validate input --- src/routers/group.py | 56 +- src/routers/registration.py | 2 +- src/routers/user.py | 16 +- src/services/__init__.py | 32 +- src/services/category.py | 4 +- src/services/expense.py | 107 +--- src/services/group.py | 591 +++++-------------- src/services/invitation.py | 21 +- src/services/replenishment.py | 59 +- src/services/user.py | 124 +--- tests/test_endpoints/test_category_e.py | 2 +- tests/test_endpoints/test_expense_e.py | 16 +- tests/test_endpoints/test_group_e.py | 4 +- tests/test_endpoints/test_invitation_e.py | 2 +- tests/test_endpoints/test_replenishment_e.py | 12 +- tests/test_services/test_user_s.py | 16 +- 16 files changed, 297 insertions(+), 767 deletions(-) diff --git a/src/routers/group.py b/src/routers/group.py index 8652d8c..2e62a18 100644 --- a/src/routers/group.py +++ b/src/routers/group.py @@ -106,7 +106,7 @@ def read_group_history( current_user: User = Depends(get_current_user), group_id: int, ) -> Page[GroupHistory]: - return paginate(db, services.group_history(db, current_user.id, group_id)) + return paginate(db, services.read_group_history(db, current_user.id, group_id)) @router.get("/{group_id}/info/", response_model=GroupInfo) @@ -141,17 +141,17 @@ def read_group_total_expenses( ) elif year_month: filter_date = transform_date_or_422(year_month) - return services.group_total_expenses( + return services.read_group_total_expenses( db, current_user.id, group_id, filter_date=filter_date ) elif start_date and end_date: start_date = transform_exact_date_or_422(start_date) end_date = transform_exact_date_or_422(end_date) - return services.group_total_expenses( + return services.read_group_total_expenses( db, current_user.id, group_id, start_date=start_date, end_date=end_date ) else: - return services.group_total_expenses(db, current_user.id, group_id) + return services.read_group_total_expenses(db, current_user.id, group_id) @router.get("/{group_id}/my-total-expenses/", response_model=GroupUserTotalExpenses) @@ -176,17 +176,17 @@ def read_group_user_total_expenses( ) elif year_month: filter_date = transform_date_or_422(year_month) - return services.group_user_total_expenses( + return services.read_group_user_total_expenses( db, current_user.id, group_id, filter_date=filter_date ) elif start_date and end_date: start_date = transform_exact_date_or_422(start_date) end_date = transform_exact_date_or_422(end_date) - return services.group_user_total_expenses( + return services.read_group_user_total_expenses( db, current_user.id, group_id, start_date=start_date, end_date=end_date ) else: - return services.group_user_total_expenses(db, current_user.id, group_id) + return services.read_group_user_total_expenses(db, current_user.id, group_id) @router.get("/{group_id}/users-spenders/", response_model=List[UserSpender]) @@ -211,17 +211,17 @@ def read_group_users_spenders( ) elif year_month: filter_date = transform_date_or_422(year_month) - return services.group_users_spenders( + return services.read_group_users_spenders( db, current_user.id, group_id, filter_date=filter_date ) elif start_date and end_date: start_date = transform_exact_date_or_422(start_date) end_date = transform_exact_date_or_422(end_date) - return services.group_users_spenders( + return services.read_group_users_spenders( db, current_user.id, group_id, start_date=start_date, end_date=end_date ) else: - return services.group_users_spenders(db, current_user.id, group_id) + return services.read_group_users_spenders(db, current_user.id, group_id) @router.get("/{group_id}/category-expenses/", response_model=List[CategoryExpenses]) @@ -246,17 +246,17 @@ def read_group_category_expenses( ) elif year_month: filter_date = transform_date_or_422(year_month) - return services.group_category_expenses( + return services.read_group_category_expenses( db, current_user.id, group_id, filter_date=filter_date ) elif start_date and end_date: start_date = transform_exact_date_or_422(start_date) end_date = transform_exact_date_or_422(end_date) - return services.group_category_expenses( + return services.read_group_category_expenses( db, current_user.id, group_id, start_date=start_date, end_date=end_date ) else: - return services.group_category_expenses(db, current_user.id, group_id) + return services.read_group_category_expenses(db, current_user.id, group_id) @router.get( @@ -368,13 +368,13 @@ def read_group_member_info( ) elif year_month: filter_date = transform_date_or_422(year_month) - return services.group_member_info( + return services.read_group_member_info( db, current_user.id, group_id, member_id, filter_date=filter_date ) elif start_date and end_date: start_date = transform_exact_date_or_422(start_date) end_date = transform_exact_date_or_422(end_date) - return services.group_member_info( + return services.read_group_member_info( db, current_user.id, group_id, @@ -383,14 +383,14 @@ def read_group_member_info( end_date=end_date, ) else: - return services.group_member_info(db, current_user.id, group_id, member_id) + return services.read_group_member_info(db, current_user.id, group_id, member_id) @router.get( "/{group_id}/member/{member_id}/category-expenses/", response_model=List[CategoryExpenses], ) -def read_group_category_expenses( +def read_group_member_category_expenses( *, db: Session = Depends(get_db), current_user: User = Depends(get_current_user), @@ -412,13 +412,13 @@ def read_group_category_expenses( ) elif year_month: filter_date = transform_date_or_422(year_month) - return services.group_member_category_expenses( + return services.read_group_member_category_expenses( db, current_user.id, group_id, member_id, filter_date=filter_date ) elif start_date and end_date: start_date = transform_exact_date_or_422(start_date) end_date = transform_exact_date_or_422(end_date) - return services.group_member_category_expenses( + return services.read_group_member_category_expenses( db, current_user.id, group_id, @@ -427,7 +427,7 @@ def read_group_category_expenses( end_date=end_date, ) else: - return services.group_member_category_expenses( + return services.read_group_member_category_expenses( db, current_user.id, group_id, member_id ) @@ -458,13 +458,13 @@ def read_group_member_daily_expenses( ) elif year_month: filter_date = transform_date_or_422(year_month) - return services.group_member_daily_expenses( + return services.read_group_member_daily_expenses( db, current_user.id, group_id, member_id, filter_date=filter_date ) elif start_date and end_date: start_date = transform_exact_date_or_422(start_date) end_date = transform_exact_date_or_422(end_date) - return services.group_member_daily_expenses( + return services.read_group_member_daily_expenses( db, current_user.id, group_id, @@ -473,7 +473,7 @@ def read_group_member_daily_expenses( end_date=end_date, ) else: - return services.group_member_daily_expenses( + return services.read_group_member_daily_expenses( db, current_user.id, group_id, member_id ) @@ -504,13 +504,13 @@ def read_group_member_daily_expenses_detail( ) elif year_month: filter_date = transform_date_or_422(year_month) - return services.group_member_daily_expenses_detail( + return services.read_group_member_daily_expenses_detail( db, current_user.id, group_id, member_id, filter_date=filter_date ) elif start_date and end_date: start_date = transform_exact_date_or_422(start_date) end_date = transform_exact_date_or_422(end_date) - return services.group_member_daily_expenses_detail( + return services.read_group_member_daily_expenses_detail( db, current_user.id, group_id, @@ -519,7 +519,7 @@ def read_group_member_daily_expenses_detail( end_date=end_date, ) else: - return services.group_member_daily_expenses_detail( + return services.read_group_member_daily_expenses_detail( db, current_user.id, group_id, member_id ) @@ -527,7 +527,7 @@ def read_group_member_daily_expenses_detail( @router.get( "/{group_id}/member/{member_id}/history/", response_model=Page[GroupHistory] ) -def read_group_history( +def read_group_member_history( *, db: Session = Depends(get_db), current_user: User = Depends(get_current_user), @@ -535,5 +535,5 @@ def read_group_history( member_id: int, ) -> Page[GroupHistory]: return paginate( - db, services.group_member_history(db, current_user.id, group_id, member_id) + db, services.read_group_member_history(db, current_user.id, group_id, member_id) ) diff --git a/src/routers/registration.py b/src/routers/registration.py index a2e0fcf..8eb8b8c 100644 --- a/src/routers/registration.py +++ b/src/routers/registration.py @@ -54,7 +54,7 @@ async def auth(*, db: Session = Depends(get_db), request: Request): except: raise HTTPException( status_code=status.HTTP_503_SERVICE_UNAVAILABLE, - detail=f"An error occurred while create category", + detail="An error occurred while create category", ) return RedirectResponse(url="https://" + settings.DOMAIN_NAME) diff --git a/src/routers/user.py b/src/routers/user.py index caf4d7d..699e24c 100644 --- a/src/routers/user.py +++ b/src/routers/user.py @@ -49,7 +49,7 @@ def read_user_balance( db: Session = Depends(get_db), current_user: User = Depends(get_current_user), ) -> UserBalance: - return services.calculate_user_balance(db, current_user.id) + return services.read_user_balance(db, current_user.id) @router.get("/info/", response_model=UserModel) @@ -65,7 +65,7 @@ def read_user_history( db: Session = Depends(get_db), current_user: User = Depends(get_current_user), ) -> Page[UserHistory]: - return paginate(db, services.user_history(current_user.id)) + return paginate(db, services.read_user_history(current_user.id)) @router.get("/{group_id}/expenses/", response_model=UserGroupExpenses) @@ -199,20 +199,20 @@ def read_user_total_expenses( ) elif year_month: filter_date = transform_date_or_422(year_month) - return services.user_total_expenses( + return services.read_user_total_expenses( db, current_user.id, filter_date=filter_date ) elif start_date and end_date: start_date = transform_exact_date_or_422(start_date) end_date = transform_exact_date_or_422(end_date) - return services.user_total_expenses( + return services.read_user_total_expenses( db, current_user.id, start_date=start_date, end_date=end_date, ) else: - return services.user_total_expenses(db, current_user.id) + return services.read_user_total_expenses(db, current_user.id) @router.get("/total-replenishments/", response_model=UserTotalReplenishments) @@ -235,17 +235,17 @@ def read_user_total_replenishments( ) elif year_month: filter_date = transform_date_or_422(year_month) - return services.user_total_replenishments( + return services.read_user_total_replenishments( db, current_user.id, filter_date=filter_date ) elif start_date and end_date: start_date = transform_exact_date_or_422(start_date) end_date = transform_exact_date_or_422(end_date) - return services.user_total_replenishments( + return services.read_user_total_replenishments( db, current_user.id, start_date=start_date, end_date=end_date, ) else: - return services.user_total_replenishments(db, current_user.id) + return services.read_user_total_replenishments(db, current_user.id) diff --git a/src/services/__init__.py b/src/services/__init__.py index 646ae6d..0bd39d3 100644 --- a/src/services/__init__.py +++ b/src/services/__init__.py @@ -4,15 +4,13 @@ update_expense, delete_expense, read_expenses, - read_expenses_by_group_month, - read_expenses_by_group_time_range, ) from .user import ( get_user, - calculate_user_balance, - user_total_expenses, - user_total_replenishments, - user_history, + read_user_balance, + read_user_total_expenses, + read_user_total_replenishments, + read_user_history, read_user_daily_expenses, read_category_expenses, read_group_expenses, @@ -23,23 +21,23 @@ update_group, disband_group, leave_group, + remove_user, read_categories_group, read_user_groups, read_users_group, - remove_user, read_group_info, - group_history, - group_total_expenses, - group_user_total_expenses, - group_users_spenders, - group_category_expenses, + read_group_history, + read_group_total_expenses, + read_group_user_total_expenses, + read_group_users_spenders, + read_group_category_expenses, read_group_daily_expenses, read_group_daily_expenses_detail, - group_member_info, - group_member_category_expenses, - group_member_daily_expenses, - group_member_daily_expenses_detail, - group_member_history, + read_group_member_info, + read_group_member_category_expenses, + read_group_member_daily_expenses, + read_group_member_daily_expenses_detail, + read_group_member_history, ) from .invitation import create_invitation, read_invitations, response_invitation from .replenishment import ( diff --git a/src/services/category.py b/src/services/category.py index 688c300..c22acd5 100644 --- a/src/services/category.py +++ b/src/services/category.py @@ -64,7 +64,7 @@ def create_category( except: raise HTTPException( status_code=status.HTTP_503_SERVICE_UNAVAILABLE, - detail=f"An error occurred while create category", + detail="An error occurred while create category", ) else: return db_category @@ -104,7 +104,7 @@ def update_category( except: raise HTTPException( status_code=status.HTTP_503_SERVICE_UNAVAILABLE, - detail=f"An error occurred while update category", + detail="An error occurred while update category", ) else: return db_category_group diff --git a/src/services/expense.py b/src/services/expense.py index cb30d74..3a63d03 100644 --- a/src/services/expense.py +++ b/src/services/expense.py @@ -69,7 +69,7 @@ def create_expense( except: raise HTTPException( status_code=status.HTTP_503_SERVICE_UNAVAILABLE, - detail=f"An error occurred while create expense", + detail="An error occurred while create expense", ) else: return db_expense @@ -92,7 +92,7 @@ def update_expense( except: raise HTTPException( status_code=status.HTTP_503_SERVICE_UNAVAILABLE, - detail=f"An error occurred while update expense", + detail="An error occurred while update expense", ) else: return db_expense @@ -108,7 +108,7 @@ def delete_expense(db: Session, user_id: int, group_id: int, expense_id: int) -> except: raise HTTPException( status_code=status.HTTP_503_SERVICE_UNAVAILABLE, - detail=f"An error occurred while delete expense", + detail="An error occurred while delete expense", ) @@ -125,7 +125,7 @@ def read_expenses( status_code=status.HTTP_404_NOT_FOUND, detail="Too many arguments! It is necessary to select either a month or a start date and an end date!", ) - if group_id is not None: + if group_id: try: db.query(UserGroup).filter_by( group_id=group_id, @@ -136,87 +136,28 @@ def read_expenses( status_code=status.HTTP_404_NOT_FOUND, detail="You are not a user of this group!", ) - expenses = read_expenses_by_group_all_time(group_id, user_id) - if filter_date: - expenses = read_expenses_by_group_month(group_id, user_id, filter_date) - elif start_date and end_date: - expenses = read_expenses_by_group_time_range( - group_id, user_id, start_date, end_date - ) - return expenses - expenses = read_expenses_all_time(user_id) - if filter_date: - expenses = read_expenses_month(user_id, filter_date) - elif start_date and end_date: - expenses = read_expenses_time_range(user_id, start_date, end_date) - return expenses - - -def read_expenses_by_group_all_time(group_id: int, user_id: int) -> List[UserExpense]: - expenses = select(Expense).filter_by(user_id=user_id, group_id=group_id) - return expenses - - -def read_expenses_by_group_month( - group_id: int, user_id: int, filter_date: date -) -> List[UserExpense]: - expenses = select(Expense).filter( - and_( - Expense.user_id == user_id, - Expense.group_id == group_id, - extract("year", Expense.time) == filter_date.year, - extract("month", Expense.time) == filter_date.month, + expenses = select(Expense).filter_by(user_id=user_id, group_id=group_id) + else: + expenses = select(Expense).filter_by( + user_id=user_id, ) - ) - return expenses - - -def read_expenses_by_group_time_range( - group_id: int, user_id: int, start_date: date, end_date: date -) -> List[UserExpense]: - if start_date > end_date: - raise HTTPException( - status_code=status.HTTP_404_NOT_FOUND, - detail="The start date cannot be older than the end date!", + if filter_date: + expenses = expenses.filter( + and_( + Expense.user_id == user_id, + extract("year", Expense.time) == filter_date.year, + extract("month", Expense.time) == filter_date.month, + ) ) - expenses = select(Expense).filter( - Expense.user_id == user_id, - Expense.group_id == group_id, - Expense.time >= start_date, - Expense.time <= end_date, - ) - return expenses - - -def read_expenses_all_time(user_id: int) -> List[UserExpense]: - expenses = select(Expense).filter_by( - user_id=user_id, - ) - return expenses - - -def read_expenses_month(user_id: int, filter_date: date) -> List[UserExpense]: - expenses = select(Expense).filter( - and_( + elif start_date and end_date: + if start_date > end_date: + raise HTTPException( + status_code=status.HTTP_404_NOT_FOUND, + detail="The start date cannot be older than the end date!", + ) + expenses = expenses.filter( Expense.user_id == user_id, - extract("year", Expense.time) == filter_date.year, - extract("month", Expense.time) == filter_date.month, - ) - ) - return expenses - - -def read_expenses_time_range( - user_id: int, start_date: date, end_date: date -) -> List[UserExpense]: - if start_date > end_date: - raise HTTPException( - status_code=status.HTTP_404_NOT_FOUND, - detail="The start date cannot be older than the end date!", + Expense.time >= start_date, + Expense.time <= end_date, ) - expenses = select(Expense).filter( - Expense.user_id == user_id, - Expense.time >= start_date, - Expense.time <= end_date, - ) return expenses diff --git a/src/services/group.py b/src/services/group.py index 58a03a4..4b9aaf4 100644 --- a/src/services/group.py +++ b/src/services/group.py @@ -32,7 +32,11 @@ ) -def group_history(db: Session, user_id: int, group_id: int) -> List[GroupHistory]: +def user_validate_input_date( + db: Session, + user_id: int, + group_id: int, +) -> None: try: ( db.query(UserGroup) @@ -47,6 +51,46 @@ def group_history(db: Session, user_id: int, group_id: int) -> List[GroupHistory status_code=status.HTTP_404_NOT_FOUND, detail="You are not in this group!", ) + + +def group_member_validate_input_data( + db: Session, + current_user: int, + member_id: int, + group_id: int, +) -> None: + try: + ( + db.query(UserGroup) + .filter_by( + user_id=current_user, + group_id=group_id, + ) + .one() + ) + except: + raise HTTPException( + status_code=status.HTTP_404_NOT_FOUND, + detail="You are not in this group!", + ) + try: + ( + db.query(UserGroup) + .filter_by( + user_id=member_id, + group_id=group_id, + ) + .one() + ) + except: + raise HTTPException( + status_code=status.HTTP_404_NOT_FOUND, + detail="This user is not in this group!", + ) + + +def read_group_history(db: Session, user_id: int, group_id: int) -> List[GroupHistory]: + user_validate_input_date(db, user_id, group_id) history = ( select( Expense.id, @@ -78,20 +122,7 @@ def group_history(db: Session, user_id: int, group_id: int) -> List[GroupHistory def read_group_info(db: Session, user_id: int, group_id: int) -> GroupInfo: - try: - ( - db.query(UserGroup) - .filter_by( - user_id=user_id, - group_id=group_id, - ) - .one() - ) - except: - raise HTTPException( - status_code=status.HTTP_404_NOT_FOUND, - detail="You are not in this group!", - ) + user_validate_input_date(db, user_id, group_id) group = ( db.query(Group).options(joinedload(Group.admin)).filter_by(id=group_id).one() ) @@ -147,7 +178,7 @@ def remove_user( except: raise HTTPException( status_code=status.HTTP_503_SERVICE_UNAVAILABLE, - detail=f"An error occurred while disband group", + detail="An error occurred while disband group", ) else: return db_users_group @@ -158,7 +189,7 @@ def remove_user( except: raise HTTPException( status_code=status.HTTP_503_SERVICE_UNAVAILABLE, - detail=f"An error occurred while remove user", + detail="An error occurred while remove user", ) else: return db_user_group @@ -192,7 +223,7 @@ def leave_group( except: raise HTTPException( status_code=status.HTTP_503_SERVICE_UNAVAILABLE, - detail=f"An error occurred while leave group", + detail="An error occurred while leave group", ) else: return db_users_group @@ -216,7 +247,7 @@ def leave_group( except: raise HTTPException( status_code=status.HTTP_503_SERVICE_UNAVAILABLE, - detail=f"An error occurred while leave group", + detail="An error occurred while leave group", ) else: return db_user_group @@ -236,7 +267,7 @@ def create_group(db: Session, user_id: int, group: GroupCreate) -> GroupModel: except: raise HTTPException( status_code=status.HTTP_503_SERVICE_UNAVAILABLE, - detail=f"An error occurred while create group", + detail="An error occurred while create group", ) else: return db_group @@ -259,7 +290,7 @@ def update_group( except: raise HTTPException( status_code=status.HTTP_503_SERVICE_UNAVAILABLE, - detail=f"An error occurred while update group", + detail="An error occurred while update group", ) else: return db_group @@ -288,20 +319,7 @@ def add_user_in_group(db: Session, user_id: int, group_id: int) -> None: def read_users_group(db: Session, user_id: int, group_id: int) -> UsersGroup: - try: - ( - db.query(UserGroup) - .filter_by( - user_id=user_id, - group_id=group_id, - ) - .one() - ) - except: - raise HTTPException( - status_code=status.HTTP_404_NOT_FOUND, - detail="You are not in this group!", - ) + user_validate_input_date(db, user_id, group_id) db_query = ( select(Group).options(joinedload(Group.users_group)).filter_by(id=group_id) ) @@ -375,7 +393,7 @@ def get_group_expenses_for_time_range( ).one()[0] -def group_total_expenses( +def read_group_total_expenses( db: Session, user_id: int, group_id: int, @@ -383,20 +401,7 @@ def group_total_expenses( start_date: Optional[date] = None, end_date: Optional[date] = None, ) -> GroupTotalExpenses: - try: - ( - db.query(UserGroup) - .filter_by( - user_id=user_id, - group_id=group_id, - ) - .one() - ) - except: - raise HTTPException( - status_code=status.HTTP_404_NOT_FOUND, - detail="You are not in this group!", - ) + user_validate_input_date(db, user_id, group_id) if filter_date and start_date or filter_date and end_date: raise HTTPException( status_code=status.HTTP_404_NOT_FOUND, @@ -484,7 +489,7 @@ def get_group_user_expenses_for_time_range( ).one()[0] -def group_user_total_expenses( +def read_group_user_total_expenses( db: Session, user_id: int, group_id: int, @@ -492,20 +497,7 @@ def group_user_total_expenses( start_date: Optional[date] = None, end_date: Optional[date] = None, ) -> GroupTotalExpenses: - try: - ( - db.query(UserGroup) - .filter_by( - user_id=user_id, - group_id=group_id, - ) - .one() - ) - except: - raise HTTPException( - status_code=status.HTTP_404_NOT_FOUND, - detail="You are not in this group!", - ) + user_validate_input_date(db, user_id, group_id) if filter_date and start_date or filter_date and end_date: raise HTTPException( status_code=status.HTTP_404_NOT_FOUND, @@ -556,7 +548,7 @@ def group_user_total_expenses( return total_expenses -def group_users_spenders( +def read_group_users_spenders( db: Session, user_id: int, group_id: int, @@ -564,20 +556,7 @@ def group_users_spenders( start_date: Optional[date] = None, end_date: Optional[date] = None, ) -> List[GroupTotalExpenses]: - try: - ( - db.query(UserGroup) - .filter_by( - user_id=user_id, - group_id=group_id, - ) - .one() - ) - except: - raise HTTPException( - status_code=status.HTTP_404_NOT_FOUND, - detail="You are not in this group!", - ) + user_validate_input_date(db, user_id, group_id) if filter_date and start_date or filter_date and end_date: raise HTTPException( status_code=status.HTTP_404_NOT_FOUND, @@ -602,60 +581,26 @@ def group_users_spenders( .filter(UserGroup.group_id == group_id) .group_by(User.id) .order_by(func.coalesce(func.sum(Expense.amount), 0).desc()) - .all() ) if filter_date: - users_spenders = ( - db.query( - User.id.label("id"), - User.first_name.label("first_name"), - User.last_name.label("last_name"), - User.picture.label("picture"), - func.coalesce(func.sum(Expense.amount), 0).label("amount"), - ) - .join(UserGroup, User.id == UserGroup.user_id) - .outerjoin( - Expense, - and_( - Expense.user_id == User.id, - Expense.group_id == group_id, - extract("year", Expense.time) == filter_date.year, - extract("month", Expense.time) == filter_date.month, - ), + users_spenders = users_spenders.filter( + and_( + extract("year", Expense.time) == filter_date.year, + extract("month", Expense.time) == filter_date.month, ) - .filter(UserGroup.group_id == group_id) - .group_by(User.id) - .order_by(func.coalesce(func.sum(Expense.amount), 0).desc()) - .all() ) elif start_date and end_date: - users_spenders = ( - db.query( - User.id.label("id"), - User.first_name.label("first_name"), - User.last_name.label("last_name"), - User.picture.label("picture"), - func.coalesce(func.sum(Expense.amount), 0).label("amount"), - ) - .join(UserGroup, User.id == UserGroup.user_id) - .outerjoin( - Expense, - and_( - Expense.user_id == User.id, - Expense.group_id == group_id, - Expense.time >= start_date, - Expense.time <= end_date, - ), + users_spenders = users_spenders.filter( + and_( + Expense.time >= start_date, + Expense.time <= end_date, ) - .filter(UserGroup.group_id == group_id) - .group_by(User.id) - .order_by(func.coalesce(func.sum(Expense.amount), 0).desc()) - .all() ) + users_spenders = users_spenders.all() return users_spenders -def group_category_expenses( +def read_group_category_expenses( db: Session, user_id: int, group_id: int, @@ -668,20 +613,7 @@ def group_category_expenses( status_code=status.HTTP_404_NOT_FOUND, detail="Too many arguments! It is necessary to select either a month or a start date and an end date!", ) - try: - ( - db.query(UserGroup) - .filter_by( - user_id=user_id, - group_id=group_id, - ) - .one() - ) - except: - raise HTTPException( - status_code=status.HTTP_404_NOT_FOUND, - detail="You are not in this group!", - ) + user_validate_input_date(db, user_id, group_id) categories_expenses_subquery = ( db.query( Expense.category_id.label("id"), @@ -691,40 +623,22 @@ def group_category_expenses( Expense.group_id == group_id, ) .group_by(Expense.category_id) - .subquery() ) if filter_date: - categories_expenses_subquery = ( - db.query( - Expense.category_id.label("id"), - func.coalesce(func.sum(Expense.amount), 0).label("amount"), - ) - .filter( - and_( - Expense.group_id == group_id, - extract("year", Expense.time) == filter_date.year, - extract("month", Expense.time) == filter_date.month, - ) + categories_expenses_subquery = categories_expenses_subquery.filter( + and_( + extract("year", Expense.time) == filter_date.year, + extract("month", Expense.time) == filter_date.month, ) - .group_by(Expense.category_id) - .subquery() ) elif start_date and end_date: - categories_expenses_subquery = ( - db.query( - Expense.category_id.label("id"), - func.coalesce(func.sum(Expense.amount), 0).label("amount"), - ) - .filter( - and_( - Expense.group_id == group_id, - Expense.time >= start_date, - Expense.time <= end_date, - ), - ) - .group_by(Expense.category_id) - .subquery() + categories_expenses_subquery = categories_expenses_subquery.filter( + and_( + Expense.time >= start_date, + Expense.time <= end_date, + ), ) + categories_expenses_subquery = categories_expenses_subquery.subquery() categories_expenses = ( db.query( Category.id.label("id"), @@ -763,20 +677,7 @@ def read_group_daily_expenses( status_code=status.HTTP_404_NOT_FOUND, detail="Too many arguments! It is necessary to select either a month or a start date and an end date!", ) - try: - ( - db.query(UserGroup) - .filter_by( - user_id=user_id, - group_id=group_id, - ) - .one() - ) - except: - raise HTTPException( - status_code=status.HTTP_404_NOT_FOUND, - detail="You are not in this group!", - ) + user_validate_input_date(db, user_id, group_id) daily_expenses = ( db.query( func.date(Expense.time).label("date"), @@ -784,40 +685,22 @@ def read_group_daily_expenses( ) .filter_by(group_id=group_id) .group_by(func.date(Expense.time)) - .all() ) if filter_date: - daily_expenses = ( - db.query( - func.date(Expense.time).label("date"), - func.sum(Expense.amount).label("amount"), - ) - .filter( - and_( - Expense.group_id == group_id, - extract("year", Expense.time) == filter_date.year, - extract("month", Expense.time) == filter_date.month, - ) + daily_expenses = daily_expenses.filter( + and_( + extract("year", Expense.time) == filter_date.year, + extract("month", Expense.time) == filter_date.month, ) - .group_by(func.date(Expense.time)) - .all() ) elif start_date and end_date: - daily_expenses = ( - db.query( - func.date(Expense.time).label("date"), - func.sum(Expense.amount).label("amount"), - ) - .filter( - and_( - Expense.group_id == group_id, - Expense.time >= start_date, - Expense.time <= end_date, - ) + daily_expenses = daily_expenses.filter( + and_( + Expense.time >= start_date, + Expense.time <= end_date, ) - .group_by(func.date(Expense.time)) - .all() ) + daily_expenses = daily_expenses.all() return daily_expenses @@ -834,20 +717,7 @@ def read_group_daily_expenses_detail( status_code=status.HTTP_404_NOT_FOUND, detail="Too many arguments! It is necessary to select either a month or a start date and an end date!", ) - try: - ( - db.query(UserGroup) - .filter_by( - user_id=user_id, - group_id=group_id, - ) - .one() - ) - except: - raise HTTPException( - status_code=status.HTTP_404_NOT_FOUND, - detail="You are not in this group!", - ) + user_validate_input_date(db, user_id, group_id) group_users = ( db.query(User).join(UserGroup).filter(UserGroup.group_id == group_id).all() ) @@ -859,42 +729,22 @@ def read_group_daily_expenses_detail( .filter(Expense.group_id == group_id) .group_by(func.date(Expense.time)) .distinct() - .all() ) if filter_date: - possible_dates = ( - db.query( - func.date(Expense.time), - func.sum(Expense.amount).label("amount"), - ) - .filter( - and_( - Expense.group_id == group_id, - extract("year", Expense.time) == filter_date.year, - extract("month", Expense.time) == filter_date.month, - ) + possible_dates = possible_dates.filter( + and_( + extract("year", Expense.time) == filter_date.year, + extract("month", Expense.time) == filter_date.month, ) - .group_by(func.date(Expense.time)) - .distinct() - .all() ) elif start_date and end_date: - possible_dates = ( - db.query( - func.date(Expense.time), - func.sum(Expense.amount).label("amount"), - ) - .filter( - and_( - Expense.group_id == group_id, - Expense.time >= start_date, - Expense.time <= end_date, - ) + possible_dates = possible_dates.filter( + and_( + Expense.time >= start_date, + Expense.time <= end_date, ) - .group_by(func.date(Expense.time)) - .distinct() - .all() ) + possible_dates = possible_dates.all() result_structure = [] for date_row in possible_dates: date = date_row[0] @@ -923,7 +773,7 @@ def read_group_daily_expenses_detail( return result_structure -def group_member_info( +def read_group_member_info( db: Session, current_user: int, group_id: int, @@ -937,37 +787,10 @@ def group_member_info( status_code=status.HTTP_404_NOT_FOUND, detail="Too many arguments! It is necessary to select either a month or a start date and an end date!", ) - try: - ( - db.query(UserGroup) - .filter_by( - user_id=current_user, - group_id=group_id, - ) - .one() - ) - except: - raise HTTPException( - status_code=status.HTTP_404_NOT_FOUND, - detail="You are not in this group!", - ) - try: - ( - db.query(UserGroup) - .filter_by( - user_id=member_id, - group_id=group_id, - ) - .one() - ) - except: - raise HTTPException( - status_code=status.HTTP_404_NOT_FOUND, - detail="This user is not in this group!", - ) + group_member_validate_input_data(db, current_user, member_id, group_id) user_info = db.query(User).filter_by(id=member_id).one() if filter_date: - total_expenses = group_user_total_expenses( + total_expenses = read_group_user_total_expenses( db, member_id, group_id, filter_date=filter_date ) try: @@ -1024,7 +847,7 @@ def group_member_info( except: best_category = None elif start_date and end_date: - total_expenses = group_user_total_expenses( + total_expenses = read_group_user_total_expenses( db, member_id, group_id, @@ -1085,7 +908,7 @@ def group_member_info( except: best_category = None else: - total_expenses = group_user_total_expenses(db, member_id, group_id) + total_expenses = read_group_user_total_expenses(db, member_id, group_id) try: (count_expenses,) = ( db.query(count(Expense.id)) @@ -1148,7 +971,7 @@ def group_member_info( return group_member -def group_member_category_expenses( +def read_group_member_category_expenses( db: Session, current_user: int, group_id: int, @@ -1162,34 +985,7 @@ def group_member_category_expenses( status_code=status.HTTP_404_NOT_FOUND, detail="Too many arguments! It is necessary to select either a month or a start date and an end date!", ) - try: - ( - db.query(UserGroup) - .filter_by( - user_id=current_user, - group_id=group_id, - ) - .one() - ) - except: - raise HTTPException( - status_code=status.HTTP_404_NOT_FOUND, - detail="You are not in this group!", - ) - try: - ( - db.query(UserGroup) - .filter_by( - user_id=member_id, - group_id=group_id, - ) - .one() - ) - except: - raise HTTPException( - status_code=status.HTTP_404_NOT_FOUND, - detail="This user is not in this group!", - ) + group_member_validate_input_data(db, current_user, member_id, group_id) categories_expenses_subquery = ( db.query( Expense.category_id.label("id"), @@ -1202,42 +998,22 @@ def group_member_category_expenses( ) ) .group_by(Expense.category_id) - .subquery() ) if filter_date: - categories_expenses_subquery = ( - db.query( - Expense.category_id.label("id"), - func.coalesce(func.sum(Expense.amount), 0).label("amount"), - ) - .filter( - and_( - Expense.group_id == group_id, - Expense.user_id == member_id, - extract("year", Expense.time) == filter_date.year, - extract("month", Expense.time) == filter_date.month, - ) + categories_expenses_subquery = categories_expenses_subquery.filter( + and_( + extract("year", Expense.time) == filter_date.year, + extract("month", Expense.time) == filter_date.month, ) - .group_by(Expense.category_id) - .subquery() ) elif start_date and end_date: - categories_expenses_subquery = ( - db.query( - Expense.category_id.label("id"), - func.coalesce(func.sum(Expense.amount), 0).label("amount"), - ) - .filter( - and_( - Expense.group_id == group_id, - Expense.user_id == member_id, - Expense.time >= start_date, - Expense.time <= end_date, - ), - ) - .group_by(Expense.category_id) - .subquery() + categories_expenses_subquery = categories_expenses_subquery.filter( + and_( + Expense.time >= start_date, + Expense.time <= end_date, + ), ) + categories_expenses_subquery = categories_expenses_subquery.subquery() categories_expenses = ( db.query( Category.id.label("id"), @@ -1263,7 +1039,7 @@ def group_member_category_expenses( return categories_expenses -def group_member_daily_expenses( +def read_group_member_daily_expenses( db: Session, current_user: int, group_id: int, @@ -1277,34 +1053,7 @@ def group_member_daily_expenses( status_code=status.HTTP_404_NOT_FOUND, detail="Too many arguments! It is necessary to select either a month or a start date and an end date!", ) - try: - ( - db.query(UserGroup) - .filter_by( - user_id=current_user, - group_id=group_id, - ) - .one() - ) - except: - raise HTTPException( - status_code=status.HTTP_404_NOT_FOUND, - detail="You are not in this group!", - ) - try: - ( - db.query(UserGroup) - .filter_by( - user_id=member_id, - group_id=group_id, - ) - .one() - ) - except: - raise HTTPException( - status_code=status.HTTP_404_NOT_FOUND, - detail="This user is not in this group!", - ) + group_member_validate_input_data(db, current_user, member_id, group_id) member_daily_expenses = ( db.query( func.date(Expense.time).label("date"), @@ -1317,46 +1066,28 @@ def group_member_daily_expenses( ) ) .group_by(func.date(Expense.time)) - .all() ) if filter_date: - member_daily_expenses = ( - db.query( - func.date(Expense.time).label("date"), - func.sum(Expense.amount).label("amount"), - ) - .filter( - and_( - Expense.user_id == member_id, - Expense.group_id == group_id, - extract("year", Expense.time) == filter_date.year, - extract("month", Expense.time) == filter_date.month, - ) + member_daily_expenses = member_daily_expenses.filter( + and_( + extract("year", Expense.time) == filter_date.year, + extract("month", Expense.time) == filter_date.month, ) - .group_by(func.date(Expense.time)) - .all() ) elif start_date and end_date: - member_daily_expenses = ( - db.query( - func.date(Expense.time).label("date"), - func.sum(Expense.amount).label("amount"), - ) - .filter( - and_( - Expense.user_id == member_id, - Expense.group_id == group_id, - Expense.time >= start_date, - Expense.time <= end_date, - ) + member_daily_expenses = member_daily_expenses.filter( + and_( + Expense.user_id == member_id, + Expense.group_id == group_id, + Expense.time >= start_date, + Expense.time <= end_date, ) - .group_by(func.date(Expense.time)) - .all() ) + member_daily_expenses = member_daily_expenses.all() return member_daily_expenses -def group_member_daily_expenses_detail( +def read_group_member_daily_expenses_detail( db: Session, current_user: int, group_id: int, @@ -1370,34 +1101,7 @@ def group_member_daily_expenses_detail( status_code=status.HTTP_404_NOT_FOUND, detail="Too many arguments! It is necessary to select either a month or a start date and an end date!", ) - try: - ( - db.query(UserGroup) - .filter_by( - user_id=current_user, - group_id=group_id, - ) - .one() - ) - except: - raise HTTPException( - status_code=status.HTTP_404_NOT_FOUND, - detail="You are not in this group!", - ) - try: - ( - db.query(UserGroup) - .filter_by( - user_id=member_id, - group_id=group_id, - ) - .one() - ) - except: - raise HTTPException( - status_code=status.HTTP_404_NOT_FOUND, - detail="This user is not in this group!", - ) + group_member_validate_input_data(db, current_user, member_id, group_id) result_structure = ( db.query( func.date(Expense.time).label("date"), @@ -1474,40 +1178,13 @@ def group_member_daily_expenses_detail( return result_list -def group_member_history( +def read_group_member_history( db: Session, current_user: int, group_id: int, member_id: int, ) -> List[GroupHistory]: - try: - ( - db.query(UserGroup) - .filter_by( - user_id=current_user, - group_id=group_id, - ) - .one() - ) - except: - raise HTTPException( - status_code=status.HTTP_404_NOT_FOUND, - detail="You are not in this group!", - ) - try: - ( - db.query(UserGroup) - .filter_by( - user_id=member_id, - group_id=group_id, - ) - .one() - ) - except: - raise HTTPException( - status_code=status.HTTP_404_NOT_FOUND, - detail="This user is not in this group!", - ) + group_member_validate_input_data(db, current_user, member_id, group_id) member_history = ( select( Expense.id, diff --git a/src/services/invitation.py b/src/services/invitation.py index 71da6cb..3092838 100644 --- a/src/services/invitation.py +++ b/src/services/invitation.py @@ -62,14 +62,14 @@ def response_invitation( except: raise HTTPException( status_code=status.HTTP_503_SERVICE_UNAVAILABLE, - detail=f"An error occurred while add user in group", + detail="An error occurred while add user in group", ) try: db.commit() except: raise HTTPException( status_code=status.HTTP_503_SERVICE_UNAVAILABLE, - detail=f"An error occurred while response invitation", + detail="An error occurred while response invitation", ) else: return db_invitation @@ -134,10 +134,17 @@ def create_invitation( .one_or_none() ) if db_invitation: - raise HTTPException( - status_code=status.HTTP_405_METHOD_NOT_ALLOWED, - detail="The invitation has already been sent. Wait for a reply!", - ) + if ( + db_invitation.creation_time + datetime.timedelta(hours=24) + < datetime.datetime.utcnow() + ): + db_invitation.status = ResponseStatusEnum.OVERDUE + db.commit() + else: + raise HTTPException( + status_code=status.HTTP_405_METHOD_NOT_ALLOWED, + detail="The invitation has already been sent. Wait for a reply!", + ) db_invitation = Invitation( status=ResponseStatusEnum.PENDING, sender_id=user_id, @@ -151,7 +158,7 @@ def create_invitation( except: raise HTTPException( status_code=status.HTTP_503_SERVICE_UNAVAILABLE, - detail=f"An error occurred while create invitation", + detail="An error occurred while create invitation", ) else: return db_invitation diff --git a/src/services/replenishment.py b/src/services/replenishment.py index 3b357fd..39344bf 100644 --- a/src/services/replenishment.py +++ b/src/services/replenishment.py @@ -24,7 +24,7 @@ def create_replenishment( except: raise HTTPException( status_code=status.HTTP_503_SERVICE_UNAVAILABLE, - detail=f"An error occurred while create replenishments", + detail="An error occurred while create replenishments", ) else: return db_replenishment @@ -49,7 +49,7 @@ def update_replenishment( except: raise HTTPException( status_code=status.HTTP_503_SERVICE_UNAVAILABLE, - detail=f"An error occurred while update replenishment", + detail="An error occurred while update replenishment", ) else: return db_replenishment @@ -69,7 +69,7 @@ def delete_replenishment(db: Session, user_id: int, replenishment_id: int): except: raise HTTPException( status_code=status.HTTP_503_SERVICE_UNAVAILABLE, - detail=f"An error occurred while delete replenishment", + detail="An error occurred while delete replenishment", ) @@ -79,45 +79,26 @@ def read_replenishments( start_date: Optional[date] = None, end_date: Optional[date] = None, ) -> List[UserReplenishment]: - replenishments = read_replenishments_all_time(user_id) - if filter_date: - replenishments = read_replenishments_month(user_id, filter_date) - elif start_date and end_date: - replenishments = read_replenishments_time_range(user_id, start_date, end_date) - return replenishments - - -def read_replenishments_all_time(user_id: int) -> List[UserReplenishment]: replenishments = select(Replenishment).filter_by( user_id=user_id, ) - return replenishments - - -def read_replenishments_month( - user_id: int, filter_date: date -) -> List[UserReplenishment]: - replenishments = select(Replenishment).filter( - and_( - Replenishment.user_id == user_id, - extract("year", Replenishment.time) == filter_date.year, - extract("month", Replenishment.time) == filter_date.month, + if filter_date: + replenishments = replenishments.filter( + and_( + Replenishment.user_id == user_id, + extract("year", Replenishment.time) == filter_date.year, + extract("month", Replenishment.time) == filter_date.month, + ) ) - ) - return replenishments - - -def read_replenishments_time_range( - user_id: int, start_date: date, end_date: date -) -> List[UserReplenishment]: - if start_date > end_date: - raise HTTPException( - status_code=status.HTTP_404_NOT_FOUND, - detail="The start date cannot be older than the end date!", + elif start_date and end_date: + if start_date > end_date: + raise HTTPException( + status_code=status.HTTP_404_NOT_FOUND, + detail="The start date cannot be older than the end date!", + ) + replenishments = replenishments.filter( + Replenishment.user_id == user_id, + Replenishment.time >= start_date, + Replenishment.time <= end_date, ) - replenishments = select(Replenishment).filter( - Replenishment.user_id == user_id, - Replenishment.time >= start_date, - Replenishment.time <= end_date, - ) return replenishments diff --git a/src/services/user.py b/src/services/user.py index 8a1d249..2873244 100644 --- a/src/services/user.py +++ b/src/services/user.py @@ -33,7 +33,7 @@ def get_user(db: Session, login: str) -> Optional[User]: return db.query(User).filter_by(login=login).one_or_none() -def calculate_user_balance(db: Session, user_id: int) -> UserBalance: +def read_user_balance(db: Session, user_id: int) -> UserBalance: (replenishments,) = db.query( coalesce(sum(Replenishment.amount).filter(Replenishment.user_id == user_id), 0) ).one() @@ -84,42 +84,18 @@ def read_group_expenses( Expense.user_id == user_id, ) .group_by(Expense.category_id) - .subquery() ) if filter_date: - categories_expenses_subquery = ( - db.query( - Expense.category_id.label("id"), - func.coalesce(func.sum(Expense.amount), 0).label("amount"), - ) - .filter( - and_( - Expense.group_id == group_id, - Expense.user_id == user_id, - extract("year", Expense.time) == filter_date.year, - extract("month", Expense.time) == filter_date.month, - ) - ) - .group_by(Expense.category_id) - .subquery() + categories_expenses_subquery = categories_expenses_subquery.filter( + extract("year", Expense.time) == filter_date.year, + extract("month", Expense.time) == filter_date.month, ) elif start_date and end_date: - categories_expenses_subquery = ( - db.query( - Expense.category_id.label("id"), - func.coalesce(func.sum(Expense.amount), 0).label("amount"), - ) - .filter( - and_( - Expense.group_id == group_id, - Expense.user_id == user_id, - Expense.time >= start_date, - Expense.time <= end_date, - ), - ) - .group_by(Expense.category_id) - .subquery() + categories_expenses_subquery = categories_expenses_subquery.filter( + Expense.time >= start_date, + Expense.time <= end_date, ) + categories_expenses_subquery = categories_expenses_subquery.subquery() categories_group = ( db.query( Category.id.label("id"), @@ -171,50 +147,22 @@ def read_category_expenses( .filter(Expense.user_id == user_id) .group_by(Category.id, Category.title) .order_by(func.sum(Expense.amount).desc()) - .all() ) if filter_date: - category_expenses = ( - db.query( - Category.id.label("id"), - Category.title.label("title"), - func.sum(Expense.amount).label("amount"), - ) - .join(Category, Expense.category_id == Category.id) - .filter( - and_( - Expense.user_id == user_id, - extract("year", Expense.time) == filter_date.year, - extract("month", Expense.time) == filter_date.month, - ) - ) - .group_by(Category.id, Category.title) - .order_by(func.sum(Expense.amount).desc()) - .all() + category_expenses = category_expenses.filter( + extract("year", Expense.time) == filter_date.year, + extract("month", Expense.time) == filter_date.month, ) elif start_date and end_date: - category_expenses = ( - db.query( - Category.id.label("id"), - Category.title.label("title"), - func.sum(Expense.amount).label("amount"), - ) - .join(Category, Expense.category_id == Category.id) - .filter( - and_( - Expense.user_id == user_id, - Expense.time >= start_date, - Expense.time <= end_date, - ) - ) - .group_by(Category.id, Category.title) - .order_by(func.sum(Expense.amount).desc()) - .all() + category_expenses = category_expenses.filter( + Expense.time >= start_date, + Expense.time <= end_date, ) + category_expenses = category_expenses.all() return category_expenses -def user_history(user_id: int) -> List[UserHistory]: +def read_user_history(user_id: int) -> List[UserHistory]: history = ( select( Expense.id, @@ -276,40 +224,18 @@ def read_user_daily_expenses( ) .filter_by(user_id=user_id) .group_by(func.date(Expense.time)) - .all() ) if filter_date: - daily_expenses = ( - db.query( - func.date(Expense.time).label("date"), - func.sum(Expense.amount).label("amount"), - ) - .filter( - and_( - Expense.user_id == user_id, - extract("year", Expense.time) == filter_date.year, - extract("month", Expense.time) == filter_date.month, - ) - ) - .group_by(func.date(Expense.time)) - .all() + daily_expenses = daily_expenses.filter( + extract("year", Expense.time) == filter_date.year, + extract("month", Expense.time) == filter_date.month, ) elif start_date and end_date: - daily_expenses = ( - db.query( - func.date(Expense.time).label("date"), - func.sum(Expense.amount).label("amount"), - ) - .filter( - and_( - Expense.user_id == user_id, - Expense.time >= start_date, - Expense.time <= end_date, - ) - ) - .group_by(func.date(Expense.time)) - .all() + daily_expenses = daily_expenses.filter( + Expense.time >= start_date, + Expense.time <= end_date, ) + daily_expenses = daily_expenses.all() return daily_expenses @@ -355,7 +281,7 @@ def get_total_actions_for_time_range( ).one()[0] -def user_total_expenses( +def read_user_total_expenses( db: Session, user_id: int, filter_date: Optional[date] = None, @@ -407,7 +333,7 @@ def user_total_expenses( return total_expenses -def user_total_replenishments( +def read_user_total_replenishments( db: Session, user_id: int, filter_date: Optional[date] = None, diff --git a/tests/test_endpoints/test_category_e.py b/tests/test_endpoints/test_category_e.py index 5ebe3c5..d2471f6 100644 --- a/tests/test_endpoints/test_category_e.py +++ b/tests/test_endpoints/test_category_e.py @@ -62,7 +62,7 @@ def test_read_categories_group(self) -> None: assert data.json() == categories_group_data def test_read_categories_group_as_non_user_group(self) -> None: - data = client.get(f"/groups/9999/categories/") + data = client.get("/groups/9999/categories/") assert data.status_code == 405 def test_create_category(self) -> None: diff --git a/tests/test_endpoints/test_expense_e.py b/tests/test_endpoints/test_expense_e.py index ec18783..7d34989 100644 --- a/tests/test_endpoints/test_expense_e.py +++ b/tests/test_endpoints/test_expense_e.py @@ -128,7 +128,7 @@ def test_delete_expense(self) -> None: ) data = client.delete(f"/groups/{self.first_group.id}/expenses/{expense.id}/") assert data.status_code == 204 - data = client.get(f"/groups/expenses/") + data = client.get("/groups/expenses/") assert data.status_code == 200 expenses_data = [] response_data = { @@ -359,7 +359,7 @@ def test_read_expenses_all_time(self) -> None: group_id=self.second_group.id, category_id=self.category.id, ) - data = client.get(f"/groups/expenses/") + data = client.get("/groups/expenses/") assert data.status_code == 200 expenses_data = [ { @@ -405,12 +405,12 @@ def test_read_expenses_all_time(self) -> None: assert data.json() == response_data def test_read_expenses_month_exc(self) -> None: - data = client.get(f"/groups/expenses/", params={"year_month": "11-2022"}) + data = client.get("/groups/expenses/", params={"year_month": "11-2022"}) assert data.status_code == 422 def test_read_expenses_month(self) -> None: time = datetime.datetime(2022, 12, 1) - data = client.get(f"/groups/expenses/", params={"year_month": "2022-12"}) + data = client.get("/groups/expenses/", params={"year_month": "2022-12"}) assert data.json() == { "items": [], "total": 0, @@ -440,7 +440,7 @@ def test_read_expenses_month(self) -> None: time=time, ) - data = client.get(f"/groups/expenses/", params={"year_month": "2022-12"}) + data = client.get("/groups/expenses/", params={"year_month": "2022-12"}) expenses_data = [ { @@ -491,7 +491,7 @@ def test_read_expenses_month(self) -> None: def test_read_expenses_time_range(self): time = datetime.datetime(2022, 12, 1) data = client.get( - f"/groups/expenses/", + "/groups/expenses/", params={"start_date": "2022-12-1", "end_date": "2022-12-12"}, ) assert data.json() == { @@ -524,7 +524,7 @@ def test_read_expenses_time_range(self): ) data = client.get( - f"/groups/expenses/", + "/groups/expenses/", params={"start_date": "2022-12-1", "end_date": "2022-12-12"}, ) expenses_data = [ @@ -575,7 +575,7 @@ def test_read_expenses_time_range(self): def test_read_expenses_time_range_date_exc(self): data = client.get( - f"/groups/expenses/", + "/groups/expenses/", params={"start_date": "2022-12-31", "end_date": "2022-12-09"}, ) assert data.status_code == 404 diff --git a/tests/test_endpoints/test_group_e.py b/tests/test_endpoints/test_group_e.py index 61bac24..ef3209d 100644 --- a/tests/test_endpoints/test_group_e.py +++ b/tests/test_endpoints/test_group_e.py @@ -131,7 +131,7 @@ def test_update_group_as_non_admin(self) -> None: title="string", description="string", icon_url="string", color_code="string" ) data = client.put( - f"/groups/9999", + "/groups/9999", json={ "title": group.title, "description": group.description, @@ -192,7 +192,7 @@ def test_leave_group_user(self) -> None: assert data == user_group_data def test_leave_group_not_found(self) -> None: - data = client.post(f"/groups/9999/leave/") + data = client.post("/groups/9999/leave/") assert data.status_code == 404 def test_leave_group_admin(self) -> None: diff --git a/tests/test_endpoints/test_invitation_e.py b/tests/test_endpoints/test_invitation_e.py index 2157c7b..cf16cea 100644 --- a/tests/test_endpoints/test_invitation_e.py +++ b/tests/test_endpoints/test_invitation_e.py @@ -201,7 +201,7 @@ def test_response_invitation(self) -> None: def test_response_invitation_not_found(self) -> None: response = UserResponseEnum.ACCEPTED data = client.post( - f"invitations/9999/response/", + "invitations/9999/response/", params={"invitation_id": 9999, "response": response}, ) assert data.status_code == 404 diff --git a/tests/test_endpoints/test_replenishment_e.py b/tests/test_endpoints/test_replenishment_e.py index 9c81829..0885867 100644 --- a/tests/test_endpoints/test_replenishment_e.py +++ b/tests/test_endpoints/test_replenishment_e.py @@ -69,7 +69,7 @@ def test_delete_replenishment(self) -> None: replenishment = ReplenishmentFactory(user_id=self.user.id) data = client.delete(f"/replenishments/{replenishment.id}/") assert data.status_code == 204 - data = client.get(f"/replenishments/") + data = client.get("/replenishments/") assert data.status_code == 200 replenishments_data = [] response_data = { @@ -84,7 +84,7 @@ def test_delete_replenishment(self) -> None: def test_read_replenishments_all_time(self) -> None: first_replenishments = ReplenishmentFactory(user_id=self.user.id) second_replenishments = ReplenishmentFactory(user_id=self.user.id) - data = client.get(f"/replenishments/") + data = client.get("/replenishments/") assert data.status_code == 200 replenishments_data = [ { @@ -115,17 +115,17 @@ def test_read_replenishments_exc_three_args(self) -> None: "end_date": "2022-12-12", "year_month": "2022-12", } - data = client.get(f"/replenishments/", params=params) + data = client.get("/replenishments/", params=params) assert data.status_code == 422 def test_read_replenishments_exc_args_no_match(self) -> None: params = {"end_date": "2022-12-12", "year_month": "2022-12"} - data = client.get(f"/replenishments/", params=params) + data = client.get("/replenishments/", params=params) assert data.status_code == 422 def test_read_replenishments_month_exc(self) -> None: params = {"year_month": "12-2022"} - data = client.get(f"/replenishments/", params=params) + data = client.get("/replenishments/", params=params) assert data.status_code == 422 def test_read_replenishments_month(self) -> None: @@ -213,5 +213,5 @@ def test_read_replenishments_time_range(self) -> None: def test_read_replenishments_time_range_date_exc(self) -> None: params = {"start_date": "2022-12-31", "end_date": "2022-12-09"} - data = client.get(f"/replenishments/", params=params) + data = client.get("/replenishments/", params=params) assert data.status_code == 404 diff --git a/tests/test_services/test_user_s.py b/tests/test_services/test_user_s.py index 37524db..7d8983e 100644 --- a/tests/test_services/test_user_s.py +++ b/tests/test_services/test_user_s.py @@ -1,9 +1,9 @@ import datetime from services import ( get_user, - calculate_user_balance, - user_total_expenses, - user_total_replenishments, + read_user_balance, + read_user_total_expenses, + read_user_total_replenishments, ) from tests.factories import ReplenishmentFactory, UserFactory, ExpenseFactory @@ -28,7 +28,7 @@ def test_read_user_current_balance(session, dependence_factory, activity) -> Non activity = activity first_replenishment = ReplenishmentFactory(user_id=factories["first_user"].id) second_replenishment = ReplenishmentFactory(user_id=factories["first_user"].id) - data = calculate_user_balance(session, factories["first_user"].id) + data = read_user_balance(session, factories["first_user"].id) assert data.balance == float( first_replenishment.amount + second_replenishment.amount @@ -41,7 +41,7 @@ def test_read_user_negative_current_balance( ) -> None: factories = dependence_factory activity = activity - data = calculate_user_balance(session, factories["first_user"].id) + data = read_user_balance(session, factories["first_user"].id) assert data.balance == -float(activity["first_expense"].amount) @@ -64,7 +64,7 @@ def test_user_percentage_increase_expenses_for_month( category_id=activity["category"].id, time=filter_date, ) - data = user_total_expenses( + data = read_user_total_expenses( db=session, user_id=factories["first_user"].id, filter_date=filter_date, @@ -101,7 +101,7 @@ def test_user_percentage_increase_expenses_for_range_time( category_id=activity["category"].id, time=end_date, ) - data = user_total_expenses( + data = read_user_total_expenses( db=session, user_id=factories["first_user"].id, start_date=start_date, @@ -132,7 +132,7 @@ def test_user_percentage_increase_replenishments_for_range_time( user_id=factories["first_user"].id, time=end_date, ) - data = user_total_replenishments( + data = read_user_total_replenishments( db=session, user_id=factories["first_user"].id, start_date=start_date,