From f123a3cb8fca9515f7e308c4132d927310fbf6ee Mon Sep 17 00:00:00 2001 From: Timofeev Nikita Date: Mon, 18 Nov 2024 16:49:41 +0000 Subject: [PATCH 1/4] 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( From eb3df2df76a301663807f4deff858e36226cbe66 Mon Sep 17 00:00:00 2001 From: Timofeev Nikita Date: Mon, 18 Nov 2024 16:52:49 +0000 Subject: [PATCH 2/4] lint --- tests/test_routes/test_comment.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/tests/test_routes/test_comment.py b/tests/test_routes/test_comment.py index 2426105..3cc88bc 100644 --- a/tests/test_routes/test_comment.py +++ b/tests/test_routes/test_comment.py @@ -103,6 +103,7 @@ 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)] ) @@ -116,10 +117,13 @@ def test_comments_by_user_id(client, lecturers_with_comments, user_id, response_ [ comment for comment in comments - if comment.user_id == user_id and comment.review_status == ReviewStatus.APPROVED and not comment.is_deleted + 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', [ From 71d107473e8d2e934ebb4ea370f24cf3508eed3d Mon Sep 17 00:00:00 2001 From: Timofeev Nikita Date: Mon, 18 Nov 2024 17:10:50 +0000 Subject: [PATCH 3/4] rewrote docs + removed unused imports --- tests/conftest.py | 15 ++++++++------- tests/test_routes/test_comment.py | 2 +- tests/test_routes/test_lecturer.py | 2 +- 3 files changed, 10 insertions(+), 9 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index 33705b0..12c3da0 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -121,28 +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, 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[0].id, 1, 'test_subject11', ReviewStatus.APPROVED, 1, 1, 1), + (lecturers[0].id, 2, 'test_subject12', 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[1].id, 1, 'test_subject11', ReviewStatus.APPROVED, 1, 1, 1), + (lecturers[1].id, 2, 'test_subject12', 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), + (lecturers[2].id, 1, 'test_subject11', ReviewStatus.APPROVED, 1, 1, 1), + (lecturers[2].id, 2, 'test_subject12', ReviewStatus.APPROVED, 0, 0, 0), ] comments = [ diff --git a/tests/test_routes/test_comment.py b/tests/test_routes/test_comment.py index 3cc88bc..c0497e0 100644 --- a/tests/test_routes/test_comment.py +++ b/tests/test_routes/test_comment.py @@ -4,7 +4,7 @@ import pytest from starlette import status -from rating_api.models import Comment, Lecturer, LecturerUserComment, ReviewStatus +from rating_api.models import Comment, LecturerUserComment, ReviewStatus from rating_api.settings import get_settings diff --git a/tests/test_routes/test_lecturer.py b/tests/test_routes/test_lecturer.py index 5bbfe21..95d5aca 100644 --- a/tests/test_routes/test_lecturer.py +++ b/tests/test_routes/test_lecturer.py @@ -3,7 +3,7 @@ import pytest from starlette import status -from rating_api.models import Comment, Lecturer, ReviewStatus +from rating_api.models import Lecturer from rating_api.settings import get_settings From 87e5a9e9b01c3e67d58d70b3f131021d43d354db Mon Sep 17 00:00:00 2001 From: Timofeev Nikita Date: Mon, 18 Nov 2024 18:41:39 +0000 Subject: [PATCH 4/4] changed user_id values --- tests/conftest.py | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index 12c3da0..f5d7f16 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -126,24 +126,24 @@ def lecturers_with_comments(dbsession, lecturers): 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[0].id, 1, 'test_subject11', ReviewStatus.APPROVED, 1, 1, 1), - (lecturers[0].id, 2, 'test_subject12', ReviewStatus.APPROVED, 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[1].id, 1, 'test_subject11', ReviewStatus.APPROVED, 1, 1, 1), - (lecturers[1].id, 2, 'test_subject12', ReviewStatus.APPROVED, -1, -1, -1), - (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, 1, 'test_subject11', ReviewStatus.APPROVED, 1, 1, 1), - (lecturers[2].id, 2, 'test_subject12', ReviewStatus.APPROVED, 0, 0, 0), + (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 = [