Skip to content

Commit

Permalink
Refactor/add users group paginate
Browse files Browse the repository at this point in the history
* ref: add users group paginate

* test: ref tests
  • Loading branch information
RezenkovD authored Aug 25, 2023
1 parent fe57680 commit daec4c1
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 30 deletions.
6 changes: 3 additions & 3 deletions src/routers/group.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,14 @@ def update_group(
return services.update_group(db, current_user.id, group, group_id)


@router.get("/{group_id}/users/", response_model=UsersGroup)
@router.get("/{group_id}/users/", response_model=Page[UsersGroup])
def read_users_group(
*,
db: Session = Depends(get_db),
current_user: User = Depends(get_current_user),
group_id: int,
) -> UsersGroup:
return services.read_users_group(db, current_user.id, group_id)
) -> Page[UsersGroup]:
return paginate(db, services.read_users_group(db, current_user.id, group_id))


@router.post("/{group_id}/leave/", response_model=Union[AboutUser, UsersGroup])
Expand Down
5 changes: 1 addition & 4 deletions src/services/group.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,10 +299,7 @@ def read_users_group(db: Session, user_id: int, group_id: int) -> UsersGroup:
detail="You are not in this group!",
)
db_query = (
db.query(Group)
.options(joinedload(Group.users_group))
.filter_by(id=group_id)
.one()
select(Group).options(joinedload(Group.users_group)).filter_by(id=group_id)
)
return db_query

Expand Down
30 changes: 19 additions & 11 deletions tests/test_endpoints/test_group_e.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,19 +146,27 @@ def test_read_users_group(self) -> None:
assert data.status_code == 200
data = data.json()
users_group_data = {
"users_group": [
"items": [
{
"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,
},
"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,
},
}
],
}
]
],
"total": 1,
"page": 1,
"size": 8,
"pages": 1,
}
assert data == users_group_data

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()["users_group"]
group_users = group_users.json()["items"][0]["users_group"]
assert len(group_users) == len(users)
for group_user, user in zip(group_users, users):
assert group_user["user"]["id"] == user.id
Expand Down
15 changes: 11 additions & 4 deletions tests/test_services/test_group_s.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import pytest
from starlette.exceptions import HTTPException
from sqlalchemy.orm import joinedload

from models import Group, UserGroup
from enums import GroupStatusEnum
Expand Down Expand Up @@ -135,8 +136,11 @@ def test_read_users_group(
session, dependence_factory, add_second_user_in_group
) -> None:
factories = dependence_factory
users_group = read_users_group(
session, factories["first_user"].id, factories["first_group"].id
users_group = (
session.query(Group)
.options(joinedload(Group.users_group))
.filter_by(id=factories["first_group"].id)
.one()
)
users = [factories["first_user"], factories["second_user"]]
for data, user in zip(users_group.users_group, users):
Expand Down Expand Up @@ -217,8 +221,11 @@ def test_leave_group_admin(
) -> None:
factories = dependence_factory
leave_group(session, factories["first_user"].id, factories["first_group"].id)
users_group = read_users_group(
session, factories["first_user"].id, factories["first_group"].id
users_group = (
session.query(Group)
.options(joinedload(Group.users_group))
.filter_by(id=factories["first_group"].id)
.one()
)
for user in users_group.users_group:
assert user.status == GroupStatusEnum.INACTIVE
Expand Down
23 changes: 16 additions & 7 deletions tests/test_services/test_invitation_s.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@

import pytest
from starlette.exceptions import HTTPException
from sqlalchemy.orm import joinedload

from enums import GroupStatusEnum, ResponseStatusEnum
from schemas import InvitationCreate
from models import Group
from services import (
create_invitation,
leave_group,
read_invitations,
read_users_group,
response_invitation,
)
from tests.factories import (
Expand Down Expand Up @@ -124,9 +125,13 @@ def test_response_invitation_denied(session, dependence_factory) -> None:
"%Y-%m-%d"
) == datetime.date.today().strftime("%Y-%m-%d")
assert invitation.recipient.id == factories["second_user"].id
users_group = read_users_group(
session, factories["first_user"].id, factories["first_group"].id
).users_group
db_query = (
session.query(Group)
.options(joinedload(Group.users_group))
.filter_by(id=factories["first_group"].id)
.one()
)
users_group = db_query.users_group
users = [factories["first_user"]]
assert len(users_group) == len(users)
for user_group, user in zip(users_group, users):
Expand All @@ -153,9 +158,13 @@ def test_response_invitation_accepted(session, dependence_factory) -> None:
) == datetime.date.today().strftime("%Y-%m-%d")
assert invitation.recipient.id == factories["second_user"].id
users = [factories["first_user"], factories["second_user"]]
users_group = read_users_group(
session, factories["first_user"].id, factories["first_group"].id
).users_group
db_query = (
session.query(Group)
.options(joinedload(Group.users_group))
.filter_by(id=factories["first_group"].id)
.one()
)
users_group = db_query.users_group
assert len(users_group) == len(users)
for user_group, user in zip(users_group, users):
assert user_group.user.id == user.id
Expand Down

0 comments on commit daec4c1

Please sign in to comment.