feat: extractors fuer pdf/md/docx/xlsx mit dynamic fixtures

This commit is contained in:
2026-05-04 22:14:59 +02:00
parent 5e44495676
commit ec55110ae4
3 changed files with 128 additions and 0 deletions

37
tests/conftest.py Normal file
View File

@@ -0,0 +1,37 @@
import io
import pytest
import fitz # pymupdf
from docx import Document
@pytest.fixture
def sample_pdf_bytes() -> bytes:
doc = fitz.open()
p1 = doc.new_page()
p1.insert_text((72, 72), "Seite eins enthaelt Lorem Ipsum.")
p2 = doc.new_page()
p2.insert_text((72, 72), "Seite zwei enthaelt mehr Text.")
data = doc.tobytes()
doc.close()
return data
@pytest.fixture
def sample_docx_bytes() -> bytes:
doc = Document()
doc.add_paragraph("Erster Absatz.")
doc.add_paragraph("Zweiter Absatz mit mehr Inhalt.")
buf = io.BytesIO()
doc.save(buf)
return buf.getvalue()
@pytest.fixture
def sample_md_bytes() -> bytes:
return "# Title\n\nFirst paragraph.\n\nSecond paragraph.\n".encode("utf-8")
@pytest.fixture
def sample_xlsx_bytes() -> bytes:
# Minimal placeholder; extractor doesn't read content for xlsx
return b"PK\x03\x04dummy"

38
tests/test_extractors.py Normal file
View File

@@ -0,0 +1,38 @@
import pytest
from app.ingest.extractors import extract, ExtractedPage, UnsupportedFileType
def test_extract_pdf_returns_pages(sample_pdf_bytes):
pages = extract(sample_pdf_bytes, "pdf", filename="x.pdf")
assert len(pages) == 2
assert pages[0].page == 1
assert "Seite eins" in pages[0].text
assert pages[1].page == 2
assert "Seite zwei" in pages[1].text
def test_extract_docx_returns_single_page(sample_docx_bytes):
pages = extract(sample_docx_bytes, "docx", filename="x.docx")
assert len(pages) == 1
assert pages[0].page == 1
assert "Erster Absatz." in pages[0].text
assert "Zweiter Absatz" in pages[0].text
def test_extract_md_returns_single_page(sample_md_bytes):
pages = extract(sample_md_bytes, "md", filename="x.md")
assert len(pages) == 1
assert pages[0].page == 1
assert "First paragraph." in pages[0].text
def test_extract_xlsx_returns_filename_pseudo_text(sample_xlsx_bytes):
pages = extract(sample_xlsx_bytes, "xlsx", filename="my-sheet.xlsx")
assert len(pages) == 1
assert pages[0].page == 1
assert pages[0].text == "Tabelle: my-sheet.xlsx"
def test_extract_unsupported_raises():
with pytest.raises(UnsupportedFileType):
extract(b"data", "txt", filename="x.txt")