from io import BytesIO from tempfile import NamedTemporaryFile from unittest.mock import Mock import fitz import pytest from pptx import Presentation from src.backend.utils import pdf class TestConvertPPTXToPDF: @pytest.fixture def mock_pptx_bytes(self) -> BytesIO: """Create a new mock PPTX presentation with a single slide.""" presentation = Presentation() slide_layout = presentation.slide_layouts[0] slide = presentation.slides.add_slide(slide_layout) title = slide.shapes.title subtitle = slide.placeholders[1] title.text = "Hello, World!" subtitle.text = ( "This is a new PowerPoint presentation created with python-pptx." ) pptx_bytes = BytesIO() presentation.save(pptx_bytes) pptx_bytes.seek(0) return pptx_bytes @pytest.mark.asyncio async def test_convert_pptx_to_pdf(self, mock_pptx_bytes): result = await pdf.convert_pptx_to_pdf(mock_pptx_bytes) assert isinstance(result, BytesIO) class TestAdjustPDFFile: @pytest.fixture def mock_pdf_bytes(self) -> BytesIO: """Create a new mock PDF document with a single blank page.""" pdf_document = fitz.open() pdf_document.new_page(width=595, height=842) pdf_bytes = BytesIO() pdf_document.save(pdf_bytes) pdf_document.close() pdf_bytes.seek(0) return pdf_bytes def test_adjust_pdf_file(self, mock_pdf_bytes): result = pdf.adjust_pdf_file(mock_pdf_bytes, new_exif_title="mock title") assert isinstance(result, BytesIO) class TestCheckPDFConversionSuccess: def test_check_pdf_conversion_success_bad_return_code(self): with pytest.raises(ValueError): pdf._check_pdf_conversion_success(1, Mock(), "mock") def test_check_pdf_conversion_success_path_not_exists(self): with pytest.raises(RuntimeError): pdf._check_pdf_conversion_success(0, Mock(), "this_path_does_not_exist") def test_check_pdf_conversion_success_path_size_is_0(self): with NamedTemporaryFile() as temp_file: with pytest.raises(RuntimeError): pdf._check_pdf_conversion_success(0, Mock(), temp_file.name)