- 更新 openspec/config.yaml 中 git 任务相关说明 - 将 scripts.core.* 改为 core.*,scripts.readers.* 改为 readers.* - 优化 lyxy_document_reader.py 中 sys.path 设置方式 - 同步更新所有测试文件的导入路径
50 lines
1.7 KiB
Python
50 lines
1.7 KiB
Python
"""测试所有 DOCX Readers 的一致性。"""
|
|
|
|
import pytest
|
|
from readers.docx import (
|
|
docling,
|
|
unstructured,
|
|
pypandoc,
|
|
markitdown,
|
|
python_docx,
|
|
native_xml,
|
|
)
|
|
|
|
|
|
class TestDocxReadersConsistency:
|
|
"""验证所有 DOCX Readers 解析同一文件时核心文字内容一致。"""
|
|
|
|
def test_all_readers_parse_same_content(self, temp_docx):
|
|
"""测试所有 Readers 解析同一文件时核心内容一致。"""
|
|
# 创建测试文件
|
|
file_path = temp_docx(
|
|
headings=[(1, "测试标题")],
|
|
paragraphs=["这是测试段落内容。", "第二段内容。"]
|
|
)
|
|
|
|
# 收集所有 readers 的解析结果
|
|
parsers = [
|
|
("docling", docling.parse),
|
|
("unstructured", unstructured.parse),
|
|
("pypandoc", pypandoc.parse),
|
|
("markitdown", markitdown.parse),
|
|
("python_docx", python_docx.parse),
|
|
("native_xml", native_xml.parse),
|
|
]
|
|
|
|
successful_results = []
|
|
for name, parser in parsers:
|
|
content, error = parser(file_path)
|
|
if content is not None and content.strip():
|
|
successful_results.append((name, content))
|
|
|
|
# 至少应该有一个 reader 成功解析
|
|
assert len(successful_results) > 0, "没有任何 reader 成功解析文件"
|
|
|
|
# 验证所有成功的 readers 都包含核心内容
|
|
core_texts = ["测试标题", "测试段落内容", "第二段"]
|
|
for name, content in successful_results:
|
|
# 至少包含一个核心文本
|
|
assert any(text in content for text in core_texts), \
|
|
f"{name} 解析结果不包含核心内容"
|