Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,6 @@ dmypy.json

# Pyre type checker
.pyre/

# uv pocket manager
uv.lock

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

не забудь убрать

4 changes: 4 additions & 0 deletions rating_api/models/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ class Lecturer(BaseDbModel):
avatar_link: Mapped[str] = mapped_column(String, nullable=True, comment="Ссылка на аву препода")
timetable_id: Mapped[int]
comments: Mapped[list[Comment]] = relationship("Comment", back_populates="lecturer")
lecturer_user_comments: Mapped[list[LecturerUserComment]] = relationship(

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

пока убрать

"LecturerUserComment", back_populates="lecturer", cascade="all, delete-orphan"
)
mark_weighted: Mapped[float] = mapped_column(
Float,
nullable=False,
Expand Down Expand Up @@ -288,6 +291,7 @@ class LecturerUserComment(BaseDbModel):
id: Mapped[int] = mapped_column(Integer, primary_key=True)
user_id: Mapped[int] = mapped_column(Integer, nullable=False)
lecturer_id: Mapped[int] = mapped_column(Integer, ForeignKey("lecturer.id"))
lecturer: Mapped[Lecturer] = relationship("Lecturer", back_populates="lecturer_user_comments")
create_ts: Mapped[datetime.datetime] = mapped_column(
DateTime, default=datetime.datetime.now(datetime.timezone.utc), nullable=False
)
Expand Down
50 changes: 39 additions & 11 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,16 +92,46 @@ def dbsession(db_container):


@pytest.fixture
def client(mocker):
user_mock = mocker.patch('auth_lib.fastapi.UnionAuth.__call__')
user_mock.return_value = {
"session_scopes": [{"id": 0, "name": "string", "comment": "string"}],
"user_scopes": [{"id": 0, "name": "string", "comment": "string"}],
"indirect_groups": [{"id": 0, "name": "string", "parent_id": 0}],
"groups": [{"id": 0, "name": "string", "parent_id": 0}],
def authlib_user():

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

здесь аналогично прошлому пр исправить(по комментам прошлого)
p.s. см коммент на этот счет в отдельном комментарии в этом пр

"""
Данные о пользователе, возвращаемые сервисом auth.
"""
return {
"auth_methods": ["email", "github_auth"],
"session_scopes": [
{"id": 145, "name": "auth.session.create"},
{"id": 146, "name": "auth.session.update"},
{"id": 165, "name": "auth.user.selfdelete"},
],
"user_scopes": [
{"id": 145, "name": "auth.session.create"},
{"id": 146, "name": "auth.session.update"},
{"id": 165, "name": "auth.user.selfdelete"},
],
"indirect_groups": [99],
"groups": [99],
"id": 0,
"email": "string",
"email": "aslimbo2001@gmail.com",
"userdata": [
{"category": "Личная информация", "param": "Полное имя", "value": "Тестовый Тест"},
],
}


@pytest.fixture()
def authlib_mock(mocker):
auth_mock = mocker.patch("auth_lib.fastapi.UnionAuth.__call__")
return auth_mock


@pytest.fixture()
def user_mock(authlib_mock, authlib_user):
authlib_mock.return_value = authlib_user
return authlib_mock


@pytest.fixture
def client(mocker, user_mock):
client = TestClient(app)
return client

Expand Down Expand Up @@ -214,11 +244,9 @@ def lecturers(dbsession):
Lecturer(id=4, first_name='test_fname3', last_name='test_lname3', middle_name='test_mname3', timetable_id=9903)
)
lecturers[-1].is_deleted = True
for lecturer in lecturers:
dbsession.add(lecturer)
dbsession.add_all(lecturers)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

лучше вернуть

dbsession.commit()
yield lecturers

for lecturer in lecturers:
for row in lecturer.comments:
dbsession.delete(row)
Expand Down
99 changes: 99 additions & 0 deletions tests/test_routes/test_comment.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import logging

import pytest
from auth_lib.fastapi import UnionAuth

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

тоже не нужно

from starlette import status

from rating_api.models import Comment, CommentReaction, LecturerUserComment, Reaction, ReviewStatus
Expand Down Expand Up @@ -177,6 +178,16 @@
def test_create_comment(client, dbsession, lecturers, body, lecturer_n, response_status):
params = {"lecturer_id": lecturers[lecturer_n].id}
post_response = client.post(url, json=body, params=params)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

не нужно

# Проверка корректности переданных в userdata "param"
user = UnionAuth.__call__(post_response)
acceptable_params = ["Полное имя", "Фото", "Имя пользователя GitHub", "Номер Телефона"]
real_params = [i["param"] for i in user.get("userdata")]
for param in real_params:
assert (
param in acceptable_params
), f"Не допустимый параметр: \"{i}\"! Список допустимых параметров: {acceptable_params}"

assert post_response.status_code == response_status
if response_status == status.HTTP_200_OK:
comment = Comment.query(session=dbsession).filter(Comment.uuid == post_response.json()["uuid"]).one_or_none()
Expand All @@ -195,6 +206,94 @@ def test_create_comment(client, dbsession, lecturers, body, lecturer_n, response
assert user_comment is not None


@pytest.mark.parametrize(
"body, total, response_status",
[
(
{
"comments": [
{
"subject": "string",
"text": "string",
"mark_kindness": 0,
"mark_freebie": 0,
"mark_clarity": 0,
"lecturer_id": 1,
"create_ts": "2026-05-25T11:41:26.777Z",
"update_ts": "2026-05-25T11:41:26.777Z",
},
{
"subject": "string",
"text": "string",
"mark_kindness": 0,
"mark_freebie": 0,
"mark_clarity": 0,
"lecturer_id": 2,
"create_ts": "2026-05-25T11:41:26.777Z",
"update_ts": "2026-05-25T11:41:26.777Z",
},
],
},
2,
status.HTTP_200_OK,
),
(
{"comments": []},
0,
status.HTTP_200_OK,
),
(
{
"comments": [
{
"subject": "string",
"text": "string",
"mark_kindness": 0,
"mark_freebie": 0,
"mark_clarity": 0,
"lecturer_id": 4,
"create_ts": "2026-05-25T11:41:26.777Z",
"update_ts": "2026-05-25T11:41:26.777Z",
},
],
},
1,
status.HTTP_200_OK,
),
(
{
"comments": [
{
"subdject": "string",
"text": "string",
"mark_kindness": 0,
"mark_freebie": 0,
"mark_clarity": 0,
"lecturer_id": "abc",
},
],
},
None,
status.HTTP_422_UNPROCESSABLE_CONTENT,
),
],
)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

тут точно больше тестов не надо? на неудачные кейсы? если нет, то все ок, сделал правильно

def test_import_comments(client, dbsession, lecturers, body, total, response_status):
response = client.post(f"{url}/import", json=body)

assert response.status_code == response_status

new_comments = response.json()
print(new_comments)

assert total == new_comments.get("total")

if new_comments.get("total") and total > 0:
for comment in new_comments.get("comments"):
comment_from_db = Comment.query(session=dbsession).filter(Comment.uuid == comment.get("uuid")).one_or_none()
assert comment_from_db is not None


@pytest.mark.parametrize(
"reaction_data, expected_reaction, comment_user_id",
[
Expand Down
Loading