2023-06-15 13:50:53 -05:00
|
|
|
from test_unstructured.partition.test_constants import EXPECTED_TABLE, EXPECTED_TEXT
|
2023-05-16 15:40:40 -04:00
|
|
|
from unstructured.cleaners.core import clean_extra_whitespace
|
|
|
|
from unstructured.documents.elements import Table
|
|
|
|
from unstructured.partition.xlsx import partition_xlsx
|
|
|
|
|
|
|
|
EXPECTED_FILETYPE = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
|
|
|
|
|
2023-05-18 16:53:23 +03:00
|
|
|
EXCEPTED_PAGE_NAME = "Stanley Cups"
|
|
|
|
|
2023-05-16 15:40:40 -04:00
|
|
|
|
|
|
|
def test_partition_xlsx_from_filename(filename="example-docs/stanley-cups.xlsx"):
|
|
|
|
elements = partition_xlsx(filename=filename)
|
|
|
|
|
|
|
|
assert all(isinstance(element, Table) for element in elements)
|
|
|
|
assert len(elements) == 2
|
|
|
|
|
|
|
|
assert clean_extra_whitespace(elements[0].text) == EXPECTED_TEXT
|
|
|
|
assert elements[0].metadata.text_as_html == EXPECTED_TABLE
|
|
|
|
assert elements[0].metadata.page_number == 1
|
|
|
|
assert elements[0].metadata.filetype == EXPECTED_FILETYPE
|
2023-05-18 16:53:23 +03:00
|
|
|
assert elements[0].metadata.page_name == EXCEPTED_PAGE_NAME
|
2023-07-05 15:02:22 -05:00
|
|
|
assert elements[0].metadata.filename == "stanley-cups.xlsx"
|
|
|
|
|
|
|
|
|
|
|
|
def test_partition_xlsx_from_filename_with_metadata_filename(
|
|
|
|
filename="example-docs/stanley-cups.xlsx",
|
|
|
|
):
|
|
|
|
elements = partition_xlsx(filename=filename, metadata_filename="test")
|
|
|
|
|
|
|
|
assert all(isinstance(element, Table) for element in elements)
|
|
|
|
assert clean_extra_whitespace(elements[0].text) == EXPECTED_TEXT
|
|
|
|
assert elements[0].metadata.filename == "test"
|
2023-05-16 15:40:40 -04:00
|
|
|
|
|
|
|
|
|
|
|
def test_partition_xlsx_from_file(filename="example-docs/stanley-cups.xlsx"):
|
|
|
|
with open(filename, "rb") as f:
|
|
|
|
elements = partition_xlsx(file=f)
|
|
|
|
|
|
|
|
assert all(isinstance(element, Table) for element in elements)
|
|
|
|
assert len(elements) == 2
|
|
|
|
assert clean_extra_whitespace(elements[0].text) == EXPECTED_TEXT
|
|
|
|
assert elements[0].metadata.text_as_html == EXPECTED_TABLE
|
|
|
|
assert elements[0].metadata.page_number == 1
|
|
|
|
assert elements[0].metadata.filetype == EXPECTED_FILETYPE
|
2023-05-18 16:53:23 +03:00
|
|
|
assert elements[0].metadata.page_name == EXCEPTED_PAGE_NAME
|
2023-07-05 15:02:22 -05:00
|
|
|
assert elements[0].metadata.filename is None
|
|
|
|
|
|
|
|
|
|
|
|
def test_partition_xlsx_from_file_with_metadata_filename(filename="example-docs/stanley-cups.xlsx"):
|
|
|
|
with open(filename, "rb") as f:
|
|
|
|
elements = partition_xlsx(file=f, metadata_filename="test")
|
|
|
|
|
|
|
|
assert all(isinstance(element, Table) for element in elements)
|
|
|
|
assert clean_extra_whitespace(elements[0].text) == EXPECTED_TEXT
|
|
|
|
assert elements[0].metadata.filename == "test"
|
2023-05-16 15:40:40 -04:00
|
|
|
|
|
|
|
|
2023-06-30 09:44:46 -05:00
|
|
|
def test_partition_xlsx_filename_exclude_metadata(filename="example-docs/stanley-cups.xlsx"):
|
2023-05-16 15:40:40 -04:00
|
|
|
elements = partition_xlsx(filename=filename, include_metadata=False)
|
|
|
|
|
|
|
|
assert all(isinstance(element, Table) for element in elements)
|
|
|
|
assert len(elements) == 2
|
|
|
|
|
|
|
|
assert clean_extra_whitespace(elements[0].text) == EXPECTED_TEXT
|
|
|
|
assert elements[0].metadata.text_as_html is None
|
|
|
|
assert elements[0].metadata.page_number is None
|
|
|
|
assert elements[0].metadata.filetype is None
|
2023-05-18 16:53:23 +03:00
|
|
|
assert elements[0].metadata.page_name is None
|
2023-07-05 15:02:22 -05:00
|
|
|
assert elements[0].metadata.filename is None
|
2023-06-30 09:44:46 -05:00
|
|
|
|
|
|
|
|
|
|
|
def test_partition_xlsx_from_file_exclude_metadata(filename="example-docs/stanley-cups.xlsx"):
|
|
|
|
with open(filename, "rb") as f:
|
|
|
|
elements = partition_xlsx(file=f, include_metadata=False)
|
|
|
|
|
|
|
|
assert all(isinstance(element, Table) for element in elements)
|
|
|
|
assert len(elements) == 2
|
|
|
|
|
|
|
|
assert clean_extra_whitespace(elements[0].text) == EXPECTED_TEXT
|
|
|
|
assert elements[0].metadata.text_as_html is None
|
|
|
|
assert elements[0].metadata.page_number is None
|
|
|
|
assert elements[0].metadata.filetype is None
|
|
|
|
assert elements[0].metadata.page_name is None
|
2023-07-05 15:02:22 -05:00
|
|
|
assert elements[0].metadata.filename is None
|
2023-07-26 15:10:14 -04:00
|
|
|
|
|
|
|
|
|
|
|
def test_partition_xlsx_metadata_date(
|
|
|
|
mocker,
|
|
|
|
filename="example-docs/stanley-cups.xlsx",
|
|
|
|
):
|
|
|
|
mocked_last_modification_date = "2029-07-05T09:24:28"
|
|
|
|
|
|
|
|
mocker.patch(
|
|
|
|
"unstructured.partition.xlsx.get_last_modified_date",
|
|
|
|
return_value=mocked_last_modification_date,
|
|
|
|
)
|
|
|
|
|
|
|
|
elements = partition_xlsx(
|
|
|
|
filename=filename,
|
|
|
|
)
|
|
|
|
|
|
|
|
assert elements[0].metadata.date == mocked_last_modification_date
|
|
|
|
|
|
|
|
|
|
|
|
def test_partition_xlsx_with_custom_metadata_date(
|
|
|
|
mocker,
|
|
|
|
filename="example-docs/stanley-cups.xlsx",
|
|
|
|
):
|
|
|
|
mocked_last_modification_date = "2029-07-05T09:24:28"
|
|
|
|
expected_last_modification_date = "2020-07-05T09:24:28"
|
|
|
|
|
|
|
|
mocker.patch(
|
|
|
|
"unstructured.partition.xlsx.get_last_modified_date",
|
|
|
|
return_value=mocked_last_modification_date,
|
|
|
|
)
|
|
|
|
|
|
|
|
elements = partition_xlsx(
|
|
|
|
filename=filename,
|
|
|
|
metadata_date=expected_last_modification_date,
|
|
|
|
)
|
|
|
|
|
|
|
|
assert elements[0].metadata.date == expected_last_modification_date
|
|
|
|
|
|
|
|
|
|
|
|
def test_partition_xlsx_from_file_metadata_date(
|
|
|
|
mocker,
|
|
|
|
filename="example-docs/stanley-cups.xlsx",
|
|
|
|
):
|
|
|
|
mocked_last_modification_date = "2029-07-05T09:24:28"
|
|
|
|
|
|
|
|
mocker.patch(
|
|
|
|
"unstructured.partition.xlsx.get_last_modified_date_from_file",
|
|
|
|
return_value=mocked_last_modification_date,
|
|
|
|
)
|
|
|
|
|
|
|
|
with open(filename, "rb") as f:
|
|
|
|
elements = partition_xlsx(
|
|
|
|
file=f,
|
|
|
|
)
|
|
|
|
|
|
|
|
assert elements[0].metadata.date == mocked_last_modification_date
|
|
|
|
|
|
|
|
|
|
|
|
def test_partition_xlsx_from_file_with_custom_metadata_date(
|
|
|
|
mocker,
|
|
|
|
filename="example-docs/stanley-cups.xlsx",
|
|
|
|
):
|
|
|
|
mocked_last_modification_date = "2029-07-05T09:24:28"
|
|
|
|
expected_last_modification_date = "2020-07-05T09:24:28"
|
|
|
|
|
|
|
|
mocker.patch(
|
|
|
|
"unstructured.partition.xlsx.get_last_modified_date_from_file",
|
|
|
|
return_value=mocked_last_modification_date,
|
|
|
|
)
|
|
|
|
|
|
|
|
with open(filename, "rb") as f:
|
|
|
|
elements = partition_xlsx(file=f, metadata_date=expected_last_modification_date)
|
|
|
|
|
|
|
|
assert elements[0].metadata.date == expected_last_modification_date
|