Added Login + Working on pdf creation
This commit is contained in:
0
tests/common/__init__.py
Normal file
0
tests/common/__init__.py
Normal file
55
tests/common/test_pdf_converter.py
Normal file
55
tests/common/test_pdf_converter.py
Normal file
@@ -0,0 +1,55 @@
|
||||
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")
|
||||
52
tests/common/test_utils.py
Normal file
52
tests/common/test_utils.py
Normal file
@@ -0,0 +1,52 @@
|
||||
import shutil
|
||||
import tempfile
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
|
||||
from tasks.common.converter_utils import detect_file_type, UnsupportedFileTypeError
|
||||
|
||||
|
||||
@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_detect_text_file(temp_dir):
|
||||
txt_file = Path(temp_dir) / "sample.txt"
|
||||
txt_file.write_text("Sample text content")
|
||||
detected_type = detect_file_type(str(txt_file))
|
||||
assert detected_type == "text"
|
||||
|
||||
|
||||
def test_i_can_detect_image_file(temp_dir):
|
||||
from PIL import Image
|
||||
|
||||
img_file = Path(temp_dir) / "sample.jpg"
|
||||
image = Image.new("RGB", (50, 50), color="blue")
|
||||
image.save(img_file)
|
||||
|
||||
detected_type = detect_file_type(str(img_file))
|
||||
assert detected_type == "image"
|
||||
|
||||
|
||||
def test_i_can_detect_word_file(temp_dir):
|
||||
import docx
|
||||
|
||||
docx_file = Path(temp_dir) / "sample.docx"
|
||||
doc = docx.Document()
|
||||
doc.add_paragraph("Sample content")
|
||||
doc.save(docx_file)
|
||||
|
||||
detected_type = detect_file_type(str(docx_file))
|
||||
assert detected_type == "word"
|
||||
|
||||
|
||||
def test_i_cannot_detect_unsupported_file(temp_dir):
|
||||
exe_file = Path(temp_dir) / "sample.exe"
|
||||
exe_file.write_bytes(b'\x4D\x5A\x90\x00\x03\x00\x00\x00')
|
||||
with pytest.raises(UnsupportedFileTypeError):
|
||||
detect_file_type(str(exe_file))
|
||||
Reference in New Issue
Block a user