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

Soft deletes final fix + tests #41

Merged
merged 6 commits into from
Nov 10, 2024
Merged
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
31 changes: 31 additions & 0 deletions migrations/versions/edcc1a448ffb_soft_deletes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
"""soft-deletes

Revision ID: edcc1a448ffb
Revises: 7388a2c219d2
Create Date: 2024-11-10 09:23:32.307828

"""

import sqlalchemy as sa
from alembic import op


# revision identifiers, used by Alembic.
revision = 'edcc1a448ffb'
down_revision = '7388a2c219d2'
branch_labels = None
depends_on = None


def upgrade():
op.add_column('comment', sa.Column('is_deleted', sa.Boolean(), nullable=False, server_default=sa.false()))
op.add_column('lecturer', sa.Column('is_deleted', sa.Boolean(), nullable=False, server_default=sa.false()))
op.add_column(
'lecturer_user_comment', sa.Column('is_deleted', sa.Boolean(), nullable=False, server_default=sa.false())
)


def downgrade():
op.drop_column('lecturer_user_comment', 'is_deleted')
op.drop_column('lecturer', 'is_deleted')
op.drop_column('comment', 'is_deleted')
11 changes: 9 additions & 2 deletions rating_api/models/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import uuid
from enum import Enum

from sqlalchemy import UUID, DateTime
from sqlalchemy import UUID, Boolean, DateTime
from sqlalchemy import Enum as DbEnum
from sqlalchemy import ForeignKey, Integer, String, and_, func, or_, true
from sqlalchemy.ext.hybrid import hybrid_method
Expand Down Expand Up @@ -34,6 +34,7 @@ class Lecturer(BaseDbModel):
avatar_link: Mapped[str] = mapped_column(String, nullable=True)
timetable_id: Mapped[int] = mapped_column(Integer, unique=True, nullable=False)
comments: Mapped[list[Comment]] = relationship("Comment", back_populates="lecturer")
is_deleted: Mapped[bool] = mapped_column(Boolean, nullable=False, default=False)

@hybrid_method
def search_by_name(self, query: str) -> bool:
Expand Down Expand Up @@ -65,8 +66,13 @@ class Comment(BaseDbModel):
mark_freebie: Mapped[int] = mapped_column(Integer, nullable=False)
mark_clarity: Mapped[int] = mapped_column(Integer, nullable=False)
lecturer_id: Mapped[int] = mapped_column(Integer, ForeignKey("lecturer.id"))
lecturer: Mapped[Lecturer] = relationship("Lecturer", back_populates="comments")
lecturer: Mapped[Lecturer] = relationship(
"Lecturer",
back_populates="comments",
primaryjoin="and_(Comment.lecturer_id == Lecturer.id, not_(Lecturer.is_deleted))",
)
review_status: Mapped[ReviewStatus] = mapped_column(DbEnum(ReviewStatus, native_enum=False), nullable=False)
is_deleted: Mapped[bool] = mapped_column(Boolean, nullable=False, default=False)


class LecturerUserComment(BaseDbModel):
Expand All @@ -75,3 +81,4 @@ class LecturerUserComment(BaseDbModel):
lecturer_id: Mapped[int] = mapped_column(Integer, ForeignKey("lecturer.id"))
create_ts: Mapped[datetime.datetime] = mapped_column(DateTime, default=datetime.datetime.utcnow, nullable=False)
update_ts: Mapped[datetime.datetime] = mapped_column(DateTime, default=datetime.datetime.utcnow, nullable=False)
is_deleted: Mapped[bool] = mapped_column(Boolean, nullable=False, default=False)
1 change: 0 additions & 1 deletion rating_api/schemas/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ class CommentGet(Base):
lecturer_id: int



class CommentPost(Base):
subject: str
text: str
Expand Down
8 changes: 6 additions & 2 deletions tests/test_routes/test_comment.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,14 @@ def test_delete_comment(client, dbsession):
dbsession.commit()
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
comment = Comment.query(session=dbsession).filter(Comment.uuid == comment.uuid).one_or_none()
assert comment is None
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()
5 changes: 5 additions & 0 deletions tests/test_routes/test_lecturer.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,3 +200,8 @@ def test_delete_lecturer(client, dbsession):
assert response.status_code == status.HTTP_200_OK
response = client.delete(f"{url}/{lecturer.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()
Loading