Test table of contents extraction from PDF documents.
()
| 942 | |
| 943 | |
| 944 | def test_table_of_contents(): |
| 945 | """Test table of contents extraction from PDF documents.""" |
| 946 | parser = DoclingPdfParser(loglevel="fatal") |
| 947 | |
| 948 | # Test with a PDF that has a TOC |
| 949 | pdf_doc = parser.load( |
| 950 | path_or_stream="tests/data/regression/table_of_contents_01.pdf", lazy=True |
| 951 | ) |
| 952 | |
| 953 | # Test get_table_of_contents() method |
| 954 | toc = pdf_doc.get_table_of_contents() |
| 955 | assert toc is not None, "TOC should not be None for table_of_contents_01.pdf" |
| 956 | assert toc.text == "<root>", "Root TOC entry should have text '<root>'" |
| 957 | assert toc.children is not None, "Root TOC should have children" |
| 958 | assert len(toc.children) > 0, "Root TOC should have at least one child" |
| 959 | |
| 960 | # Verify expected top-level entries exist |
| 961 | top_level_titles = [child.text for child in toc.children] |
| 962 | assert "Introduction" in top_level_titles, "TOC should contain 'Introduction'" |
| 963 | assert "Model Architecture" in top_level_titles, ( |
| 964 | "TOC should contain 'Model Architecture'" |
| 965 | ) |
| 966 | assert "Conclusion" in top_level_titles, "TOC should contain 'Conclusion'" |
| 967 | |
| 968 | # Verify nested structure exists |
| 969 | model_arch_entry = next( |
| 970 | (child for child in toc.children if child.text == "Model Architecture"), None |
| 971 | ) |
| 972 | assert model_arch_entry is not None, "Should find 'Model Architecture' entry" |
| 973 | assert model_arch_entry.children is not None, ( |
| 974 | "'Model Architecture' should have children" |
| 975 | ) |
| 976 | assert len(model_arch_entry.children) >= 2, ( |
| 977 | "'Model Architecture' should have at least 2 children" |
| 978 | ) |
| 979 | |
| 980 | nested_titles = [child.text for child in model_arch_entry.children] |
| 981 | assert "Dense Models" in nested_titles, "Should contain 'Dense Models' nested entry" |
| 982 | assert "Mixture-of-Expert models" in nested_titles, ( |
| 983 | "Should contain 'Mixture-of-Expert models' nested entry" |
| 984 | ) |
| 985 | |
| 986 | # Test caching - calling again should return same instance |
| 987 | toc2 = pdf_doc.get_table_of_contents() |
| 988 | assert toc is toc2, "get_table_of_contents should return cached instance" |
| 989 | |
| 990 | # Test get_annotations().table_of_contents |
| 991 | annotations = pdf_doc.get_annotations() |
| 992 | assert annotations is not None, "Annotations should not be None" |
| 993 | assert annotations.table_of_contents is not None, ( |
| 994 | "annotations.table_of_contents should not be None" |
| 995 | ) |
| 996 | assert len(annotations.table_of_contents) > 0, ( |
| 997 | "annotations.table_of_contents should have entries" |
| 998 | ) |
| 999 | |
| 1000 | # Verify PdfTocEntry structure |
| 1001 | first_entry = annotations.table_of_contents[0] |
nothing calls this directly
no test coverage detected