refactor: migrate to DDD architecture with Dishka DI

Complete architectural refactoring from simple MVC to Clean Architecture/DDD pattern:

Domain Layer:

- Add entities (Post, BaseEntity) with business logic

- Add value objects (Title, Content, Slug) with validation

- Add repository interfaces (PostRepository)

- Add domain exceptions

Application Layer:

- Add use cases (CreatePost, GetPost, UpdatePost, DeletePost, ListPosts, PublishPost)

- Add DTOs for data transfer

- Add TransactionManager interface

Infrastructure Layer:

- Add SQLAlchemy models and async database connection

- Add SQLAlchemyPostRepository implementation

- Add Dishka DI container with providers

- Add error handlers and middleware

Presentation Layer:

- Add FastAPI routes with Dishka integration

- Add Pydantic schemas

- Add dependency injection using FromDishka[T]

Other Changes:

- Remove old flat structure (api/, common/, core/, modules/)

- Add hatchling build system for package scripts

- Add blog CLI command

- Update AGENTS.md with new architecture docs

- All 48 tests passing, mypy clean, ruff clean
This commit is contained in:
2026-05-01 20:20:41 +03:00
parent b8334efa5a
commit 87b094220d
75 changed files with 2783 additions and 459 deletions

View File

View File

@@ -0,0 +1,128 @@
"""Tests for domain entities."""
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

View File

@@ -0,0 +1,48 @@
"""Tests for domain exceptions."""
from app.domain.exceptions import (
AlreadyExistsException,
DomainException,
ForbiddenException,
NotFoundException,
UnauthorizedException,
ValidationException,
)
class TestDomainExceptions:
def test_base_exception(self) -> None:
"""Test base domain exception."""
exc = DomainException("Something went wrong")
assert exc.message == "Something went wrong"
assert str(exc) == "Something went wrong"
def test_validation_exception(self) -> None:
"""Test validation exception."""
exc = ValidationException("Invalid input")
assert isinstance(exc, DomainException)
assert exc.message == "Invalid input"
def test_not_found_exception(self) -> None:
"""Test not found exception."""
exc = NotFoundException("Resource not found")
assert isinstance(exc, DomainException)
assert exc.message == "Resource not found"
def test_already_exists_exception(self) -> None:
"""Test already exists exception."""
exc = AlreadyExistsException("Already exists")
assert isinstance(exc, DomainException)
assert exc.message == "Already exists"
def test_unauthorized_exception(self) -> None:
"""Test unauthorized exception."""
exc = UnauthorizedException("Unauthorized")
assert isinstance(exc, DomainException)
assert exc.message == "Unauthorized"
def test_forbidden_exception(self) -> None:
"""Test forbidden exception."""
exc = ForbiddenException("Forbidden")
assert isinstance(exc, DomainException)
assert exc.message == "Forbidden"

View File

@@ -0,0 +1,93 @@
"""Tests for domain value objects."""
import pytest
from app.domain.value_objects import Content, Slug, Title
class TestTitle:
def test_valid_title(self) -> None:
"""Test creating a valid title."""
title = Title("Valid Title")
assert title.value == "Valid Title"
def test_title_too_short(self) -> None:
"""Test title that is too short."""
with pytest.raises(ValueError, match="at least"):
Title("ab")
def test_title_too_long(self) -> None:
"""Test title that is too long."""
with pytest.raises(ValueError, match="at most"):
Title("a" * 201)
def test_title_empty(self) -> None:
"""Test empty title."""
with pytest.raises(ValueError, match="empty"):
Title(" ")
def test_title_not_string(self) -> None:
"""Test non-string title."""
with pytest.raises(ValueError, match="string"):
Title(123) # type: ignore[arg-type]
class TestContent:
def test_valid_content(self) -> None:
"""Test creating valid content."""
content = Content("This is valid content with enough characters")
assert content.value == "This is valid content with enough characters"
def test_content_too_short(self) -> None:
"""Test content that is too short."""
with pytest.raises(ValueError, match="at least"):
Content("short")
def test_content_too_long(self) -> None:
"""Test content that is too long."""
with pytest.raises(ValueError, match="at most"):
Content("a" * 50001)
def test_content_empty(self) -> None:
"""Test empty content."""
with pytest.raises(ValueError, match="empty"):
Content(" ")
class TestSlug:
def test_valid_slug(self) -> None:
"""Test creating a valid slug."""
slug = Slug("valid-slug")
assert slug.value == "valid-slug"
def test_slug_from_title(self) -> None:
"""Test generating slug from title."""
slug = Slug.from_title("Hello World Post")
assert slug.value == "hello-world-post"
def test_slug_from_title_with_special_chars(self) -> None:
"""Test generating slug from title with special characters."""
slug = Slug.from_title("Hello, World! Post @#$%")
assert slug.value == "hello-world-post"
def test_slug_from_title_only_special_chars(self) -> None:
"""Test generating slug from title with only special characters."""
slug = Slug.from_title("!@#$%")
assert slug.value == "post"
def test_slug_invalid_chars(self) -> None:
"""Test slug with invalid characters."""
with pytest.raises(ValueError, match="lowercase"):
Slug("Invalid_Slug")
def test_slug_uppercase(self) -> None:
"""Test slug with uppercase letters."""
with pytest.raises(ValueError, match="lowercase"):
Slug("Uppercase-Slug")
def test_slug_equality(self) -> None:
"""Test slug value equality."""
slug1 = Slug("test-slug")
slug2 = Slug("test-slug")
assert slug1 == slug2
assert hash(slug1) == hash(slug2)