|
| 1 | +import os |
| 2 | +import pytest |
| 3 | + |
| 4 | +import docx |
| 5 | + |
| 6 | +from unstructured.documents.elements import Address, ListItem, NarrativeText, Title, Text |
| 7 | +from unstructured.partition.common import convert_office_doc |
| 8 | +from unstructured.partition.doc import partition_doc |
| 9 | +from unstructured.partition.docx import partition_docx |
| 10 | + |
| 11 | + |
| 12 | +@pytest.fixture |
| 13 | +def mock_document(): |
| 14 | + document = docx.Document() |
| 15 | + |
| 16 | + document.add_paragraph("These are a few of my favorite things:", style="Heading 1") |
| 17 | + # NOTE(robinson) - this should get picked up as a list item due to the • |
| 18 | + document.add_paragraph("• Parrots", style="Normal") |
| 19 | + # NOTE(robinson) - this should get dropped because it's empty |
| 20 | + document.add_paragraph("• ", style="Normal") |
| 21 | + document.add_paragraph("Hockey", style="List Bullet") |
| 22 | + # NOTE(robinson) - this should get dropped because it's empty |
| 23 | + document.add_paragraph("", style="List Bullet") |
| 24 | + # NOTE(robinson) - this should get picked up as a title |
| 25 | + document.add_paragraph("Analysis", style="Normal") |
| 26 | + # NOTE(robinson) - this should get dropped because it is empty |
| 27 | + document.add_paragraph("", style="Normal") |
| 28 | + # NOTE(robinson) - this should get picked up as a narrative text |
| 29 | + document.add_paragraph("This is my first thought. This is my second thought.", style="Normal") |
| 30 | + document.add_paragraph("This is my third thought.", style="Body Text") |
| 31 | + # NOTE(robinson) - this should just be regular text |
| 32 | + document.add_paragraph("2023") |
| 33 | + # NOTE(robinson) - this should be an address |
| 34 | + document.add_paragraph("DOYLESTOWN, PA 18901") |
| 35 | + |
| 36 | + return document |
| 37 | + |
| 38 | + |
| 39 | +@pytest.fixture |
| 40 | +def expected_elements(): |
| 41 | + return [ |
| 42 | + Title("These are a few of my favorite things:"), |
| 43 | + ListItem("Parrots"), |
| 44 | + ListItem("Hockey"), |
| 45 | + Title("Analysis"), |
| 46 | + NarrativeText("This is my first thought. This is my second thought."), |
| 47 | + NarrativeText("This is my third thought."), |
| 48 | + Text("2023"), |
| 49 | + Address("DOYLESTOWN, PA 18901"), |
| 50 | + ] |
| 51 | + |
| 52 | + |
| 53 | +def test_partition_doc_with_filename(mock_document, expected_elements, tmpdir): |
| 54 | + docx_filename = os.path.join(tmpdir.dirname, "mock_document.docx") |
| 55 | + doc_filename = os.path.join(tmpdir.dirname, "mock_document.doc") |
| 56 | + mock_document.save(docx_filename) |
| 57 | + convert_office_doc(docx_filename, tmpdir.dirname, "doc") |
| 58 | + |
| 59 | + elements = partition_doc(filename=doc_filename) |
| 60 | + assert elements == expected_elements |
| 61 | + |
| 62 | + |
| 63 | +def test_partition_doc_matches_partition_docx(mock_document, expected_elements, tmpdir): |
| 64 | + docx_filename = os.path.join(tmpdir.dirname, "mock_document.docx") |
| 65 | + doc_filename = os.path.join(tmpdir.dirname, "mock_document.doc") |
| 66 | + mock_document.save(docx_filename) |
| 67 | + convert_office_doc(docx_filename, tmpdir.dirname, "doc") |
| 68 | + |
| 69 | + partition_doc(filename=doc_filename) == partition_docx(filename=docx_filename) |
| 70 | + |
| 71 | + |
| 72 | +def test_partition_raises_with_missing_doc(mock_document, expected_elements, tmpdir): |
| 73 | + doc_filename = os.path.join(tmpdir.dirname, "asdf.doc") |
| 74 | + |
| 75 | + with pytest.raises(ValueError): |
| 76 | + partition_doc(filename=doc_filename) |
| 77 | + |
| 78 | + |
| 79 | +def test_partition_doc_with_file(mock_document, expected_elements, tmpdir): |
| 80 | + docx_filename = os.path.join(tmpdir.dirname, "mock_document.docx") |
| 81 | + doc_filename = os.path.join(tmpdir.dirname, "mock_document.doc") |
| 82 | + mock_document.save(docx_filename) |
| 83 | + convert_office_doc(docx_filename, tmpdir.dirname, "doc") |
| 84 | + |
| 85 | + with open(doc_filename, "rb") as f: |
| 86 | + elements = partition_doc(file=f) |
| 87 | + assert elements == expected_elements |
| 88 | + |
| 89 | + |
| 90 | +def test_partition_doc_raises_with_both_specified(mock_document, tmpdir): |
| 91 | + docx_filename = os.path.join(tmpdir.dirname, "mock_document.docx") |
| 92 | + doc_filename = os.path.join(tmpdir.dirname, "mock_document.doc") |
| 93 | + mock_document.save(docx_filename) |
| 94 | + convert_office_doc(docx_filename, tmpdir.dirname, "doc") |
| 95 | + |
| 96 | + with open(doc_filename, "rb") as f: |
| 97 | + with pytest.raises(ValueError): |
| 98 | + partition_doc(filename=doc_filename, file=f) |
| 99 | + |
| 100 | + |
| 101 | +def test_partition_doc_raises_with_neither(): |
| 102 | + with pytest.raises(ValueError): |
| 103 | + partition_doc() |
0 commit comments