56 lines
1.4 KiB
Python
56 lines
1.4 KiB
Python
import shutil
|
|
import tempfile
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
|
|
from tasks.common.pdf_converter import TextToPdfConverter, ImageToPdfConverter, WordToPdfConverter
|
|
|
|
|
|
@pytest.fixture
|
|
def temp_dir():
|
|
"""Create a temporary directory for output PDFs."""
|
|
dir_path = tempfile.mkdtemp()
|
|
yield dir_path
|
|
shutil.rmtree(dir_path)
|
|
|
|
|
|
def test_i_can_convert_text_to_pdf(temp_dir):
|
|
input_txt = Path(temp_dir) / "test.txt"
|
|
input_txt.write_text("Hello World!\nThis is a test.")
|
|
|
|
converter = TextToPdfConverter(str(input_txt), output_dir=temp_dir)
|
|
output_pdf = converter.convert()
|
|
|
|
assert Path(output_pdf).exists()
|
|
assert output_pdf.endswith(".pdf")
|
|
|
|
|
|
def test_i_can_convert_image_to_pdf(temp_dir):
|
|
from PIL import Image
|
|
|
|
input_img = Path(temp_dir) / "image.png"
|
|
image = Image.new("RGB", (100, 100), color="red")
|
|
image.save(input_img)
|
|
|
|
converter = ImageToPdfConverter(str(input_img), output_dir=temp_dir)
|
|
output_pdf = converter.convert()
|
|
|
|
assert Path(output_pdf).exists()
|
|
assert output_pdf.endswith(".pdf")
|
|
|
|
|
|
def test_i_can_convert_word_to_pdf(temp_dir):
|
|
import docx
|
|
|
|
input_docx = Path(temp_dir) / "document.docx"
|
|
doc = docx.Document()
|
|
doc.add_paragraph("Hello Word!")
|
|
doc.save(input_docx)
|
|
|
|
converter = WordToPdfConverter(str(input_docx), output_dir=temp_dir)
|
|
output_pdf = converter.convert()
|
|
|
|
assert Path(output_pdf).exists()
|
|
assert output_pdf.endswith(".pdf")
|