Skip to content

Python tool for converting files and office documents to Markdown.

Replace local parser mocks with real conversion fixtures (#2586)

Adam Fourneyd9cae89636ac
diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml
index ddb6eac8..c653d523 100644
--- a/.github/workflows/tests.yml
+++ b/.github/workflows/tests.yml
@@ -29,6 +29,15 @@ jobs:
with:
python-version: ${{ matrix.python-version }}
+ - name: Install media tools
+ if: runner.os == 'Linux'
+ run: |
+ sudo apt-get update
+ sudo apt-get install -y --no-install-recommends libimage-exiftool-perl ffmpeg
+ exiftool -ver
+ ffmpeg -version
+ ffprobe -version
+
- name: Install Hatch
run: pipx install hatch
- name: Run tests
diff --git a/packages/markitdown-ocr/tests/test_pdf_converter.py b/packages/markitdown-ocr/tests/test_pdf_converter.py
index 48b6387b..5e6aa1c4 100644
--- a/packages/markitdown-ocr/tests/test_pdf_converter.py
+++ b/packages/markitdown-ocr/tests/test_pdf_converter.py
@@ -14,7 +14,6 @@ import io
import sys
from pathlib import Path
from typing import Any
-from unittest.mock import MagicMock, patch
import pytest
@@ -207,28 +206,30 @@ def test_pdf_scanned_report(svc: MockOCRService) -> None:
# ---------------------------------------------------------------------------
-def test_pdf_scanned_fallback_format(svc: MockOCRService) -> None:
- """_ocr_full_pages emits *[Image OCR]...[End OCR]* for each page."""
+def test_pdf_scanned_fallback_format() -> None:
+ """Render a real PDF, replacing only the remote OCR service."""
+ from PIL import Image
+
path = TEST_DATA_DIR / "pdf_image_start.pdf"
- if not path.exists():
- pytest.skip(f"Test file not found: {path}")
+ seen = []
+
+ class RecordingOCRService:
+ def extract_text(self, image_stream: Any) -> OCRResult:
+ assert image_stream.tell() == 0
+ with Image.open(image_stream) as image:
+ assert image.format == "PNG"
+ assert image.width > 0 and image.height > 0
+ image.load()
+ seen.append(image.size)
+ return OCRResult(text=_MOCK_TEXT, backend_used="mock")
converter = PdfConverterWithOCR()
- with patch("pdfplumber.open") as mock_plumber:
- mock_pdf = MagicMock()
- mock_page = MagicMock()
- mock_page.page_number = 1
- mock_pdf.pages = [mock_page]
- mock_pdf.__enter__.return_value = mock_pdf
- mock_plumber.return_value = mock_pdf
-
- with open(path, "rb") as f:
- md = converter._ocr_full_pages(io.BytesIO(f.read()), svc)
-
- expected = "## Page 1\n\n\n" "*[Image OCR]\nMOCK_OCR_TEXT_12345\n[End OCR]*"
- assert (
- md == expected
- ), f"_ocr_full_pages must produce:\n{expected!r}\nActual:\n{md!r}"
+ markdown = converter._ocr_full_pages(
+ io.BytesIO(path.read_bytes()), RecordingOCRService()
+ )
+
+ assert len(seen) == 1
+ assert markdown == "## Page 1\n\n\n" + _OCR_BLOCK
# ---------------------------------------------------------------------------
diff --git a/packages/markitdown-ocr/tests/test_pptx_converter.py b/packages/markitdown-ocr/tests/test_pptx_converter.py
index a0fc098f..683a6885 100644
--- a/packages/markitdown-ocr/tests/test_pptx_converter.py
+++ b/packages/markitdown-ocr/tests/test_pptx_converter.py
@@ -12,11 +12,16 @@ OCR blocks use shared HTML escaping and Markdown hard breaks:
Slide text, notes, and layout come from the core PPTX converter.
"""
+import io
import sys
from pathlib import Path
from typing import Any
import pytest
+from pptx import Presentation
+from pptx.chart.data import CategoryChartData
+from pptx.enum.chart import XL_CHART_TYPE
+from pptx.util import Inches
sys.path.insert(0, str(Path(__file__).parent.parent / "src"))
@@ -138,54 +143,45 @@ def test_pptx_no_ocr_service_no_tags() -> None:
assert "[End OCR]*" not in md
-def test_pptx_ocr_chart_no_title_text_frame() -> None:
- from unittest.mock import MagicMock
- from markitdown_ocr._pptx_converter_with_ocr import PptxConverterWithOCR
-
- # python-pptx's ChartTitle.text_frame is destructive -- it creates a text
- # frame if one isn't already present, so it never returns None.
- # has_text_frame is the property that actually reflects presence/absence.
- mock_chart = MagicMock()
- mock_chart.has_title = True
- mock_chart.chart_title.has_text_frame = False
-
- mock_category = MagicMock()
- mock_category.label = "Cat 1"
- mock_chart.plots = [MagicMock(categories=[mock_category])]
-
- mock_series = MagicMock()
- mock_series.name = "Series 1"
- mock_series.values = [10.0]
- mock_chart.series = [mock_series]
-
+def _chart_presentation(title: str | None) -> io.BytesIO:
+ # Modify an in-memory copy of an existing deck; keep its fixture intact.
+ presentation = Presentation(TEST_DATA_DIR / "pptx_image_middle.pptx")
+ slide = presentation.slides.add_slide(presentation.slide_layouts[6])
+ data = CategoryChartData()
+ data.categories = ["Cat 1"]
+ data.add_series("Series 1", [10.0])
+ chart = slide.shapes.add_chart(
+ XL_CHART_TYPE.COLUMN_CLUSTERED,
+ Inches(1),
+ Inches(1),
+ Inches(5),
+ Inches(3),
+ data,
+ ).chart
+ chart.has_title = True
+ if title is not None:
+ chart.chart_title.text_frame.text = title
+ assert chart.chart_title.has_text_frame is (title is not None)
+ stream = io.BytesIO()
+ presentation.save(stream)
+ stream.seek(0)
+ return stream
+
+
[email protected]("title", [None, "Revenue"])
+def test_pptx_ocr_chart_title_text_frame(title: str | None) -> None:
+ stream = _chart_presentation(title)
+ # Reload the serialized package so the real parser reads title XML.
+ chart = Presentation(stream).slides[-1].shapes[-1].chart
converter = PptxConverterWithOCR()
- result = converter._convert_chart_to_markdown(mock_chart)
-
- assert "### Chart" in result
+ result = converter._convert_chart_to_markdown(chart)
+ heading = "### Chart" if title is None else "### Chart: Revenue"
+ assert result.strip().splitlines()[0] == heading
assert "Cat 1" in result
assert "Series 1" in result
- assert ":" not in result
-
-
-def test_pptx_ocr_chart_with_title_text_frame() -> None:
- from unittest.mock import MagicMock
- from markitdown_ocr._pptx_converter_with_ocr import PptxConverterWithOCR
-
- mock_chart = MagicMock()
- mock_chart.has_title = True
- mock_chart.chart_title.has_text_frame = True
- mock_chart.chart_title.text_frame.text = "Revenue"
-
- mock_category = MagicMock()
- mock_category.label = "Cat 1"
- mock_chart.plots = [MagicMock(categories=[mock_category])]
-
- mock_series = MagicMock()
- mock_series.name = "Series 1"
- mock_series.values = [10.0]
- mock_chart.series = [mock_series]
-
- converter = PptxConverterWithOCR()
- result = converter._convert_chart_to_markdown(mock_chart)
-
- assert "### Chart: Revenue" in result
+ assert "10.0" in result
+ # Accessing a title without a text frame must not create one.
+ assert chart.chart_title.has_text_frame is (title is not None)
+ stream.seek(0)
+ markdown = converter.convert(stream, StreamInfo(extension=".pptx")).markdown
+ assert result.strip() in markdown
diff --git a/packages/markitdown/tests/README.md b/packages/markitdown/tests/README.md
index 34c10c5c..86fd8f46 100644
--- a/packages/markitdown/tests/README.md
+++ b/packages/markitdown/tests/README.md
@@ -52,3 +52,10 @@ The existing remote, credential, platform, and optional-dependency skip conditio
apply. A consolidation should preserve test bodies, parametrization, fixtures,
and skip conditions; compare collected cases as well as test results to catch
accidentally dropped or overwritten tests.
+
+The `test_files/pdf_cleanup_{plain,form,mixed}.pdf` fixtures contain copied pages
+from `test.pdf` and `REPAIR-2022-INV-001_multipage.pdf`. They exercise real PDF
+parsing, fallback, and cleanup without changing either source fixture. Rebuild
+them with `python tests/test_files/generate_pdf_cleanup_fixtures.py` in an
+environment with PyMuPDF installed; PyMuPDF is needed only to rebuild the files,
+not to run the core tests.
diff --git a/packages/markitdown/tests/_test_vectors.py b/packages/markitdown/tests/_test_vectors.py
index 74fa9bd0..c8c4ee83 100644
--- a/packages/markitdown/tests/_test_vectors.py
+++ b/packages/markitdown/tests/_test_vectors.py
@@ -97,6 +97,45 @@ GENERAL_TEST_VECTORS = [
],
must_not_include=[],
),
+ FileTestVector(
+ filename="pdf_cleanup_plain.pdf",
+ mimetype="application/pdf",
+ charset=None,
+ url=None,
+ must_include=[
+ "While there is contemporaneous exploration of multi-agent approaches",
+ ],
+ must_not_include=["ZAVA AUTO REPAIR", "| Customer Information |"],
+ ),
+ FileTestVector(
+ filename="pdf_cleanup_form.pdf",
+ mimetype="application/pdf",
+ charset=None,
+ url=None,
+ must_include=[
+ "ZAVA AUTO REPAIR",
+ "| Customer Information |",
+ "Gabriel Diaz",
+ "| GRAND TOTAL",
+ "5,738",
+ ],
+ must_not_include=["While there is contemporaneous exploration"],
+ ),
+ FileTestVector(
+ filename="pdf_cleanup_mixed.pdf",
+ mimetype="application/pdf",
+ charset=None,
+ url=None,
+ must_include=[
+ "ZAVA AUTO REPAIR",
+ "| Customer Information |",
+ "Gabriel Diaz",
+ "| GRAND TOTAL",
+ "5,738",
+ "While there is contemporaneous exploration of multi-agent approaches",
+ ],
+ must_not_include=[],
+ ),
FileTestVector(
filename="test_blog.html",
mimetype="text/html",
diff --git a/packages/markitdown/tests/test_files/generate_pdf_cleanup_fixtures.py b/packages/markitdown/tests/test_files/generate_pdf_cleanup_fixtures.py
new file mode 100644
index 00000000..d13a113c
--- /dev/null
+++ b/packages/markitdown/tests/test_files/generate_pdf_cleanup_fixtures.py
@@ -0,0 +1,31 @@
+"""Rebuild copied PDF fixtures with PyMuPDF (already used by the OCR package).
+
+Run from packages/markitdown with: python tests/test_files/generate_pdf_cleanup_fixtures.py
+The existing source PDFs are opened read-only; only pdf_cleanup_*.pdf is written.
+"""
+
+from pathlib import Path
+
+import pymupdf
+
+
+FIXTURES = Path(__file__).resolve().parent
+
+
+def main() -> None:
+ with pymupdf.open(FIXTURES / "test.pdf") as plain, pymupdf.open(
+ FIXTURES / "REPAIR-2022-INV-001_multipage.pdf"
+ ) as form:
+ for name, pages in (
+ ("plain", [plain, plain, plain]),
+ ("form", [form, form, form]),
+ ("mixed", [form, plain, form]),
+ ):
+ with pymupdf.open() as copied:
+ for source in pages:
+ copied.insert_pdf(source, from_page=0, to_page=0)
+ copied.save(FIXTURES / f"pdf_cleanup_{name}.pdf", deflate=True)
+
+
+if __name__ == "__main__":
+ main()
diff --git a/packages/markitdown/tests/test_files/pdf_cleanup_form.pdf b/packages/markitdown/tests/test_files/pdf_cleanup_form.pdf
new file mode 100644
index 00000000..5d4747a5
Binary files /dev/null and b/packages/markitdown/tests/test_files/pdf_cleanup_form.pdf differ
diff --git a/packages/markitdown/tests/test_files/pdf_cleanup_mixed.pdf b/packages/markitdown/tests/test_files/pdf_cleanup_mixed.pdf
new file mode 100644
index 00000000..5ecae72c
Binary files /dev/null and b/packages/markitdown/tests/test_files/pdf_cleanup_mixed.pdf differ
diff --git a/packages/markitdown/tests/test_files/pdf_cleanup_plain.pdf b/packages/markitdown/tests/test_files/pdf_cleanup_plain.pdf
new file mode 100644
index 00000000..ca00de2b
Binary files /dev/null and b/packages/markitdown/tests/test_files/pdf_cleanup_plain.pdf differ
diff --git a/packages/markitdown/tests/test_image.py b/packages/markitdown/tests/test_image.py
index 2018f1b4..dbbc8f2e 100644
--- a/packages/markitdown/tests/test_image.py
+++ b/packages/markitdown/tests/test_image.py
@@ -173,7 +173,11 @@ MP3_TEST_EXIFTOOL = {
skip_exiftool,
reason="do not run if exiftool is not installed",
)
-def test_markitdown_exiftool() -> None:
+def test_markitdown_exiftool(monkeypatch: pytest.MonkeyPatch) -> None:
+ import speech_recognition as sr
+
+ # Decode audio locally, but avoid the remote speech service in metadata tests.
+ monkeypatch.setattr(sr.Recognizer, "recognize_google", lambda *args, **kwargs: "")
which_exiftool = shutil.which("exiftool")
assert which_exiftool is not None
@@ -185,7 +189,7 @@ def test_markitdown_exiftool() -> None:
assert target in result.text_content
# Test setting the exiftool path through an environment variable
- os.environ["EXIFTOOL_PATH"] = which_exiftool
+ monkeypatch.setenv("EXIFTOOL_PATH", which_exiftool)
markitdown = MarkItDown()
result = markitdown.convert(os.path.join(TEST_FILES_DIR, "test.jpg"))
for key in JPG_TEST_EXIFTOOL:
diff --git a/packages/markitdown/tests/test_module_misc.py b/packages/markitdown/tests/test_module_misc.py
index 716f5389..31eefaff 100644
--- a/packages/markitdown/tests/test_module_misc.py
+++ b/packages/markitdown/tests/test_module_misc.py
@@ -9,6 +9,7 @@ from types import SimpleNamespace
from unittest.mock import MagicMock
import pytest
+import requests
from charset_normalizer import from_bytes
import markitdown._uri_utils as uri_utils
@@ -273,19 +274,19 @@ def test_input_as_strings() -> None:
assert "# Test" in result.text_content
-def _mock_response(content_disposition: str) -> MagicMock:
- response = MagicMock()
- response.headers = {"content-disposition": content_disposition}
+def _response(content_disposition: str) -> requests.Response:
+ response = requests.Response()
+ response.status_code = 200
+ response.headers["content-disposition"] = content_disposition
response.url = "https://example.com/download"
- response.iter_content.return_value = [b"name,value\nalpha,beta\n"]
- response.raise_for_status.return_value = None
+ response.raw = io.BytesIO(b"name,value\nalpha,beta\n")
return response
def test_convert_response_uses_rfc5987_content_disposition_filename() -> None:
markitdown = MarkItDown()
result = markitdown.convert_response(
- _mock_response("attachment; filename*=UTF-8''data.csv")
+ _response("attachment; filename*=UTF-8''data.csv")
)
assert result.markdown == "\n".join(
@@ -300,7 +301,7 @@ def test_convert_response_uses_rfc5987_content_disposition_filename() -> None:
def test_convert_response_prefers_extended_content_disposition_filename() -> None:
markitdown = MarkItDown()
result = markitdown.convert_response(
- _mock_response("attachment; filename=fallback.txt; filename*=UTF-8''data.csv")
+ _response("attachment; filename=fallback.txt; filename*=UTF-8''data.csv")
)
assert result.markdown == "\n".join(
diff --git a/packages/markitdown/tests/test_pdf.py b/packages/markitdown/tests/test_pdf.py
index 8ae2ec46..c4f8c64a 100644
--- a/packages/markitdown/tests/test_pdf.py
+++ b/packages/markitdown/tests/test_pdf.py
@@ -1,9 +1,8 @@
"""PDF conversion, table extraction, numbering, and page cleanup."""
-import io
import os
import re
-from unittest.mock import MagicMock, patch
+from unittest.mock import patch
import pytest
@@ -1207,217 +1206,100 @@ class TestMasterFormatPartialNumbering:
# - Mixed PDFs use form extraction only on form-style pages
-def _make_form_page():
- """Create a mock page with 3-column table-like word positions."""
- page = MagicMock()
- page.width = 612
- page.close = MagicMock()
- page.extract_words.return_value = [
- {"text": "Name", "x0": 50, "x1": 100, "top": 10, "bottom": 20},
- {"text": "Value", "x0": 250, "x1": 300, "top": 10, "bottom": 20},
- {"text": "Unit", "x0": 450, "x1": 500, "top": 10, "bottom": 20},
- {"text": "Alpha", "x0": 50, "x1": 100, "top": 30, "bottom": 40},
- {"text": "100", "x0": 250, "x1": 280, "top": 30, "bottom": 40},
- {"text": "kg", "x0": 450, "x1": 470, "top": 30, "bottom": 40},
- {"text": "Beta", "x0": 50, "x1": 100, "top": 50, "bottom": 60},
- {"text": "200", "x0": 250, "x1": 280, "top": 50, "bottom": 60},
- {"text": "lb", "x0": 450, "x1": 470, "top": 50, "bottom": 60},
- ]
- return page
-
-
-def _make_plain_page():
- """Create a mock page with single-line paragraph (no table structure)."""
- page = MagicMock()
- page.width = 612
- page.close = MagicMock()
- page.extract_words.return_value = [
- {
- "text": "This is a long paragraph of plain text.",
- "x0": 50,
- "x1": 550,
- "top": 10,
- "bottom": 20,
- },
- ]
- page.extract_text.return_value = "This is a long paragraph of plain text."
- return page
-
-
-def _mock_pdfplumber_open(pages):
- """Return a mock pdfplumber.open that yields the given pages."""
-
- def mock_open(stream):
- mock_pdf = MagicMock()
- mock_pdf.pages = pages
- mock_pdf.__enter__ = MagicMock(return_value=mock_pdf)
- mock_pdf.__exit__ = MagicMock(return_value=False)
- return mock_pdf
-
- return mock_open
+def pdf_activity(monkeypatch):
+ """Record parser activity while executing every real library operation."""
+ import pdfplumber
+ from markitdown.converters import _pdf_converter
+ events = []
+ original_open = pdfplumber.open
+ original_page_close = pdfplumber.page.Page.close
+ original_extract_text = _pdf_converter.pdfminer.high_level.extract_text
+ original_form_extraction = _pdf_converter._extract_form_content_from_words
-class TestPdfMemoryOptimization:
- """Test that PDF conversion cleans up per-page caches to limit memory."""
-
- def test_page_close_called_on_every_page(self):
- """Verify page.close() is called on every page during conversion.
+ def open_pdf(*args, **kwargs):
+ events.append(("open", None))
+ return original_open(*args, **kwargs)
- This ensures cached word/layout data is freed after each page,
- preventing O(n) memory growth with page count.
- """
- num_pages = 20
- pages = [_make_form_page() for _ in range(num_pages)]
+ def close_page(page):
+ events.append(("close", page.page_number))
+ return original_page_close(page)
- with patch(
- "markitdown.converters._pdf_converter.pdfplumber"
- ) as mock_pdfplumber:
- mock_pdfplumber.open.side_effect = _mock_pdfplumber_open(pages)
+ def extract_text(*args, **kwargs):
+ events.append(("pdfminer", None))
+ return original_extract_text(*args, **kwargs)
- md = MarkItDown()
- buf = io.BytesIO(b"fake pdf content")
- from markitdown import StreamInfo
-
- md.convert_stream(
- buf,
- stream_info=StreamInfo(extension=".pdf", mimetype="application/pdf"),
- )
+ def extract_form(page):
+ result = original_form_extraction(page)
+ events.append(("form" if result is not None else "plain", page.page_number))
+ return result
- # page.close() must be called on ALL pages
- for i, page in enumerate(pages):
- assert page.close.called, (
- f"page.close() was NOT called on page {i} — "
- "this would cause memory to accumulate"
- )
-
- def test_plain_text_pdf_falls_back_to_pdfminer(self):
- """Verify all-plain-text PDFs fall back to pdfminer.
-
- When no page has form-style content, the converter should discard
- pdfplumber results and use pdfminer for the whole document (better
- text spacing for prose).
- """
- num_pages = 50
- pages = [_make_plain_page() for _ in range(num_pages)]
-
- with patch(
- "markitdown.converters._pdf_converter.pdfplumber"
- ) as mock_pdfplumber, patch(
- "markitdown.converters._pdf_converter.pdfminer"
- ) as mock_pdfminer:
- mock_pdfplumber.open.side_effect = _mock_pdfplumber_open(pages)
- mock_pdfminer.high_level.extract_text.return_value = "Plain text content"
-
- md = MarkItDown()
- buf = io.BytesIO(b"fake pdf content")
- from markitdown import StreamInfo
-
- result = md.convert_stream(
- buf,
- stream_info=StreamInfo(extension=".pdf", mimetype="application/pdf"),
- )
+ monkeypatch.setattr(pdfplumber, "open", open_pdf)
+ monkeypatch.setattr(pdfplumber.page.Page, "close", close_page)
+ monkeypatch.setattr(
+ _pdf_converter.pdfminer.high_level, "extract_text", extract_text
+ )
+ monkeypatch.setattr(
+ _pdf_converter, "_extract_form_content_from_words", extract_form
+ )
+ return events
- # pdfminer should be used for the final text extraction
- assert mock_pdfminer.high_level.extract_text.called, (
- "pdfminer.high_level.extract_text was not called — "
- "plain-text PDFs should fall back to pdfminer"
- )
- assert result.text_content is not None
-
- def test_plain_text_pdf_still_closes_all_pages(self):
- """Even for plain-text PDFs, page.close() must be called on every page."""
- num_pages = 30
- pages = [_make_plain_page() for _ in range(num_pages)]
-
- with patch(
- "markitdown.converters._pdf_converter.pdfplumber"
- ) as mock_pdfplumber, patch(
- "markitdown.converters._pdf_converter.pdfminer"
- ) as mock_pdfminer:
- mock_pdfplumber.open.side_effect = _mock_pdfplumber_open(pages)
- mock_pdfminer.high_level.extract_text.return_value = "text"
-
- md = MarkItDown()
- buf = io.BytesIO(b"fake pdf content")
- from markitdown import StreamInfo
-
- md.convert_stream(
- buf,
- stream_info=StreamInfo(extension=".pdf", mimetype="application/pdf"),
- )
- for i, page in enumerate(pages):
- assert (
- page.close.called
- ), f"page.close() was NOT called on plain-text page {i}"
+def _convert_cleanup_fixture(kind):
+ return (
+ MarkItDown()
+ .convert(os.path.join(TEST_FILES_DIR, f"pdf_cleanup_{kind}.pdf"))
+ .markdown
+ )
- def test_mixed_pdf_uses_form_extraction_per_page(self):
- """In a mixed PDF, form pages get table extraction while plain pages don't.
- Ensures we don't miss form-style pages and don't waste work
- running form extraction on plain-text pages.
- """
- # Pages 0,2,4 are form-style; pages 1,3 are plain text
- pages = [
- _make_form_page(), # 0 - form
- _make_plain_page(), # 1 - plain
- _make_form_page(), # 2 - form
- _make_plain_page(), # 3 - plain
- _make_form_page(), # 4 - form
+class TestPdfMemoryOptimization:
+ """Exercise page cleanup and fallback using copied, real PDF packages."""
+
+ def test_page_close_called_on_every_page(self, pdf_activity):
+ markdown = _convert_cleanup_fixture("form")
+ assert markdown.count("ZAVA AUTO REPAIR") == 3
+ for page in (1, 2, 3):
+ extracted = pdf_activity.index(("form", page))
+ closed = pdf_activity.index(("close", page))
+ assert extracted < closed
+ if page < 3:
+ assert closed < pdf_activity.index(("form", page + 1))
+
+ def test_plain_text_pdf_falls_back_to_pdfminer(self, pdf_activity):
+ markdown = _convert_cleanup_fixture("plain")
+ assert pdf_activity.count(("pdfminer", None)) == 1
+ assert [(kind, page) for kind, page in pdf_activity if kind == "plain"] == [
+ ("plain", 1),
+ ("plain", 2),
+ ("plain", 3),
]
-
- with patch(
- "markitdown.converters._pdf_converter.pdfplumber"
- ) as mock_pdfplumber:
- mock_pdfplumber.open.side_effect = _mock_pdfplumber_open(pages)
-
- md = MarkItDown()
- buf = io.BytesIO(b"fake pdf content")
- from markitdown import StreamInfo
-
- result = md.convert_stream(
- buf,
- stream_info=StreamInfo(extension=".pdf", mimetype="application/pdf"),
- )
-
- # All pages should have close() called
- for i, page in enumerate(pages):
- assert page.close.called, f"page.close() not called on page {i}"
-
- # Form pages (0,2,4) should have extract_words called
- for i in [0, 2, 4]:
- assert pages[
- i
- ].extract_words.called, f"extract_words not called on form page {i}"
-
- # Result should contain table content from form pages
- assert result.text_content is not None
- assert (
- "|" in result.text_content
- ), "Expected markdown table pipes in output from form-style pages"
-
- def test_only_one_pdfplumber_open_call(self):
- """Verify pdfplumber.open is called exactly once (single pass)."""
- pages = [_make_form_page() for _ in range(10)]
-
- with patch(
- "markitdown.converters._pdf_converter.pdfplumber"
- ) as mock_pdfplumber:
- mock_pdfplumber.open.side_effect = _mock_pdfplumber_open(pages)
-
- md = MarkItDown()
- buf = io.BytesIO(b"fake pdf content")
- from markitdown import StreamInfo
-
- md.convert_stream(
- buf,
- stream_info=StreamInfo(extension=".pdf", mimetype="application/pdf"),
- )
-
- assert mock_pdfplumber.open.call_count == 1, (
- f"Expected 1 pdfplumber.open call (single pass), "
- f"got {mock_pdfplumber.open.call_count}"
- )
+ assert markdown.count("While there is contemporaneous exploration") == 3
+
+ def test_plain_text_pdf_still_closes_all_pages(self, pdf_activity):
+ _convert_cleanup_fixture("plain")
+ fallback = pdf_activity.index(("pdfminer", None))
+ for page in (1, 2, 3):
+ extracted = pdf_activity.index(("plain", page))
+ closed = pdf_activity.index(("close", page))
+ assert extracted < closed < fallback
+ if page < 3:
+ assert closed < pdf_activity.index(("plain", page + 1))
+
+ def test_mixed_pdf_uses_form_extraction_per_page(self, pdf_activity):
+ markdown = _convert_cleanup_fixture("mixed")
+ assert [
+ (kind, page) for kind, page in pdf_activity if kind in ("form", "plain")
+ ] == [("form", 1), ("plain", 2), ("form", 3)]
+ assert ("pdfminer", None) not in pdf_activity
+ assert markdown.count("ZAVA AUTO REPAIR") == 2
+ assert "While there is contemporaneous exploration" in markdown
+ assert "|" in markdown
+
+ def test_only_one_pdfplumber_open_call(self, pdf_activity):
+ _convert_cleanup_fixture("form")
+ assert pdf_activity.count(("open", None)) == 1
@pytest.mark.skipif(
not os.path.exists(
diff --git a/packages/markitdown/tests/test_pptx.py b/packages/markitdown/tests/test_pptx.py
index 8d2b9eb1..44f14a73 100644
--- a/packages/markitdown/tests/test_pptx.py
+++ b/packages/markitdown/tests/test_pptx.py
@@ -872,57 +872,48 @@ def test_pptx_converter_treats_none_llm_caption_as_empty(monkeypatch) -> None:
)
-def test_pptx_chart_no_title_text_frame() -> None:
- from markitdown.converters._pptx_converter import PptxConverter
-
- # python-pptx's ChartTitle.text_frame is destructive -- it creates a text
- # frame if one isn't already present, so it never returns None.
- # has_text_frame is the property that actually reflects presence/absence,
- # which is what the has_title=True-but-no-text-frame case looks like
- # against the real library.
- mock_chart = MagicMock()
- mock_chart.has_title = True
- mock_chart.chart_title.has_text_frame = False
-
- mock_category = MagicMock()
- mock_category.label = "Cat 1"
- mock_chart.plots = [MagicMock(categories=[mock_category])]
-
- mock_series = MagicMock()
- mock_series.name = "Series 1"
- mock_series.values = [10.0]
- mock_chart.series = [mock_series]
+def _chart_presentation(title: str | None) -> io.BytesIO:
+ # Modify an in-memory copy of an existing deck; keep its fixture intact.
+ presentation = Presentation(Path(PPTX_FIXTURE))
+ slide = presentation.slides.add_slide(presentation.slide_layouts[6])
+ data = CategoryChartData()
+ data.categories = ["Cat 1"]
+ data.add_series("Series 1", [10.0])
+ chart = slide.shapes.add_chart(
+ XL_CHART_TYPE.COLUMN_CLUSTERED,
+ Inches(1),
+ Inches(1),
+ Inches(5),
+ Inches(3),
+ data,
+ ).chart
+ chart.has_title = True
+ if title is not None:
+ chart.chart_title.text_frame.text = title
+ assert chart.chart_title.has_text_frame is (title is not None)
+ stream = io.BytesIO()
+ presentation.save(stream)
+ stream.seek(0)
+ return stream
- converter = PptxConverter()
- result = converter._convert_chart_to_markdown(mock_chart)
- assert "### Chart" in result
[email protected]("title", [None, "Revenue"])
+def test_pptx_chart_title_text_frame(title: str | None) -> None:
+ stream = _chart_presentation(title)
+ # Reload the serialized package so the real parser reads title XML.
+ chart = Presentation(stream).slides[-1].shapes[-1].chart
+ converter = PptxConverter()
+ result = converter._convert_chart_to_markdown(chart)
+ heading = "### Chart" if title is None else "### Chart: Revenue"
+ assert result.strip().splitlines()[0] == heading
assert "Cat 1" in result
assert "Series 1" in result
- assert ":" not in result
-
-
-def test_pptx_chart_with_title_text_frame() -> None:
- from markitdown.converters._pptx_converter import PptxConverter
-
- mock_chart = MagicMock()
- mock_chart.has_title = True
- mock_chart.chart_title.has_text_frame = True
- mock_chart.chart_title.text_frame.text = "Revenue"
-
- mock_category = MagicMock()
- mock_category.label = "Cat 1"
- mock_chart.plots = [MagicMock(categories=[mock_category])]
-
- mock_series = MagicMock()
- mock_series.name = "Series 1"
- mock_series.values = [10.0]
- mock_chart.series = [mock_series]
-
- converter = PptxConverter()
- result = converter._convert_chart_to_markdown(mock_chart)
-
- assert "### Chart: Revenue" in result
+ assert "10.0" in result
+ # Accessing a title without a text frame must not create one.
+ assert chart.chart_title.has_text_frame is (title is not None)
+ stream.seek(0)
+ markdown = converter.convert(stream, StreamInfo(extension=".pptx")).markdown
+ assert result.strip() in markdown
if __name__ == "__main__":