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

Merge #54

Merged
merged 12 commits into from
Nov 18, 2024
8 changes: 7 additions & 1 deletion rating_api/models/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

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