mirror of
https://github.com/docling-project/docling-core.git
synced 2026-11-18 20:27:00 +00:00
On Windows, mimetypes.init() reads the registry, which maps .zip to application/x-zip-compressed and replaces application/zip in mimetypes.types_map. DocumentOrigin then rejects application/zip, so docling cannot build the origin for a .dclx archive there. Add application/zip to the extra MIME types, as was done for EPUB, M4A and RTF. Signed-off-by: DEVJHAWAR11 <jhawardev11@gmail.com>
159 lines
5.5 KiB
Python
159 lines
5.5 KiB
Python
import mimetypes
|
|
|
|
import pytest
|
|
from pydantic import ValidationError
|
|
|
|
from docling_core.types.doc import DocItemLabel, DoclingDocument, DocumentOrigin, GraphData, TableData, TrackSource
|
|
from docling_core.types.doc.items.node import NodeItem
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"mimetype",
|
|
[
|
|
"application/pdf",
|
|
"application/vnd.box.boxnote",
|
|
"application/vnd.cv+pdf",
|
|
"application/vnd.docling.ebcdic",
|
|
"application/x-ebcdic",
|
|
"text/markdown",
|
|
],
|
|
)
|
|
def test_document_origin_mimetype(mimetype: str):
|
|
"""Test that DocumentOrigin accepts the supported MIME types."""
|
|
origin = DocumentOrigin(mimetype=mimetype, binary_hash=42, filename="test")
|
|
assert origin.mimetype == mimetype
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"mimetype",
|
|
[
|
|
"application/calendar+json",
|
|
"application/calendar+xml",
|
|
"text/calendar",
|
|
"text/x-vcalendar",
|
|
],
|
|
)
|
|
def test_document_origin_calendar_mimetype_without_system_table(monkeypatch, mimetype: str):
|
|
"""Test that calendar MIME types do not depend on the host MIME table.
|
|
|
|
CPython's built-in table has no calendar types; only a system table such as
|
|
/etc/mime.types or the Windows registry adds them.
|
|
"""
|
|
monkeypatch.setattr(mimetypes, "types_map", {})
|
|
origin = DocumentOrigin(mimetype=mimetype, binary_hash=42, filename="test")
|
|
assert origin.mimetype == mimetype
|
|
|
|
|
|
def test_document_origin_zip_mimetype_without_application_zip(monkeypatch: pytest.MonkeyPatch):
|
|
"""Test that DocumentOrigin accepts application/zip when the platform maps .zip elsewhere.
|
|
|
|
On Windows the registry maps ".zip" to application/x-zip-compressed, which replaces
|
|
application/zip in mimetypes.types_map once mimetypes.init() has run.
|
|
"""
|
|
types_map = {ext: mt for ext, mt in mimetypes.types_map.items() if mt != "application/zip"}
|
|
types_map[".zip"] = "application/x-zip-compressed"
|
|
monkeypatch.setattr(mimetypes, "types_map", types_map)
|
|
|
|
origin = DocumentOrigin(mimetype="application/zip", binary_hash=42, filename="test.dclx")
|
|
assert origin.mimetype == "application/zip"
|
|
|
|
|
|
def test_document_origin_invalid_mimetype():
|
|
"""Test that DocumentOrigin rejects unknown MIME types."""
|
|
with pytest.raises(ValidationError, match="is not a valid MIME type"):
|
|
DocumentOrigin(mimetype="application/x-not-a-mimetype", binary_hash=42, filename="test")
|
|
|
|
|
|
def test_track_source():
|
|
"""Test the class TrackSource."""
|
|
valid_track = TrackSource(
|
|
start_time=11.0,
|
|
end_time=12.0,
|
|
identifier="test",
|
|
voice="Mary",
|
|
)
|
|
|
|
assert valid_track
|
|
assert valid_track.start_time == 11.0
|
|
assert valid_track.end_time == 12.0
|
|
assert valid_track.identifier == "test"
|
|
assert valid_track.voice == "Mary"
|
|
|
|
with pytest.raises(ValidationError, match="end_time"):
|
|
TrackSource(start_time=11.0)
|
|
|
|
with pytest.raises(ValidationError, match="should be a valid string"):
|
|
TrackSource(
|
|
start_time=11.0,
|
|
end_time=12.0,
|
|
voice=["Mary"],
|
|
)
|
|
|
|
with pytest.raises(ValidationError, match="must be greater than start"):
|
|
TrackSource(
|
|
start_time=11.0,
|
|
end_time=11.0,
|
|
)
|
|
|
|
doc = DoclingDocument(name="Unknown")
|
|
item = doc.add_text(text="Hello world", label=DocItemLabel.TEXT, source=valid_track)
|
|
assert item.source
|
|
assert len(item.source) == 1
|
|
assert item.source[0] == valid_track
|
|
|
|
|
|
def test_add_doc_items_with_source():
|
|
"""Test that add_* helpers append sources to created DocItems."""
|
|
source = TrackSource(start_time=11.0, end_time=12.0)
|
|
doc = DoclingDocument(name="Unknown")
|
|
list_group = doc.add_list_group()
|
|
|
|
items = [
|
|
doc.add_list_item(text="List item", source=source, parent=list_group),
|
|
doc.add_text(text="Text", label=DocItemLabel.TEXT, source=source),
|
|
doc.add_comment(text="Comment", source=source),
|
|
doc.add_table(data=TableData(), source=source),
|
|
doc.add_picture(source=source),
|
|
doc.add_title(text="Title", source=source),
|
|
doc.add_code(text="Code", source=source),
|
|
doc.add_formula(text="Formula", source=source),
|
|
doc.add_heading(text="Heading", source=source),
|
|
doc.add_key_values(graph=GraphData(), source=source),
|
|
doc.add_form(graph=GraphData(), source=source),
|
|
doc.add_field_region(source=source),
|
|
doc.add_field_heading(text="Field heading", source=source),
|
|
doc.add_field_item(source=source),
|
|
doc.add_field_key(text="Field key", source=source),
|
|
doc.add_field_value(text="Field value", source=source),
|
|
doc.add_field_hint(text="Field hint", source=source),
|
|
doc.add_marker(text="Marker", source=source),
|
|
]
|
|
|
|
for item in items:
|
|
assert item.source == [source]
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"label",
|
|
[
|
|
DocItemLabel.TITLE,
|
|
DocItemLabel.LIST_ITEM,
|
|
DocItemLabel.SECTION_HEADER,
|
|
DocItemLabel.CODE,
|
|
DocItemLabel.FORMULA,
|
|
DocItemLabel.FIELD_HEADING,
|
|
DocItemLabel.FIELD_VALUE,
|
|
],
|
|
)
|
|
def test_add_text_dispatch_preserves_source(label: DocItemLabel):
|
|
"""Test that add_text preserves source when dispatching to specialized helpers."""
|
|
source = TrackSource(start_time=11.0, end_time=12.0)
|
|
doc = DoclingDocument(name="Unknown")
|
|
|
|
parent: NodeItem | None = None
|
|
if label == DocItemLabel.LIST_ITEM:
|
|
parent = doc.add_list_group()
|
|
item = doc.add_text(label=label, text="Hello world", source=source, parent=parent)
|
|
|
|
assert item.source == [source]
|