106 lines
3.9 KiB
Python
106 lines
3.9 KiB
Python
"""
|
|
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_keeps_rows_together_until_table_limit():
|
|
tables = [
|
|
{
|
|
'sheet': 'Rules',
|
|
'headers': ['条件', '处理', '责任'],
|
|
'rows': [
|
|
['库存不足', '补货', '采购部'],
|
|
['价格变动', '更新报价', '销售部'],
|
|
],
|
|
}
|
|
]
|
|
output = table_linearize.run(tables, Context())
|
|
|
|
# 段内使用单换行保留多条完整行;空行只用于真正的 Dify 段边界。
|
|
assert '\n- 当' in output
|
|
assert output.count('- 当') == 2
|
|
assert output.startswith('## Sheet: Rules')
|
|
|
|
|
|
def test_narrow_table_keeps_data_rows_in_one_dify_segment_by_default():
|
|
tables = [
|
|
{
|
|
'sheet': 'Data',
|
|
'headers': ['A', 'B', 'C'],
|
|
'rows': [
|
|
['1', '2', '3'],
|
|
['4', '5', '6'],
|
|
],
|
|
}
|
|
]
|
|
output = table_linearize.run(tables, Context())
|
|
|
|
assert '| --- | --- | --- |' 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
|
|
|
|
|
|
def test_wide_table_uses_row_text_blocks():
|
|
headers = [f'列{i}' for i in range(1, 11)]
|
|
rows = [[str(i * 10 + j) for j in range(10)] for i in range(1, 4)]
|
|
tables = [{'sheet': 'Wide', 'headers': headers, 'rows': rows}]
|
|
|
|
output = table_linearize.run(tables, Context())
|
|
|
|
assert '【行】' in output
|
|
assert '\n- 【行】' in output
|
|
assert output.count('【行】') == 3
|
|
assert '列1=10' in output
|
|
assert '|' not in output.split('## Sheet: Wide')[-1]
|
|
|
|
|
|
def test_multiple_sheets_separated_by_double_newline():
|
|
tables = [
|
|
{'sheet': 'S1', 'headers': ['X'], 'rows': [['a']]},
|
|
{'sheet': 'S2', 'headers': ['Y'], 'rows': [['b']]},
|
|
]
|
|
output = table_linearize.run(tables, Context())
|
|
|
|
assert '## Sheet: S1' in output
|
|
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| 序号 | 荣誉 | 认定单位 |')
|