test: add unit tests for roles, web deps, use cases, VO boundaries — reach 70% coverage
Some checks failed
ci/woodpecker/pr/pipeline Pipeline failed

This commit is contained in:
2026-05-09 12:57:25 +03:00
parent 2b8a5676bd
commit 79f4d9caf5
12 changed files with 466 additions and 8 deletions

View File

@@ -126,6 +126,37 @@ class TestGetPostUseCase:
with pytest.raises(NotFoundException):
await use_case.by_id(post_id)
@pytest.mark.asyncio
async def test_get_post_by_slug_success(
self,
mock_post_repository: Mock,
mock_transaction_manager: Mock,
test_post: Post,
) -> None:
"""Test successful get post by slug."""
mock_post_repository.get_by_slug = AsyncMock(return_value=test_post)
use_case = GetPostUseCase(mock_post_repository, mock_transaction_manager)
result = await use_case.by_slug(test_post.slug.value)
assert result.id == test_post.id
assert result.title == test_post.title.value
mock_post_repository.get_by_slug.assert_called_once_with(test_post.slug.value)
@pytest.mark.asyncio
async def test_get_post_by_slug_not_found(
self,
mock_post_repository: Mock,
mock_transaction_manager: Mock,
) -> None:
"""Test get post by slug when not found."""
mock_post_repository.get_by_slug = AsyncMock(return_value=None)
use_case = GetPostUseCase(mock_post_repository, mock_transaction_manager)
with pytest.raises(NotFoundException):
await use_case.by_slug("nonexistent-slug")
class TestUpdatePostUseCase:
@pytest.mark.asyncio
@@ -170,6 +201,46 @@ class TestUpdatePostUseCase:
mock_post_repository.update.assert_called_once()
mock_transaction_manager.commit.assert_called_once()
@pytest.mark.asyncio
async def test_update_post_not_found(
self,
mock_post_repository: Mock,
mock_transaction_manager: Mock,
) -> None:
"""Test update post when post does not exist."""
mock_post_repository.get_by_id = AsyncMock(return_value=None)
use_case = UpdatePostUseCase(mock_post_repository, mock_transaction_manager)
dto = UpdatePostDTO(title="New Title")
post_id = uuid4()
with pytest.raises(NotFoundException):
await use_case.execute(post_id, dto, "user-123")
@pytest.mark.asyncio
async def test_update_post_with_content_and_tags(
self,
mock_post_repository: Mock,
mock_transaction_manager: Mock,
test_post: Post,
) -> None:
"""Test update post content and tags."""
mock_post_repository.get_by_id = AsyncMock(return_value=test_post)
mock_post_repository.update = AsyncMock()
use_case = UpdatePostUseCase(mock_post_repository, mock_transaction_manager)
dto = UpdatePostDTO(
content="Updated content with enough characters for validation",
tags=["new", "tags"],
)
result = await use_case.execute(test_post.id, dto, "user-123")
assert result.content == dto.content
assert result.tags == dto.tags
mock_post_repository.update.assert_called_once()
mock_transaction_manager.commit.assert_called_once()
class TestDeletePostUseCase:
@pytest.mark.asyncio
@@ -231,6 +302,21 @@ class TestDeletePostUseCase:
mock_post_repository.delete.assert_called_once_with(test_post.id)
mock_transaction_manager.commit.assert_called_once()
@pytest.mark.asyncio
async def test_delete_post_not_found(
self,
mock_post_repository: Mock,
mock_transaction_manager: Mock,
) -> None:
"""Test delete post when post does not exist."""
mock_post_repository.get_by_id = AsyncMock(return_value=None)
use_case = DeletePostUseCase(mock_post_repository, mock_transaction_manager)
post_id = uuid4()
with pytest.raises(NotFoundException):
await use_case.execute(post_id, "user-123")
class TestPublishPostUseCase:
@pytest.mark.asyncio