146 lines
4.6 KiB
Python
146 lines
4.6 KiB
Python
"""Tests for domain entities."""
|
|
|
|
from typing import Any
|
|
from uuid import UUID
|
|
|
|
from app.domain.entities import Post
|
|
from app.domain.value_objects import Content, Title
|
|
|
|
|
|
class TestPost:
|
|
def test_post_creation(self) -> None:
|
|
"""Test creating a post."""
|
|
post = Post.create(
|
|
title_str="Test Title",
|
|
content_str="This is test content that is long enough",
|
|
author_id="user-123",
|
|
tags=["test", "python"],
|
|
)
|
|
|
|
assert isinstance(post.id, UUID)
|
|
assert post.title.value == "Test Title"
|
|
assert post.content.value == "This is test content that is long enough"
|
|
assert post.slug.value == "test-title"
|
|
assert post.author_id == "user-123"
|
|
assert post.published is False
|
|
assert post.tags == ["test", "python"]
|
|
|
|
def test_post_publish(self) -> None:
|
|
"""Test publishing a post."""
|
|
post = Post.create(
|
|
title_str="Test Title",
|
|
content_str="This is test content that is long enough",
|
|
author_id="user-123",
|
|
)
|
|
|
|
assert post.published is False
|
|
post.publish()
|
|
assert post.published is True
|
|
|
|
def test_post_unpublish(self) -> None:
|
|
"""Test unpublishing a post."""
|
|
post = Post.create(
|
|
title_str="Test Title",
|
|
content_str="This is test content that is long enough",
|
|
author_id="user-123",
|
|
)
|
|
|
|
post.publish()
|
|
assert post.published is True
|
|
post.unpublish()
|
|
assert post.published is False
|
|
|
|
def test_post_update_title(self) -> None:
|
|
"""Test updating post title."""
|
|
post = Post.create(
|
|
title_str="Original Title",
|
|
content_str="This is test content that is long enough",
|
|
author_id="user-123",
|
|
)
|
|
|
|
old_updated_at = post.updated_at
|
|
post.update_title(Title("New Title"))
|
|
|
|
assert post.title.value == "New Title"
|
|
assert post.slug.value == "new-title"
|
|
assert post.updated_at > old_updated_at
|
|
|
|
def test_post_update_content(self) -> None:
|
|
"""Test updating post content."""
|
|
post = Post.create(
|
|
title_str="Test Title",
|
|
content_str="This is test content that is long enough",
|
|
author_id="user-123",
|
|
)
|
|
|
|
old_updated_at = post.updated_at
|
|
post.update_content(Content("Updated content that is also long enough"))
|
|
|
|
assert post.content.value == "Updated content that is also long enough"
|
|
assert post.updated_at > old_updated_at
|
|
|
|
def test_post_add_tag(self) -> None:
|
|
"""Test adding a tag."""
|
|
post = Post.create(
|
|
title_str="Test Title",
|
|
content_str="This is test content that is long enough",
|
|
author_id="user-123",
|
|
)
|
|
|
|
post.add_tag("python")
|
|
assert "python" in post.tags
|
|
|
|
# Adding same tag twice should not duplicate
|
|
post.add_tag("python")
|
|
assert post.tags.count("python") == 1
|
|
|
|
def test_post_remove_tag(self) -> None:
|
|
"""Test removing a tag."""
|
|
post = Post.create(
|
|
title_str="Test Title",
|
|
content_str="This is test content that is long enough",
|
|
author_id="user-123",
|
|
tags=["python", "fastapi"],
|
|
)
|
|
|
|
post.remove_tag("python")
|
|
assert "python" not in post.tags
|
|
assert "fastapi" in post.tags
|
|
|
|
def test_post_to_dict(self) -> None:
|
|
"""Test converting post to dict."""
|
|
post = Post.create(
|
|
title_str="Test Title",
|
|
content_str="This is test content that is long enough",
|
|
author_id="user-123",
|
|
tags=["test"],
|
|
)
|
|
|
|
data = post.to_dict()
|
|
|
|
assert data["title"] == "Test Title"
|
|
assert data["content"] == "This is test content that is long enough"
|
|
assert data["slug"] == "test-title"
|
|
assert data["author_id"] == "user-123"
|
|
assert data["published"] is False
|
|
assert data["tags"] == ["test"]
|
|
assert "id" in data
|
|
assert "created_at" in data
|
|
assert "updated_at" in data
|
|
|
|
def test_base_entity_eq_and_hash(self) -> None:
|
|
"""Test BaseEntity equality and hash directly."""
|
|
from app.domain.entities.base import BaseEntity
|
|
|
|
class ConcreteEntity(BaseEntity):
|
|
def to_dict(self) -> dict[str, Any]:
|
|
return {}
|
|
|
|
e1 = ConcreteEntity()
|
|
e2 = ConcreteEntity()
|
|
e2.id = e1.id
|
|
|
|
assert BaseEntity.__eq__(e1, e2) is True
|
|
assert BaseEntity.__eq__(e1, "not an entity") == NotImplemented
|
|
assert BaseEntity.__hash__(e1) == hash(e1.id)
|