|
| 1 | +import logging |
| 2 | +from typing import Union |
| 3 | + |
| 4 | +import pytest |
| 5 | + |
| 6 | +from haystack.components.converters.xlsx import XLSXToDocument |
| 7 | + |
| 8 | + |
| 9 | +class TestXLSXToDocument: |
| 10 | + def test_init(self) -> None: |
| 11 | + converter = XLSXToDocument() |
| 12 | + assert converter.sheet_name is None |
| 13 | + assert converter.read_excel_kwargs == {} |
| 14 | + assert converter.table_format == "csv" |
| 15 | + assert converter.table_format_kwargs == {} |
| 16 | + |
| 17 | + def test_run_basic_tables(self, test_files_path) -> None: |
| 18 | + converter = XLSXToDocument() |
| 19 | + paths = [test_files_path / "xlsx" / "basic_tables_two_sheets.xlsx"] |
| 20 | + results = converter.run(sources=paths, meta={"date_added": "2022-01-01T00:00:00"}) |
| 21 | + documents = results["documents"] |
| 22 | + assert len(documents) == 2 |
| 23 | + assert documents[0].content == ",A,B\n1,col_a,col_b\n2,1.5,test\n" |
| 24 | + assert documents[0].meta == { |
| 25 | + "date_added": "2022-01-01T00:00:00", |
| 26 | + "file_path": str(test_files_path / "xlsx" / "basic_tables_two_sheets.xlsx"), |
| 27 | + "xlsx": {"sheet_name": "Basic Table"}, |
| 28 | + } |
| 29 | + assert documents[1].content == ",A,B\n1,col_c,col_d\n2,True,\n" |
| 30 | + assert documents[1].meta == { |
| 31 | + "date_added": "2022-01-01T00:00:00", |
| 32 | + "file_path": str(test_files_path / "xlsx" / "basic_tables_two_sheets.xlsx"), |
| 33 | + "xlsx": {"sheet_name": "Table Missing Value"}, |
| 34 | + } |
| 35 | + |
| 36 | + def test_run_table_empty_rows_and_columns(self, test_files_path) -> None: |
| 37 | + converter = XLSXToDocument() |
| 38 | + paths = [test_files_path / "xlsx" / "table_empty_rows_and_columns.xlsx"] |
| 39 | + results = converter.run(sources=paths, meta={"date_added": "2022-01-01T00:00:00"}) |
| 40 | + documents = results["documents"] |
| 41 | + assert len(documents) == 1 |
| 42 | + assert documents[0].content == ",A,B,C\n1,,,\n2,,,\n3,,,\n4,,col_a,col_b\n5,,1.5,test\n" |
| 43 | + assert documents[0].meta == { |
| 44 | + "date_added": "2022-01-01T00:00:00", |
| 45 | + "file_path": str(test_files_path / "xlsx" / "table_empty_rows_and_columns.xlsx"), |
| 46 | + "xlsx": {"sheet_name": "Sheet1"}, |
| 47 | + } |
| 48 | + |
| 49 | + def test_run_multiple_tables_in_one_sheet(self, test_files_path) -> None: |
| 50 | + converter = XLSXToDocument() |
| 51 | + paths = [test_files_path / "xlsx" / "multiple_tables.xlsx"] |
| 52 | + results = converter.run(sources=paths, meta={"date_added": "2022-01-01T00:00:00"}) |
| 53 | + documents = results["documents"] |
| 54 | + assert len(documents) == 1 |
| 55 | + assert ( |
| 56 | + documents[0].content |
| 57 | + == ",A,B,C,D,E,F\n1,,,,,,\n2,,,,,,\n3,,col_a,col_b,,,\n4,,1.5,test,,col_c,col_d\n5,,,,,3,True\n" |
| 58 | + ) |
| 59 | + assert documents[0].meta == { |
| 60 | + "date_added": "2022-01-01T00:00:00", |
| 61 | + "file_path": str(test_files_path / "xlsx" / "multiple_tables.xlsx"), |
| 62 | + "xlsx": {"sheet_name": "Sheet1"}, |
| 63 | + } |
| 64 | + |
| 65 | + def test_run_markdown(self, test_files_path) -> None: |
| 66 | + converter = XLSXToDocument(table_format="markdown") |
| 67 | + paths = [test_files_path / "xlsx" / "basic_tables_two_sheets.xlsx"] |
| 68 | + results = converter.run(sources=paths, meta={"date_added": "2022-01-01T00:00:00"}) |
| 69 | + documents = results["documents"] |
| 70 | + assert len(documents) == 2 |
| 71 | + assert ( |
| 72 | + documents[0].content |
| 73 | + == "| | A | B |\n|---:|:------|:------|\n| 1 | col_a | col_b |\n| 2 | 1.5 | test |" |
| 74 | + ) |
| 75 | + assert documents[0].meta == { |
| 76 | + "date_added": "2022-01-01T00:00:00", |
| 77 | + "file_path": str(test_files_path / "xlsx" / "basic_tables_two_sheets.xlsx"), |
| 78 | + "xlsx": {"sheet_name": "Basic Table"}, |
| 79 | + } |
| 80 | + assert ( |
| 81 | + documents[1].content |
| 82 | + == "| | A | B |\n|---:|:------|:------|\n| 1 | col_c | col_d |\n| 2 | True | nan |" |
| 83 | + ) |
| 84 | + assert documents[1].meta == { |
| 85 | + "date_added": "2022-01-01T00:00:00", |
| 86 | + "file_path": str(test_files_path / "xlsx" / "basic_tables_two_sheets.xlsx"), |
| 87 | + "xlsx": {"sheet_name": "Table Missing Value"}, |
| 88 | + } |
| 89 | + |
| 90 | + @pytest.mark.parametrize( |
| 91 | + "sheet_name, expected_sheet_name, expected_content", |
| 92 | + [ |
| 93 | + ("Basic Table", "Basic Table", ",A,B\n1,col_a,col_b\n2,1.5,test\n"), |
| 94 | + ("Table Missing Value", "Table Missing Value", ",A,B\n1,col_c,col_d\n2,True,\n"), |
| 95 | + (0, 0, ",A,B\n1,col_a,col_b\n2,1.5,test\n"), |
| 96 | + (1, 1, ",A,B\n1,col_c,col_d\n2,True,\n"), |
| 97 | + ], |
| 98 | + ) |
| 99 | + def test_run_sheet_name( |
| 100 | + self, sheet_name: Union[int, str], expected_sheet_name: str, expected_content: str, test_files_path |
| 101 | + ) -> None: |
| 102 | + converter = XLSXToDocument(sheet_name=sheet_name) |
| 103 | + paths = [test_files_path / "xlsx" / "basic_tables_two_sheets.xlsx"] |
| 104 | + results = converter.run(sources=paths) |
| 105 | + documents = results["documents"] |
| 106 | + assert len(documents) == 1 |
| 107 | + assert documents[0].content == expected_content |
| 108 | + assert documents[0].meta == { |
| 109 | + "file_path": str(test_files_path / "xlsx" / "basic_tables_two_sheets.xlsx"), |
| 110 | + "xlsx": {"sheet_name": expected_sheet_name}, |
| 111 | + } |
| 112 | + |
| 113 | + def test_run_with_read_excel_kwargs(self, test_files_path) -> None: |
| 114 | + converter = XLSXToDocument(sheet_name="Basic Table", read_excel_kwargs={"skiprows": 1}) |
| 115 | + paths = [test_files_path / "xlsx" / "basic_tables_two_sheets.xlsx"] |
| 116 | + results = converter.run(sources=paths, meta={"date_added": "2022-01-01T00:00:00"}) |
| 117 | + documents = results["documents"] |
| 118 | + assert len(documents) == 1 |
| 119 | + assert documents[0].content == ",A,B\n1,1.5,test\n" |
| 120 | + assert documents[0].meta == { |
| 121 | + "date_added": "2022-01-01T00:00:00", |
| 122 | + "file_path": str(test_files_path / "xlsx" / "basic_tables_two_sheets.xlsx"), |
| 123 | + "xlsx": {"sheet_name": "Basic Table"}, |
| 124 | + } |
| 125 | + |
| 126 | + def test_run_error_wrong_file_type(self, caplog: pytest.LogCaptureFixture, test_files_path) -> None: |
| 127 | + converter = XLSXToDocument() |
| 128 | + sources = [test_files_path / "pdf" / "sample_pdf_1.pdf"] |
| 129 | + with caplog.at_level(logging.WARNING): |
| 130 | + results = converter.run(sources=sources) |
| 131 | + assert "sample_pdf_1.pdf and convert it" in caplog.text |
| 132 | + assert results["documents"] == [] |
| 133 | + |
| 134 | + def test_run_error_non_existent_file(self, caplog: pytest.LogCaptureFixture) -> None: |
| 135 | + converter = XLSXToDocument() |
| 136 | + paths = ["non_existing_file.docx"] |
| 137 | + with caplog.at_level(logging.WARNING): |
| 138 | + converter.run(sources=paths) |
| 139 | + assert "Could not read non_existing_file.docx" in caplog.text |
0 commit comments