| 
									
										
										
										
											2024-08-05 17:48:37 -07:00
										 |  |  | # pyright: reportPrivateUsage=false | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | from __future__ import annotations | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import io | 
					
						
							| 
									
										
										
										
											2023-07-26 15:10:14 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-10 16:28:57 -07:00
										 |  |  | import pytest | 
					
						
							| 
									
										
										
										
											2024-08-05 17:48:37 -07:00
										 |  |  | from pytest_mock import MockFixture | 
					
						
							| 
									
										
										
										
											2023-08-10 16:28:57 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | from test_unstructured.partition.test_constants import ( | 
					
						
							|  |  |  |     EXPECTED_TABLE, | 
					
						
							| 
									
										
										
										
											2023-12-13 18:57:46 -05:00
										 |  |  |     EXPECTED_TABLE_SEMICOLON_DELIMITER, | 
					
						
							| 
									
										
										
										
											2023-08-10 16:28:57 -07:00
										 |  |  |     EXPECTED_TABLE_WITH_EMOJI, | 
					
						
							|  |  |  |     EXPECTED_TEXT, | 
					
						
							| 
									
										
										
										
											2023-12-13 18:57:46 -05:00
										 |  |  |     EXPECTED_TEXT_SEMICOLON_DELIMITER, | 
					
						
							| 
									
										
										
										
											2023-08-10 16:28:57 -07:00
										 |  |  |     EXPECTED_TEXT_WITH_EMOJI, | 
					
						
							| 
									
										
										
										
											2023-10-31 01:16:36 -07:00
										 |  |  |     EXPECTED_TEXT_XLSX, | 
					
						
							| 
									
										
										
										
											2023-08-10 16:28:57 -07:00
										 |  |  | ) | 
					
						
							| 
									
										
										
										
											2024-08-05 17:48:37 -07:00
										 |  |  | from test_unstructured.unit_utils import ( | 
					
						
							|  |  |  |     FixtureRequest, | 
					
						
							|  |  |  |     Mock, | 
					
						
							|  |  |  |     assert_round_trips_through_JSON, | 
					
						
							|  |  |  |     example_doc_path, | 
					
						
							|  |  |  |     function_mock, | 
					
						
							|  |  |  | ) | 
					
						
							| 
									
										
										
										
											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 | 
					
						
							| 
									
										
										
										
											2024-08-05 17:48:37 -07:00
										 |  |  | from unstructured.partition.csv import _CsvPartitioningContext, 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), | 
					
						
							| 
									
										
										
										
											2023-12-13 18:57:46 -05:00
										 |  |  |         ( | 
					
						
							|  |  |  |             "table-semicolon-delimiter.csv", | 
					
						
							|  |  |  |             EXPECTED_TEXT_SEMICOLON_DELIMITER, | 
					
						
							|  |  |  |             EXPECTED_TABLE_SEMICOLON_DELIMITER, | 
					
						
							|  |  |  |         ), | 
					
						
							| 
									
										
										
										
											2023-08-10 16:28:57 -07:00
										 |  |  |     ], | 
					
						
							|  |  |  | ) | 
					
						
							| 
									
										
										
										
											2024-08-05 17:48:37 -07:00
										 |  |  | def test_partition_csv_from_filename(filename: str, expected_text: str, expected_table: str): | 
					
						
							| 
									
										
										
										
											2023-08-10 16:28:57 -07:00
										 |  |  |     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
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-05 17:48:37 -07:00
										 |  |  | @pytest.mark.parametrize("infer_table_structure", [True, False]) | 
					
						
							|  |  |  | def test_partition_csv_from_filename_infer_table_structure(infer_table_structure: bool): | 
					
						
							| 
									
										
										
										
											2023-10-23 17:11:53 -07:00
										 |  |  |     f_path = "example-docs/stanley-cups.csv" | 
					
						
							|  |  |  |     elements = partition_csv(filename=f_path, infer_table_structure=infer_table_structure) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     table_element_has_text_as_html_field = ( | 
					
						
							|  |  |  |         hasattr(elements[0].metadata, "text_as_html") | 
					
						
							|  |  |  |         and elements[0].metadata.text_as_html is not None | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  |     assert table_element_has_text_as_html_field == infer_table_structure | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-05 17:48:37 -07:00
										 |  |  | def test_partition_csv_from_filename_with_metadata_filename(): | 
					
						
							|  |  |  |     elements = partition_csv(example_doc_path("stanley-cups.csv"), metadata_filename="test") | 
					
						
							| 
									
										
										
										
											2023-07-05 15:02:22 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     assert clean_extra_whitespace(elements[0].text) == EXPECTED_TEXT | 
					
						
							|  |  |  |     assert elements[0].metadata.filename == "test" | 
					
						
							| 
									
										
										
										
											2023-05-19 15:57:42 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-28 10:19:58 -04:00
										 |  |  | def test_partition_csv_with_encoding(): | 
					
						
							|  |  |  |     elements = partition_csv(example_doc_path("stanley-cups-utf-16.csv"), encoding="utf-16") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     assert clean_extra_whitespace(elements[0].text) == EXPECTED_TEXT | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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), | 
					
						
							|  |  |  |     ], | 
					
						
							|  |  |  | ) | 
					
						
							| 
									
										
										
										
											2024-08-05 17:48:37 -07:00
										 |  |  | def test_partition_csv_from_file(filename: str, expected_text: str, expected_table: str): | 
					
						
							| 
									
										
										
										
											2023-08-10 16:28:57 -07:00
										 |  |  |     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
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-05 17:48:37 -07:00
										 |  |  | def test_partition_csv_from_file_with_metadata_filename(): | 
					
						
							|  |  |  |     with open(example_doc_path("stanley-cups.csv"), "rb") as f: | 
					
						
							| 
									
										
										
										
											2023-07-05 15:02:22 -05:00
										 |  |  |         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
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-23 15:23:10 -07:00
										 |  |  | # -- .metadata.last_modified --------------------------------------------------------------------- | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def test_partition_csv_from_file_path_gets_last_modified_from_filesystem(mocker: MockFixture): | 
					
						
							|  |  |  |     filesystem_last_modified = "2029-07-05T09:24:28" | 
					
						
							| 
									
										
										
										
											2023-07-26 15:10:14 -04:00
										 |  |  |     mocker.patch( | 
					
						
							|  |  |  |         "unstructured.partition.csv.get_last_modified_date", | 
					
						
							| 
									
										
										
										
											2024-09-23 15:23:10 -07:00
										 |  |  |         return_value=filesystem_last_modified, | 
					
						
							| 
									
										
										
										
											2023-07-26 15:10:14 -04:00
										 |  |  |     ) | 
					
						
							| 
									
										
										
										
											2024-08-05 17:48:37 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     elements = partition_csv(example_doc_path("stanley-cups.csv")) | 
					
						
							| 
									
										
										
										
											2023-07-26 15:10:14 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-23 15:23:10 -07:00
										 |  |  |     assert elements[0].metadata.last_modified == filesystem_last_modified | 
					
						
							| 
									
										
										
										
											2023-07-26 15:10:14 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-23 15:23:10 -07:00
										 |  |  | def test_partition_csv_from_file_path_prefers_metadata_last_modified(mocker: MockFixture): | 
					
						
							|  |  |  |     filesystem_last_modified = "2029-07-05T09:24:28" | 
					
						
							|  |  |  |     metadata_last_modified = "2020-07-05T09:24:28" | 
					
						
							| 
									
										
										
										
											2023-07-26 15:10:14 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     mocker.patch( | 
					
						
							| 
									
										
										
										
											2024-09-23 15:23:10 -07:00
										 |  |  |         "unstructured.partition.csv.get_last_modified_date", return_value=filesystem_last_modified | 
					
						
							| 
									
										
										
										
											2023-07-26 15:10:14 -04:00
										 |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     elements = partition_csv( | 
					
						
							| 
									
										
										
										
											2024-09-23 15:23:10 -07:00
										 |  |  |         example_doc_path("stanley-cups.csv"), metadata_last_modified=metadata_last_modified | 
					
						
							| 
									
										
										
										
											2023-07-26 15:10:14 -04:00
										 |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-23 15:23:10 -07:00
										 |  |  |     assert elements[0].metadata.last_modified == metadata_last_modified | 
					
						
							| 
									
										
										
										
											2023-07-26 15:10:14 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-23 15:23:10 -07:00
										 |  |  | def test_partition_csv_from_file_gets_last_modified_None(): | 
					
						
							| 
									
										
										
										
											2024-08-05 17:48:37 -07:00
										 |  |  |     with open(example_doc_path("stanley-cups.csv"), "rb") as f: | 
					
						
							| 
									
										
										
										
											2024-09-23 15:23:10 -07:00
										 |  |  |         elements = partition_csv(file=f) | 
					
						
							| 
									
										
										
										
											2023-07-26 15:10:14 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-18 02:09:44 +01:00
										 |  |  |     assert elements[0].metadata.last_modified is None | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-23 15:23:10 -07:00
										 |  |  | def test_partition_csv_from_file_prefers_metadata_last_modified(): | 
					
						
							|  |  |  |     metadata_last_modified = "2020-07-05T09:24:28" | 
					
						
							| 
									
										
										
										
											2023-07-26 15:10:14 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-05 17:48:37 -07:00
										 |  |  |     with open(example_doc_path("stanley-cups.csv"), "rb") as f: | 
					
						
							| 
									
										
										
										
											2024-09-23 15:23:10 -07:00
										 |  |  |         elements = partition_csv(file=f, metadata_last_modified=metadata_last_modified) | 
					
						
							| 
									
										
										
										
											2023-07-26 15:10:14 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-23 15:23:10 -07:00
										 |  |  |     assert elements[0].metadata.last_modified == metadata_last_modified | 
					
						
							| 
									
										
										
										
											2023-07-26 15:10:14 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-23 15:23:10 -07:00
										 |  |  | # ------------------------------------------------------------------------------------------------ | 
					
						
							| 
									
										
										
										
											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, | 
					
						
							| 
									
										
										
										
											2023-10-31 01:16:36 -07:00
										 |  |  |         include_header=False, | 
					
						
							| 
									
										
										
										
											2023-10-03 09:40:34 -07:00
										 |  |  |     ) | 
					
						
							|  |  |  |     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" | 
					
						
							| 
									
										
										
										
											2023-10-31 01:16:36 -07:00
										 |  |  |     elements = partition_csv(filename=filename, strategy="fast", include_header=False) | 
					
						
							| 
									
										
										
										
											2023-10-10 20:47:56 -05:00
										 |  |  |     assert elements[0].metadata.languages == ["eng"] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def test_partition_csv_respects_languages_arg(): | 
					
						
							|  |  |  |     filename = "example-docs/stanley-cups.csv" | 
					
						
							| 
									
										
										
										
											2023-10-31 01:16:36 -07:00
										 |  |  |     elements = partition_csv( | 
					
						
							|  |  |  |         filename=filename, strategy="fast", languages=["deu"], include_header=False | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2023-10-10 20:47:56 -05:00
										 |  |  |     assert elements[0].metadata.languages == ["deu"] | 
					
						
							| 
									
										
										
										
											2023-10-31 01:16:36 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def test_partition_csv_header(): | 
					
						
							| 
									
										
										
										
											2024-08-05 17:48:37 -07:00
										 |  |  |     elements = partition_csv( | 
					
						
							|  |  |  |         example_doc_path("stanley-cups.csv"), strategy="fast", include_header=True | 
					
						
							| 
									
										
										
										
											2023-10-31 01:16:36 -07:00
										 |  |  |     ) | 
					
						
							| 
									
										
										
										
											2024-05-10 14:19:31 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-05 17:48:37 -07:00
										 |  |  |     table = elements[0] | 
					
						
							| 
									
										
										
										
											2024-10-18 23:49:09 -07:00
										 |  |  |     assert table.text == "Stanley Cups Unnamed: 1 Unnamed: 2 " + EXPECTED_TEXT_XLSX | 
					
						
							| 
									
										
										
										
											2024-08-05 17:48:37 -07:00
										 |  |  |     assert table.metadata.text_as_html is not None | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # ================================================================================================ | 
					
						
							|  |  |  | # UNIT-TESTS | 
					
						
							|  |  |  | # ================================================================================================ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class Describe_CsvPartitioningContext: | 
					
						
							|  |  |  |     """Unit-test suite for `unstructured.partition.csv._CsvPartitioningContext`.""" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # -- .load() ------------------------------------------------ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def it_provides_a_validating_alternate_constructor(self): | 
					
						
							|  |  |  |         ctx = _CsvPartitioningContext.load( | 
					
						
							|  |  |  |             file_path=example_doc_path("stanley-cups.csv"), | 
					
						
							|  |  |  |             file=None, | 
					
						
							| 
									
										
										
										
											2024-08-28 10:19:58 -04:00
										 |  |  |             encoding=None, | 
					
						
							| 
									
										
										
										
											2024-08-05 17:48:37 -07:00
										 |  |  |             include_header=True, | 
					
						
							|  |  |  |             infer_table_structure=True, | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  |         assert isinstance(ctx, _CsvPartitioningContext) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def and_the_validating_constructor_raises_on_an_invalid_context(self): | 
					
						
							|  |  |  |         with pytest.raises(ValueError, match="either file-path or file-like object must be prov"): | 
					
						
							|  |  |  |             _CsvPartitioningContext.load( | 
					
						
							|  |  |  |                 file_path=None, | 
					
						
							|  |  |  |                 file=None, | 
					
						
							| 
									
										
										
										
											2024-08-28 10:19:58 -04:00
										 |  |  |                 encoding=None, | 
					
						
							| 
									
										
										
										
											2024-08-05 17:48:37 -07:00
										 |  |  |                 include_header=True, | 
					
						
							|  |  |  |                 infer_table_structure=True, | 
					
						
							|  |  |  |             ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # -- .delimiter --------------------------------------------- | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @pytest.mark.parametrize( | 
					
						
							|  |  |  |         "file_name", | 
					
						
							|  |  |  |         [ | 
					
						
							|  |  |  |             "stanley-cups.csv", | 
					
						
							|  |  |  |             # -- Issue #2643: previously raised `_csv.Error: Could not determine delimiter` on | 
					
						
							|  |  |  |             # -- this file | 
					
						
							|  |  |  |             "csv-with-long-lines.csv", | 
					
						
							|  |  |  |         ], | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  |     def it_auto_detects_the_delimiter_for_a_comma_delimited_CSV_file(self, file_name: str): | 
					
						
							|  |  |  |         ctx = _CsvPartitioningContext(example_doc_path(file_name)) | 
					
						
							|  |  |  |         assert ctx.delimiter == "," | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def and_it_auto_detects_the_delimiter_for_a_semicolon_delimited_CSV_file(self): | 
					
						
							|  |  |  |         ctx = _CsvPartitioningContext(example_doc_path("semicolon-delimited.csv")) | 
					
						
							|  |  |  |         assert ctx.delimiter == ";" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def but_it_returns_None_as_the_delimiter_for_a_single_column_CSV_file(self): | 
					
						
							|  |  |  |         ctx = _CsvPartitioningContext(example_doc_path("single-column.csv")) | 
					
						
							|  |  |  |         assert ctx.delimiter is None | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # -- .header ------------------------------------------------ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @pytest.mark.parametrize(("include_header", "expected_value"), [(False, None), (True, 0)]) | 
					
						
							|  |  |  |     def it_identifies_the_header_row_based_on_include_header_arg( | 
					
						
							|  |  |  |         self, include_header: bool, expected_value: int | None | 
					
						
							|  |  |  |     ): | 
					
						
							|  |  |  |         assert _CsvPartitioningContext(include_header=include_header).header == expected_value | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-01 15:40:58 -07:00
										 |  |  |     # -- .last_modified ----------------------------------------- | 
					
						
							| 
									
										
										
										
											2024-08-05 17:48:37 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-01 15:40:58 -07:00
										 |  |  |     def it_gets_last_modified_from_the_filesystem_when_a_path_is_provided( | 
					
						
							| 
									
										
										
										
											2024-08-05 17:48:37 -07:00
										 |  |  |         self, get_last_modified_date_: Mock | 
					
						
							|  |  |  |     ): | 
					
						
							| 
									
										
										
										
											2024-10-01 15:40:58 -07:00
										 |  |  |         filesystem_last_modified = "2024-08-04T02:23:53" | 
					
						
							|  |  |  |         get_last_modified_date_.return_value = filesystem_last_modified | 
					
						
							| 
									
										
										
										
											2024-08-05 17:48:37 -07:00
										 |  |  |         ctx = _CsvPartitioningContext(file_path="a/b/document.csv") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         last_modified = ctx.last_modified | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         get_last_modified_date_.assert_called_once_with("a/b/document.csv") | 
					
						
							| 
									
										
										
										
											2024-10-01 15:40:58 -07:00
										 |  |  |         assert last_modified == filesystem_last_modified | 
					
						
							| 
									
										
										
										
											2024-08-05 17:48:37 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-23 15:23:10 -07:00
										 |  |  |     def and_it_falls_back_to_None_for_the_last_modified_date_when_file_path_is_not_provided(self): | 
					
						
							| 
									
										
										
										
											2024-08-05 17:48:37 -07:00
										 |  |  |         file = io.BytesIO(b"abcdefg") | 
					
						
							| 
									
										
										
										
											2024-09-23 15:23:10 -07:00
										 |  |  |         ctx = _CsvPartitioningContext(file=file) | 
					
						
							| 
									
										
										
										
											2024-08-05 17:48:37 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |         last_modified = ctx.last_modified | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         assert last_modified is None | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # -- .open() ------------------------------------------------ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def it_provides_transparent_access_to_the_source_file_when_it_is_a_file_like_object(self): | 
					
						
							|  |  |  |         with open(example_doc_path("stanley-cups.csv"), "rb") as f: | 
					
						
							|  |  |  |             # -- read so file cursor is at end of file -- | 
					
						
							|  |  |  |             f.read() | 
					
						
							|  |  |  |             ctx = _CsvPartitioningContext(file=f) | 
					
						
							|  |  |  |             with ctx.open() as file: | 
					
						
							|  |  |  |                 assert file is f | 
					
						
							|  |  |  |                 # -- read cursor is reset to 0 on .open() context entry -- | 
					
						
							|  |  |  |                 assert f.tell() == 0 | 
					
						
							|  |  |  |                 assert file.read(14) == b"Stanley Cups,," | 
					
						
							|  |  |  |                 assert f.tell() == 14 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             # -- and read cursor is reset to 0 on .open() context exit -- | 
					
						
							|  |  |  |             assert f.tell() == 0 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def it_provides_transparent_access_to_the_source_file_when_it_is_a_file_path(self): | 
					
						
							|  |  |  |         ctx = _CsvPartitioningContext(example_doc_path("stanley-cups.csv")) | 
					
						
							|  |  |  |         with ctx.open() as file: | 
					
						
							|  |  |  |             assert file.read(14) == b"Stanley Cups,," | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # -- .validate() -------------------------------------------- | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def it_raises_when_neither_file_path_nor_file_is_provided(self): | 
					
						
							|  |  |  |         with pytest.raises(ValueError, match="either file-path or file-like object must be prov"): | 
					
						
							|  |  |  |             _CsvPartitioningContext()._validate() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # -- fixtures -------------------------------------------------------------------------------- | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @pytest.fixture() | 
					
						
							|  |  |  |     def get_last_modified_date_(self, request: FixtureRequest) -> Mock: | 
					
						
							|  |  |  |         return function_mock(request, "unstructured.partition.csv.get_last_modified_date") |