add testing scripts for extract_text_from_excel.

pull/20625/head
haiyangpengai 12 months ago
parent 9da5a2aad2
commit f2a45af1bd

@ -1,5 +1,7 @@
import io
from unittest.mock import Mock, patch
import pandas as pd
import pytest
from docx.oxml.text.paragraph import CT_P
@ -16,8 +18,7 @@ from core.workflow.nodes.document_extractor.node import (
_extract_text_from_plain_text,
)
from core.workflow.nodes.enums import NodeType
import pandas as pd
import io
@pytest.fixture
def document_extractor_node():
@ -191,10 +192,7 @@ def test_extract_text_from_excel_single_sheet(mock_excel_file):
"""Test extracting text from Excel file with single sheet and multiline content."""
# Test multi-line cell
data = {
'Name\nwith\nnewline': ['John\nDoe', 'Jane\nSmith'],
'Age': [25, 30]
}
data = {"Name\nwith\nnewline": ["John\nDoe", "Jane\nSmith"], "Age": [25, 30]}
df = pd.DataFrame(data)
@ -212,21 +210,16 @@ def test_extract_text_from_excel_single_sheet(mock_excel_file):
assert expected_manual == result
mock_excel_instance.parse.assert_called_once_with(sheet_name="Sheet1")
@patch("pandas.ExcelFile")
def test_extract_text_from_excel_multiple_sheets(mock_excel_file):
"""Test extracting text from Excel file with multiple sheets and multiline content."""
# Test multi-line cell
data1 = {
'Product\nName': ['Apple\nRed', 'Banana\nYellow'],
'Price': [1.50, 0.99]
}
data1 = {"Product\nName": ["Apple\nRed", "Banana\nYellow"], "Price": [1.50, 0.99]}
df1 = pd.DataFrame(data1)
data2 = {
'City\nName': ['New\nYork', 'Los\nAngeles'],
'Population': [8000000, 3900000]
}
data2 = {"City\nName": ["New\nYork", "Los\nAngeles"], "Population": [8000000, 3900000]}
df2 = pd.DataFrame(data2)
# Mock ExcelFile
@ -248,6 +241,7 @@ def test_extract_text_from_excel_multiple_sheets(mock_excel_file):
assert mock_excel_instance.parse.call_count == 2
@patch("pandas.ExcelFile")
def test_extract_text_from_excel_empty_sheets(mock_excel_file):
"""Test extracting text from Excel file with empty sheets."""
@ -269,15 +263,13 @@ def test_extract_text_from_excel_empty_sheets(mock_excel_file):
mock_excel_instance.parse.assert_called_once_with(sheet_name="EmptySheet")
@patch("pandas.ExcelFile")
def test_extract_text_from_excel_sheet_parse_error(mock_excel_file):
"""Test handling of sheet parsing errors - should continue with other sheets."""
# Test error
data = {
'Data': ['Test'],
'Value': [123]
}
data = {"Data": ["Test"], "Value": [123]}
df = pd.DataFrame(data)
# Mock ExcelFile
@ -295,15 +287,13 @@ def test_extract_text_from_excel_sheet_parse_error(mock_excel_file):
assert mock_excel_instance.parse.call_count == 2
@patch("pandas.ExcelFile")
def test_extract_text_from_excel_io_bytesio_usage(mock_excel_file):
"""Test that BytesIO is properly used with the file content."""
# Test bytesio
data = {
'Test': [1],
'Data': ['A']
}
data = {"Test": [1], "Data": ["A"]}
df = pd.DataFrame(data)
# Mock ExcelFile
@ -322,6 +312,7 @@ def test_extract_text_from_excel_io_bytesio_usage(mock_excel_file):
expected_manual = "| Test | Data |\n| ---- | ---- |\n| 1 | A |\n\n"
assert expected_manual == result
@patch("pandas.ExcelFile")
def test_extract_text_from_excel_all_sheets_fail(mock_excel_file):
"""Test when all sheets fail to parse - should return empty string."""

Loading…
Cancel
Save