test: add comprehensive pytest test suite
Add complete test infrastructure for yaml2pptx project with 245+ tests covering unit, integration, and end-to-end scenarios. Test structure: - Unit tests: elements, template system, validators, loaders, utils - Integration tests: presentation and rendering flows - E2E tests: CLI commands (convert, check, preview) Key features: - PptxFileValidator for Level 2 PPTX validation (file structure, element count, content matching, position tolerance) - Comprehensive fixtures for test data consistency - Mock-based testing for external dependencies - Test images generated with PIL/Pillow - Boundary case coverage for edge scenarios Dependencies added: - pytest, pytest-cov, pytest-mock - pillow (for test image generation) Documentation updated: - README.md: test running instructions - README_DEV.md: test development guide Co-authored-by: OpenSpec change: add-comprehensive-tests
This commit is contained in:
289
tests/integration/test_rendering_flow.py
Normal file
289
tests/integration/test_rendering_flow.py
Normal file
@@ -0,0 +1,289 @@
|
||||
"""
|
||||
渲染流程集成测试
|
||||
|
||||
测试完整的 YAML 到 PPTX 渲染流程
|
||||
"""
|
||||
|
||||
import pytest
|
||||
from pathlib import Path
|
||||
from pptx import Presentation
|
||||
from core.presentation import Presentation as CorePresentation
|
||||
from renderers.pptx_renderer import PptxGenerator
|
||||
|
||||
|
||||
class TestRenderingFlow:
|
||||
"""渲染流程测试类"""
|
||||
|
||||
def test_full_rendering_flow(self, sample_yaml, temp_dir, pptx_validator):
|
||||
"""测试完整渲染流程"""
|
||||
# 加载演示文稿
|
||||
pres = CorePresentation(str(sample_yaml))
|
||||
|
||||
# 创建 PPTX 生成器
|
||||
gen = PptxGenerator(size='16:9')
|
||||
|
||||
# 渲染所有幻灯片
|
||||
for slide_data in pres.data.get('slides', []):
|
||||
rendered = pres.render_slide(slide_data)
|
||||
gen.add_slide(rendered, base_path=sample_yaml.parent)
|
||||
|
||||
# 保存文件
|
||||
output_path = temp_dir / "output.pptx"
|
||||
gen.save(output_path)
|
||||
|
||||
# 验证文件
|
||||
assert pptx_validator.validate_file(output_path) is True
|
||||
assert pptx_validator.validate_slides_count(Presentation(str(output_path)), 1) is True
|
||||
|
||||
def test_text_element_rendering(self, temp_dir, pptx_validator):
|
||||
"""测试文本元素渲染"""
|
||||
yaml_content = """
|
||||
metadata:
|
||||
size: 16:9
|
||||
|
||||
slides:
|
||||
- elements:
|
||||
- type: text
|
||||
box: [1, 1, 8, 1]
|
||||
content: "Test Content"
|
||||
font:
|
||||
size: 24
|
||||
bold: true
|
||||
color: "#333333"
|
||||
align: center
|
||||
"""
|
||||
yaml_path = temp_dir / "test.yaml"
|
||||
yaml_path.write_text(yaml_content)
|
||||
|
||||
pres = CorePresentation(str(yaml_path))
|
||||
gen = PptxGenerator(size='16:9')
|
||||
|
||||
for slide_data in pres.data.get('slides', []):
|
||||
rendered = pres.render_slide(slide_data)
|
||||
gen.add_slide(rendered)
|
||||
|
||||
output_path = temp_dir / "output.pptx"
|
||||
gen.save(output_path)
|
||||
|
||||
# 验证文本元素
|
||||
prs = Presentation(str(output_path))
|
||||
assert len(prs.slides) == 1
|
||||
|
||||
pptx_validator.clear()
|
||||
assert pptx_validator.validate_text_element(
|
||||
prs.slides[0],
|
||||
index=0,
|
||||
expected_content="Test Content",
|
||||
expected_font_size=24
|
||||
) is True
|
||||
|
||||
def test_image_element_rendering(self, temp_dir, sample_image, pptx_validator):
|
||||
"""测试图片元素渲染"""
|
||||
yaml_content = f"""
|
||||
metadata:
|
||||
size: 16:9
|
||||
|
||||
slides:
|
||||
- elements:
|
||||
- type: image
|
||||
box: [1, 1, 4, 3]
|
||||
src: "{sample_image.name}"
|
||||
"""
|
||||
yaml_path = temp_dir / "test.yaml"
|
||||
yaml_path.write_text(yaml_content)
|
||||
|
||||
pres = CorePresentation(str(yaml_path))
|
||||
gen = PptxGenerator(size='16:9')
|
||||
|
||||
for slide_data in pres.data.get('slides', []):
|
||||
rendered = pres.render_slide(slide_data)
|
||||
gen.add_slide(rendered, base_path=yaml_path.parent)
|
||||
|
||||
output_path = temp_dir / "output.pptx"
|
||||
gen.save(output_path)
|
||||
|
||||
# 验证图片元素
|
||||
prs = Presentation(str(output_path))
|
||||
counts = pptx_validator.count_elements_by_type(prs.slides[0])
|
||||
assert counts["picture"] == 1
|
||||
|
||||
def test_shape_element_rendering(self, temp_dir, pptx_validator):
|
||||
"""测试形状元素渲染"""
|
||||
yaml_content = """
|
||||
metadata:
|
||||
size: 16:9
|
||||
|
||||
slides:
|
||||
- elements:
|
||||
- type: shape
|
||||
box: [1, 1, 2, 1]
|
||||
shape: rectangle
|
||||
fill: "#4a90e2"
|
||||
line:
|
||||
color: "#000000"
|
||||
width: 2
|
||||
"""
|
||||
yaml_path = temp_dir / "test.yaml"
|
||||
yaml_path.write_text(yaml_content)
|
||||
|
||||
pres = CorePresentation(str(yaml_path))
|
||||
gen = PptxGenerator(size='16:9')
|
||||
|
||||
for slide_data in pres.data.get('slides', []):
|
||||
rendered = pres.render_slide(slide_data)
|
||||
gen.add_slide(rendered)
|
||||
|
||||
output_path = temp_dir / "output.pptx"
|
||||
gen.save(output_path)
|
||||
|
||||
# 验证形状元素
|
||||
prs = Presentation(str(output_path))
|
||||
counts = pptx_validator.count_elements_by_type(prs.slides[0])
|
||||
assert counts["shape"] >= 1
|
||||
|
||||
def test_table_element_rendering(self, temp_dir, pptx_validator):
|
||||
"""测试表格元素渲染"""
|
||||
yaml_content = """
|
||||
metadata:
|
||||
size: 16:9
|
||||
|
||||
slides:
|
||||
- elements:
|
||||
- type: table
|
||||
position: [1, 1]
|
||||
col_widths: [2, 2, 2]
|
||||
data:
|
||||
- ["Header 1", "Header 2", "Header 3"]
|
||||
- ["Data 1", "Data 2", "Data 3"]
|
||||
style:
|
||||
font_size: 14
|
||||
"""
|
||||
yaml_path = temp_dir / "test.yaml"
|
||||
yaml_path.write_text(yaml_content)
|
||||
|
||||
pres = CorePresentation(str(yaml_path))
|
||||
gen = PptxGenerator(size='16:9')
|
||||
|
||||
for slide_data in pres.data.get('slides', []):
|
||||
rendered = pres.render_slide(slide_data)
|
||||
gen.add_slide(rendered)
|
||||
|
||||
output_path = temp_dir / "output.pptx"
|
||||
gen.save(output_path)
|
||||
|
||||
# 验证表格元素
|
||||
prs = Presentation(str(output_path))
|
||||
counts = pptx_validator.count_elements_by_type(prs.slides[0])
|
||||
assert counts["table"] == 1
|
||||
|
||||
def test_background_rendering(self, temp_dir, pptx_validator):
|
||||
"""测试背景渲染"""
|
||||
yaml_content = """
|
||||
metadata:
|
||||
size: 16:9
|
||||
|
||||
slides:
|
||||
- background:
|
||||
color: "#ffffff"
|
||||
elements:
|
||||
- type: text
|
||||
box: [1, 1, 8, 1]
|
||||
content: "Test"
|
||||
font:
|
||||
size: 24
|
||||
"""
|
||||
yaml_path = temp_dir / "test.yaml"
|
||||
yaml_path.write_text(yaml_content)
|
||||
|
||||
pres = CorePresentation(str(yaml_path))
|
||||
gen = PptxGenerator(size='16:9')
|
||||
|
||||
for slide_data in pres.data.get('slides', []):
|
||||
rendered = pres.render_slide(slide_data)
|
||||
gen.add_slide(rendered)
|
||||
|
||||
output_path = temp_dir / "output.pptx"
|
||||
gen.save(output_path)
|
||||
|
||||
# 验证背景颜色
|
||||
prs = Presentation(str(output_path))
|
||||
pptx_validator.clear()
|
||||
assert pptx_validator.validate_background_color(
|
||||
prs.slides[0],
|
||||
expected_rgb=(255, 255, 255)
|
||||
) is True
|
||||
|
||||
def test_template_slide_rendering(self, temp_dir, sample_template, pptx_validator):
|
||||
"""测试使用模板的幻灯片渲染"""
|
||||
yaml_content = f"""
|
||||
metadata:
|
||||
size: 16:9
|
||||
|
||||
slides:
|
||||
- template: title-slide
|
||||
vars:
|
||||
title: "Template Test"
|
||||
subtitle: "Template Subtitle"
|
||||
"""
|
||||
yaml_path = temp_dir / "test.yaml"
|
||||
yaml_path.write_text(yaml_content)
|
||||
|
||||
pres = CorePresentation(str(yaml_path), str(sample_template))
|
||||
gen = PptxGenerator(size='16:9')
|
||||
|
||||
for slide_data in pres.data.get('slides', []):
|
||||
rendered = pres.render_slide(slide_data)
|
||||
gen.add_slide(rendered)
|
||||
|
||||
output_path = temp_dir / "output.pptx"
|
||||
gen.save(output_path)
|
||||
|
||||
# 验证文件生成
|
||||
assert pptx_validator.validate_file(output_path) is True
|
||||
|
||||
# 验证文本内容
|
||||
prs = Presentation(str(output_path))
|
||||
pptx_validator.clear()
|
||||
assert pptx_validator.validate_text_element(
|
||||
prs.slides[0],
|
||||
index=0,
|
||||
expected_content="Template Test"
|
||||
) is True
|
||||
|
||||
def test_multiple_slides_rendering(self, temp_dir, pptx_validator):
|
||||
"""测试多张幻灯片渲染"""
|
||||
yaml_content = """
|
||||
metadata:
|
||||
size: 16:9
|
||||
|
||||
slides:
|
||||
- elements:
|
||||
- type: text
|
||||
box: [1, 1, 8, 1]
|
||||
content: "Slide 1"
|
||||
font:
|
||||
size: 24
|
||||
|
||||
- elements:
|
||||
- type: text
|
||||
box: [1, 1, 8, 1]
|
||||
content: "Slide 2"
|
||||
font:
|
||||
size: 24
|
||||
"""
|
||||
yaml_path = temp_dir / "test.yaml"
|
||||
yaml_path.write_text(yaml_content)
|
||||
|
||||
pres = CorePresentation(str(yaml_path))
|
||||
gen = PptxGenerator(size='16:9')
|
||||
|
||||
for slide_data in pres.data.get('slides', []):
|
||||
rendered = pres.render_slide(slide_data)
|
||||
gen.add_slide(rendered)
|
||||
|
||||
output_path = temp_dir / "output.pptx"
|
||||
gen.save(output_path)
|
||||
|
||||
# 验证幻灯片数量
|
||||
prs = Presentation(str(output_path))
|
||||
assert pptx_validator.validate_slides_count(prs, 2) is True
|
||||
Reference in New Issue
Block a user