Skip to content

Commit ce49923

Browse files
test: fix picture checks in shared verifier (#3130)
Signed-off-by: Hassan Raza <raihassanraza10@gmail.com>
1 parent 6238aa3 commit ce49923

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed

tests/test_verify_utils.py

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import pytest
2-
from docling_core.types.doc import DoclingDocument, ProvenanceItem
2+
from docling_core.types.doc import DoclingDocument, ImageRef, ProvenanceItem
33
from docling_core.types.doc.base import BoundingBox, Size
44
from docling_core.types.doc.labels import DocItemLabel
5+
from PIL import Image
56

67
from tests.verify_utils import verify_docitems
78

@@ -24,6 +25,14 @@ def _make_doc_with_bbox(
2425
return doc
2526

2627

28+
def _make_doc_with_picture(*, image_size: tuple[int, int]) -> DoclingDocument:
29+
doc = DoclingDocument(name="test")
30+
doc.add_picture(
31+
image=ImageRef.from_pil(Image.new("RGB", image_size, "red"), dpi=72)
32+
)
33+
return doc
34+
35+
2736
def test_verify_docitems_allows_small_bbox_variance_for_non_fuzzy_docs():
2837
verify_docitems(
2938
doc_pred=_make_doc_with_bbox(left=11.53),
@@ -78,3 +87,29 @@ def test_verify_docitems_rejects_bbox_presence_mismatch():
7887
fuzzy=False,
7988
pdf_filename="fixture.json",
8089
)
90+
91+
92+
def test_verify_docitems_rejects_picture_count_mismatch():
93+
doc_true = _make_doc_with_picture(image_size=(2, 2))
94+
doc_pred = DoclingDocument(name="test")
95+
96+
with pytest.raises(AssertionError, match="Picture lengths do not match"):
97+
verify_docitems(
98+
doc_pred=doc_pred,
99+
doc_true=doc_true,
100+
fuzzy=False,
101+
pdf_filename="fixture.json",
102+
)
103+
104+
105+
def test_verify_docitems_uses_predicted_picture_image():
106+
doc_true = _make_doc_with_picture(image_size=(2, 2))
107+
doc_pred = _make_doc_with_picture(image_size=(3, 2))
108+
109+
with pytest.raises(AssertionError):
110+
verify_docitems(
111+
doc_pred=doc_pred,
112+
doc_true=doc_true,
113+
fuzzy=False,
114+
pdf_filename="fixture.json",
115+
)

tests/verify_utils.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,9 @@ def verify_docitems(
276276
assert len(doc_true.tables) == len(doc_pred.tables), (
277277
f"[{pdf_filename}] document has different count of tables than expected."
278278
)
279+
assert len(doc_true.pictures) == len(doc_pred.pictures), (
280+
f"[{pdf_filename}] Picture lengths do not match: {len(doc_true.pictures)} != {len(doc_pred.pictures)}"
281+
)
279282

280283
for (true_item, _true_level), (pred_item, _pred_level) in zip(
281284
doc_true.iterate_items(), doc_pred.iterate_items()
@@ -359,7 +362,7 @@ def verify_docitems(
359362
)
360363

361364
true_image = true_item.get_image(doc=doc_true)
362-
pred_image = true_item.get_image(doc=doc_pred)
365+
pred_image = pred_item.get_image(doc=doc_pred)
363366
if true_image is not None:
364367
assert verify_picture_image_v2(true_image, pred_image), (
365368
f"[{pdf_filename}] Picture image mismatch"

0 commit comments

Comments
 (0)