Files
wehub-resource-sync 461bf6fd40
CI / lint (3.11) (push) Has been cancelled
CI / lint (3.12) (push) Has been cancelled
CI / lint (3.13) (push) Has been cancelled
CI / shellcheck (push) Has been cancelled
CI / shfmt (push) Has been cancelled
CI / setup (3.11) (push) Has been cancelled
CI / setup (3.12) (push) Has been cancelled
CI / setup (3.13) (push) Has been cancelled
CI / check-licenses (3.12) (push) Has been cancelled
CI / test_unit (3.11) (push) Has been cancelled
CI / test_unit (3.12) (push) Has been cancelled
CI / test_unit (3.13) (push) Has been cancelled
CI / test_unit_no_extras (3.11) (push) Has been cancelled
CI / test_unit_no_extras (3.12) (push) Has been cancelled
CI / test_json_to_html (3.12) (push) Has been cancelled
CI / test_unit_no_extras (3.13) (push) Has been cancelled
CI / test_unit_dependency_extras (csv, 3.12, --extra csv) (push) Has been cancelled
CI / test_unit_dependency_extras (xlsx, 3.11, --extra xlsx) (push) Has been cancelled
CI / test_unit_dependency_extras (xlsx, 3.12, --extra xlsx) (push) Has been cancelled
CI / test_unit_dependency_extras (csv, 3.11, --extra csv) (push) Has been cancelled
CI / test_unit_dependency_extras (csv, 3.13, --extra csv) (push) Has been cancelled
CI / test_unit_dependency_extras (docx, 3.11, --extra docx) (push) Has been cancelled
CI / test_unit_dependency_extras (docx, 3.12, --extra docx) (push) Has been cancelled
CI / test_unit_dependency_extras (docx, 3.13, --extra docx) (push) Has been cancelled
CI / test_unit_dependency_extras (markdown, 3.11, --extra md) (push) Has been cancelled
CI / test_unit_dependency_extras (markdown, 3.12, --extra md) (push) Has been cancelled
CI / test_unit_dependency_extras (markdown, 3.13, --extra md) (push) Has been cancelled
CI / test_unit_dependency_extras (odt, 3.11, --extra odt) (push) Has been cancelled
CI / test_unit_dependency_extras (odt, 3.12, --extra odt) (push) Has been cancelled
CI / test_unit_dependency_extras (odt, 3.13, --extra odt) (push) Has been cancelled
CI / test_unit_dependency_extras (pdf-image, 3.11, --extra pdf --extra image --extra paddleocr) (push) Has been cancelled
CI / test_unit_dependency_extras (pdf-image, 3.12, --extra pdf --extra image --extra paddleocr) (push) Has been cancelled
CI / test_unit_dependency_extras (pdf-image, 3.13, --extra pdf --extra image --extra paddleocr) (push) Has been cancelled
CI / test_unit_dependency_extras (pptx, 3.11, --extra pptx) (push) Has been cancelled
CI / test_unit_dependency_extras (pptx, 3.12, --extra pptx) (push) Has been cancelled
CI / test_unit_dependency_extras (pptx, 3.13, --extra pptx) (push) Has been cancelled
CI / test_unit_dependency_extras (pypandoc, 3.11, --extra epub --extra org --extra rtf --extra rst) (push) Has been cancelled
CI / test_unit_dependency_extras (pypandoc, 3.12, --extra epub --extra org --extra rtf --extra rst) (push) Has been cancelled
CI / test_unit_dependency_extras (pypandoc, 3.13, --extra epub --extra org --extra rtf --extra rst) (push) Has been cancelled
Build And Push Docker Image / set-short-sha (push) Has been cancelled
Partition Benchmark / setup (push) Has been cancelled
Partition Benchmark / Measure and compare partition() runtime (push) Has been cancelled
CI / test_unit_dependency_extras (xlsx, 3.13, --extra xlsx) (push) Has been cancelled
CI / test_ingest_src (3.12) (push) Has been cancelled
CI / test_json_to_markdown (3.12) (push) Has been cancelled
CI / changelog (push) Has been cancelled
CI / test_dockerfile (push) Has been cancelled
CodeQL / Analyze (python) (push) Has been cancelled
Build And Push Docker Image / build-images (linux/amd64, opensource-linux-8core) (push) Has been cancelled
Build And Push Docker Image / build-images (linux/arm64, ubuntu-24.04-arm) (push) Has been cancelled
Build And Push Docker Image / publish-images (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 13:33:56 +08:00

159 lines
5.6 KiB
Python

from __future__ import annotations
from pathlib import Path
from pytest_mock import MockFixture
from test_unstructured.unit_utils import (
assert_round_trips_through_JSON,
example_doc_path,
find_text_in_elements,
)
from unstructured.chunking.title import chunk_by_title
from unstructured.documents.elements import Title
from unstructured.partition.org import partition_org
def test_partition_org_from_filename():
elements = partition_org(example_doc_path("README.org"))
assert elements[0] == Title("Example Docs")
assert elements[0].metadata.filetype == "text/org"
def test_partition_org_from_file():
with open(example_doc_path("README.org"), "rb") as f:
elements = partition_org(file=f)
assert elements[0] == Title("Example Docs")
# -- .metadata.filename --------------------------------------------------------------------------
def test_partition_org_from_filename_gets_filename_from_filename_arg():
elements = partition_org(example_doc_path("README.org"))
assert len(elements) > 0
assert all(e.metadata.filename == "README.org" for e in elements)
def test_partition_org_from_file_gets_filename_None():
with open(example_doc_path("README.org"), "rb") as f:
elements = partition_org(file=f)
assert len(elements) > 0
assert all(e.metadata.filename is None for e in elements)
def test_partition_org_from_filename_prefers_metadata_filename():
elements = partition_org(example_doc_path("README.org"), metadata_filename="orig-name.org")
assert len(elements) > 0
assert all(element.metadata.filename == "orig-name.org" for element in elements)
def test_partition_org_from_file_prefers_metadata_filename():
with open(example_doc_path("README.org"), "rb") as f:
elements = partition_org(file=f, metadata_filename="orig-name.org")
assert all(e.metadata.filename == "orig-name.org" for e in elements)
# -- .metadata.filetype --------------------------------------------------------------------------
def test_partition_org_gets_the_ORG_MIME_type_in_metadata_filetype():
ORG_MIME_TYPE = "text/org"
elements = partition_org(example_doc_path("README.org"))
assert all(e.metadata.filetype == ORG_MIME_TYPE for e in elements), (
f"Expected all elements to have '{ORG_MIME_TYPE}' as their filetype, but got:"
f" {repr(elements[0].metadata.filetype)}"
)
# -- .metadata.last_modified ---------------------------------------------------------------------
def test_partition_org_from_filename_gets_last_modified_from_filesystem(mocker: MockFixture):
filesystem_last_modified = "2024-06-14T16:01:29"
mocker.patch(
"unstructured.partition.org.get_last_modified_date", return_value=filesystem_last_modified
)
elements = partition_org(example_doc_path("README.org"))
assert all(e.metadata.last_modified == filesystem_last_modified for e in elements)
def test_partition_org_from_file_gets_last_modified_None():
with open(example_doc_path("README.org"), "rb") as f:
elements = partition_org(file=f)
assert all(e.metadata.last_modified is None for e in elements)
def test_partition_org_from_filename_prefers_metadata_last_modified(mocker: MockFixture):
filesystem_last_modified = "2020-08-04T06:11:47"
metadata_last_modified = "2024-06-14T16:01:29"
mocker.patch(
"unstructured.partition.org.get_last_modified_date", return_value=filesystem_last_modified
)
elements = partition_org(
example_doc_path("README.org"), metadata_last_modified=metadata_last_modified
)
assert all(e.metadata.last_modified == metadata_last_modified for e in elements)
def test_partition_org_from_file_prefers_metadata_last_modified():
metadata_last_modified = "2020-07-05T09:24:28"
with open(example_doc_path("README.org"), "rb") as f:
elements = partition_org(file=f, metadata_last_modified=metadata_last_modified)
assert all(e.metadata.last_modified == metadata_last_modified for e in elements)
# ------------------------------------------------------------------------------------------------
def test_partition_org_with_json():
elements = partition_org(example_doc_path("README.org"))
assert_round_trips_through_JSON(elements)
def test_add_chunking_strategy_by_title_on_partition_org():
file_path = example_doc_path("README.org")
elements = partition_org(file_path)
chunk_elements = partition_org(file_path, chunking_strategy="by_title")
chunks = chunk_by_title(elements)
assert chunk_elements != elements
assert chunk_elements == chunks
def test_partition_org_element_metadata_has_languages():
elements = partition_org(example_doc_path("README.org"))
assert elements[0].metadata.languages == ["eng"]
def test_partition_org_respects_detect_language_per_element():
elements = partition_org(
example_doc_path("language-docs/eng_spa_mult.org"), detect_language_per_element=True
)
langs = [element.metadata.languages for element in elements]
assert langs == [["eng"], ["spa", "eng"], ["eng"], ["eng"], ["spa"]]
def test_org_wont_include_external_files():
# Make sure our import file is in place (otherwise the import fails silently and test passes)
assert Path(example_doc_path("file_we_dont_want_imported")).exists()
elements = partition_org(example_doc_path("README-w-include.org"))
# The partition should contain some elements
assert elements
# We find something we expect to find from file we partitioned directly
assert find_text_in_elements("instructions", elements)
# But we don't find something from the file included within the file we partitioned directly
assert not find_text_in_elements("wombat", elements)