import pytest from uuid import uuid4 from unittest.mock import AsyncMock try: from backend.src.application.use_cases.document_use_cases import DocumentUseCases from backend.src.shared.exceptions import NotFoundError, ForbiddenError from backend.src.application.services.document_parser_service import DocumentParserService except ImportError: from src.application.use_cases.document_use_cases import DocumentUseCases from src.shared.exceptions import NotFoundError, ForbiddenError from src.application.services.document_parser_service import DocumentParserService class TestDocumentUseCases: @pytest.fixture def document_use_cases(self, mock_document_repository, mock_collection_repository): mock_parser = AsyncMock() mock_parser.parse_pdf = AsyncMock(return_value=("Парсенный документ", "Содержание")) return DocumentUseCases( document_repository=mock_document_repository, collection_repository=mock_collection_repository, parser_service=mock_parser ) @pytest.mark.asyncio async def test_create_document_success(self, document_use_cases, mock_collection, mock_document_repository, mock_collection_repository): collection_id = uuid4() mock_collection_repository.get_by_id = AsyncMock(return_value=mock_collection) mock_document_repository.create = AsyncMock(return_value=mock_collection) result = await document_use_cases.create_document( collection_id=collection_id, title="Тестовый документ", content="Содержание", metadata={"type": "law"} ) assert result is not None mock_collection_repository.get_by_id.assert_called_once_with(collection_id) mock_document_repository.create.assert_called_once() @pytest.mark.asyncio async def test_create_document_collection_not_found(self, document_use_cases, mock_collection_repository): collection_id = uuid4() mock_collection_repository.get_by_id = AsyncMock(return_value=None) with pytest.raises(NotFoundError): await document_use_cases.create_document( collection_id=collection_id, title="Документ", content="Содержание" ) @pytest.mark.asyncio async def test_get_document_success(self, document_use_cases, mock_document, mock_document_repository): document_id = uuid4() mock_document_repository.get_by_id = AsyncMock(return_value=mock_document) result = await document_use_cases.get_document(document_id) assert result == mock_document mock_document_repository.get_by_id.assert_called_once_with(document_id) @pytest.mark.asyncio async def test_get_document_not_found(self, document_use_cases, mock_document_repository): document_id = uuid4() mock_document_repository.get_by_id = AsyncMock(return_value=None) with pytest.raises(NotFoundError): await document_use_cases.get_document(document_id) @pytest.mark.asyncio async def test_update_document_success(self, document_use_cases, mock_document, mock_collection, mock_document_repository, mock_collection_repository): document_id = uuid4() user_id = uuid4() mock_document.collection_id = uuid4() mock_collection.owner_id = user_id mock_document_repository.get_by_id = AsyncMock(return_value=mock_document) mock_collection_repository.get_by_id = AsyncMock(return_value=mock_collection) mock_document_repository.update = AsyncMock(return_value=mock_document) result = await document_use_cases.update_document( document_id=document_id, user_id=user_id, title="Обновленное название" ) assert result is not None assert mock_document.title == "Обновленное название" @pytest.mark.asyncio async def test_update_document_forbidden(self, document_use_cases, mock_document, mock_collection, mock_document_repository, mock_collection_repository): document_id = uuid4() user_id = uuid4() owner_id = uuid4() mock_document.collection_id = uuid4() mock_collection.owner_id = owner_id mock_document_repository.get_by_id = AsyncMock(return_value=mock_document) mock_collection_repository.get_by_id = AsyncMock(return_value=mock_collection) with pytest.raises(ForbiddenError): await document_use_cases.update_document( document_id=document_id, user_id=user_id, title="Название" ) @pytest.mark.asyncio async def test_delete_document_success(self, document_use_cases, mock_document, mock_collection, mock_document_repository, mock_collection_repository): document_id = uuid4() user_id = uuid4() mock_document.collection_id = uuid4() mock_collection.owner_id = user_id mock_document_repository.get_by_id = AsyncMock(return_value=mock_document) mock_collection_repository.get_by_id = AsyncMock(return_value=mock_collection) mock_document_repository.delete = AsyncMock(return_value=True) result = await document_use_cases.delete_document(document_id, user_id) assert result is True mock_document_repository.delete.assert_called_once_with(document_id) @pytest.mark.asyncio async def test_list_collection_documents(self, document_use_cases, mock_collection, mock_documents_list, mock_collection_repository, mock_document_repository): collection_id = uuid4() mock_collection_repository.get_by_id = AsyncMock(return_value=mock_collection) mock_document_repository.list_by_collection = AsyncMock(return_value=mock_documents_list) result = await document_use_cases.list_collection_documents(collection_id, skip=0, limit=10) assert len(result) == len(mock_documents_list) mock_document_repository.list_by_collection.assert_called_once_with(collection_id, skip=0, limit=10)