Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add user_id to get_comments and bugfix #56

Merged
merged 4 commits into from
Nov 24, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
9 changes: 8 additions & 1 deletion 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, 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),
zipperman1 marked this conversation as resolved.
Show resolved Hide resolved
(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_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_subject11', ReviewStatus.APPROVED, 1, 1, 1),
(lecturers[2].id, 2, 'test_subject12', ReviewStatus.APPROVED, 0, 0, 0),
]

comments = [
Expand Down
22 changes: 21 additions & 1 deletion tests/test_routes/test_comment.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -104,6 +104,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
10 changes: 5 additions & 5 deletions tests/test_routes/test_lecturer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down 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
Loading