-
Notifications
You must be signed in to change notification settings - Fork 3
Test/dev 72 add tests for post comment import #172
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
base: main
Are you sure you want to change the base?
Changes from all commits
f85af90
9ee4001
47b118c
54ccba8
944ae37
b00ad8b
e5d6cf1
810eeb4
a118c23
0342965
4f26d3c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -127,3 +127,6 @@ dmypy.json | |
|
|
||
| # Pyre type checker | ||
| .pyre/ | ||
|
|
||
| # uv pocket manager | ||
| uv.lock | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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( | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, | ||
|
|
@@ -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 | ||
| ) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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(): | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. здесь аналогично прошлому пр исправить(по комментам прошлого) |
||
| """ | ||
| Данные о пользователе, возвращаемые сервисом 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 | ||
|
|
||
|
|
@@ -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) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,6 +2,7 @@ | |
| import logging | ||
|
|
||
| import pytest | ||
| from auth_lib.fastapi import UnionAuth | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
|
@@ -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) | ||
|
|
||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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() | ||
|
|
@@ -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, | ||
| ), | ||
| ], | ||
| ) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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", | ||
| [ | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
не забудь убрать