Skip to content

Commit e68d437

Browse files
committed
Fix test comparison
Signed-off-by: Christoph Auer <cau@zurich.ibm.com>
1 parent 6637c7e commit e68d437

1 file changed

Lines changed: 21 additions & 31 deletions

File tree

tests/cvat_to_docling/test_regression.py

Lines changed: 21 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
"""Regression tests for CVAT to DoclingDocument conversion."""
22

3-
import difflib
43
import json
54
import os
65
from pathlib import Path
@@ -14,30 +13,22 @@
1413
from docling_cvat_tools.visualisation.visualisations import save_single_document_html
1514

1615

17-
def print_doc_diff(
18-
actual: DoclingDocument, expected: DoclingDocument, test_name: str
19-
) -> None:
20-
"""Print a concise diff between two DoclingDocuments."""
21-
actual_json = json.dumps(actual.export_to_dict(), indent=2, sort_keys=True)
22-
expected_json = json.dumps(expected.export_to_dict(), indent=2, sort_keys=True)
23-
24-
diff = difflib.unified_diff(
25-
expected_json.splitlines(keepends=True),
26-
actual_json.splitlines(keepends=True),
27-
fromfile=f"expected ({test_name})",
28-
tofile=f"actual ({test_name})",
29-
lineterm="",
30-
)
31-
32-
diff_lines = list(diff)
33-
if diff_lines:
34-
print(f"\n{'=' * 80}")
35-
print(f"DIFF for {test_name}:")
36-
print("=" * 80)
37-
print("".join(diff_lines[:100])) # Limit to first 100 lines
38-
if len(diff_lines) > 100:
39-
print(f"\n... ({len(diff_lines) - 100} more diff lines truncated)")
40-
print("=" * 80 + "\n")
16+
def strip_image_uris(d):
17+
"""Strip image URIs from dict for platform-independent comparison.
18+
19+
Adopted from docling-core tests - images are platform-dependent due to
20+
rendering differences (fonts, anti-aliasing, etc.) between macOS and Linux.
21+
"""
22+
if isinstance(d, dict):
23+
return {
24+
k: strip_image_uris(v)
25+
for k, v in d.items()
26+
if k not in {"uri", "image_uri"}
27+
}
28+
elif isinstance(d, list):
29+
return [strip_image_uris(x) for x in d]
30+
else:
31+
return d
4132

4233

4334
def load_metadata(fixture_dir: Path) -> dict[str, Any]:
@@ -205,16 +196,15 @@ def test_cvat_to_docling_regression(fixture_dir: Path) -> None:
205196
actual_doc._normalize_references()
206197
expected_doc._normalize_references()
207198

208-
# Compare using DoclingDocument equality
209-
matches = actual_doc == expected_doc
199+
# Compare using stripped dicts (ignore image URIs - platform-dependent rendering)
200+
# Following docling-core test pattern: "test was flaky due to URIs"
201+
actual_stripped = strip_image_uris(actual_doc.export_to_dict())
202+
expected_stripped = strip_image_uris(expected_doc.export_to_dict())
203+
matches = actual_stripped == expected_stripped
210204

211205
# Get observation status
212206
observation_status = metadata.get("observation_status", "unknown")
213207

214-
# Print diff if mismatch
215-
if not matches:
216-
print_doc_diff(actual_doc, expected_doc, fixture_dir.name)
217-
218208
# Handle broken tests
219209
if matches and observation_status == "broken":
220210
# Reproduced the known broken behavior - expected failure

0 commit comments

Comments
 (0)