feat: excel清理优化

This commit is contained in:
sophie committed 2026-08-20 19:37:09 +08:00
1 parent 87ac58c394
commit ca5782b6cd
16 files changed
+132 -27

No files matched your search

+41 -6
View File
@@ -1,11 +1,17 @@
"""Tests for Excel table linearization chunk boundaries."""
"""
backend/tests/test_table_linearize.py
用途:验证 Excel/CSV 表格线性化的行原子性和 Dify 段边界。
依赖:edkb_system.skills.table_linearize、Context。
逻辑:覆盖窄表、宽表和条件动作表;确保表格专用字符上限只在完整原始行之间换段。
"""
from __future__ import annotations
from edkb_system.contracts import Context
from edkb_system.skills import table_linearize
def test_condition_action_table_uses_double_newline_between_bullets():
def test_condition_action_table_keeps_rows_together_until_table_limit():
tables = [
{
'sheet': 'Rules',
@@ -18,12 +24,13 @@ def test_condition_action_table_uses_double_newline_between_bullets():
]
output = table_linearize.run(tables, Context())
assert '\n\n- 当' in output
# 段内使用单换行保留多条完整行;空行只用于真正的 Dify 段边界。
assert '\n- 当' in output
assert output.count('- 当') == 2
assert output.startswith('## Sheet: Rules')
def test_narrow_table_separates_data_rows_with_double_newline():
def test_narrow_table_keeps_data_rows_in_one_dify_segment_by_default():
tables = [
{
'sheet': 'Data',
@@ -37,7 +44,7 @@ def test_narrow_table_separates_data_rows_with_double_newline():
output = table_linearize.run(tables, Context())
assert '| --- | --- | --- |' in output
assert '\n\n|' in output
assert '\n| 1 | 2 | 3 |\n| 4 | 5 | 6 |' in output
assert '| 1 | 2 | 3 |' in output
assert '| 4 | 5 | 6 |' in output
assert '【行】' not in output
@@ -51,7 +58,7 @@ def test_wide_table_uses_row_text_blocks():
output = table_linearize.run(tables, Context())
assert '【行】' in output
assert '\n\n' in output
assert '\n- 【行】' in output
assert output.count('【行】') == 3
assert '列1=10' in output
assert '|' not in output.split('## Sheet: Wide')[-1]
@@ -68,3 +75,31 @@ def test_multiple_sheets_separated_by_double_newline():
assert '## Sheet: S2' in output
assert output.index('## Sheet: S2') > output.index('## Sheet: S1')
assert '\n\n## Sheet: S2' in output
def test_table_segment_max_chars_groups_complete_rows_and_repeats_headers():
"""
表格专用字符上限必须只在完整行之间切段,并为后续段补回列语义。
核心逻辑:以刚好容纳一条数据行的上限运行;第二条较长数据行进入新段,
断言其文本未被截取且新段含工作表名和表头。
"""
tables = [
{
'sheet': 'Honors',
'headers': ['序号', '荣誉', '认定单位'],
'rows': [
['1', '国家级专精特新“小巨人”企业', '工业和信息化部'],
['2', '2020年度开发区智能化改造和数字化转型先进企业', '中共吴江经济技术开发区工作委员会'],
],
}
]
output = table_linearize.run(tables, Context(), table_segment_max_chars=80)
segments = output.strip().split('\n\n')
# 关键断言:两条原始数据行分别完整出现,长行不会被 max_chars 从中间截断。
assert '| 1 | 国家级专精特新“小巨人”企业 | 工业和信息化部 |' in segments[0]
assert '| 2 | 2020年度开发区智能化改造和数字化转型先进企业 | 中共吴江经济技术开发区工作委员会 |' in segments[1]
# 后段必须带回 Sheet 和表头,使 Dify 单独召回时仍能理解每列含义。
assert segments[1].startswith('## Sheet: Honors\n| 序号 | 荣誉 | 认定单位 |')