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
120 lines
3.3 KiB
Python
120 lines
3.3 KiB
Python
"""
|
|
主验证器单元测试
|
|
|
|
测试 Validator 类的协调和分类功能
|
|
"""
|
|
|
|
import pytest
|
|
from pathlib import Path
|
|
from validators.validator import Validator
|
|
from validators.result import ValidationResult
|
|
|
|
|
|
class TestValidator:
|
|
"""Validator 测试类"""
|
|
|
|
def test_init(self):
|
|
"""测试初始化"""
|
|
validator = Validator()
|
|
assert validator.SLIDE_SIZES == {
|
|
"16:9": (10, 5.625),
|
|
"4:3": (10, 7.5)
|
|
}
|
|
|
|
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
|
|
|
|
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_categorize_issues_error(self):
|
|
"""测试分类问题为错误"""
|
|
validator = Validator()
|
|
from validators.result import ValidationIssue
|
|
|
|
errors = []
|
|
warnings = []
|
|
infos = []
|
|
|
|
issue = ValidationIssue("ERROR", "Test", "", "ERR")
|
|
validator._categorize_issues([issue], errors, warnings, infos)
|
|
|
|
assert len(errors) == 1
|
|
assert len(warnings) == 0
|
|
assert len(infos) == 0
|
|
|
|
def test_categorize_issues_warning(self):
|
|
"""测试分类问题为警告"""
|
|
validator = Validator()
|
|
from validators.result import ValidationIssue
|
|
|
|
errors = []
|
|
warnings = []
|
|
infos = []
|
|
|
|
issue = ValidationIssue("WARNING", "Test", "", "WARN")
|
|
validator._categorize_issues([issue], errors, warnings, infos)
|
|
|
|
assert len(errors) == 0
|
|
assert len(warnings) == 1
|
|
assert len(infos) == 0
|
|
|
|
def test_categorize_issues_info(self):
|
|
"""测试分类问题为提示"""
|
|
validator = Validator()
|
|
from validators.result import ValidationIssue
|
|
|
|
errors = []
|
|
warnings = []
|
|
infos = []
|
|
|
|
issue = ValidationIssue("INFO", "Test", "", "INFO")
|
|
validator._categorize_issues([issue], errors, warnings, infos)
|
|
|
|
assert len(errors) == 0
|
|
assert len(warnings) == 0
|
|
assert len(infos) == 1
|
|
|
|
def test_categorize_mixed_issues(self):
|
|
"""测试分类混合问题"""
|
|
validator = Validator()
|
|
from validators.result import ValidationIssue
|
|
|
|
errors = []
|
|
warnings = []
|
|
infos = []
|
|
|
|
issues = [
|
|
ValidationIssue("ERROR", "Error", "", "ERR"),
|
|
ValidationIssue("WARNING", "Warning", "", "WARN"),
|
|
ValidationIssue("INFO", "Info", "", "INFO"),
|
|
]
|
|
validator._categorize_issues(issues, errors, warnings, infos)
|
|
|
|
assert len(errors) == 1
|
|
assert len(warnings) == 1
|
|
assert len(infos) == 1
|
|
|
|
|
|
class TestSlideSizes:
|
|
"""幻灯片尺寸常量测试"""
|
|
|
|
def test_16_9_size(self):
|
|
"""测试 16:9 尺寸"""
|
|
validator = Validator()
|
|
assert validator.SLIDE_SIZES["16:9"] == (10, 5.625)
|
|
|
|
def test_4_3_size(self):
|
|
"""测试 4:3 尺寸"""
|
|
validator = Validator()
|
|
assert validator.SLIDE_SIZES["4:3"] == (10, 7.5)
|