Files
MyDocManager/tests/common/test_utils.py

53 lines
1.4 KiB
Python

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))