Skip to content

Commit

Permalink
Merge pull request #56 from profcomff/getuseridremake
Browse files Browse the repository at this point in the history
add user_id to get_comments and bugfix
  • Loading branch information
Temmmmmo authored Nov 24, 2024
2 parents 953231e + 87e5a9e commit 33c5e86
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 15 deletions.
9 changes: 8 additions & 1 deletion rating_api/routes/comment.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,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 @@ -90,15 +91,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
27 changes: 17 additions & 10 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,22 +121,29 @@ def lecturers(dbsession):
def lecturers_with_comments(dbsession, lecturers):
"""
Creates 4 lecturers(one with flag is_deleted=True)
with 4 comments to non-deleted lecturers 2 approved and one dismissed and one pending.
with 6 comments to non-deleted lecturers 4 approved and one dismissed and one pending.
Two of them have alike names.
Two of them have a different user_id.
"""
comments_data = [
(lecturers[0].id, 0, 'test_subject', ReviewStatus.APPROVED, 1, 1, 1),
(lecturers[0].id, 9990, 'test_subject', ReviewStatus.APPROVED, 1, 1, 1),
(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[1].id, 0, 'test_subject', ReviewStatus.APPROVED, 1, 1, 1),
(lecturers[0].id, 9990, 'test_subject2', ReviewStatus.DISMISSED, -1, -1, -1),
(lecturers[0].id, 9990, 'test_subject2', ReviewStatus.PENDING, -2, -2, -2),
(lecturers[0].id, 9991, 'test_subject11', ReviewStatus.APPROVED, 1, 1, 1),
(lecturers[0].id, 9992, 'test_subject12', ReviewStatus.APPROVED, 2, 2, 2),
(lecturers[1].id, 9990, '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[2].id, 0, 'test_subject', ReviewStatus.APPROVED, 1, 1, 1),
(lecturers[1].id, 9990, 'test_subject2', ReviewStatus.DISMISSED, -2, -2, -2),
(lecturers[1].id, 9990, 'test_subject2', ReviewStatus.PENDING, -2, -2, -2),
(lecturers[1].id, 9991, 'test_subject11', ReviewStatus.APPROVED, 1, 1, 1),
(lecturers[1].id, 9992, 'test_subject12', ReviewStatus.APPROVED, -1, -1, -1),
(lecturers[2].id, 9990, '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, 9990, 'test_subject2', ReviewStatus.DISMISSED, 2, 2, 2),
(lecturers[2].id, 9990, 'test_subject2', ReviewStatus.PENDING, -2, -2, -2),
(lecturers[2].id, 9991, 'test_subject11', ReviewStatus.APPROVED, 1, 1, 1),
(lecturers[2].id, 9992, 'test_subject12', ReviewStatus.APPROVED, 0, 0, 0),
]

comments = [
Expand Down
20 changes: 20 additions & 0 deletions tests/test_routes/test_comment.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,26 @@ 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 33c5e86

Please sign in to comment.