|
1 | 1 | """Regression tests for CVAT to DoclingDocument conversion.""" |
2 | 2 |
|
3 | | -import difflib |
4 | 3 | import json |
5 | 4 | import os |
6 | 5 | from pathlib import Path |
|
14 | 13 | from docling_cvat_tools.visualisation.visualisations import save_single_document_html |
15 | 14 |
|
16 | 15 |
|
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 |
41 | 32 |
|
42 | 33 |
|
43 | 34 | def load_metadata(fixture_dir: Path) -> dict[str, Any]: |
@@ -205,16 +196,15 @@ def test_cvat_to_docling_regression(fixture_dir: Path) -> None: |
205 | 196 | actual_doc._normalize_references() |
206 | 197 | expected_doc._normalize_references() |
207 | 198 |
|
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 |
210 | 204 |
|
211 | 205 | # Get observation status |
212 | 206 | observation_status = metadata.get("observation_status", "unknown") |
213 | 207 |
|
214 | | - # Print diff if mismatch |
215 | | - if not matches: |
216 | | - print_doc_diff(actual_doc, expected_doc, fixture_dir.name) |
217 | | - |
218 | 208 | # Handle broken tests |
219 | 209 | if matches and observation_status == "broken": |
220 | 210 | # Reproduced the known broken behavior - expected failure |
|
0 commit comments