|
| 1 | +import os |
| 2 | +import pathlib |
| 3 | +import tempfile |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | +from unstructured.partition.auto import partition |
| 8 | +from unstructured.partition.json import partition_json |
| 9 | +from unstructured.staging.base import elements_to_json |
| 10 | + |
| 11 | +DIRECTORY = pathlib.Path(__file__).parent.resolve() |
| 12 | + |
| 13 | +test_files = [ |
| 14 | + "fake-text.txt", |
| 15 | + "layout-parser-paper-fast.pdf", |
| 16 | + "fake-html.html", |
| 17 | + "fake.doc", |
| 18 | + "fake-email.eml", |
| 19 | + "fake-power-point.ppt", |
| 20 | + "fake.docx", |
| 21 | + "fake-power-point.pptx", |
| 22 | +] |
| 23 | + |
| 24 | + |
| 25 | +@pytest.mark.parametrize("filename", test_files) |
| 26 | +def test_partition_json_from_filename(filename: str): |
| 27 | + path = os.path.join(DIRECTORY, "..", "..", "example-docs", filename) |
| 28 | + elements = partition(filename=path) |
| 29 | + |
| 30 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 31 | + test_path = os.path.join(tmpdir, filename + ".json") |
| 32 | + elements_to_json(elements, filename=test_path, indent=2) |
| 33 | + test_elements = partition_json(filename=test_path) |
| 34 | + |
| 35 | + assert len(elements) > 0 |
| 36 | + assert len(str(elements[0])) > 0 |
| 37 | + |
| 38 | + assert len(elements) == len(test_elements) |
| 39 | + for i in range(len(elements)): |
| 40 | + assert elements[i] == test_elements[i] |
| 41 | + |
| 42 | + |
| 43 | +@pytest.mark.parametrize("filename", test_files) |
| 44 | +def test_partition_json_from_file(filename: str): |
| 45 | + path = os.path.join(DIRECTORY, "..", "..", "example-docs", filename) |
| 46 | + elements = partition(filename=path) |
| 47 | + |
| 48 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 49 | + test_path = os.path.join(tmpdir, filename + ".json") |
| 50 | + elements_to_json(elements, filename=test_path, indent=2) |
| 51 | + with open(test_path) as f: |
| 52 | + test_elements = partition_json(file=f) |
| 53 | + |
| 54 | + assert len(elements) > 0 |
| 55 | + assert len(str(elements[0])) > 0 |
| 56 | + |
| 57 | + assert len(elements) == len(test_elements) |
| 58 | + for i in range(len(elements)): |
| 59 | + assert elements[i] == test_elements[i] |
| 60 | + |
| 61 | + |
| 62 | +@pytest.mark.parametrize("filename", test_files) |
| 63 | +def test_partition_json_from_text(filename: str): |
| 64 | + path = os.path.join(DIRECTORY, "..", "..", "example-docs", filename) |
| 65 | + elements = partition(filename=path) |
| 66 | + |
| 67 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 68 | + test_path = os.path.join(tmpdir, filename + ".json") |
| 69 | + elements_to_json(elements, filename=test_path, indent=2) |
| 70 | + with open(test_path) as f: |
| 71 | + text = f.read() |
| 72 | + test_elements = partition_json(text=text) |
| 73 | + |
| 74 | + assert len(elements) > 0 |
| 75 | + assert len(str(elements[0])) > 0 |
| 76 | + |
| 77 | + assert len(elements) == len(test_elements) |
| 78 | + for i in range(len(elements)): |
| 79 | + assert elements[i] == test_elements[i] |
| 80 | + |
| 81 | + |
| 82 | +def test_partition_json_raises_with_none_specified(): |
| 83 | + with pytest.raises(ValueError): |
| 84 | + partition_json() |
| 85 | + |
| 86 | + |
| 87 | +def test_partition_json_raises_with_too_many_specified(): |
| 88 | + path = os.path.join(DIRECTORY, "..", "..", "example-docs", "fake-text.txt") |
| 89 | + elements = partition(filename=path) |
| 90 | + |
| 91 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 92 | + test_path = os.path.join(tmpdir, "fake-text.txt.json") |
| 93 | + elements_to_json(elements, filename=test_path, indent=2) |
| 94 | + with open(test_path) as f: |
| 95 | + text = f.read() |
| 96 | + |
| 97 | + with pytest.raises(ValueError): |
| 98 | + partition_json(filename=test_path, file=f) |
| 99 | + |
| 100 | + with pytest.raises(ValueError): |
| 101 | + partition_json(filename=test_path, text=text) |
| 102 | + |
| 103 | + with pytest.raises(ValueError): |
| 104 | + partition_json(file=f, text=text) |
| 105 | + |
| 106 | + with pytest.raises(ValueError): |
| 107 | + partition_json(filename=test_path, file=f, text=text) |
0 commit comments