feat: add like/unlike toggle on blog posts with per-user tracking

- PostLike domain entity (post_id, liked_by) with BaseEntity integration
- Post entity: add like_count field (default 0) and to_dict serialization
- PostRepository interface: add get_like, add_like, remove_like methods
- TogglePostLikeUseCase: toggle logic (like → unlike, unlike → like)
- PostResponseDTO/PostResponseSchema: add like_count field
- PostLikeORM model with FK to posts and cascade delete
- SQLAlchemyPostRepository: implement like query/add/remove with ORM mapping
- DI provider registration for TogglePostLikeUseCase
- API endpoint POST /api/v1/posts/{id}/like (auth required)
- Unit tests: PostLike entity, Post.like_count, TogglePostLikeUseCase (7 tests)
- API tests: POST /api/v1/posts/{id}/like (4 tests)
- Test model files: FEATURE_LIKES.md, TEST_MODEL.md updated
This commit is contained in:
2026-05-10 18:24:09 +03:00
parent 4497f452a1
commit 3cf6c94da2
21 changed files with 876 additions and 6 deletions

View File

@@ -0,0 +1,50 @@
"""Tests for PostLike domain entity.
This module tests the PostLike entity creation, attributes,
and BaseEntity integration.
"""
from uuid import UUID
from app.domain.entities.like import PostLike
class TestPostLikeEntity:
"""Tests for the PostLike domain entity.
Covers TC-UNIT-826: PostLike entity valid creation.
"""
def test_post_like_creation(self) -> None:
"""Test creating a PostLike with valid attributes.
TC-UNIT-826: Positive — create PostLike instance.
Expected:
- post_id matches input
- liked_by matches input
- id is a valid UUID
- created_at is set
"""
post_id = UUID("00000000-0000-0000-0000-000000000001")
liked_by = "user-123"
like = PostLike(post_id=post_id, liked_by=liked_by)
assert like.post_id == post_id
assert like.liked_by == liked_by
assert isinstance(like.id, UUID)
assert like.created_at is not None
def test_post_like_to_dict(self) -> None:
"""Test PostLike to_dict serialization."""
post_id = UUID("00000000-0000-0000-0000-000000000001")
liked_by = "device-abc-123"
like = PostLike(post_id=post_id, liked_by=liked_by)
data = like.to_dict()
assert data["post_id"] == str(post_id)
assert data["liked_by"] == liked_by
assert "id" in data
assert "created_at" in data