修复内容: - E2E测试命令执行方式:将 python -m uv run 改为 uv run - HTML渲染器:添加 & 字符的HTML转义 - Presentation尺寸验证:添加尺寸值类型验证 - PPTX验证器:修复文本框检测兼容性 - 验证结果格式化:修复提示信息显示 - Mock配置:修复表格渲染等测试的Mock配置 测试结果: - 修复前: 264 通过, 42 失败, 1 错误 - 修复后: 297 通过, 9 失败, 1 错误 剩余9个失败为待实现的功能增强(验证器模板变量验证)
191 lines
5.3 KiB
Python
191 lines
5.3 KiB
Python
"""
|
||
Presentation 类集成测试
|
||
|
||
测试 Presentation 类的模板加载和幻灯片渲染功能
|
||
"""
|
||
|
||
import pytest
|
||
from pathlib import Path
|
||
from core.presentation import Presentation
|
||
|
||
|
||
class TestPresentationInit:
|
||
"""Presentation 初始化测试"""
|
||
|
||
def test_init_with_yaml(self, sample_yaml):
|
||
"""测试使用 YAML 文件初始化"""
|
||
pres = Presentation(str(sample_yaml))
|
||
assert pres.data is not None
|
||
assert "slides" in pres.data
|
||
|
||
def test_init_with_template_dir(self, sample_yaml, sample_template):
|
||
"""测试带模板目录初始化"""
|
||
pres = Presentation(str(sample_yaml), str(sample_template))
|
||
assert pres.templates_dir == str(sample_template)
|
||
|
||
|
||
class TestTemplateCaching:
|
||
"""模板缓存测试"""
|
||
|
||
def test_template_is_cached(self, temp_dir, sample_template):
|
||
"""测试模板被缓存"""
|
||
# 创建使用模板的 YAML
|
||
yaml_content = f"""
|
||
metadata:
|
||
size: "16:9"
|
||
|
||
slides:
|
||
- template: title-slide
|
||
vars:
|
||
title: "Test"
|
||
"""
|
||
yaml_path = temp_dir / "test.yaml"
|
||
yaml_path.write_text(yaml_content)
|
||
|
||
pres = Presentation(str(yaml_path), str(sample_template))
|
||
|
||
# 第一次获取模板
|
||
template1 = pres.get_template("title-slide")
|
||
# 第二次获取模板
|
||
template2 = pres.get_template("title-slide")
|
||
|
||
# 应该是同一个实例(缓存)
|
||
assert template1 is template2
|
||
|
||
|
||
class TestRenderSlide:
|
||
"""render_slide 方法测试"""
|
||
|
||
def test_render_simple_slide(self, sample_yaml):
|
||
"""测试渲染简单幻灯片"""
|
||
pres = Presentation(str(sample_yaml))
|
||
slide_data = pres.data["slides"][0]
|
||
rendered = pres.render_slide(slide_data)
|
||
|
||
assert "elements" in rendered
|
||
assert len(rendered["elements"]) > 0
|
||
|
||
def test_render_slide_with_template(self, temp_dir, sample_template):
|
||
"""测试渲染使用模板的幻灯片"""
|
||
yaml_content = f"""
|
||
metadata:
|
||
size: "16:9"
|
||
|
||
slides:
|
||
- template: title-slide
|
||
vars:
|
||
title: "Test Title"
|
||
subtitle: "Test Subtitle"
|
||
"""
|
||
yaml_path = temp_dir / "test.yaml"
|
||
yaml_path.write_text(yaml_content)
|
||
|
||
pres = Presentation(str(yaml_path), str(sample_template))
|
||
slide_data = pres.data["slides"][0]
|
||
rendered = pres.render_slide(slide_data)
|
||
|
||
# 模板变量应该被替换
|
||
elements = rendered["elements"]
|
||
title_elem = next(
|
||
e
|
||
for e in elements
|
||
if e.get("type") == "text" and "Test Title" in e.get("content", "")
|
||
)
|
||
assert title_elem is not None
|
||
|
||
def test_render_slide_with_conditional_element(self, temp_dir, sample_template):
|
||
"""测试条件渲染元素"""
|
||
# 有 subtitle 的情况
|
||
yaml_with = f"""
|
||
metadata:
|
||
size: "16:9"
|
||
|
||
slides:
|
||
- template: title-slide
|
||
vars:
|
||
title: "Test"
|
||
subtitle: "With Subtitle"
|
||
"""
|
||
yaml_path_with = temp_dir / "test_with.yaml"
|
||
yaml_path_with.write_text(yaml_with)
|
||
|
||
pres_with = Presentation(str(yaml_path_with), str(sample_template))
|
||
slide_data = pres_with.data["slides"][0]
|
||
rendered_with = pres_with.render_slide(slide_data)
|
||
|
||
# 应该有 2 个元素(title 和 subtitle 都显示)
|
||
assert len(rendered_with["elements"]) == 2
|
||
|
||
# 没有 subtitle 的情况
|
||
yaml_without = f"""
|
||
metadata:
|
||
size: "16:9"
|
||
|
||
slides:
|
||
- template: title-slide
|
||
vars:
|
||
title: "Test"
|
||
"""
|
||
yaml_path_without = temp_dir / "test_without.yaml"
|
||
yaml_path_without.write_text(yaml_without)
|
||
|
||
pres_without = Presentation(str(yaml_path_without), str(sample_template))
|
||
slide_data = pres_without.data["slides"][0]
|
||
rendered_without = pres_without.render_slide(slide_data)
|
||
|
||
# 应该只有 1 个元素(subtitle 不显示)
|
||
assert len(rendered_without["elements"]) == 1
|
||
|
||
def test_render_slide_with_variables(self, temp_dir, sample_template):
|
||
"""测试变量传递"""
|
||
yaml_content = f"""
|
||
metadata:
|
||
size: "16:9"
|
||
|
||
slides:
|
||
- template: title-slide
|
||
vars:
|
||
title: "My Title"
|
||
subtitle: "My Subtitle"
|
||
"""
|
||
yaml_path = temp_dir / "test.yaml"
|
||
yaml_path.write_text(yaml_content)
|
||
|
||
pres = Presentation(str(yaml_path), str(sample_template))
|
||
slide_data = pres.data["slides"][0]
|
||
rendered = pres.render_slide(slide_data)
|
||
|
||
# 检查变量是否被正确替换
|
||
elements = rendered["elements"]
|
||
assert any("My Title" in e.get("content", "") for e in elements)
|
||
assert any("My Subtitle" in e.get("content", "") for e in elements)
|
||
|
||
|
||
class TestPresentationWithoutTemplate:
|
||
"""无模板的演示文稿测试"""
|
||
|
||
def test_render_direct_elements(self, temp_dir):
|
||
"""测试直接渲染元素(不使用模板)"""
|
||
yaml_content = """
|
||
metadata:
|
||
size: "16:9"
|
||
|
||
slides:
|
||
- elements:
|
||
- type: text
|
||
box: [1, 1, 8, 1]
|
||
content: "Direct Text"
|
||
font:
|
||
size: 24
|
||
"""
|
||
yaml_path = temp_dir / "test.yaml"
|
||
yaml_path.write_text(yaml_content)
|
||
|
||
pres = Presentation(str(yaml_path))
|
||
slide_data = pres.data["slides"][0]
|
||
rendered = pres.render_slide(slide_data)
|
||
|
||
# 元素应该直接被渲染
|
||
assert len(rendered["elements"]) == 1
|
||
assert rendered["elements"][0]["content"] == "Direct Text"
|