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:
3
tests/integration/__init__.py
Normal file
3
tests/integration/__init__.py
Normal file
@@ -0,0 +1,3 @@
|
||||
"""
|
||||
集成测试包
|
||||
"""
|
||||
186
tests/integration/test_presentation.py
Normal file
186
tests/integration/test_presentation.py
Normal file
@@ -0,0 +1,186 @@
|
||||
"""
|
||||
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.template_dir == 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"
|
||||
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
|
||||
269
tests/integration/test_validation_flow.py
Normal file
269
tests/integration/test_validation_flow.py
Normal file
@@ -0,0 +1,269 @@
|
||||
"""
|
||||
验证流程集成测试
|
||||
|
||||
测试完整的验证流程和验证结果收集
|
||||
"""
|
||||
|
||||
import pytest
|
||||
from pathlib import Path
|
||||
from validators.validator import Validator
|
||||
from validators.result import ValidationResult
|
||||
|
||||
|
||||
class TestValidationFlow:
|
||||
"""验证流程测试类"""
|
||||
|
||||
def test_validate_valid_yaml(self, sample_yaml):
|
||||
"""测试验证有效的 YAML 文件"""
|
||||
validator = Validator()
|
||||
result = validator.validate(sample_yaml)
|
||||
|
||||
assert isinstance(result, ValidationResult)
|
||||
assert result.valid is True
|
||||
assert len(result.errors) == 0
|
||||
|
||||
def test_validate_with_warnings(self, temp_dir):
|
||||
"""测试验证包含警告的 YAML"""
|
||||
yaml_content = """
|
||||
metadata:
|
||||
size: 16:9
|
||||
|
||||
slides:
|
||||
- elements:
|
||||
- type: text
|
||||
box: [8, 1, 3, 1] # 右边界超出
|
||||
content: "Test"
|
||||
font:
|
||||
size: 4 # 字体太小
|
||||
"""
|
||||
yaml_path = temp_dir / "test.yaml"
|
||||
yaml_path.write_text(yaml_content)
|
||||
|
||||
validator = Validator()
|
||||
result = validator.validate(yaml_path)
|
||||
|
||||
# 应该有警告但 valid 仍为 True(没有错误)
|
||||
assert len(result.warnings) > 0
|
||||
|
||||
def test_validate_with_errors(self, temp_dir):
|
||||
"""测试验证包含错误的 YAML"""
|
||||
yaml_content = """
|
||||
metadata:
|
||||
size: 16:9
|
||||
|
||||
slides:
|
||||
- elements:
|
||||
- type: text
|
||||
box: [1, 1, 8, 1]
|
||||
content: "Test"
|
||||
font:
|
||||
color: "red" # 无效颜色格式
|
||||
"""
|
||||
yaml_path = temp_dir / "test.yaml"
|
||||
yaml_path.write_text(yaml_content)
|
||||
|
||||
validator = Validator()
|
||||
result = validator.validate(yaml_path)
|
||||
|
||||
assert result.valid is False
|
||||
assert len(result.errors) > 0
|
||||
|
||||
def test_validate_nonexistent_file(self, temp_dir):
|
||||
"""测试验证不存在的文件"""
|
||||
validator = Validator()
|
||||
nonexistent = temp_dir / "nonexistent.yaml"
|
||||
result = validator.validate(nonexistent)
|
||||
|
||||
assert result.valid is False
|
||||
assert len(result.errors) > 0
|
||||
|
||||
def test_collect_multiple_errors(self, temp_dir):
|
||||
"""测试收集多个错误"""
|
||||
yaml_content = """
|
||||
metadata:
|
||||
size: 16:9
|
||||
|
||||
slides:
|
||||
- elements:
|
||||
- type: text
|
||||
box: [1, 1, 8, 1]
|
||||
content: "Test 1"
|
||||
font:
|
||||
color: "red"
|
||||
|
||||
- type: text
|
||||
box: [2, 2, 8, 1]
|
||||
content: "Test 2"
|
||||
font:
|
||||
color: "blue"
|
||||
"""
|
||||
yaml_path = temp_dir / "test.yaml"
|
||||
yaml_path.write_text(yaml_content)
|
||||
|
||||
validator = Validator()
|
||||
result = validator.validate(yaml_path)
|
||||
|
||||
# 应该收集到多个错误
|
||||
assert len(result.errors) >= 2
|
||||
|
||||
def test_error_location_information(self, temp_dir):
|
||||
"""测试错误包含位置信息"""
|
||||
yaml_content = """
|
||||
metadata:
|
||||
size: 16:9
|
||||
|
||||
slides:
|
||||
- elements:
|
||||
- type: text
|
||||
box: [1, 1, 8, 1]
|
||||
content: "Test"
|
||||
font:
|
||||
color: "red"
|
||||
"""
|
||||
yaml_path = temp_dir / "test.yaml"
|
||||
yaml_path.write_text(yaml_content)
|
||||
|
||||
validator = Validator()
|
||||
result = validator.validate(yaml_path)
|
||||
|
||||
# 错误应该包含位置信息
|
||||
if result.errors:
|
||||
assert "幻灯片 1" in result.errors[0].location
|
||||
|
||||
def test_validate_with_template(self, temp_dir, sample_template):
|
||||
"""测试验证使用模板的 YAML"""
|
||||
yaml_content = f"""
|
||||
metadata:
|
||||
size: 16:9
|
||||
|
||||
slides:
|
||||
- template: title-slide
|
||||
vars:
|
||||
title: "Test Title"
|
||||
"""
|
||||
yaml_path = temp_dir / "test.yaml"
|
||||
yaml_path.write_text(yaml_content)
|
||||
|
||||
validator = Validator()
|
||||
result = validator.validate(yaml_path, template_dir=sample_template)
|
||||
|
||||
assert isinstance(result, ValidationResult)
|
||||
# 有效模板应该验证通过
|
||||
assert len(result.errors) == 0
|
||||
|
||||
def test_validate_with_missing_required_variable(self, temp_dir, sample_template):
|
||||
"""测试验证缺少必需变量的模板"""
|
||||
yaml_content = f"""
|
||||
metadata:
|
||||
size: 16:9
|
||||
|
||||
slides:
|
||||
- template: title-slide
|
||||
vars: {{}}
|
||||
"""
|
||||
yaml_path = temp_dir / "test.yaml"
|
||||
yaml_path.write_text(yaml_content)
|
||||
|
||||
validator = Validator()
|
||||
result = validator.validate(yaml_path, template_dir=sample_template)
|
||||
|
||||
# 应该有错误(缺少必需的 title 变量)
|
||||
assert len(result.errors) > 0
|
||||
|
||||
def test_categorize_issues_by_level(self, temp_dir):
|
||||
"""测试按级别分类问题"""
|
||||
# 创建包含错误和警告的 YAML
|
||||
yaml_content = """
|
||||
metadata:
|
||||
size: 16:9
|
||||
|
||||
slides:
|
||||
- elements:
|
||||
- type: text
|
||||
box: [1, 1, 8, 1]
|
||||
content: "Test"
|
||||
font:
|
||||
color: "red" # 错误
|
||||
size: 4 # 警告
|
||||
"""
|
||||
yaml_path = temp_dir / "test.yaml"
|
||||
yaml_path.write_text(yaml_content)
|
||||
|
||||
validator = Validator()
|
||||
result = validator.validate(yaml_path)
|
||||
|
||||
# 应该同时有错误和警告
|
||||
assert len(result.errors) > 0
|
||||
assert len(result.warnings) > 0
|
||||
|
||||
def test_format_validation_result(self, temp_dir):
|
||||
"""测试验证结果格式化"""
|
||||
yaml_content = """
|
||||
metadata:
|
||||
size: 16:9
|
||||
|
||||
slides:
|
||||
- elements:
|
||||
- type: text
|
||||
box: [1, 1, 8, 1]
|
||||
content: "Test"
|
||||
font:
|
||||
color: "red"
|
||||
"""
|
||||
yaml_path = temp_dir / "test.yaml"
|
||||
yaml_path.write_text(yaml_content)
|
||||
|
||||
validator = Validator()
|
||||
result = validator.validate(yaml_path)
|
||||
|
||||
# 格式化输出
|
||||
output = result.format_output()
|
||||
|
||||
assert "检查" in output
|
||||
if result.errors:
|
||||
assert "错误" in output
|
||||
if result.warnings:
|
||||
assert "警告" in output
|
||||
|
||||
def test_validate_image_resource(self, temp_dir, sample_image):
|
||||
"""测试验证图片资源"""
|
||||
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)
|
||||
|
||||
validator = Validator()
|
||||
result = validator.validate(yaml_path)
|
||||
|
||||
# 图片存在,应该没有错误
|
||||
assert len(result.errors) == 0
|
||||
|
||||
def test_validate_missing_image_resource(self, temp_dir):
|
||||
"""测试验证不存在的图片资源"""
|
||||
yaml_content = """
|
||||
metadata:
|
||||
size: 16:9
|
||||
|
||||
slides:
|
||||
- elements:
|
||||
- type: image
|
||||
box: [1, 1, 4, 3]
|
||||
src: "nonexistent.png"
|
||||
"""
|
||||
yaml_path = temp_dir / "test.yaml"
|
||||
yaml_path.write_text(yaml_content)
|
||||
|
||||
validator = Validator()
|
||||
result = validator.validate(yaml_path)
|
||||
|
||||
# 应该有错误(图片不存在)
|
||||
assert len(result.errors) > 0
|
||||
assert any("图片文件不存在" in e.message for e in result.errors)
|
||||
Reference in New Issue
Block a user