2023-02-17 11:57:08 -05:00
|
|
|
import os
|
|
|
|
import pathlib
|
2023-02-27 17:30:54 +01:00
|
|
|
|
2023-02-17 11:57:08 -05:00
|
|
|
import pytest
|
|
|
|
|
|
|
|
from unstructured.documents.elements import ListItem, NarrativeText, Title
|
2023-02-27 17:30:54 +01:00
|
|
|
from unstructured.partition.ppt import partition_ppt
|
2023-02-17 11:57:08 -05:00
|
|
|
|
|
|
|
DIRECTORY = pathlib.Path(__file__).parent.resolve()
|
|
|
|
EXAMPLE_DOCS_DIRECTORY = os.path.join(DIRECTORY, "..", "..", "example-docs")
|
|
|
|
|
|
|
|
EXPECTED_PPT_OUTPUT = [
|
|
|
|
Title(text="Adding a Bullet Slide"),
|
|
|
|
ListItem(text="Find the bullet slide layout"),
|
|
|
|
ListItem(text="Use _TextFrame.text for first bullet"),
|
|
|
|
ListItem(text="Use _TextFrame.add_paragraph() for subsequent bullets"),
|
|
|
|
NarrativeText(text="Here is a lot of text!"),
|
|
|
|
NarrativeText(text="Here is some text in a text box!"),
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
def test_partition_ppt_from_filename():
|
|
|
|
filename = os.path.join(EXAMPLE_DOCS_DIRECTORY, "fake-power-point.ppt")
|
|
|
|
elements = partition_ppt(filename=filename)
|
|
|
|
assert elements == EXPECTED_PPT_OUTPUT
|
2023-07-05 15:02:22 -05:00
|
|
|
for element in elements:
|
|
|
|
assert element.metadata.filename == "fake-power-point.ppt"
|
|
|
|
|
|
|
|
|
|
|
|
def test_partition_ppt_from_filename_with_metadata_filename():
|
|
|
|
filename = os.path.join(EXAMPLE_DOCS_DIRECTORY, "fake-power-point.ppt")
|
|
|
|
elements = partition_ppt(filename=filename, metadata_filename="test")
|
|
|
|
assert all(element.metadata.filename == "test" for element in elements)
|
2023-02-17 11:57:08 -05:00
|
|
|
|
|
|
|
|
|
|
|
def test_partition_ppt_raises_with_missing_file():
|
|
|
|
filename = os.path.join(EXAMPLE_DOCS_DIRECTORY, "doesnt-exist.ppt")
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
partition_ppt(filename=filename)
|
|
|
|
|
|
|
|
|
|
|
|
def test_partition_ppt_from_file():
|
|
|
|
filename = os.path.join(EXAMPLE_DOCS_DIRECTORY, "fake-power-point.ppt")
|
|
|
|
with open(filename, "rb") as f:
|
|
|
|
elements = partition_ppt(file=f)
|
|
|
|
assert elements == EXPECTED_PPT_OUTPUT
|
2023-07-05 15:02:22 -05:00
|
|
|
for element in elements:
|
|
|
|
assert element.metadata.filename is None
|
|
|
|
|
|
|
|
|
|
|
|
def test_partition_ppt_from_file_with_metadata_filename():
|
|
|
|
filename = os.path.join(EXAMPLE_DOCS_DIRECTORY, "fake-power-point.ppt")
|
|
|
|
with open(filename, "rb") as f:
|
|
|
|
elements = partition_ppt(file=f, metadata_filename="test")
|
|
|
|
assert elements == EXPECTED_PPT_OUTPUT
|
|
|
|
for element in elements:
|
|
|
|
assert element.metadata.filename == "test"
|
2023-02-17 11:57:08 -05:00
|
|
|
|
|
|
|
|
|
|
|
def test_partition_ppt_raises_with_both_specified():
|
|
|
|
filename = os.path.join(EXAMPLE_DOCS_DIRECTORY, "fake-power-point.ppt")
|
2023-02-27 17:30:54 +01:00
|
|
|
with open(filename, "rb") as f, pytest.raises(ValueError):
|
|
|
|
partition_ppt(filename=filename, file=f)
|
2023-02-17 11:57:08 -05:00
|
|
|
|
|
|
|
|
|
|
|
def test_partition_ppt_raises_with_neither():
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
partition_ppt()
|
2023-06-30 09:44:46 -05:00
|
|
|
|
|
|
|
|
|
|
|
def test_partition_ppt_from_filename_exclude_metadata():
|
|
|
|
filename = os.path.join(EXAMPLE_DOCS_DIRECTORY, "fake-power-point.ppt")
|
|
|
|
elements = partition_ppt(filename=filename, include_metadata=False)
|
|
|
|
for i in range(len(elements)):
|
|
|
|
assert elements[i].metadata.to_dict() == {}
|
|
|
|
|
|
|
|
|
|
|
|
def test_partition_ppt_from_file_exclude_metadata():
|
|
|
|
filename = os.path.join(EXAMPLE_DOCS_DIRECTORY, "fake-power-point.ppt")
|
|
|
|
with open(filename, "rb") as f:
|
|
|
|
elements = partition_ppt(file=f, include_metadata=False)
|
|
|
|
for i in range(len(elements)):
|
|
|
|
assert elements[i].metadata.to_dict() == {}
|
2023-07-26 15:10:14 -04:00
|
|
|
|
|
|
|
|
|
|
|
def test_partition_ppt_metadata_date(
|
|
|
|
mocker,
|
|
|
|
filename="example-docs/fake-power-point.ppt",
|
|
|
|
):
|
|
|
|
mocked_last_modification_date = "2029-07-05T09:24:28"
|
|
|
|
|
|
|
|
mocker.patch(
|
|
|
|
"unstructured.partition.ppt.get_last_modified_date",
|
|
|
|
return_value=mocked_last_modification_date,
|
|
|
|
)
|
|
|
|
|
|
|
|
elements = partition_ppt(
|
|
|
|
filename=filename,
|
|
|
|
)
|
|
|
|
|
2023-07-31 19:55:43 -07:00
|
|
|
assert elements[0].metadata.last_modified == mocked_last_modification_date
|
2023-07-26 15:10:14 -04:00
|
|
|
|
|
|
|
|
|
|
|
def test_partition_ppt_with_custom_metadata_date(
|
|
|
|
mocker,
|
|
|
|
filename="example-docs/fake-power-point.ppt",
|
|
|
|
):
|
|
|
|
mocked_last_modification_date = "2029-07-05T09:24:28"
|
|
|
|
expected_last_modification_date = "2020-07-05T09:24:28"
|
|
|
|
|
|
|
|
mocker.patch(
|
|
|
|
"unstructured.partition.ppt.get_last_modified_date",
|
|
|
|
return_value=mocked_last_modification_date,
|
|
|
|
)
|
|
|
|
|
|
|
|
elements = partition_ppt(
|
|
|
|
filename=filename,
|
2023-07-31 19:55:43 -07:00
|
|
|
metadata_last_modified=expected_last_modification_date,
|
2023-07-26 15:10:14 -04:00
|
|
|
)
|
|
|
|
|
2023-07-31 19:55:43 -07:00
|
|
|
assert elements[0].metadata.last_modified == expected_last_modification_date
|
2023-07-26 15:10:14 -04:00
|
|
|
|
|
|
|
|
|
|
|
def test_partition_ppt_from_file_metadata_date(
|
|
|
|
mocker,
|
|
|
|
filename="example-docs/fake-power-point.ppt",
|
|
|
|
):
|
|
|
|
mocked_last_modification_date = "2029-07-05T09:24:28"
|
|
|
|
|
|
|
|
mocker.patch(
|
|
|
|
"unstructured.partition.ppt.get_last_modified_date_from_file",
|
|
|
|
return_value=mocked_last_modification_date,
|
|
|
|
)
|
|
|
|
|
|
|
|
with open(filename, "rb") as f:
|
|
|
|
elements = partition_ppt(
|
|
|
|
file=f,
|
|
|
|
)
|
|
|
|
|
2023-07-31 19:55:43 -07:00
|
|
|
assert elements[0].metadata.last_modified == mocked_last_modification_date
|
2023-07-26 15:10:14 -04:00
|
|
|
|
|
|
|
|
|
|
|
def test_partition_ppt_from_file_with_custom_metadata_date(
|
|
|
|
mocker,
|
|
|
|
filename="example-docs/fake-power-point.ppt",
|
|
|
|
):
|
|
|
|
mocked_last_modification_date = "2029-07-05T09:24:28"
|
|
|
|
expected_last_modification_date = "2020-07-05T09:24:28"
|
|
|
|
|
|
|
|
mocker.patch(
|
|
|
|
"unstructured.partition.ppt.get_last_modified_date_from_file",
|
|
|
|
return_value=mocked_last_modification_date,
|
|
|
|
)
|
|
|
|
|
|
|
|
with open(filename, "rb") as f:
|
2023-07-31 19:55:43 -07:00
|
|
|
elements = partition_ppt(file=f, metadata_last_modified=expected_last_modification_date)
|
2023-07-26 15:10:14 -04:00
|
|
|
|
2023-07-31 19:55:43 -07:00
|
|
|
assert elements[0].metadata.last_modified == expected_last_modification_date
|