From f123a3cb8fca9515f7e308c4132d927310fbf6ee Mon Sep 17 00:00:00 2001 From: Timofeev Nikita Date: Mon, 18 Nov 2024 16:49:41 +0000 Subject: [PATCH] add user_id to get_comments and bugfix --- rating_api/routes/comment.py | 9 ++++++++- tests/conftest.py | 6 ++++++ tests/test_routes/test_comment.py | 16 ++++++++++++++++ tests/test_routes/test_lecturer.py | 8 ++++---- 4 files changed, 34 insertions(+), 5 deletions(-) diff --git a/rating_api/routes/comment.py b/rating_api/routes/comment.py index c0cbcf4..65600b9 100644 --- a/rating_api/routes/comment.py +++ b/rating_api/routes/comment.py @@ -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)), @@ -80,6 +81,8 @@ async def get_comments( `lecturer_id` - вернет все комментарии для преподавателя с конкретным id, по дефолту возвращает вообще все аппрувнутые комментарии. + `user_id` - вернет все комментарии пользователя с конкретным id + `unreviewed` - вернет все непроверенные комментарии, если True. По дефолту False. """ comments = Comment.query(session=db.session).all() @@ -87,8 +90,12 @@ async def get_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) diff --git a/tests/conftest.py b/tests/conftest.py index a0a455e..33705b0 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -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 = [ diff --git a/tests/test_routes/test_comment.py b/tests/test_routes/test_comment.py index 44c3462..2426105 100644 --- a/tests/test_routes/test_comment.py +++ b/tests/test_routes/test_comment.py @@ -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', diff --git a/tests/test_routes/test_lecturer.py b/tests/test_routes/test_lecturer.py index 81b90cb..5bbfe21 100644 --- a/tests/test_routes/test_lecturer.py +++ b/tests/test_routes/test_lecturer.py @@ -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(