diff --git a/rating_api/models/db.py b/rating_api/models/db.py index a25a820..ab645c6 100644 --- a/rating_api/models/db.py +++ b/rating_api/models/db.py @@ -41,8 +41,14 @@ def search_by_name(self, query: str) -> bool: response = true query = query.split(' ') for q in query: + q = q.lower() response = and_( - response, or_(self.first_name.contains(q), self.middle_name.contains(q), self.last_name.contains(q)) + response, + or_( + func.lower(self.first_name).contains(q), + func.lower(self.middle_name).contains(q), + func.lower(self.last_name).contains(q), + ), ) return response diff --git a/tests/conftest.py b/tests/conftest.py index cc467ce..a0a455e 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -3,6 +3,7 @@ from sqlalchemy import create_engine from sqlalchemy.orm import Session, sessionmaker +from rating_api.models.db import * from rating_api.routes import app from rating_api.settings import Settings @@ -27,4 +28,135 @@ def dbsession() -> Session: settings = Settings() engine = create_engine(str(settings.DB_DSN), pool_pre_ping=True) TestingSessionLocal = sessionmaker(bind=engine) - yield TestingSessionLocal() + session = TestingSessionLocal() + yield session + + +@pytest.fixture +def lecturer(dbsession): + _lecturer = Lecturer(first_name="test_fname", last_name="test_lname", middle_name="test_mname", timetable_id=9900) + dbsession.add(_lecturer) + dbsession.commit() + yield _lecturer + dbsession.refresh(_lecturer) + dbsession.delete(_lecturer) + dbsession.commit() + + +@pytest.fixture +def comment(dbsession, lecturer): + _comment = Comment( + subject="test_subject", + text="test_comment", + mark_kindness=1, + mark_clarity=1, + mark_freebie=1, + lecturer_id=lecturer.id, + review_status=ReviewStatus.APPROVED, + ) + dbsession.add(_comment) + dbsession.commit() + yield _comment + dbsession.refresh(_comment) + dbsession.delete(_comment) + dbsession.commit() + + +@pytest.fixture +def unreviewed_comment(dbsession, lecturer): + _comment = Comment( + subject="test_subject", + text="test_comment", + mark_kindness=1, + mark_clarity=1, + mark_freebie=1, + lecturer_id=lecturer.id, + review_status=ReviewStatus.PENDING, + ) + dbsession.add(_comment) + dbsession.commit() + yield _comment + dbsession.refresh(_comment) + dbsession.delete(_comment) + dbsession.commit() + + +@pytest.fixture(scope='function') +def lecturers(dbsession): + """ + Creates 4 lecturers(one with flag is_deleted=True) + """ + lecturers_data = [ + ("test_fname1", "test_lname1", "test_mname1", 9900), + ("test_fname2", "test_lname2", "test_mname2", 9901), + ("Bibka", "Bobka", "Bobkovich", 9902), + ] + + lecturers = [ + Lecturer(first_name=fname, last_name=lname, middle_name=mname, timetable_id=timetable_id) + for fname, lname, mname, timetable_id in lecturers_data + ] + lecturers.append( + Lecturer(first_name='test_fname3', last_name='test_lname3', middle_name='test_mname3', timetable_id=3) + ) + lecturers[-1].is_deleted = True + for lecturer in lecturers: + dbsession.add(lecturer) + dbsession.commit() + yield lecturers + for lecturer in lecturers: + dbsession.refresh(lecturer) + for row in lecturer.comments: + dbsession.delete(row) + lecturer_user_comments = dbsession.query(LecturerUserComment).filter( + LecturerUserComment.lecturer_id == lecturer.id + ) + for row in lecturer_user_comments: + dbsession.delete(row) + dbsession.delete(lecturer) + dbsession.commit() + + +@pytest.fixture +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. + Two of them have alike names. + """ + 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[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[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), + ] + + comments = [ + Comment( + subject=subject, + text="test_comment", + mark_kindness=mark_kindness, + mark_clarity=mark_clarity, + mark_freebie=mark_freebie, + lecturer_id=lecturer_id, + user_id=user_id, + review_status=review_status, + ) + for lecturer_id, user_id, subject, review_status, mark_kindness, mark_clarity, mark_freebie in comments_data + ] + + dbsession.add_all(comments) + dbsession.commit() + yield lecturers, comments + for comment in comments: + dbsession.refresh(comment) + dbsession.delete(comment) + dbsession.commit() diff --git a/tests/test_routes/test_comment.py b/tests/test_routes/test_comment.py index d31c912..44c3462 100644 --- a/tests/test_routes/test_comment.py +++ b/tests/test_routes/test_comment.py @@ -1,6 +1,7 @@ import logging import uuid +import pytest from starlette import status from rating_api.models import Comment, Lecturer, LecturerUserComment, ReviewStatus @@ -13,113 +14,118 @@ settings = get_settings() -def test_create_comment(client, dbsession): - body = {"first_name": 'Иван', "last_name": 'Иванов', "middle_name": 'Иванович', "timetable_id": 0} - lecturer: Lecturer = Lecturer(**body) - dbsession.add(lecturer) - dbsession.commit() - - body = { - "subject": "Физика", - "text": "Хороший препод", - "mark_kindness": 1, - "mark_freebie": -2, - "mark_clarity": 0, - } - params = {"lecturer_id": lecturer.id} - post_response = client.post(url, json=body, params=params) - print(post_response.json()) - assert post_response.status_code == status.HTTP_200_OK - json_response = post_response.json() - comment = Comment.query(session=dbsession).filter(Comment.uuid == json_response["uuid"]).one_or_none() - assert comment is not None - user_comment = ( - LecturerUserComment.query(session=dbsession) - .filter(LecturerUserComment.lecturer_id == lecturer.id) - .one_or_none() - ) - assert user_comment is not None - dbsession.delete(user_comment) - dbsession.delete(comment) - dbsession.delete(lecturer) - dbsession.commit() - post_response = client.post(url, json=body, params=params) - assert post_response.status_code == status.HTTP_404_NOT_FOUND - - -def test_post_bad_mark(client, dbsession): - body = {"first_name": 'Иван', "last_name": 'Иванов', "middle_name": 'Иванович', "timetable_id": 0} - lecturer: Lecturer = Lecturer(**body) - dbsession.add(lecturer) - dbsession.commit() - - body = { - "subject": "Физика", - "text": "Хороший препод", - "mark_kindness": 4, - "mark_freebie": -2, - "mark_clarity": 0, - } - params = {"lecturer_id": lecturer.id} +@pytest.mark.parametrize( + 'body,lecturer_n,response_status', + [ + ( + { + "subject": "test_subject", + "text": "test_text", + "mark_kindness": 1, + "mark_freebie": 0, + "mark_clarity": 0, + }, + 0, + status.HTTP_200_OK, + ), + ( + { + "subject": "test1_subject", + "text": "test_text", + "mark_kindness": -2, + "mark_freebie": -2, + "mark_clarity": -2, + }, + 1, + status.HTTP_200_OK, + ), + ( # bad mark + { + "subject": "test_subject", + "text": "test_text", + "mark_kindness": 5, + "mark_freebie": -2, + "mark_clarity": 0, + }, + 2, + status.HTTP_400_BAD_REQUEST, + ), + ( # deleted lecturer + { + "subject": "test_subject", + "text": "test_text", + "mark_kindness": 1, + "mark_freebie": -2, + "mark_clarity": 0, + }, + 3, + status.HTTP_404_NOT_FOUND, + ), + ], +) +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) - assert post_response.status_code == status.HTTP_400_BAD_REQUEST - dbsession.delete(lecturer) - dbsession.commit() - - -def test_get_comment(client, dbsession): - body = { - "first_name": 'Иван', - "last_name": 'Иванов', - "middle_name": 'Иванович', - "timetable_id": 0, - } - lecturer: Lecturer = Lecturer(**body) - dbsession.add(lecturer) - dbsession.commit() - - body = { - "lecturer_id": lecturer.id, - "subject": "Физика", - "text": "Хороший препод", - "mark_kindness": 1, - "mark_freebie": -2, - "mark_clarity": 0, - "review_status": ReviewStatus.APPROVED, - } - comment: Comment = Comment(**body) - dbsession.add(comment) - dbsession.commit() + 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() + assert comment is not None + user_comment = ( + LecturerUserComment.query(session=dbsession) + .filter(LecturerUserComment.lecturer_id == lecturers[lecturer_n].id) + .one_or_none() + ) + assert user_comment is not None + + +def test_get_comment(client, comment): response_comment = client.get(f'{url}/{comment.uuid}') assert response_comment.status_code == status.HTTP_200_OK random_uuid = uuid.uuid4() response = client.get(f'{url}/{random_uuid}') assert response.status_code == status.HTTP_404_NOT_FOUND - comment = Comment.query(session=dbsession).filter(Comment.uuid == response_comment.json()["uuid"]).one_or_none() - assert comment is not None - dbsession.delete(comment) - dbsession.delete(lecturer) - dbsession.commit() -def test_delete_comment(client, dbsession): - body = {"first_name": 'Иван', "last_name": 'Иванов', "middle_name": 'Иванович', "timetable_id": 0} - lecturer: Lecturer = Lecturer(**body) - dbsession.add(lecturer) - dbsession.commit() - - body = { - "lecturer_id": lecturer.id, - "subject": "Физика", - "text": "Хороший препод", - "mark_kindness": 1, - "mark_freebie": -2, - "mark_clarity": 0, - "review_status": ReviewStatus.APPROVED, - } - comment: Comment = Comment(**body) - dbsession.add(comment) - dbsession.commit() +@pytest.mark.parametrize( + 'lecturer_n,response_status', [(0, status.HTTP_200_OK), (1, status.HTTP_200_OK), (3, status.HTTP_200_OK)] +) +def test_comments_by_lecturer_id(client, lecturers_with_comments, lecturer_n, response_status): + lecturers, comments = lecturers_with_comments + response = client.get(f'{url}', params={"lecturer_id": lecturers[lecturer_n].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 lecturers[lecturer_n].comments + if comment.review_status == ReviewStatus.APPROVED and not comment.is_deleted + ] + ) + + +@pytest.mark.parametrize( + 'review_status, response_status,is_reviewed', + [ + ("approved", status.HTTP_200_OK, True), + ("approved", status.HTTP_200_OK, False), + ("dismissed", status.HTTP_200_OK, True), + ("dismissed", status.HTTP_200_OK, False), + ("wrong_status", status.HTTP_422_UNPROCESSABLE_ENTITY, True), + ("wrong_status", status.HTTP_422_UNPROCESSABLE_ENTITY, False), + ], +) +def test_review_comment(client, dbsession, unreviewed_comment, comment, review_status, response_status, is_reviewed): + commment_to_reivew = comment if is_reviewed else unreviewed_comment + query = {"review_status": review_status} + response = client.patch(f"{url}/{commment_to_reivew.uuid}", params=query) + assert response.status_code == response_status + if response.status_code == status.HTTP_200_OK: + dbsession.refresh(commment_to_reivew) + assert commment_to_reivew.review_status == ReviewStatus(review_status) + + +def test_delete_comment(client, dbsession, comment): response = client.delete(f'{url}/{comment.uuid}') assert response.status_code == status.HTTP_200_OK response = client.get(f'{url}/{comment.uuid}') @@ -127,9 +133,7 @@ def test_delete_comment(client, dbsession): random_uuid = uuid.uuid4() response = client.delete(f'{url}/{random_uuid}') assert response.status_code == status.HTTP_404_NOT_FOUND - comment1 = Comment.query(session=dbsession).filter(Comment.uuid == comment.uuid).one_or_none() - assert comment1 is None - comment.is_deleted = True - dbsession.delete(comment) - dbsession.delete(lecturer) - dbsession.commit() + dbsession.refresh(comment) + assert comment.is_deleted + response = client.get(f'{url}/{comment.uuid}') + assert response.status_code == status.HTTP_404_NOT_FOUND diff --git a/tests/test_routes/test_lecturer.py b/tests/test_routes/test_lecturer.py index 39027d0..81b90cb 100644 --- a/tests/test_routes/test_lecturer.py +++ b/tests/test_routes/test_lecturer.py @@ -1,5 +1,6 @@ import logging +import pytest from starlette import status from rating_api.models import Comment, Lecturer, ReviewStatus @@ -12,196 +13,149 @@ settings = get_settings() -def test_create_lecturer(client, dbsession): +@pytest.mark.parametrize('response_status', [status.HTTP_200_OK, status.HTTP_409_CONFLICT]) +def test_create_lecturer(client, dbsession, response_status): body = {"first_name": 'Иван', "last_name": 'Иванов', "middle_name": 'Иванович', "timetable_id": 0} post_response = client.post(url, json=body) - assert post_response.status_code == status.HTTP_200_OK - check_same_response = client.post(url, json=body) - assert check_same_response.status_code == status.HTTP_409_CONFLICT - lecturer = dbsession.query(Lecturer).filter(Lecturer.timetable_id == 0).one_or_none() - assert lecturer is not None - dbsession.delete(lecturer) - dbsession.commit() - lecturer = dbsession.query(Lecturer).filter(Lecturer.timetable_id == 0).one_or_none() - assert lecturer is None - - -def test_get_lecturer(client, dbsession): - body = {"first_name": 'Иван', "last_name": 'Иванов', "middle_name": 'Иванович', "timetable_id": 0} - lecturer: Lecturer = Lecturer(**body) - dbsession.add(lecturer) - dbsession.commit() - db_lecturer: Lecturer = Lecturer.query(session=dbsession).filter(Lecturer.timetable_id == 0).one_or_none() - assert db_lecturer is not None - get_response = client.get(f'{url}/{db_lecturer.id}') - print(get_response.json()) - assert get_response.status_code == status.HTTP_200_OK - json_response = get_response.json() - assert json_response["mark_kindness"] is None - assert json_response["mark_freebie"] is None - assert json_response["mark_clarity"] is None - assert json_response["mark_general"] is None - assert json_response["comments"] is None - dbsession.delete(lecturer) - dbsession.commit() - - -def test_get_lecturer_with_comments(client, dbsession): - body = {"first_name": 'Иван', "last_name": 'Иванов', "middle_name": 'Иванович', "timetable_id": 0} - lecturer: Lecturer = Lecturer(**body) - dbsession.add(lecturer) - dbsession.commit() - db_lecturer: Lecturer = Lecturer.query(session=dbsession).filter(Lecturer.timetable_id == 0).one_or_none() - assert db_lecturer is not None - - comment1: dict = { - "subject": "Физика", - "text": "Хороший преподаватель", - "mark_kindness": 2, - "mark_freebie": 0, - "mark_clarity": 2, - "lecturer_id": db_lecturer.id, - "review_status": ReviewStatus.APPROVED, - } - comment2: dict = { - "subject": "Физика", - "text": "Средне", - "mark_kindness": -1, - "mark_freebie": 1, - "mark_clarity": -1, - "lecturer_id": db_lecturer.id, - "review_status": ReviewStatus.APPROVED, - } - comment3: dict = { - "subject": "Физика", - "text": "Средне", - "mark_kindness": 2, - "mark_freebie": 2, - "mark_clarity": 2, - "lecturer_id": db_lecturer.id, - "review_status": ReviewStatus.PENDING, - } - comment1: Comment = Comment.create(session=dbsession, **comment1) - comment2: Comment = Comment.create(session=dbsession, **comment2) - comment3: Comment = Comment.create(session=dbsession, **comment3) - dbsession.commit() - assert comment1 is not None - assert comment2 is not None - assert comment3 is not None - query = { - "info": ['comments', 'mark'], - } - response = client.get(f'{url}/{db_lecturer.id}', params=query) - print(response.json()) - assert response.status_code == status.HTTP_200_OK - json_response = response.json() - assert json_response["mark_kindness"] == 0.5 - assert json_response["mark_freebie"] == 0.5 - assert json_response["mark_clarity"] == 0.5 - assert json_response["mark_general"] == 0.5 - assert "Физика" in json_response["subjects"] - assert len(json_response["comments"]) != 0 - dbsession.delete(comment1) - dbsession.delete(comment2) - dbsession.delete(comment3) - dbsession.delete(lecturer) - dbsession.commit() - - -def test_get_lecturers_by_name(client, dbsession): - body_list = [ - {"first_name": 'Алиса', "last_name": 'Селезнёва', "middle_name": 'Ивановна', "timetable_id": 0}, - {"first_name": 'Марат', "last_name": 'Сельков', "middle_name": 'Анатольевич', "timetable_id": 1}, - {"first_name": 'М.', "last_name": 'Измайлов', "middle_name": 'Р.', "timetable_id": 2}, - {"first_name": 'Михаил', "last_name": 'Измайлов', "middle_name": 'Ильич', "timetable_id": 3}, - ] - lecturer_list: list[Lecturer] = [Lecturer(**body_list[i]) for i in range(4)] - for lecturer in lecturer_list: - dbsession.add(lecturer) - dbsession.commit() - db_lecturer: Lecturer = Lecturer.query(session=dbsession).filter(Lecturer.timetable_id == 0).one_or_none() - assert db_lecturer is not None - query = {"name": "Селезнёва"} - get_response = client.get(f'{url}', params=query) - assert get_response.status_code == status.HTTP_200_OK - json_response = get_response.json() - assert json_response["total"] == 1 - assert json_response["lecturers"][0]["first_name"] == "Алиса" - - query = {"name": "Сел"} - get_response = client.get(f'{url}', params=query) - assert get_response.status_code == status.HTTP_200_OK - json_response = get_response.json() - assert json_response["total"] == 2 - assert json_response["lecturers"][0]["first_name"] == "Алиса" - assert json_response["lecturers"][1]["first_name"] == "Марат" - - query = {"name": "Измайлова"} - get_response = client.get(f'{url}', params=query) - assert get_response.status_code == status.HTTP_404_NOT_FOUND - - for lecturer in lecturer_list: + assert post_response.status_code == response_status + # cleanup on a last run + if response_status == status.HTTP_409_CONFLICT: + lecturer = dbsession.query(Lecturer).filter(Lecturer.timetable_id == 0).one_or_none() + assert lecturer is not None dbsession.delete(lecturer) - dbsession.commit() - - -def test_update_lecturer(client, dbsession): - body = {"first_name": 'Иван', "last_name": 'Иванов', "middle_name": 'Иванович', "timetable_id": 0} - lecturer: Lecturer = Lecturer(**body) - dbsession.add(lecturer) - dbsession.commit() - body = { - "first_name": 'Алексей', - "last_name": 'Алексеев', - "middle_name": 'Алексеевич', - } - db_lecturer: Lecturer = Lecturer.query(session=dbsession).filter(Lecturer.timetable_id == 0).one_or_none() - assert db_lecturer is not None - response = client.patch(f"{url}/{db_lecturer.id}", json=body) + dbsession.commit() + lecturer = dbsession.query(Lecturer).filter(Lecturer.timetable_id == 0).one_or_none() + assert lecturer is None + + +@pytest.mark.parametrize( + 'lecturer_n,response_status', + [ + (0, status.HTTP_200_OK), + (1, status.HTTP_200_OK), + (2, status.HTTP_200_OK), + (3, status.HTTP_404_NOT_FOUND), + ], +) +def test_get_lecturer(client, dbsession, lecturers, lecturer_n, response_status): + lecturer = ( + dbsession.query(Lecturer).filter(Lecturer.timetable_id == lecturers[lecturer_n].timetable_id).one_or_none() + ) + # check non-existing id request + lecturer_id = -1 + if lecturer: + lecturer_id = lecturer.id + get_response = client.get(f'{url}/{lecturer_id}') + assert get_response.status_code == response_status + if response_status == status.HTTP_200_OK: + json_response = get_response.json() + assert json_response["mark_kindness"] is None + assert json_response["mark_freebie"] is None + assert json_response["mark_clarity"] is None + assert json_response["mark_general"] is None + assert json_response["comments"] is None + + +@pytest.mark.parametrize( + 'lecturer_n,mark_kindness,mark_freebie,mark_clarity,mark_general', + [(0, 1.5, 1.5, 1.5, 1.5), (1, 0, 0, 0, 0), (2, 0.5, 0.5, 0.5, 0.5)], +) +def test_get_lecturer_with_comments( + client, lecturers_with_comments, lecturer_n, mark_kindness, mark_freebie, mark_clarity, mark_general +): + lecturers, comments = lecturers_with_comments + query = {"info": ['comments', 'mark']} + response = client.get(f'{url}/{lecturers[lecturer_n].id}', params=query) assert response.status_code == status.HTTP_200_OK json_response = response.json() - assert json_response["first_name"] == 'Алексей' - assert json_response["last_name"] == 'Алексеев' - assert json_response["middle_name"] == "Алексеевич" - body = { - "first_name": 'Иван', - "last_name": 'Иванов', - "middle_name": 'Иванович', - } - response = client.patch(f"{url}/{db_lecturer.id}", json=body) + assert json_response["mark_kindness"] == mark_kindness + 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 + + +@pytest.mark.parametrize( + 'query,total,response_status', + [ + ({'name': 'test_lname1'}, 1, status.HTTP_200_OK), + ({'name': 'TeSt_LnAmE1'}, 1, status.HTTP_200_OK), + ({'name': 'test'}, 2, status.HTTP_200_OK), + ({'name': 'testlname123'}, 0, status.HTTP_404_NOT_FOUND), + ], +) +def test_get_lecturers_by_name(client, lecturers, query, total, response_status): + get_response = client.get(f'{url}', params=query) + assert get_response.status_code == response_status + if response_status == status.HTTP_200_OK: + json_response = get_response.json() + json_response["total"] == total + assert json_response["lecturers"][0]["first_name"] == lecturers[0].first_name + + +@pytest.mark.parametrize( + 'body,response_status', + [ + ( + { + "first_name": 'Test', + "last_name": 'Testov', + "middle_name": 'Testovich', + }, + status.HTTP_200_OK, + ), + ( + { + "first_name": 'Testa', + "last_name": 'Testova', + "middle_name": 'Testovna', + }, + status.HTTP_200_OK, + ), + ( + { + "first_name": 'Test', + "last_name": 'Testov', + "middle_name": 1, + }, + status.HTTP_422_UNPROCESSABLE_ENTITY, + ), + ], +) +def test_update_lecturer(client, lecturer, body, response_status): + response = client.get(f'{url}/{lecturer.id}') assert response.status_code == status.HTTP_200_OK json_response = response.json() - assert json_response["first_name"] == 'Иван' - assert json_response["last_name"] == 'Иванов' - assert json_response["middle_name"] == "Иванович" - dbsession.delete(lecturer) - dbsession.commit() - - -def test_delete_lecturer(client, dbsession): - body = {"first_name": 'Иван', "last_name": 'Иванов', "middle_name": 'Иванович', "timetable_id": 0} - lecturer: Lecturer = Lecturer(**body) - dbsession.add(lecturer) - dbsession.commit() - comment: dict = { - "subject": "Физика", - "text": "Хороший преподаватель", - "mark_kindness": 2, - "mark_freebie": 0, - "mark_clarity": 2, - "lecturer_id": lecturer.id, - "review_status": ReviewStatus.APPROVED, - } - comment: Comment = Comment.create(session=dbsession, **comment) - dbsession.commit() - lecturer = dbsession.query(Lecturer).filter(Lecturer.timetable_id == 0).one_or_none() - assert lecturer is not None - response = client.delete(f"{url}/{lecturer.id}") + assert json_response['first_name'] == lecturer.first_name + assert json_response['last_name'] == lecturer.last_name + assert json_response['middle_name'] == lecturer.middle_name + response = client.patch(f"{url}/{lecturer.id}", json=body) + assert response.status_code == response_status + if response_status == status.HTTP_200_OK: + json_response = response.json() + assert json_response["first_name"] == body["first_name"] + assert json_response["last_name"] == body["last_name"] + assert json_response["middle_name"] == body["middle_name"] + + +def test_delete_lecturer(client, dbsession, lecturers_with_comments): + lecturers, comments = lecturers_with_comments + response = client.delete(f"{url}/{lecturers[0].id}") assert response.status_code == status.HTTP_200_OK - response = client.delete(f"{url}/{lecturer.id}") + # trying to delete deleted + response = client.delete(f"{url}/{lecturers[0].id}") + assert response.status_code == status.HTTP_404_NOT_FOUND + dbsession.refresh(comments[0]) + dbsession.refresh(comments[1]) + dbsession.refresh(comments[2]) + dbsession.refresh(lecturers[0]) + assert comments[0].is_deleted + assert comments[1].is_deleted + assert comments[2].is_deleted + assert lecturers[0].is_deleted + # trying to get deleted + response = client.get(f'{url}/{lecturers[0].id}') assert response.status_code == status.HTTP_404_NOT_FOUND - lecturer.is_deleted = True - comment.is_deleted = True - dbsession.delete(comment) - dbsession.delete(lecturer) - dbsession.commit()