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
156 lines
4.9 KiB
Python
156 lines
4.9 KiB
Python
"""
|
|
验证结果数据结构单元测试
|
|
|
|
测试 ValidationIssue 和 ValidationResult 类
|
|
"""
|
|
|
|
import pytest
|
|
from validators.result import ValidationIssue, ValidationResult
|
|
|
|
|
|
class TestValidationIssue:
|
|
"""ValidationIssue 测试类"""
|
|
|
|
def test_create_error_issue(self):
|
|
"""测试创建错误级别问题"""
|
|
issue = ValidationIssue(
|
|
level="ERROR",
|
|
message="Test error",
|
|
location="Slide 1",
|
|
code="TEST_ERROR"
|
|
)
|
|
assert issue.level == "ERROR"
|
|
assert issue.message == "Test error"
|
|
assert issue.location == "Slide 1"
|
|
assert issue.code == "TEST_ERROR"
|
|
|
|
def test_create_warning_issue(self):
|
|
"""测试创建警告级别问题"""
|
|
issue = ValidationIssue(
|
|
level="WARNING",
|
|
message="Test warning",
|
|
location="",
|
|
code="TEST_WARNING"
|
|
)
|
|
assert issue.level == "WARNING"
|
|
|
|
def test_create_info_issue(self):
|
|
"""测试创建提示级别问题"""
|
|
issue = ValidationIssue(
|
|
level="INFO",
|
|
message="Test info",
|
|
location="",
|
|
code="TEST_INFO"
|
|
)
|
|
assert issue.level == "INFO"
|
|
|
|
|
|
class TestValidationResult:
|
|
"""ValidationResult 测试类"""
|
|
|
|
def test_create_with_defaults(self):
|
|
"""测试使用默认值创建"""
|
|
result = ValidationResult(valid=True)
|
|
assert result.valid is True
|
|
assert result.errors == []
|
|
assert result.warnings == []
|
|
assert result.infos == []
|
|
|
|
def test_create_with_issues(self):
|
|
"""测试创建包含问题的结果"""
|
|
errors = [ValidationIssue("ERROR", "Error 1", "", "ERR1")]
|
|
warnings = [ValidationIssue("WARNING", "Warning 1", "", "WARN1")]
|
|
infos = [ValidationIssue("INFO", "Info 1", "", "INFO1")]
|
|
|
|
result = ValidationResult(
|
|
valid=False,
|
|
errors=errors,
|
|
warnings=warnings,
|
|
infos=infos
|
|
)
|
|
assert result.valid is False
|
|
assert len(result.errors) == 1
|
|
assert len(result.warnings) == 1
|
|
assert len(result.infos) == 1
|
|
|
|
def test_has_errors(self):
|
|
"""测试 has_errors 方法"""
|
|
result = ValidationResult(
|
|
valid=True,
|
|
errors=[ValidationIssue("ERROR", "Error", "", "ERR")]
|
|
)
|
|
assert result.has_errors() is True
|
|
|
|
def test_has_errors_no_errors(self):
|
|
"""测试没有错误时 has_errors 返回 False"""
|
|
result = ValidationResult(valid=True)
|
|
assert result.has_errors() is False
|
|
|
|
def test_format_output_clean(self):
|
|
"""测试格式化输出 - 无问题"""
|
|
result = ValidationResult(valid=True)
|
|
output = result.format_output()
|
|
assert "验证通过" in output
|
|
|
|
def test_format_output_with_errors(self):
|
|
"""测试格式化输出 - 包含错误"""
|
|
result = ValidationResult(
|
|
valid=False,
|
|
errors=[
|
|
ValidationIssue("ERROR", "Error 1", "Slide 1", "ERR1"),
|
|
ValidationIssue("ERROR", "Error 2", "Slide 2", "ERR2")
|
|
]
|
|
)
|
|
output = result.format_output()
|
|
assert "2 个错误" in output
|
|
assert "Error 1" in output
|
|
assert "Error 2" in output
|
|
assert "[Slide 1]" in output
|
|
|
|
def test_format_output_with_warnings(self):
|
|
"""测试格式化输出 - 包含警告"""
|
|
result = ValidationResult(
|
|
valid=True,
|
|
warnings=[
|
|
ValidationIssue("WARNING", "Warning 1", "Slide 1", "WARN1")
|
|
]
|
|
)
|
|
output = result.format_output()
|
|
assert "1 个警告" in output
|
|
assert "Warning 1" in output
|
|
|
|
def test_format_output_with_infos(self):
|
|
"""测试格式化输出 - 包含提示"""
|
|
result = ValidationResult(
|
|
valid=True,
|
|
infos=[
|
|
ValidationIssue("INFO", "Info 1", "", "INFO1")
|
|
]
|
|
)
|
|
output = result.format_output()
|
|
assert "1 个提示" in output
|
|
|
|
def test_format_output_mixed_issues(self):
|
|
"""测试格式化输出 - 混合问题"""
|
|
result = ValidationResult(
|
|
valid=False,
|
|
errors=[ValidationIssue("ERROR", "Error", "", "ERR")],
|
|
warnings=[ValidationIssue("WARNING", "Warning", "", "WARN")],
|
|
infos=[ValidationIssue("INFO", "Info", "", "INFO")]
|
|
)
|
|
output = result.format_output()
|
|
assert "1 个错误" in output
|
|
assert "1 个警告" in output
|
|
assert "1 个提示" in output
|
|
|
|
def test_format_output_issue_without_location(self):
|
|
"""测试没有位置信息的问题"""
|
|
result = ValidationResult(
|
|
valid=False,
|
|
errors=[ValidationIssue("ERROR", "Error", "", "ERR")]
|
|
)
|
|
output = result.format_output()
|
|
assert "Error" in output
|
|
# 不应该有位置前缀
|
|
assert "[]" not in output
|