- 更新 openspec/config.yaml 中 git 任务相关说明 - 将 scripts.core.* 改为 core.*,scripts.readers.* 改为 readers.* - 优化 lyxy_document_reader.py 中 sys.path 设置方式 - 同步更新所有测试文件的导入路径
122 lines
4.2 KiB
Python
122 lines
4.2 KiB
Python
"""测试 PPTX Reader 的解析功能。"""
|
|
|
|
import pytest
|
|
import os
|
|
from readers.pptx import PptxReader
|
|
|
|
|
|
class TestPythonPptxReaderParse:
|
|
"""测试 PPTX Reader 的 parse 方法。"""
|
|
|
|
def test_normal_file(self, temp_pptx):
|
|
"""测试正常 PPTX 文件解析。"""
|
|
# 创建包含多个幻灯片的测试文件
|
|
file_path = temp_pptx(slides=[
|
|
("主标题", "这是第一张幻灯片的内容。"),
|
|
("子标题", "这是第二张幻灯片的内容。"),
|
|
])
|
|
|
|
reader = PptxReader()
|
|
content, failures = reader.parse(file_path)
|
|
|
|
# 验证解析成功
|
|
assert content is not None, f"解析失败: {failures}"
|
|
assert len(failures) == 0 or all("成功" in f or not f for f in failures)
|
|
|
|
# 验证关键内容存在
|
|
assert "主标题" in content
|
|
assert "子标题" in content
|
|
assert "第一张幻灯片" in content or "第二张幻灯片" in content
|
|
|
|
def test_file_not_exists(self, tmp_path):
|
|
"""测试文件不存在的情况。"""
|
|
non_existent_file = str(tmp_path / "non_existent.pptx")
|
|
|
|
reader = PptxReader()
|
|
content, failures = reader.parse(non_existent_file)
|
|
|
|
# 验证返回 None 和错误信息
|
|
assert content is None
|
|
assert len(failures) > 0
|
|
assert any("不存在" in f or "找不到" in f for f in failures)
|
|
|
|
def test_empty_file(self, temp_pptx):
|
|
"""测试空 PPTX 文件。"""
|
|
# 创建没有任何内容的文件
|
|
file_path = temp_pptx()
|
|
|
|
reader = PptxReader()
|
|
content, failures = reader.parse(file_path)
|
|
|
|
# 空文件应该返回 None 或空字符串
|
|
assert content is None or content.strip() == ""
|
|
|
|
def test_corrupted_file(self, temp_pptx, tmp_path):
|
|
"""测试损坏的 PPTX 文件。"""
|
|
# 先创建正常文件
|
|
file_path = temp_pptx(slides=[("测试", "测试内容")])
|
|
|
|
# 破坏文件内容 - 完全覆盖文件
|
|
with open(file_path, "wb") as f:
|
|
f.write(b"corrupted content that is not a valid pptx file")
|
|
|
|
reader = PptxReader()
|
|
content, failures = reader.parse(file_path)
|
|
|
|
# 验证返回 None 和错误信息
|
|
assert content is None
|
|
assert len(failures) > 0
|
|
|
|
def test_special_chars(self, temp_pptx):
|
|
"""测试特殊字符处理。"""
|
|
special_slides = [
|
|
("中文标题", "中文测试内容"),
|
|
("Emoji测试", "😀🎉🚀"),
|
|
("特殊符号", "©®™°±"),
|
|
("混合内容", "Hello你好🎉World世界"),
|
|
]
|
|
|
|
file_path = temp_pptx(slides=special_slides)
|
|
|
|
reader = PptxReader()
|
|
content, failures = reader.parse(file_path)
|
|
|
|
assert content is not None, f"解析失败: {failures}"
|
|
|
|
# 验证各种特殊字符都被正确处理
|
|
assert "中文" in content
|
|
assert "😀" in content or "🎉" in content # 至少包含一个 emoji
|
|
assert "©" in content or "®" in content # 至少包含一个特殊符号
|
|
assert "Hello" in content or "World" in content
|
|
|
|
|
|
class TestPythonPptxReaderSupports:
|
|
"""测试 PPTX Reader 的 supports 方法。"""
|
|
|
|
def test_supports_pptx_extension(self):
|
|
"""测试识别 .pptx 扩展名。"""
|
|
reader = PptxReader()
|
|
assert reader.supports("test.pptx") is True
|
|
|
|
def test_supports_uppercase_extension(self):
|
|
"""测试识别大写扩展名。"""
|
|
reader = PptxReader()
|
|
assert reader.supports("TEST.PPTX") is True
|
|
|
|
def test_rejects_unsupported_format(self):
|
|
"""测试拒绝不支持的格式。"""
|
|
reader = PptxReader()
|
|
assert reader.supports("test.pdf") is False
|
|
assert reader.supports("test.txt") is False
|
|
|
|
def test_supports_path_with_spaces(self):
|
|
"""测试包含空格的路径。"""
|
|
reader = PptxReader()
|
|
assert reader.supports("path with spaces/test.pptx") is True
|
|
|
|
def test_supports_absolute_path(self):
|
|
"""测试绝对路径。"""
|
|
reader = PptxReader()
|
|
assert reader.supports("/absolute/path/test.pptx") is True
|
|
assert reader.supports("C:\\Windows\\path\\test.pptx") is True
|