Skip to content

Commit

Permalink
Fix: some bugs
Browse files Browse the repository at this point in the history
* style: change response message

* fix: return onlu active users and remove users_group massive
  • Loading branch information
RezenkovD authored Sep 28, 2023
1 parent 22c83de commit a56ac66
Show file tree
Hide file tree
Showing 11 changed files with 38 additions and 27 deletions.
4 changes: 2 additions & 2 deletions src/routers/group.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))


Expand Down
2 changes: 1 addition & 1 deletion src/services/category.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion src/services/expense.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
23 changes: 19 additions & 4 deletions src/services/group.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
UserDailyExpenses,
UserDailyExpensesDetail,
)
from enums import GroupStatusEnum


def user_validate_input_date(
Expand Down Expand Up @@ -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()
Expand All @@ -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:
(
Expand Down Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion src/services/invitation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
22 changes: 9 additions & 13 deletions tests/test_endpoints/test_group_e.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion tests/test_endpoints/test_invitation_e.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/test_services/test_category_s.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion tests/test_services/test_expense_s.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion tests/test_services/test_group_s.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion tests/test_services/test_invitation_s.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit a56ac66

Please sign in to comment.