2023-07-26 15:10:14 -04:00
|
|
|
from tempfile import SpooledTemporaryFile
|
|
|
|
|
2023-08-10 16:28:57 -07:00
|
|
|
import pytest
|
|
|
|
|
|
|
|
from test_unstructured.partition.test_constants import (
|
|
|
|
EXPECTED_TABLE,
|
|
|
|
EXPECTED_TABLE_WITH_EMOJI,
|
|
|
|
EXPECTED_TEXT,
|
|
|
|
EXPECTED_TEXT_WITH_EMOJI,
|
|
|
|
)
|
2023-10-12 12:47:55 -07:00
|
|
|
from test_unstructured.unit_utils import assert_round_trips_through_JSON, example_doc_path
|
2023-10-03 09:40:34 -07:00
|
|
|
from unstructured.chunking.title import chunk_by_title
|
2023-05-19 15:57:42 -04:00
|
|
|
from unstructured.cleaners.core import clean_extra_whitespace
|
|
|
|
from unstructured.documents.elements import Table
|
|
|
|
from unstructured.partition.csv import partition_csv
|
2023-10-05 15:26:47 -05:00
|
|
|
from unstructured.partition.utils.constants import UNSTRUCTURED_INCLUDE_DEBUG_METADATA
|
2023-05-19 15:57:42 -04:00
|
|
|
|
|
|
|
EXPECTED_FILETYPE = "text/csv"
|
|
|
|
|
|
|
|
|
2023-08-10 16:28:57 -07:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
("filename", "expected_text", "expected_table"),
|
|
|
|
[
|
|
|
|
("stanley-cups.csv", EXPECTED_TEXT, EXPECTED_TABLE),
|
|
|
|
("stanley-cups-with-emoji.csv", EXPECTED_TEXT_WITH_EMOJI, EXPECTED_TABLE_WITH_EMOJI),
|
|
|
|
],
|
|
|
|
)
|
|
|
|
def test_partition_csv_from_filename(filename, expected_text, expected_table):
|
|
|
|
f_path = f"example-docs/{filename}"
|
|
|
|
elements = partition_csv(filename=f_path)
|
2023-05-19 15:57:42 -04:00
|
|
|
|
2023-08-10 16:28:57 -07:00
|
|
|
assert clean_extra_whitespace(elements[0].text) == expected_text
|
|
|
|
assert elements[0].metadata.text_as_html == expected_table
|
2023-05-19 15:57:42 -04:00
|
|
|
assert elements[0].metadata.filetype == EXPECTED_FILETYPE
|
2023-08-10 16:28:57 -07:00
|
|
|
assert elements[0].metadata.filename == filename
|
2023-07-05 15:02:22 -05:00
|
|
|
|
|
|
|
|
|
|
|
def test_partition_csv_from_filename_with_metadata_filename(
|
|
|
|
filename="example-docs/stanley-cups.csv",
|
|
|
|
):
|
|
|
|
elements = partition_csv(filename=filename, metadata_filename="test")
|
|
|
|
|
|
|
|
assert clean_extra_whitespace(elements[0].text) == EXPECTED_TEXT
|
|
|
|
assert elements[0].metadata.filename == "test"
|
2023-05-19 15:57:42 -04:00
|
|
|
|
|
|
|
|
2023-08-10 16:28:57 -07:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
("filename", "expected_text", "expected_table"),
|
|
|
|
[
|
|
|
|
("stanley-cups.csv", EXPECTED_TEXT, EXPECTED_TABLE),
|
|
|
|
("stanley-cups-with-emoji.csv", EXPECTED_TEXT_WITH_EMOJI, EXPECTED_TABLE_WITH_EMOJI),
|
|
|
|
],
|
|
|
|
)
|
|
|
|
def test_partition_csv_from_file(filename, expected_text, expected_table):
|
|
|
|
f_path = f"example-docs/{filename}"
|
|
|
|
with open(f_path, "rb") as f:
|
2023-05-19 15:57:42 -04:00
|
|
|
elements = partition_csv(file=f)
|
2023-08-10 16:28:57 -07:00
|
|
|
assert clean_extra_whitespace(elements[0].text) == expected_text
|
2023-05-19 15:57:42 -04:00
|
|
|
assert isinstance(elements[0], Table)
|
2023-08-10 16:28:57 -07:00
|
|
|
assert elements[0].metadata.text_as_html == expected_table
|
2023-05-19 15:57:42 -04:00
|
|
|
assert elements[0].metadata.filetype == EXPECTED_FILETYPE
|
2023-07-05 15:02:22 -05:00
|
|
|
assert elements[0].metadata.filename is None
|
2023-10-05 15:26:47 -05:00
|
|
|
if UNSTRUCTURED_INCLUDE_DEBUG_METADATA:
|
|
|
|
assert {element.metadata.detection_origin for element in elements} == {"csv"}
|
2023-07-05 15:02:22 -05:00
|
|
|
|
|
|
|
|
|
|
|
def test_partition_csv_from_file_with_metadata_filename(filename="example-docs/stanley-cups.csv"):
|
|
|
|
with open(filename, "rb") as f:
|
|
|
|
elements = partition_csv(file=f, metadata_filename="test")
|
|
|
|
|
|
|
|
assert clean_extra_whitespace(elements[0].text) == EXPECTED_TEXT
|
|
|
|
assert elements[0].metadata.filename == "test"
|
2023-05-19 15:57:42 -04:00
|
|
|
|
|
|
|
|
|
|
|
def test_partition_csv_can_exclude_metadata(filename="example-docs/stanley-cups.csv"):
|
|
|
|
elements = partition_csv(filename=filename, include_metadata=False)
|
|
|
|
|
|
|
|
assert clean_extra_whitespace(elements[0].text) == EXPECTED_TEXT
|
|
|
|
assert isinstance(elements[0], Table)
|
|
|
|
assert elements[0].metadata.text_as_html is None
|
|
|
|
assert elements[0].metadata.filetype 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_csv_metadata_date(mocker, filename="example-docs/stanley-cups.csv"):
|
|
|
|
mocked_last_modification_date = "2029-07-05T09:24:28"
|
|
|
|
mocker.patch(
|
|
|
|
"unstructured.partition.csv.get_last_modified_date",
|
|
|
|
return_value=mocked_last_modification_date,
|
|
|
|
)
|
|
|
|
elements = partition_csv(filename=filename)
|
|
|
|
|
|
|
|
assert clean_extra_whitespace(elements[0].text) == EXPECTED_TEXT
|
|
|
|
assert isinstance(elements[0], Table)
|
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_csv_custom_metadata_date(
|
|
|
|
mocker,
|
|
|
|
filename="example-docs/stanley-cups.csv",
|
|
|
|
):
|
|
|
|
mocked_last_modification_date = "2029-07-05T09:24:28"
|
|
|
|
expected_last_modification_date = "2020-07-05T09:24:28"
|
|
|
|
|
|
|
|
mocker.patch(
|
|
|
|
"unstructured.partition.csv.get_last_modified_date",
|
|
|
|
return_value=mocked_last_modification_date,
|
|
|
|
)
|
|
|
|
|
|
|
|
elements = partition_csv(
|
|
|
|
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
|
|
|
)
|
|
|
|
|
|
|
|
assert clean_extra_whitespace(elements[0].text) == EXPECTED_TEXT
|
|
|
|
assert isinstance(elements[0], Table)
|
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_csv_from_file_metadata_date(
|
|
|
|
mocker,
|
|
|
|
filename="example-docs/stanley-cups.csv",
|
|
|
|
):
|
|
|
|
mocked_last_modification_date = "2029-07-05T09:24:28"
|
|
|
|
|
|
|
|
mocker.patch(
|
|
|
|
"unstructured.partition.csv.get_last_modified_date_from_file",
|
|
|
|
return_value=mocked_last_modification_date,
|
|
|
|
)
|
|
|
|
|
|
|
|
with open(filename, "rb") as f:
|
|
|
|
elements = partition_csv(file=f)
|
|
|
|
|
|
|
|
assert clean_extra_whitespace(elements[0].text) == EXPECTED_TEXT
|
|
|
|
assert isinstance(elements[0], Table)
|
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_csv_from_file_custom_metadata_date(
|
|
|
|
mocker,
|
|
|
|
filename="example-docs/stanley-cups.csv",
|
|
|
|
):
|
|
|
|
mocked_last_modification_date = "2029-07-05T09:24:28"
|
|
|
|
expected_last_modification_date = "2020-07-05T09:24:28"
|
|
|
|
|
|
|
|
mocker.patch(
|
|
|
|
"unstructured.partition.csv.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_csv(file=f, metadata_last_modified=expected_last_modification_date)
|
2023-07-26 15:10:14 -04:00
|
|
|
|
|
|
|
assert clean_extra_whitespace(elements[0].text) == EXPECTED_TEXT
|
|
|
|
assert isinstance(elements[0], Table)
|
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_csv_from_file_without_metadata(
|
|
|
|
mocker,
|
|
|
|
filename="example-docs/stanley-cups.csv",
|
|
|
|
):
|
|
|
|
"""Test partition_csv() with file that are not possible to get last modified date"""
|
|
|
|
|
|
|
|
with open(filename, "rb") as f:
|
|
|
|
sf = SpooledTemporaryFile()
|
|
|
|
sf.write(f.read())
|
|
|
|
sf.seek(0)
|
|
|
|
elements = partition_csv(file=sf)
|
|
|
|
|
|
|
|
assert clean_extra_whitespace(elements[0].text) == EXPECTED_TEXT
|
|
|
|
assert isinstance(elements[0], Table)
|
2023-07-31 19:55:43 -07:00
|
|
|
assert elements[0].metadata.last_modified is None
|
2023-08-29 16:59:26 -04:00
|
|
|
|
|
|
|
|
2023-10-12 12:47:55 -07:00
|
|
|
@pytest.mark.parametrize("filename", ["stanley-cups.csv", "stanley-cups-with-emoji.csv"])
|
|
|
|
def test_partition_csv_with_json(filename: str):
|
|
|
|
elements = partition_csv(filename=example_doc_path(filename))
|
|
|
|
assert_round_trips_through_JSON(elements)
|
2023-10-03 09:40:34 -07:00
|
|
|
|
|
|
|
|
|
|
|
def test_add_chunking_strategy_to_partition_csv_non_default():
|
|
|
|
filename = "example-docs/stanley-cups.csv"
|
|
|
|
|
|
|
|
elements = partition_csv(filename=filename)
|
|
|
|
chunk_elements = partition_csv(
|
|
|
|
filename,
|
|
|
|
chunking_strategy="by_title",
|
|
|
|
max_characters=9,
|
|
|
|
combine_text_under_n_chars=0,
|
|
|
|
)
|
|
|
|
chunks = chunk_by_title(elements, max_characters=9, combine_text_under_n_chars=0)
|
|
|
|
assert chunk_elements != elements
|
|
|
|
assert chunk_elements == chunks
|
2023-10-10 20:47:56 -05:00
|
|
|
|
|
|
|
|
|
|
|
# NOTE (jennings) partition_csv returns a single TableElement per sheet,
|
|
|
|
# so leaving off additional tests for multiple languages like the other partitions
|
|
|
|
def test_partition_csv_element_metadata_has_languages():
|
|
|
|
filename = "example-docs/stanley-cups.csv"
|
|
|
|
elements = partition_csv(filename=filename, strategy="fast")
|
|
|
|
assert elements[0].metadata.languages == ["eng"]
|
|
|
|
|
|
|
|
|
|
|
|
def test_partition_csv_respects_languages_arg():
|
|
|
|
filename = "example-docs/stanley-cups.csv"
|
|
|
|
elements = partition_csv(filename=filename, strategy="fast", languages=["deu"])
|
|
|
|
assert elements[0].metadata.languages == ["deu"]
|