From a56ac661bf4509749d5101e6985a8c61de2df197 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: Thu, 28 Sep 2023 19:24:41 +0300 Subject: [PATCH] Fix: some bugs * style: change response message * fix: return onlu active users and remove users_group massive --- src/routers/group.py | 4 ++-- src/services/category.py | 2 +- src/services/expense.py | 2 +- src/services/group.py | 23 +++++++++++++++++++---- src/services/invitation.py | 2 +- tests/test_endpoints/test_group_e.py | 22 +++++++++------------- tests/test_endpoints/test_invitation_e.py | 2 +- tests/test_services/test_category_s.py | 2 +- tests/test_services/test_expense_s.py | 2 +- tests/test_services/test_group_s.py | 2 +- tests/test_services/test_invitation_s.py | 2 +- 11 files changed, 38 insertions(+), 27 deletions(-) diff --git a/src/routers/group.py b/src/routers/group.py index 2e62a18..a2ea6fe 100644 --- a/src/routers/group.py +++ b/src/routers/group.py @@ -65,13 +65,13 @@ def update_group( return services.update_group(db, current_user.id, group, group_id) -@router.get("/{group_id}/users/", response_model=Page[UsersGroup]) +@router.get("/{group_id}/users/", response_model=Page[AboutUser]) def read_users_group( *, db: Session = Depends(get_db), current_user: User = Depends(get_current_user), group_id: int, -) -> Page[UsersGroup]: +) -> Page[AboutUser]: return paginate(db, services.read_users_group(db, current_user.id, group_id)) diff --git a/src/services/category.py b/src/services/category.py index c22acd5..bc7afa8 100644 --- a/src/services/category.py +++ b/src/services/category.py @@ -18,7 +18,7 @@ def validate_input_data( except exc.NoResultFound: raise HTTPException( status_code=status.HTTP_404_NOT_FOUND, - detail="You are not admin in this group!", + detail="You are not admin of this group!", ) if group.status == GroupStatusEnum.INACTIVE: raise HTTPException( diff --git a/src/services/expense.py b/src/services/expense.py index 3a63d03..6852c22 100644 --- a/src/services/expense.py +++ b/src/services/expense.py @@ -43,7 +43,7 @@ def validate_input_data( except exc.NoResultFound: raise HTTPException( status_code=status.HTTP_404_NOT_FOUND, - detail="The group has no such category!", + detail="The group does not have such a category!", ) if expense_id: try: diff --git a/src/services/group.py b/src/services/group.py index 4b9aaf4..db73e6b 100644 --- a/src/services/group.py +++ b/src/services/group.py @@ -30,6 +30,7 @@ UserDailyExpenses, UserDailyExpensesDetail, ) +from enums import GroupStatusEnum def user_validate_input_date( @@ -127,7 +128,14 @@ def read_group_info(db: Session, user_id: int, group_id: int) -> GroupInfo: db.query(Group).options(joinedload(Group.admin)).filter_by(id=group_id).one() ) group_users_count = ( - db.query(func.count(UserGroup.user_id)).filter_by(group_id=group_id).scalar() + db.query(func.count(UserGroup.user_id)) + .filter( + and_( + UserGroup.group_id == group_id, + UserGroup.status == GroupStatusEnum.ACTIVE, + ) + ) + .scalar() ) group_expenses_count = ( db.query(func.count(Expense.id)).filter_by(group_id=group_id).scalar() @@ -154,7 +162,7 @@ def remove_user( except exc.NoResultFound: raise HTTPException( status_code=status.HTTP_404_NOT_FOUND, - detail="You are not admin in this group!", + detail="You are not admin of this group!", ) try: ( @@ -318,10 +326,17 @@ def add_user_in_group(db: Session, user_id: int, group_id: int) -> None: db.add(db_user_group) -def read_users_group(db: Session, user_id: int, group_id: int) -> UsersGroup: +def read_users_group(db: Session, user_id: int, group_id: int) -> List[AboutUser]: user_validate_input_date(db, user_id, group_id) db_query = ( - select(Group).options(joinedload(Group.users_group)).filter_by(id=group_id) + select(UserGroup) + .options(joinedload(UserGroup.user)) + .filter( + and_( + UserGroup.group_id == group_id, + UserGroup.status == GroupStatusEnum.ACTIVE, + ) + ) ) return db_query diff --git a/src/services/invitation.py b/src/services/invitation.py index 3092838..04478c0 100644 --- a/src/services/invitation.py +++ b/src/services/invitation.py @@ -96,7 +96,7 @@ def create_invitation( except exc.NoResultFound: raise HTTPException( status_code=status.HTTP_404_NOT_FOUND, - detail="You are not admin in this group!", + detail="You are not admin of this group!", ) if db_group.status == GroupStatusEnum.INACTIVE: raise HTTPException( diff --git a/tests/test_endpoints/test_group_e.py b/tests/test_endpoints/test_group_e.py index ef3209d..b57ae6a 100644 --- a/tests/test_endpoints/test_group_e.py +++ b/tests/test_endpoints/test_group_e.py @@ -148,19 +148,15 @@ def test_read_users_group(self) -> None: users_group_data = { "items": [ { - "users_group": [ - { - "status": GroupStatusEnum.ACTIVE, - "date_join": datetime.date.today().strftime("%Y-%m-%d"), - "user": { - "id": self.user.id, - "first_name": self.user.first_name, - "last_name": self.user.last_name, - "login": self.user.login, - "picture": self.user.picture, - }, - } - ], + "status": GroupStatusEnum.ACTIVE, + "date_join": datetime.date.today().strftime("%Y-%m-%d"), + "user": { + "id": self.user.id, + "first_name": self.user.first_name, + "last_name": self.user.last_name, + "login": self.user.login, + "picture": self.user.picture, + }, } ], "total": 1, diff --git a/tests/test_endpoints/test_invitation_e.py b/tests/test_endpoints/test_invitation_e.py index cf16cea..4d973c1 100644 --- a/tests/test_endpoints/test_invitation_e.py +++ b/tests/test_endpoints/test_invitation_e.py @@ -192,7 +192,7 @@ def test_response_invitation(self) -> None: users = [self.second_user, self.first_user] group_users = client.get(f"/groups/{self.second_group.id}/users/") assert group_users.status_code == 200 - group_users = group_users.json()["items"][0]["users_group"] + group_users = group_users.json()["items"] assert len(group_users) == len(users) for group_user, user in zip(group_users, users): assert group_user["user"]["id"] == user.id diff --git a/tests/test_services/test_category_s.py b/tests/test_services/test_category_s.py index 97c05f4..f80ec84 100644 --- a/tests/test_services/test_category_s.py +++ b/tests/test_services/test_category_s.py @@ -70,7 +70,7 @@ def test_create_category_not_admin(session, dependence_factory) -> None: category = CategoryCreate(title="Book", color_code="string", icon_url="string") with pytest.raises(HTTPException) as ex_info: create_category(session, 9999, factories["first_group"].id, category) - assert "You are not admin in this group!" in str(ex_info.value.detail) + assert "You are not admin of this group!" in str(ex_info.value.detail) def test_create_category_inactive_group(session) -> None: diff --git a/tests/test_services/test_expense_s.py b/tests/test_services/test_expense_s.py index f760100..6a56085 100644 --- a/tests/test_services/test_expense_s.py +++ b/tests/test_services/test_expense_s.py @@ -128,7 +128,7 @@ def test_create_expense_another_category(session, dependence_factory) -> None: create_expense( session, factories["first_user"].id, factories["first_group"].id, expense ) - assert "The group has no such category!" in str(ex_info.value.detail) + assert "The group does not have such a category!" in str(ex_info.value.detail) def test_read_expenses_by_another_group(session) -> None: diff --git a/tests/test_services/test_group_s.py b/tests/test_services/test_group_s.py index 64481d1..9568058 100644 --- a/tests/test_services/test_group_s.py +++ b/tests/test_services/test_group_s.py @@ -279,7 +279,7 @@ def test_remove_user_as_non_admin( factories["first_group"].id, factories["first_user"].id, ) - assert "You are not admin in this group!" in str(ex_info.value.detail) + assert "You are not admin of this group!" in str(ex_info.value.detail) def test_remove_inactive_user(session, dependence_factory) -> None: diff --git a/tests/test_services/test_invitation_s.py b/tests/test_services/test_invitation_s.py index b1ae653..b2d2691 100644 --- a/tests/test_services/test_invitation_s.py +++ b/tests/test_services/test_invitation_s.py @@ -38,7 +38,7 @@ def test_create_invitation_as_non_admin(session, dependence_factory) -> None: data = InvitationCreate(recipient_id=factories["second_user"].id, group_id=9999) with pytest.raises(HTTPException) as ex_info: create_invitation(session, factories["first_user"].id, data) - assert "You are not admin in this group!" in str(ex_info.value.detail) + assert "You are not admin of this group!" in str(ex_info.value.detail) def test_create_invitation_to_inactive_group(session, dependence_factory) -> None: