Skip to content

Commit

Permalink
Merge pull request #49 from profcomff/tests
Browse files Browse the repository at this point in the history
fixtures+ tests
  • Loading branch information
parfenovma authored Nov 16, 2024
2 parents c81b676 + 6576929 commit 6d34a33
Show file tree
Hide file tree
Showing 3 changed files with 380 additions and 290 deletions.
134 changes: 133 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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()
214 changes: 109 additions & 105 deletions tests/test_routes/test_comment.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import logging
import uuid

import pytest
from starlette import status

from rating_api.models import Comment, Lecturer, LecturerUserComment, ReviewStatus
Expand All @@ -13,123 +14,126 @@
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}')
assert response.status_code == status.HTTP_404_NOT_FOUND
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
Loading

0 comments on commit 6d34a33

Please sign in to comment.