Skip to content

Commit

Permalink
add user_id to get_comments and bugfix
Browse files Browse the repository at this point in the history
  • Loading branch information
zipperman1 committed Nov 18, 2024
1 parent 6d34a33 commit f123a3c
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 5 deletions.
9 changes: 8 additions & 1 deletion rating_api/routes/comment.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ async def get_comments(
limit: int = 10,
offset: int = 0,
lecturer_id: int | None = None,
user_id: int | None = None,
order_by: list[Literal["create_ts"]] = Query(default=[]),
unreviewed: bool = False,
user=Depends(UnionAuth(scopes=['rating.comment.review'], auto_error=False, allow_none=True)),
Expand All @@ -80,15 +81,21 @@ async def get_comments(
`lecturer_id` - вернет все комментарии для преподавателя с конкретным id, по дефолту возвращает вообще все аппрувнутые комментарии.
`user_id` - вернет все комментарии пользователя с конкретным id
`unreviewed` - вернет все непроверенные комментарии, если True. По дефолту False.
"""
comments = Comment.query(session=db.session).all()
if not comments:
raise ObjectNotFound(Comment, 'all')
result = CommentGetAll(limit=limit, offset=offset, total=len(comments))
result.comments = comments
if lecturer_id:
if user_id is not None:
result.comments = [comment for comment in result.comments if comment.user_id == user_id]

if lecturer_id is not None:
result.comments = [comment for comment in result.comments if comment.lecturer_id == lecturer_id]

if unreviewed:
if not user:
raise ForbiddenAction(Comment)
Expand Down
6 changes: 6 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,14 +129,20 @@ def lecturers_with_comments(dbsession, lecturers):
(lecturers[0].id, None, 'test_subject1', ReviewStatus.APPROVED, 2, 2, 2),
(lecturers[0].id, 0, 'test_subject2', ReviewStatus.DISMISSED, -1, -1, -1),
(lecturers[0].id, 0, 'test_subject2', ReviewStatus.PENDING, -2, -2, -2),
(lecturers[0].id, 1, 'test_subject', ReviewStatus.APPROVED, 1, 1, 1),
(lecturers[0].id, 2, 'test_subject1', ReviewStatus.APPROVED, 2, 2, 2),
(lecturers[1].id, 0, 'test_subject', ReviewStatus.APPROVED, 1, 1, 1),
(lecturers[1].id, None, 'test_subject1', ReviewStatus.APPROVED, -1, -1, -1),
(lecturers[1].id, 0, 'test_subject2', ReviewStatus.DISMISSED, -2, -2, -2),
(lecturers[1].id, 0, 'test_subject2', ReviewStatus.PENDING, -2, -2, -2),
(lecturers[1].id, 1, 'test_subject', ReviewStatus.APPROVED, 1, 1, 1),
(lecturers[1].id, 2, 'test_subject1', ReviewStatus.APPROVED, -1, -1, -1),
(lecturers[2].id, 0, 'test_subject', ReviewStatus.APPROVED, 1, 1, 1),
(lecturers[2].id, None, 'test_subject1', ReviewStatus.APPROVED, 0, 0, 0),
(lecturers[2].id, 0, 'test_subject2', ReviewStatus.DISMISSED, 2, 2, 2),
(lecturers[2].id, 0, 'test_subject2', ReviewStatus.PENDING, -2, -2, -2),
(lecturers[2].id, 1, 'test_subject', ReviewStatus.APPROVED, 1, 1, 1),
(lecturers[2].id, 2, 'test_subject1', ReviewStatus.APPROVED, 0, 0, 0),
]

comments = [
Expand Down
16 changes: 16 additions & 0 deletions tests/test_routes/test_comment.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,22 @@ def test_comments_by_lecturer_id(client, lecturers_with_comments, lecturer_n, re
]
)

@pytest.mark.parametrize(
'user_id,response_status', [(0, status.HTTP_200_OK), (1, status.HTTP_200_OK), (2, status.HTTP_200_OK)]
)
def test_comments_by_user_id(client, lecturers_with_comments, user_id, response_status):
_, comments = lecturers_with_comments
response = response = client.get(f'{url}', params={"user_id": user_id})
assert response.status_code == response_status
if response.status_code == status.HTTP_200_OK:
json_response = response.json()
assert len(json_response["comments"]) == len(
[
comment
for comment in comments
if comment.user_id == user_id and comment.review_status == ReviewStatus.APPROVED and not comment.is_deleted
]
)

@pytest.mark.parametrize(
'review_status, response_status,is_reviewed',
Expand Down
8 changes: 4 additions & 4 deletions tests/test_routes/test_lecturer.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,10 @@ def test_get_lecturer_with_comments(
assert json_response["mark_freebie"] == mark_freebie
assert json_response["mark_clarity"] == mark_clarity
assert json_response["mark_general"] == mark_general
assert comments[lecturer_n * 4 + 0].subject in json_response["subjects"]
assert comments[lecturer_n * 4 + 1].subject in json_response["subjects"]
assert comments[lecturer_n * 4 + 2].subject not in json_response["subjects"]
assert len(json_response["comments"]) == 2
assert comments[lecturer_n * 6 + 0].subject in json_response["subjects"]
assert comments[lecturer_n * 6 + 1].subject in json_response["subjects"]
assert comments[lecturer_n * 6 + 2].subject not in json_response["subjects"]
assert len(json_response["comments"]) == 4


@pytest.mark.parametrize(
Expand Down

0 comments on commit f123a3c

Please sign in to comment.