#![cfg(feature = "ml")]
#[test]
fn text_layer_matches_no_ocr() {
let bytes = std::fs::read(concat!(
env!("CARGO_MANIFEST_DIR"),
"/../../tests/data/pdf/sources/code_and_formula.pdf"
))
.expect("corpus pdf");
let text_layer = docling_pdf::convert_text_layer(&bytes, "code_and_formula.pdf")
.expect("text-layer conversion");
let no_ocr = docling_pdf::convert_with_options(
&bytes,
None,
"code_and_formula.pdf",
true, true, false, false, docling_pdf::EnrichmentOptions::default(),
None,
None,
)
.expect("no_ocr conversion");
let a = text_layer.export_to_markdown();
assert!(!a.trim().is_empty(), "text layer should extract");
assert_eq!(a, no_ocr.export_to_markdown());
}
#[test]
fn scanned_page_extracts_table_and_keeps_chart() {
let root = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("../..");
for needed in [
".models/layout_heron_int8.onnx",
".models/ocr_rec_en.onnx",
".models/tableformer/encoder.onnx",
] {
if !root.join(needed).exists() {
eprintln!("skipping scanned-table e2e: {needed} not found");
return;
}
}
std::env::set_current_dir(&root).expect("chdir to repo root");
let bytes = std::fs::read(concat!(
env!("CARGO_MANIFEST_DIR"),
"/../../tests/data/scanned/sources/scanned_chart_table.pdf"
))
.expect("scanned fixture");
let doc =
docling_pdf::convert(&bytes, None, "scanned_chart_table.pdf").expect("scanned conversion");
let md = doc.export_to_markdown();
assert!(
md.contains("<!-- image -->"),
"uncaptioned chart must keep its picture crop:\n{md}"
);
let rows: Vec<&str> = md.lines().filter(|l| l.starts_with('|')).collect();
assert!(
rows.len() >= 9,
"expected a header + 7-row table, got {} pipe rows:\n{md}",
rows.len()
);
for cell in ["Aquifer", "Guarani", "Nubian", "380", "540"] {
assert!(md.contains(cell), "table cell {cell:?} missing:\n{md}");
}
}
#[test]
fn scanned_pdf_yields_empty_document() {
let scan = std::fs::read(
std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
.join("../../tests/data/scanned/sources/ocr_test_raster.pdf"),
)
.unwrap();
let doc = docling_pdf::convert_text_layer(&scan, "scan.pdf").expect("no error");
assert!(doc.nodes.is_empty());
}
#[test]
fn heif_without_feature_reports_clearly() {
let stub = b"\x00\x00\x00\x18ftypheic\0\0\0\0";
let err = docling_pdf::convert_image(stub, "photo.heic").unwrap_err();
let msg = err.to_string();
#[cfg(not(feature = "heif"))]
assert!(msg.contains("--features heif"), "unexpected error: {msg}");
#[cfg(feature = "heif")]
assert!(msg.contains("heif"), "unexpected error: {msg}");
}
#[test]
fn picture_classifier_tells_pictures_apart() {
let root = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("../..");
let model = root.join(".models/picture_classifier.onnx");
if !model.exists() {
eprintln!(
"skipping picture-classifier check: {} not found",
model.display()
);
return;
}
std::env::set_var("DOCLING_PICTURE_CLASSIFIER_ONNX", &model);
let mut classifier =
docling_pdf::enrich::PictureClassifier::load_with(1).expect("classifier loads");
for (file, class) in [
(
"latex/sources/2310.06825/images/230927_bars.png",
"bar_chart",
),
(
"latex/sources/1706.03762/Figures/ModalNet-21.png",
"flow_chart",
),
("scanned/sources/qr_bill_example.jpg", "qr_code"),
] {
let img = image::open(root.join("tests/data").join(file))
.expect("fixture image")
.to_rgb8();
let preds = classifier.classify(&img).expect("classify");
let top = &preds[0];
assert!(
top.class_name == class && top.confidence > 0.9,
"{file}: expected {class}, got {} {:.3}",
top.class_name,
top.confidence
);
}
}
#[test]
fn ocr_cells_carry_their_confidence() {
if !std::path::Path::new(concat!(
env!("CARGO_MANIFEST_DIR"),
"/../../.models/ocr_det.onnx"
))
.exists()
|| !std::path::Path::new(concat!(
env!("CARGO_MANIFEST_DIR"),
"/../../.models/layout_heron.onnx"
))
.exists()
{
eprintln!("skip: OCR/layout models not present");
return;
}
let pdf = b"%PDF-1.4\n1 0 obj\n<< /Type /Catalog /Pages 2 0 R >>\nendobj\n\
2 0 obj\n<< /Type /Pages /Kids [3 0 R] /Count 1 >>\nendobj\n\
3 0 obj\n<< /Type /Page /Parent 2 0 R /MediaBox [0 0 600 300] /Contents 4 0 R /Resources << /Font << /F1 5 0 R >> >> >>\nendobj\n\
4 0 obj\n<< /Length 46 >>\nstream\nBT /F1 18 Tf 1 0 0 1 30 240 Tm (VISIBLE) Tj ET\nendstream\nendobj\n\
5 0 obj\n<< /Type /Font /Subtype /Type1 /BaseFont /Helvetica >>\nendobj\n\
trailer\n<< /Size 6 /Root 1 0 R >>\n%%EOF\n";
let convert = |no_ocr: bool| {
docling_pdf::convert_with_options(
pdf,
None,
"fixture.pdf",
true,
no_ocr,
!no_ocr, false,
docling_pdf::EnrichmentOptions::default(),
None,
None,
)
.expect("conversion")
};
let doc = convert(false);
let report = doc.confidence.as_ref().expect("confidence report");
let cells = report.ocr_cells(1);
assert!(!cells.is_empty(), "OCR cells expected");
assert!(
cells.iter().any(|c| c.text.contains("VISIBLE")),
"{cells:?}"
);
assert!(cells.iter().all(|c| (0.0..=1.0).contains(&c.confidence)));
let mean = cells.iter().map(|c| f64::from(c.confidence)).sum::<f64>() / cells.len() as f64;
let ocr_score = report.pages[&1].ocr_score.expect("ocr_score");
assert!((mean - ocr_score).abs() < 1e-6, "{mean} vs {ocr_score}");
assert!(!doc
.export_to_json_value()
.to_string()
.contains("confidence"));
let digital = convert(true);
assert!(digital.confidence.as_ref().unwrap().ocr_cells(1).is_empty());
}