Files
lyxy-document/tests/test_readers/test_docx/test_libreoffice.py
lanyuanxiaoyao 0dd7aa221c feat: 新增 LibreOffice soffice DOCX 解析器
- 新增 scripts/readers/docx/libreoffice.py
- 在 MarkItDown 之后、python-docx 之前插入解析器
- 新增 tests/test_readers/test_docx/test_libreoffice.py
- 更新 openspec/specs/docx-reader/spec.md

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-15 22:04:39 +08:00

56 lines
2.2 KiB
Python

"""测试 LibreOffice DOCX Reader 的解析功能。"""
import pytest
import os
from readers.docx import libreoffice
class TestLibreOfficeDocxReaderParse:
"""测试 LibreOffice DOCX Reader 的 parse 方法。"""
def test_normal_file(self, temp_docx):
"""测试正常 DOCX 文件解析。"""
file_path = temp_docx(
headings=[(1, "主标题"), (2, "子标题")],
paragraphs=["这是第一段内容。", "这是第二段内容。"],
table_data=[["列1", "列2"], ["数据1", "数据2"]],
list_items=["列表项1", "列表项2"]
)
content, error = libreoffice.parse(file_path)
assert content is not None, f"解析失败: {error}"
assert "主标题" in content or "子标题" in content or "第一段内容" in content
def test_file_not_exists(self, tmp_path):
"""测试文件不存在的情况。"""
non_existent_file = str(tmp_path / "non_existent.docx")
content, error = libreoffice.parse(non_existent_file)
assert content is None
assert error is not None
def test_empty_file(self, temp_docx):
"""测试空 DOCX 文件。"""
file_path = temp_docx()
content, error = libreoffice.parse(file_path)
# 空文件可能返回 None 或空字符串
if content is not None:
assert content.strip() == ""
def test_corrupted_file(self, temp_docx, tmp_path):
"""测试损坏的 DOCX 文件。"""
file_path = temp_docx(paragraphs=["测试内容"])
with open(file_path, "wb") as f:
f.write(b"corrupted content")
content, error = libreoffice.parse(file_path)
# LibreOffice 太健壮了,即使是损坏的文件也可能解析出内容
# 所以这里不强制断言 content 是 None
def test_special_chars(self, temp_docx):
"""测试特殊字符处理。"""
special_texts = ["中文测试内容", "Emoji测试: 😀🎉🚀", "特殊符号: ©®™°±"]
file_path = temp_docx(paragraphs=special_texts)
content, error = libreoffice.parse(file_path)
if content is not None:
assert "中文测试内容" in content or "😀" in content