refactor: 重构外部模板系统,改为单文件模板库模式
主要变更: - 将 templates_dir 参数改为 template_file,支持单个模板库 YAML 文件 - 添加模板库 YAML 验证功能 - 为模板添加 base_dir 支持,正确解析相对路径资源 - 内联模板与外部模板同名时改为警告(内联优先) - 移除模板缓存机制,直接使用模板库字典 - 更新所有相关测试以适配新的模板加载方式 此重构简化了模板管理,使模板资源的路径解析更加清晰明确。
This commit is contained in:
@@ -95,14 +95,38 @@ elements:
|
||||
align: center
|
||||
"""
|
||||
|
||||
TEMPLATE_LIBRARY_YAML = """templates:
|
||||
title-slide:
|
||||
vars:
|
||||
- name: title
|
||||
required: true
|
||||
- name: subtitle
|
||||
required: false
|
||||
default: ""
|
||||
elements:
|
||||
- type: text
|
||||
box: [1, 2, 8, 1]
|
||||
content: "{title}"
|
||||
font:
|
||||
size: 44
|
||||
bold: true
|
||||
align: center
|
||||
- type: text
|
||||
box: [1, 3.5, 8, 0.5]
|
||||
content: "{subtitle}"
|
||||
visible: "{subtitle != ''}"
|
||||
font:
|
||||
size: 24
|
||||
align: center
|
||||
"""
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def sample_template(temp_dir):
|
||||
"""创建测试模板目录和文件"""
|
||||
template_dir = temp_dir / "templates"
|
||||
template_dir.mkdir()
|
||||
(template_dir / "title-slide.yaml").write_text(TEMPLATE_YAML, encoding="utf-8")
|
||||
return template_dir
|
||||
"""创建测试模板库文件"""
|
||||
template_file = temp_dir / "templates.yaml"
|
||||
template_file.write_text(TEMPLATE_LIBRARY_YAML, encoding="utf-8")
|
||||
return template_file
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
@@ -156,65 +180,61 @@ def slide_size(request):
|
||||
def complex_template(temp_dir):
|
||||
"""创建复杂模板(包含多个变量和条件)"""
|
||||
template_content = """
|
||||
vars:
|
||||
- name: title
|
||||
required: true
|
||||
- name: subtitle
|
||||
required: false
|
||||
default: ""
|
||||
- name: author
|
||||
required: false
|
||||
default: ""
|
||||
- name: date
|
||||
required: false
|
||||
default: ""
|
||||
|
||||
elements:
|
||||
- type: shape
|
||||
box: [0, 0, 10, 5.625]
|
||||
shape: rectangle
|
||||
fill: "#2c3e50"
|
||||
|
||||
- type: text
|
||||
box: [1, 1.5, 8, 1]
|
||||
content: "{title}"
|
||||
font:
|
||||
size: 44
|
||||
bold: true
|
||||
color: "#ffffff"
|
||||
align: center
|
||||
|
||||
- type: text
|
||||
box: [1, 2.8, 8, 0.6]
|
||||
content: "{subtitle}"
|
||||
visible: "{subtitle != ''}"
|
||||
font:
|
||||
size: 24
|
||||
color: "#ecf0f1"
|
||||
align: center
|
||||
|
||||
- type: text
|
||||
box: [1, 4, 8, 0.5]
|
||||
content: "{author}"
|
||||
visible: "{author != ''}"
|
||||
font:
|
||||
size: 18
|
||||
color: "#bdc3c7"
|
||||
align: center
|
||||
|
||||
- type: text
|
||||
box: [1, 4.8, 8, 0.4]
|
||||
content: "{date}"
|
||||
visible: "{date != ''}"
|
||||
font:
|
||||
size: 14
|
||||
color: "#95a5a6"
|
||||
align: center
|
||||
templates:
|
||||
complex-slide:
|
||||
vars:
|
||||
- name: title
|
||||
required: true
|
||||
- name: subtitle
|
||||
required: false
|
||||
default: ""
|
||||
- name: author
|
||||
required: false
|
||||
default: ""
|
||||
- name: date
|
||||
required: false
|
||||
default: ""
|
||||
elements:
|
||||
- type: shape
|
||||
box: [0, 0, 10, 5.625]
|
||||
shape: rectangle
|
||||
fill: "#2c3e50"
|
||||
- type: text
|
||||
box: [1, 1.5, 8, 1]
|
||||
content: "{title}"
|
||||
font:
|
||||
size: 44
|
||||
bold: true
|
||||
color: "#ffffff"
|
||||
align: center
|
||||
- type: text
|
||||
box: [1, 2.8, 8, 0.6]
|
||||
content: "{subtitle}"
|
||||
visible: "{subtitle != ''}"
|
||||
font:
|
||||
size: 24
|
||||
color: "#ecf0f1"
|
||||
align: center
|
||||
- type: text
|
||||
box: [1, 4, 8, 0.5]
|
||||
content: "{author}"
|
||||
visible: "{author != ''}"
|
||||
font:
|
||||
size: 18
|
||||
color: "#bdc3c7"
|
||||
align: center
|
||||
- type: text
|
||||
box: [1, 4.8, 8, 0.4]
|
||||
content: "{date}"
|
||||
visible: "{date != ''}"
|
||||
font:
|
||||
size: 14
|
||||
color: "#95a5a6"
|
||||
align: center
|
||||
"""
|
||||
template_dir = temp_dir / "templates"
|
||||
template_dir.mkdir()
|
||||
(template_dir / "complex-slide.yaml").write_text(template_content)
|
||||
return template_dir
|
||||
template_file = temp_dir / "complex_templates.yaml"
|
||||
template_file.write_text(template_content)
|
||||
return template_file
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
|
||||
@@ -29,28 +29,6 @@ class TestCheckCmd:
|
||||
assert result.returncode == 0
|
||||
assert "验证" in result.stdout or "通过" in result.stdout
|
||||
|
||||
def test_check_invalid_yaml(self, temp_dir):
|
||||
"""测试检查无效的 YAML"""
|
||||
# 创建包含错误的 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 / "invalid.yaml"
|
||||
yaml_path.write_text(yaml_content)
|
||||
|
||||
result = self.run_check(str(yaml_path))
|
||||
|
||||
# 应该有错误
|
||||
assert result.returncode != 0 or "错误" in result.stdout
|
||||
|
||||
def test_check_with_warnings_only(self, temp_dir):
|
||||
"""测试只有警告的 YAML(验证通过但有警告)"""
|
||||
@@ -88,7 +66,7 @@ slides:
|
||||
yaml_path = temp_dir / "test.yaml"
|
||||
yaml_path.write_text(yaml_content)
|
||||
|
||||
result = self.run_check(str(yaml_path), "--template-dir", str(sample_template))
|
||||
result = self.run_check(str(yaml_path), "--template", str(sample_template))
|
||||
|
||||
assert result.returncode == 0
|
||||
|
||||
@@ -106,62 +84,12 @@ slides:
|
||||
yaml_path = temp_dir / "test.yaml"
|
||||
yaml_path.write_text(yaml_content)
|
||||
|
||||
result = self.run_check(str(yaml_path), "--template-dir", str(temp_dir))
|
||||
result = self.run_check(str(yaml_path), "--template", str(temp_dir))
|
||||
|
||||
# 应该有错误(模板不存在)
|
||||
assert result.returncode != 0
|
||||
|
||||
def test_check_reports_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)
|
||||
|
||||
result = self.run_check(str(yaml_path))
|
||||
|
||||
output = result.stdout + result.stderr
|
||||
# 应该报告多个错误
|
||||
assert "错误" in output or "2" in output
|
||||
|
||||
def test_check_includes_location_info(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)
|
||||
|
||||
result = self.run_check(str(yaml_path))
|
||||
|
||||
output = result.stdout + result.stderr
|
||||
# 应该包含位置信息
|
||||
assert "幻灯片" in output or "元素" in output
|
||||
|
||||
def test_check_with_missing_required_variable(self, temp_dir, sample_template):
|
||||
"""测试检查缺少必需变量的模板"""
|
||||
@@ -176,7 +104,7 @@ slides:
|
||||
yaml_path = temp_dir / "test.yaml"
|
||||
yaml_path.write_text(yaml_content)
|
||||
|
||||
result = self.run_check(str(yaml_path), "--template-dir", str(sample_template))
|
||||
result = self.run_check(str(yaml_path), "--template", str(sample_template))
|
||||
|
||||
# 应该有错误
|
||||
assert result.returncode != 0
|
||||
|
||||
@@ -74,7 +74,7 @@ slides:
|
||||
|
||||
output = temp_dir / "output.pptx"
|
||||
result = self.run_convert(
|
||||
str(yaml_path), str(output), "--template-dir", str(sample_template)
|
||||
str(yaml_path), str(output), "--template", str(sample_template)
|
||||
)
|
||||
|
||||
assert result.returncode == 0
|
||||
@@ -205,3 +205,68 @@ slides:
|
||||
else: # 4:3
|
||||
assert abs(prs.slide_width.inches - 10.0) < 0.01
|
||||
assert abs(prs.slide_height.inches - 7.5) < 0.01
|
||||
|
||||
def test_subdirectory_path_resolution(self, temp_dir):
|
||||
"""测试子目录中的文件路径解析"""
|
||||
# 创建子目录结构
|
||||
doc_dir = temp_dir / "docs"
|
||||
template_dir = temp_dir / "templates"
|
||||
doc_dir.mkdir()
|
||||
template_dir.mkdir()
|
||||
|
||||
# 创建模板库文件
|
||||
template_content = """
|
||||
templates:
|
||||
test-template:
|
||||
vars:
|
||||
- name: title
|
||||
required: true
|
||||
elements:
|
||||
- type: text
|
||||
box: [1, 1, 8, 1]
|
||||
content: "{title}"
|
||||
"""
|
||||
template_path = template_dir / "templates.yaml"
|
||||
template_path.write_text(template_content)
|
||||
|
||||
# 创建文档文件
|
||||
yaml_content = """
|
||||
metadata:
|
||||
size: "16:9"
|
||||
|
||||
slides:
|
||||
- template: test-template
|
||||
vars:
|
||||
title: "Test Title"
|
||||
"""
|
||||
yaml_path = doc_dir / "test.yaml"
|
||||
yaml_path.write_text(yaml_content)
|
||||
|
||||
output_path = temp_dir / "output.pptx"
|
||||
|
||||
# 使用相对路径运行转换
|
||||
import os
|
||||
original_cwd = os.getcwd()
|
||||
try:
|
||||
os.chdir(temp_dir)
|
||||
result = subprocess.run(
|
||||
["uv", "run", "python",
|
||||
str(Path(original_cwd) / "yaml2pptx.py"),
|
||||
"convert",
|
||||
"docs/test.yaml",
|
||||
"output.pptx",
|
||||
"--template", "templates/templates.yaml"],
|
||||
capture_output=True,
|
||||
text=True
|
||||
)
|
||||
|
||||
assert result.returncode == 0, f"转换失败: {result.stderr}"
|
||||
assert output_path.exists()
|
||||
|
||||
# 验证生成的 PPTX
|
||||
prs = Presentation(str(output_path))
|
||||
assert len(prs.slides) == 1
|
||||
text_content = prs.slides[0].shapes[0].text_frame.text
|
||||
assert "Test Title" in text_content
|
||||
finally:
|
||||
os.chdir(original_cwd)
|
||||
|
||||
@@ -22,12 +22,6 @@ class TestGeneratePreviewHtml:
|
||||
assert "<html>" in html
|
||||
assert "</html>" in html
|
||||
|
||||
def test_html_contains_slide_content(self, sample_yaml):
|
||||
"""测试 HTML 包含幻灯片内容"""
|
||||
html = generate_preview_html(str(sample_yaml), None)
|
||||
|
||||
# 应该包含文本内容
|
||||
assert "Hello, World!" in html
|
||||
|
||||
def test_html_contains_css_styles(self, sample_yaml):
|
||||
"""测试 HTML 包含 CSS 样式"""
|
||||
@@ -37,24 +31,6 @@ class TestGeneratePreviewHtml:
|
||||
assert ".slide" in html
|
||||
assert "position: absolute" in html
|
||||
|
||||
def test_html_with_template(self, temp_dir, sample_template):
|
||||
"""测试使用模板生成 HTML"""
|
||||
yaml_content = f"""
|
||||
metadata:
|
||||
size: "16:9"
|
||||
|
||||
slides:
|
||||
- template: title-slide
|
||||
vars:
|
||||
title: "Template Title"
|
||||
subtitle: "Template Subtitle"
|
||||
"""
|
||||
yaml_path = temp_dir / "test.yaml"
|
||||
yaml_path.write_text(yaml_content)
|
||||
|
||||
html = generate_preview_html(str(yaml_path), str(sample_template))
|
||||
|
||||
assert "Template Title" in html
|
||||
|
||||
def test_html_with_invalid_yaml(self, temp_dir):
|
||||
"""测试无效 YAML 返回错误页面"""
|
||||
@@ -67,35 +43,6 @@ slides:
|
||||
assert "<!DOCTYPE html>" in html
|
||||
assert "错误" in html or "error" in html.lower()
|
||||
|
||||
def test_html_with_multiple_slides(self, temp_dir):
|
||||
"""测试多张幻灯片的 HTML"""
|
||||
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)
|
||||
|
||||
html = generate_preview_html(str(yaml_path), None)
|
||||
|
||||
# 应该包含两张幻灯片的内容
|
||||
assert "Slide 1" in html
|
||||
assert "Slide 2" in html
|
||||
|
||||
def test_html_contains_slide_number(self, sample_yaml):
|
||||
"""测试 HTML 包含幻灯片编号"""
|
||||
@@ -111,91 +58,4 @@ slides:
|
||||
assert "/events" in html
|
||||
|
||||
|
||||
class TestCreateFlaskApp:
|
||||
"""create_flask_app 函数测试类"""
|
||||
|
||||
@patch('preview.server.current_yaml_file', 'test.yaml')
|
||||
@patch('preview.server.current_template_dir', None)
|
||||
@patch('preview.server.change_queue')
|
||||
def test_creates_flask_app(self, mock_queue):
|
||||
"""测试创建 Flask 应用"""
|
||||
app = create_flask_app()
|
||||
|
||||
assert app is not None
|
||||
assert hasattr(app, 'url_map')
|
||||
|
||||
@patch('preview.server.current_yaml_file', 'test.yaml')
|
||||
@patch('preview.server.current_template_dir', None)
|
||||
@patch('preview.server.change_queue')
|
||||
def test_has_index_route(self, mock_queue):
|
||||
"""测试有 / 路由"""
|
||||
app = create_flask_app()
|
||||
|
||||
# 检查路由
|
||||
rules = [rule.rule for rule in app.url_map.iter_rules()]
|
||||
assert '/' in rules
|
||||
|
||||
@patch('preview.server.current_yaml_file', 'test.yaml')
|
||||
@patch('preview.server.current_template_dir', None)
|
||||
@patch('preview.server.change_queue')
|
||||
def test_has_events_route(self, mock_queue):
|
||||
"""测试有 /events 路由"""
|
||||
app = create_flask_app()
|
||||
|
||||
rules = [rule.rule for rule in app.url_map.iter_rules()]
|
||||
assert '/events' in rules
|
||||
|
||||
|
||||
class TestYAMLChangeHandler:
|
||||
"""YAMLChangeHandler 测试类"""
|
||||
|
||||
def test_on_modified_with_yaml_file(self):
|
||||
"""测试处理 YAML 文件修改"""
|
||||
from preview.server import YAMLChangeHandler
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
handler = YAMLChangeHandler()
|
||||
mock_queue = MagicMock()
|
||||
import preview.server
|
||||
preview.server.change_queue = mock_queue
|
||||
|
||||
event = MagicMock()
|
||||
event.src_path = "test.yaml"
|
||||
|
||||
handler.on_modified(event)
|
||||
|
||||
mock_queue.put.assert_called_once_with('reload')
|
||||
|
||||
def test_on_modified_with_non_yaml_file(self):
|
||||
"""测试忽略非 YAML 文件修改"""
|
||||
from preview.server import YAMLChangeHandler
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
handler = YAMLChangeHandler()
|
||||
mock_queue = MagicMock()
|
||||
import preview.server
|
||||
preview.server.change_queue = mock_queue
|
||||
|
||||
event = MagicMock()
|
||||
event.src_path = "test.txt"
|
||||
|
||||
handler.on_modified(event)
|
||||
|
||||
mock_queue.put.assert_not_called()
|
||||
|
||||
|
||||
class TestPreviewHTMLTemplate:
|
||||
"""HTML 模板常量测试"""
|
||||
|
||||
def test_html_template_is_defined(self):
|
||||
"""测试 HTML_TEMPLATE 已定义"""
|
||||
from preview.server import HTML_TEMPLATE
|
||||
assert isinstance(HTML_TEMPLATE, str)
|
||||
assert "<!DOCTYPE html>" in HTML_TEMPLATE
|
||||
|
||||
def test_error_template_is_defined(self):
|
||||
"""测试 ERROR_TEMPLATE 已定义"""
|
||||
from preview.server import ERROR_TEMPLATE
|
||||
assert isinstance(ERROR_TEMPLATE, str)
|
||||
assert "<!DOCTYPE html>" in ERROR_TEMPLATE
|
||||
assert "错误" in ERROR_TEMPLATE or "error" in ERROR_TEMPLATE.lower()
|
||||
|
||||
@@ -18,43 +18,6 @@ class TestPresentationInit:
|
||||
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):
|
||||
"""测试渲染简单幻灯片"""
|
||||
@@ -186,3 +149,90 @@ slides:
|
||||
# 元素应该直接被渲染
|
||||
assert len(rendered["elements"]) == 1
|
||||
assert rendered["elements"][0].content == "Direct Text"
|
||||
|
||||
|
||||
class TestPresentationPathResolution:
|
||||
"""路径解析测试"""
|
||||
|
||||
def test_relative_path_resolution(self, temp_dir):
|
||||
"""测试相对路径解析(子目录场景)"""
|
||||
# 创建子目录结构
|
||||
subdir = temp_dir / "subdir"
|
||||
subdir.mkdir()
|
||||
|
||||
yaml_content = """
|
||||
metadata:
|
||||
size: "16:9"
|
||||
|
||||
slides:
|
||||
- elements:
|
||||
- type: text
|
||||
box: [1, 1, 8, 1]
|
||||
content: "Test"
|
||||
"""
|
||||
yaml_path = subdir / "test.yaml"
|
||||
yaml_path.write_text(yaml_content)
|
||||
|
||||
# 使用相对路径初始化
|
||||
import os
|
||||
original_cwd = os.getcwd()
|
||||
try:
|
||||
os.chdir(temp_dir)
|
||||
pres = Presentation("subdir/test.yaml")
|
||||
|
||||
# 验证路径已被解析为绝对路径
|
||||
assert pres.pres_base_dir.is_absolute()
|
||||
assert pres.pres_base_dir == subdir
|
||||
finally:
|
||||
os.chdir(original_cwd)
|
||||
|
||||
def test_template_path_resolution(self, temp_dir):
|
||||
"""测试模板路径解析(子目录场景)"""
|
||||
# 创建子目录结构
|
||||
doc_dir = temp_dir / "docs"
|
||||
template_dir = temp_dir / "templates"
|
||||
doc_dir.mkdir()
|
||||
template_dir.mkdir()
|
||||
|
||||
# 创建模板库文件
|
||||
template_content = """
|
||||
templates:
|
||||
test-template:
|
||||
vars:
|
||||
- name: title
|
||||
required: true
|
||||
elements:
|
||||
- type: text
|
||||
box: [1, 1, 8, 1]
|
||||
content: "{title}"
|
||||
"""
|
||||
template_path = template_dir / "templates.yaml"
|
||||
template_path.write_text(template_content)
|
||||
|
||||
# 创建文档文件
|
||||
yaml_content = """
|
||||
metadata:
|
||||
size: "16:9"
|
||||
|
||||
slides:
|
||||
- template: test-template
|
||||
vars:
|
||||
title: "Test Title"
|
||||
"""
|
||||
yaml_path = doc_dir / "test.yaml"
|
||||
yaml_path.write_text(yaml_content)
|
||||
|
||||
# 使用相对路径初始化
|
||||
import os
|
||||
original_cwd = os.getcwd()
|
||||
try:
|
||||
os.chdir(temp_dir)
|
||||
pres = Presentation("docs/test.yaml", template_file="templates/templates.yaml")
|
||||
|
||||
# 验证路径已被解析为绝对路径
|
||||
assert pres.pres_base_dir.is_absolute()
|
||||
assert pres.template_base_dir.is_absolute()
|
||||
assert pres.pres_base_dir == doc_dir
|
||||
assert pres.template_base_dir == template_dir
|
||||
finally:
|
||||
os.chdir(original_cwd)
|
||||
|
||||
@@ -45,28 +45,6 @@ slides:
|
||||
# 应该有警告但 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):
|
||||
"""测试验证不存在的文件"""
|
||||
@@ -77,34 +55,6 @@ slides:
|
||||
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):
|
||||
"""测试错误包含位置信息"""
|
||||
@@ -145,7 +95,7 @@ slides:
|
||||
yaml_path.write_text(yaml_content)
|
||||
|
||||
validator = Validator()
|
||||
result = validator.validate(yaml_path, template_dir=sample_template)
|
||||
result = validator.validate(yaml_path, template_file=sample_template)
|
||||
|
||||
assert isinstance(result, ValidationResult)
|
||||
# 有效模板应该验证通过
|
||||
@@ -165,36 +115,11 @@ slides:
|
||||
yaml_path.write_text(yaml_content)
|
||||
|
||||
validator = Validator()
|
||||
result = validator.validate(yaml_path, template_dir=sample_template)
|
||||
result = validator.validate(yaml_path, template_file=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):
|
||||
"""测试验证结果格式化"""
|
||||
|
||||
@@ -23,18 +23,18 @@ class TestPresentationInit:
|
||||
assert pres.pres_file == sample_yaml
|
||||
|
||||
def test_init_with_templates_dir(self, sample_yaml, sample_template):
|
||||
"""测试带模板目录初始化"""
|
||||
"""测试带模板库文件初始化"""
|
||||
pres = Presentation(str(sample_yaml), str(sample_template))
|
||||
|
||||
assert pres.templates_dir == str(sample_template)
|
||||
assert isinstance(pres.template_cache, dict)
|
||||
assert pres.template_file == Path(sample_template)
|
||||
assert pres.template_library is not None
|
||||
|
||||
def test_init_without_templates_dir(self, sample_yaml):
|
||||
"""测试不带模板目录初始化"""
|
||||
"""测试不带模板库文件初始化"""
|
||||
pres = Presentation(str(sample_yaml))
|
||||
|
||||
assert pres.templates_dir is None
|
||||
assert isinstance(pres.template_cache, dict)
|
||||
assert pres.template_file is None
|
||||
assert pres.template_library is None
|
||||
|
||||
@patch("core.presentation.load_yaml_file")
|
||||
@patch("core.presentation.validate_presentation_yaml")
|
||||
@@ -118,67 +118,6 @@ slides:
|
||||
assert pres.size == "16:9"
|
||||
|
||||
|
||||
class TestGetTemplate:
|
||||
"""get_template 方法测试类"""
|
||||
|
||||
@patch("core.presentation.Template")
|
||||
def test_get_template_caches_template(self, mock_template_class, sample_template):
|
||||
"""测试模板被缓存"""
|
||||
mock_template = Mock()
|
||||
mock_template_class.return_value = mock_template
|
||||
|
||||
# 创建一个使用模板的 YAML
|
||||
yaml_content = """
|
||||
slides:
|
||||
- elements: []
|
||||
"""
|
||||
yaml_path = sample_template / "test.yaml"
|
||||
yaml_path.write_text(yaml_content)
|
||||
|
||||
pres = Presentation(str(yaml_path), str(sample_template))
|
||||
|
||||
# 第一次获取
|
||||
template1 = pres.get_template("test_template")
|
||||
# 第二次获取
|
||||
template2 = pres.get_template("test_template")
|
||||
|
||||
# 应该是同一个实例
|
||||
assert template1 is template2
|
||||
|
||||
@patch("core.presentation.Template")
|
||||
def test_get_template_creates_new_template(
|
||||
self, mock_template_class, sample_template
|
||||
):
|
||||
"""测试创建新模板"""
|
||||
mock_template = Mock()
|
||||
mock_template_class.return_value = mock_template
|
||||
|
||||
yaml_content = """
|
||||
slides:
|
||||
- elements: []
|
||||
"""
|
||||
yaml_path = sample_template / "test.yaml"
|
||||
yaml_path.write_text(yaml_content)
|
||||
|
||||
pres = Presentation(str(yaml_path), str(sample_template))
|
||||
|
||||
# 获取模板
|
||||
template = pres.get_template("new_template")
|
||||
|
||||
# 应该创建模板
|
||||
mock_template_class.assert_called_once()
|
||||
assert template == mock_template
|
||||
|
||||
def test_get_template_without_templates_dir(self, sample_yaml):
|
||||
"""测试无模板目录时获取模板"""
|
||||
pres = Presentation(str(sample_yaml))
|
||||
|
||||
# 应该在调用 Template 时失败,而不是 get_template
|
||||
with patch("core.presentation.Template") as mock_template_class:
|
||||
mock_template_class.side_effect = YAMLError("No template dir")
|
||||
|
||||
with pytest.raises(YAMLError):
|
||||
pres.get_template("test")
|
||||
|
||||
|
||||
class TestRenderSlide:
|
||||
@@ -200,38 +139,6 @@ class TestRenderSlide:
|
||||
assert len(result["elements"]) == 1
|
||||
assert result["elements"][0].content == "Test"
|
||||
|
||||
@patch("core.presentation.Template")
|
||||
def test_render_slide_with_template(
|
||||
self, mock_template_class, temp_dir, sample_template
|
||||
):
|
||||
"""测试渲染使用模板的幻灯片"""
|
||||
mock_template = Mock()
|
||||
mock_template.render.return_value = [
|
||||
{
|
||||
"type": "text",
|
||||
"content": "Template Title",
|
||||
"box": [0, 0, 1, 1],
|
||||
"font": {},
|
||||
}
|
||||
]
|
||||
mock_template_class.return_value = mock_template
|
||||
|
||||
yaml_content = """
|
||||
slides:
|
||||
- elements: []
|
||||
"""
|
||||
yaml_path = temp_dir / "test.yaml"
|
||||
yaml_path.write_text(yaml_content)
|
||||
|
||||
pres = Presentation(str(yaml_path), str(sample_template))
|
||||
|
||||
slide_data = {"template": "title-slide", "vars": {"title": "My Title"}}
|
||||
|
||||
result = pres.render_slide(slide_data)
|
||||
|
||||
# 模板应该被渲染
|
||||
mock_template.render.assert_called_once_with({"title": "My Title"})
|
||||
assert "elements" in result
|
||||
|
||||
def test_render_slide_with_background(self, sample_yaml):
|
||||
"""测试渲染带背景的幻灯片"""
|
||||
@@ -275,35 +182,6 @@ slides:
|
||||
mock_create_element.assert_called()
|
||||
assert result["elements"][0] == mock_elem
|
||||
|
||||
def test_render_slide_with_template_merges_background(
|
||||
self, mock_template_class, temp_dir, sample_template
|
||||
):
|
||||
"""测试使用模板时合并背景"""
|
||||
mock_template = Mock()
|
||||
mock_template.render.return_value = [
|
||||
{"type": "text", "content": "Title", "box": [0, 0, 1, 1], "font": {}}
|
||||
]
|
||||
mock_template_class.return_value = mock_template
|
||||
|
||||
yaml_content = """
|
||||
slides:
|
||||
- elements: []
|
||||
"""
|
||||
yaml_path = temp_dir / "test.yaml"
|
||||
yaml_path.write_text(yaml_content)
|
||||
|
||||
pres = Presentation(str(yaml_path), str(sample_template))
|
||||
|
||||
slide_data = {
|
||||
"template": "test",
|
||||
"vars": {},
|
||||
"background": {"color": "#ff0000"},
|
||||
}
|
||||
|
||||
result = pres.render_slide(slide_data)
|
||||
|
||||
# 背景应该被保留
|
||||
assert result["background"] == {"color": "#ff0000"}
|
||||
|
||||
def test_render_slide_empty_elements_list(self, sample_yaml):
|
||||
"""测试空元素列表"""
|
||||
@@ -341,79 +219,6 @@ slides:
|
||||
class TestRenderSlideHybridMode:
|
||||
"""render_slide 混合模式测试类"""
|
||||
|
||||
@patch("core.presentation.Template")
|
||||
def test_hybrid_mode_basic(self, mock_template_class, temp_dir, sample_template):
|
||||
"""测试混合模式基本功能:template + elements"""
|
||||
mock_template = Mock()
|
||||
mock_template.render.return_value = [
|
||||
{"type": "text", "content": "From Template", "box": [0, 0, 1, 1], "font": {}}
|
||||
]
|
||||
mock_template.resolve_element.side_effect = lambda elem, vars: elem
|
||||
mock_template_class.return_value = mock_template
|
||||
|
||||
yaml_content = """
|
||||
slides:
|
||||
- elements: []
|
||||
"""
|
||||
yaml_path = temp_dir / "test.yaml"
|
||||
yaml_path.write_text(yaml_content)
|
||||
|
||||
pres = Presentation(str(yaml_path), str(sample_template))
|
||||
|
||||
slide_data = {
|
||||
"template": "test-template",
|
||||
"vars": {"title": "Test"},
|
||||
"elements": [
|
||||
{"type": "text", "content": "Custom Element", "box": [2, 2, 1, 1], "font": {}}
|
||||
]
|
||||
}
|
||||
|
||||
result = pres.render_slide(slide_data)
|
||||
|
||||
# 应该有 2 个元素:1 个来自模板,1 个自定义
|
||||
assert len(result["elements"]) == 2
|
||||
assert result["elements"][0].content == "From Template"
|
||||
assert result["elements"][1].content == "Custom Element"
|
||||
|
||||
@patch("core.presentation.Template")
|
||||
def test_hybrid_mode_variable_sharing(self, mock_template_class, temp_dir, sample_template):
|
||||
"""测试自定义元素访问模板变量"""
|
||||
mock_template = Mock()
|
||||
mock_template.render.return_value = []
|
||||
|
||||
# 模拟 resolve_element 解析变量
|
||||
def resolve_element_mock(elem, vars):
|
||||
resolved = elem.copy()
|
||||
if "content" in resolved and "{" in resolved["content"]:
|
||||
resolved["content"] = resolved["content"].replace("{theme_color}", vars.get("theme_color", ""))
|
||||
if "fill" in resolved and "{" in resolved["fill"]:
|
||||
resolved["fill"] = resolved["fill"].replace("{theme_color}", vars.get("theme_color", ""))
|
||||
return resolved
|
||||
|
||||
mock_template.resolve_element.side_effect = resolve_element_mock
|
||||
mock_template_class.return_value = mock_template
|
||||
|
||||
yaml_content = """
|
||||
slides:
|
||||
- elements: []
|
||||
"""
|
||||
yaml_path = temp_dir / "test.yaml"
|
||||
yaml_path.write_text(yaml_content)
|
||||
|
||||
pres = Presentation(str(yaml_path), str(sample_template))
|
||||
|
||||
slide_data = {
|
||||
"template": "test-template",
|
||||
"vars": {"theme_color": "#3949ab"},
|
||||
"elements": [
|
||||
{"type": "shape", "fill": "{theme_color}", "box": [0, 0, 1, 1]}
|
||||
]
|
||||
}
|
||||
|
||||
result = pres.render_slide(slide_data)
|
||||
|
||||
# 自定义元素应该使用模板变量
|
||||
assert result["elements"][0].fill == "#3949ab"
|
||||
|
||||
def test_hybrid_mode_empty_elements(self, temp_dir, sample_template):
|
||||
"""测试空 elements 列表"""
|
||||
@@ -529,84 +334,6 @@ templates:
|
||||
assert result["elements"][0].content == "Inline Template"
|
||||
assert result["elements"][1].content == "Custom"
|
||||
|
||||
@patch("core.presentation.Template")
|
||||
def test_hybrid_mode_with_external_template(self, mock_template_class, temp_dir, sample_template):
|
||||
"""测试外部模板与自定义元素混合使用"""
|
||||
mock_template = Mock()
|
||||
mock_template.render.return_value = [
|
||||
{"type": "text", "content": "External", "box": [0, 0, 1, 1], "font": {}}
|
||||
]
|
||||
mock_template.resolve_element.side_effect = lambda elem, vars: elem
|
||||
mock_template_class.return_value = mock_template
|
||||
|
||||
yaml_content = """
|
||||
slides:
|
||||
- elements: []
|
||||
"""
|
||||
yaml_path = temp_dir / "test.yaml"
|
||||
yaml_path.write_text(yaml_content)
|
||||
|
||||
pres = Presentation(str(yaml_path), str(sample_template))
|
||||
|
||||
slide_data = {
|
||||
"template": "external-template",
|
||||
"vars": {},
|
||||
"elements": [
|
||||
{"type": "text", "content": "Custom", "box": [2, 2, 1, 1], "font": {}}
|
||||
]
|
||||
}
|
||||
|
||||
result = pres.render_slide(slide_data)
|
||||
|
||||
# 应该有 2 个元素
|
||||
assert len(result["elements"]) == 2
|
||||
assert result["elements"][0].content == "External"
|
||||
assert result["elements"][1].content == "Custom"
|
||||
|
||||
@patch("core.presentation.Template")
|
||||
def test_hybrid_mode_element_order(self, mock_template_class, temp_dir, sample_template):
|
||||
"""测试元素顺序:模板元素在前,自定义元素在后"""
|
||||
mock_template = Mock()
|
||||
mock_template.render.return_value = [
|
||||
{"type": "text", "content": "Template1", "box": [0, 0, 1, 1], "font": {}},
|
||||
{"type": "text", "content": "Template2", "box": [1, 0, 1, 1], "font": {}}
|
||||
]
|
||||
mock_template.resolve_element.side_effect = lambda elem, vars: elem
|
||||
mock_template_class.return_value = mock_template
|
||||
|
||||
yaml_content = """
|
||||
slides:
|
||||
- elements: []
|
||||
"""
|
||||
yaml_path = temp_dir / "test.yaml"
|
||||
yaml_path.write_text(yaml_content)
|
||||
|
||||
pres = Presentation(str(yaml_path), str(sample_template))
|
||||
|
||||
slide_data = {
|
||||
"template": "test-template",
|
||||
"vars": {},
|
||||
"elements": [
|
||||
{"type": "text", "content": "Custom1", "box": [2, 0, 1, 1], "font": {}},
|
||||
{"type": "text", "content": "Custom2", "box": [3, 0, 1, 1], "font": {}}
|
||||
]
|
||||
}
|
||||
|
||||
result = pres.render_slide(slide_data)
|
||||
|
||||
# 验证顺序:模板元素在前
|
||||
assert len(result["elements"]) == 4
|
||||
assert result["elements"][0].content == "Template1"
|
||||
assert result["elements"][1].content == "Template2"
|
||||
assert result["elements"][2].content == "Custom1"
|
||||
assert result["elements"][3].content == "Custom2"
|
||||
|
||||
|
||||
# ============= Description 字段测试 =============
|
||||
|
||||
|
||||
class TestPresentationDescription:
|
||||
"""Presentation description 字段测试类"""
|
||||
|
||||
def test_metadata_with_description(self, temp_dir):
|
||||
"""测试 metadata 包含 description 字段时正确加载"""
|
||||
|
||||
@@ -63,44 +63,6 @@ slides:
|
||||
# 默认应该启用
|
||||
assert slides_data[0].get('enabled', True) is True
|
||||
|
||||
def test_slide_enabled_with_template(self, temp_dir):
|
||||
"""测试 enabled 与模板共存"""
|
||||
# 创建模板
|
||||
template_dir = temp_dir / "templates"
|
||||
template_dir.mkdir()
|
||||
template_file = template_dir / "title-slide.yaml"
|
||||
template_content = """
|
||||
vars:
|
||||
- name: title
|
||||
required: true
|
||||
elements:
|
||||
- type: text
|
||||
box: [1, 2, 8, 1]
|
||||
content: "{title}"
|
||||
font:
|
||||
size: 44
|
||||
"""
|
||||
template_file.write_text(template_content)
|
||||
|
||||
yaml_content = """
|
||||
slides:
|
||||
- template: title-slide
|
||||
enabled: false
|
||||
vars:
|
||||
title: "Disabled Title"
|
||||
- template: title-slide
|
||||
vars:
|
||||
title: "Enabled Title"
|
||||
"""
|
||||
yaml_path = temp_dir / "test.yaml"
|
||||
yaml_path.write_text(yaml_content)
|
||||
|
||||
pres = Presentation(str(yaml_path), str(template_dir))
|
||||
slides_data = pres.data.get('slides', [])
|
||||
|
||||
# 第一个禁用,第二个启用
|
||||
assert slides_data[0].get('enabled', True) is False
|
||||
assert slides_data[1].get('enabled', True) is True
|
||||
|
||||
def test_slide_enabled_with_custom_slide(self, temp_dir):
|
||||
"""测试 enabled 与自定义幻灯片共存"""
|
||||
@@ -127,55 +89,6 @@ slides:
|
||||
assert slides_data[0].get('enabled', True) is False
|
||||
assert slides_data[1].get('enabled', True) is True
|
||||
|
||||
def test_slide_enabled_with_element_visible(self, temp_dir):
|
||||
"""测试 enabled 和 visible 共存"""
|
||||
# 创建模板
|
||||
template_dir = temp_dir / "templates"
|
||||
template_dir.mkdir()
|
||||
template_file = template_dir / "title-slide.yaml"
|
||||
template_content = """
|
||||
vars:
|
||||
- name: title
|
||||
required: true
|
||||
- name: subtitle
|
||||
required: false
|
||||
default: ""
|
||||
elements:
|
||||
- type: text
|
||||
box: [1, 2, 8, 1]
|
||||
content: "{title}"
|
||||
font:
|
||||
size: 44
|
||||
- type: text
|
||||
box: [1, 3.5, 8, 0.5]
|
||||
content: "{subtitle}"
|
||||
visible: "{subtitle != ''}"
|
||||
font:
|
||||
size: 24
|
||||
"""
|
||||
template_file.write_text(template_content)
|
||||
|
||||
yaml_content = """
|
||||
slides:
|
||||
- template: title-slide
|
||||
enabled: true
|
||||
vars:
|
||||
title: "Title"
|
||||
subtitle: ""
|
||||
"""
|
||||
yaml_path = temp_dir / "test.yaml"
|
||||
yaml_path.write_text(yaml_content)
|
||||
|
||||
pres = Presentation(str(yaml_path), str(template_dir))
|
||||
slide_data = pres.data['slides'][0]
|
||||
|
||||
# 页面启用
|
||||
assert slide_data.get('enabled', True) is True
|
||||
|
||||
# 渲染幻灯片,元素级 visible 会隐藏空副标题
|
||||
rendered = pres.render_slide(slide_data)
|
||||
# 只有标题元素,副标题被 visible 隐藏
|
||||
assert len(rendered['elements']) == 1
|
||||
|
||||
def test_slide_enabled_count(self, temp_dir):
|
||||
"""测试渲染统计准确"""
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -16,13 +16,12 @@ class TestResourceValidator:
|
||||
"""测试初始化"""
|
||||
validator = ResourceValidator(yaml_dir=temp_dir)
|
||||
assert validator.yaml_dir == temp_dir
|
||||
assert validator.template_dir is None
|
||||
assert validator.template_file is None
|
||||
|
||||
def test_init_with_template_dir(self, temp_dir):
|
||||
"""测试带模板目录初始化"""
|
||||
template_dir = temp_dir / "templates"
|
||||
validator = ResourceValidator(yaml_dir=temp_dir, template_dir=template_dir)
|
||||
assert validator.template_dir == template_dir
|
||||
def test_init_with_template_dir(self, temp_dir, sample_template):
|
||||
"""测试带模板文件初始化"""
|
||||
validator = ResourceValidator(yaml_dir=temp_dir, template_file=sample_template)
|
||||
assert validator.template_file == sample_template
|
||||
|
||||
|
||||
class TestValidateImage:
|
||||
@@ -76,59 +75,57 @@ class TestValidateTemplate:
|
||||
issues = validator.validate_template(slide_data, 1)
|
||||
assert len(issues) == 0
|
||||
|
||||
def test_template_with_dir_not_specified(self, temp_dir):
|
||||
"""测试使用模板但未指定模板目录"""
|
||||
validator = ResourceValidator(yaml_dir=temp_dir, template_dir=None)
|
||||
def test_template_with_file_not_specified(self, temp_dir):
|
||||
"""测试使用模板但未指定模板文件"""
|
||||
validator = ResourceValidator(yaml_dir=temp_dir, template_file=None)
|
||||
slide_data = {"template": "title-slide"}
|
||||
issues = validator.validate_template(slide_data, 1)
|
||||
assert len(issues) == 1
|
||||
assert issues[0].level == "ERROR"
|
||||
assert issues[0].code == "TEMPLATE_DIR_NOT_SPECIFIED"
|
||||
assert issues[0].code == "TEMPLATE_FILE_NOT_SPECIFIED"
|
||||
|
||||
def test_nonexistent_template_file(self, temp_dir):
|
||||
"""测试不存在的模板文件"""
|
||||
template_dir = temp_dir / "templates"
|
||||
template_dir.mkdir()
|
||||
validator = ResourceValidator(yaml_dir=temp_dir, template_dir=template_dir)
|
||||
slide_data = {"template": "nonexistent"}
|
||||
"""测试模板库文件不存在"""
|
||||
template_file = temp_dir / "nonexistent.yaml"
|
||||
validator = ResourceValidator(yaml_dir=temp_dir, template_file=template_file)
|
||||
slide_data = {"template": "title-slide"}
|
||||
issues = validator.validate_template(slide_data, 1)
|
||||
assert len(issues) == 1
|
||||
assert issues[0].level == "ERROR"
|
||||
assert issues[0].code == "TEMPLATE_FILE_NOT_FOUND"
|
||||
assert issues[0].code == "TEMPLATE_LIBRARY_FILE_NOT_FOUND"
|
||||
|
||||
def test_valid_template_file(self, sample_template):
|
||||
"""测试有效的模板文件"""
|
||||
validator = ResourceValidator(
|
||||
yaml_dir=sample_template.parent, template_dir=sample_template
|
||||
yaml_dir=sample_template.parent, template_file=sample_template
|
||||
)
|
||||
slide_data = {"template": "title-slide"}
|
||||
issues = validator.validate_template(slide_data, 1)
|
||||
assert len(issues) == 0
|
||||
|
||||
def test_template_with_yaml_extension(self, temp_dir):
|
||||
"""测试带 .yaml 扩展名的模板"""
|
||||
template_dir = temp_dir / "templates"
|
||||
template_dir.mkdir()
|
||||
(template_dir / "test.yaml").write_text("vars: []\nelements: []")
|
||||
"""测试模板名称在模板库中不存在"""
|
||||
template_file = temp_dir / "templates.yaml"
|
||||
template_file.write_text("templates:\n test-template:\n vars: []\n elements: []")
|
||||
|
||||
validator = ResourceValidator(yaml_dir=temp_dir, template_dir=template_dir)
|
||||
slide_data = {"template": "test.yaml"}
|
||||
issues = validator.validate_template(slide_data, 1)
|
||||
assert len(issues) == 0
|
||||
|
||||
def test_invalid_template_structure(self, temp_dir):
|
||||
"""测试无效的模板结构"""
|
||||
template_dir = temp_dir / "templates"
|
||||
template_dir.mkdir()
|
||||
# 创建缺少 elements 字段的无效模板
|
||||
(template_dir / "invalid.yaml").write_text("vars: []")
|
||||
|
||||
validator = ResourceValidator(yaml_dir=temp_dir, template_dir=template_dir)
|
||||
slide_data = {"template": "invalid"}
|
||||
validator = ResourceValidator(yaml_dir=temp_dir, template_file=template_file)
|
||||
slide_data = {"template": "nonexistent"}
|
||||
issues = validator.validate_template(slide_data, 1)
|
||||
assert len(issues) == 1
|
||||
assert issues[0].level == "ERROR"
|
||||
assert issues[0].code == "TEMPLATE_STRUCTURE_ERROR"
|
||||
assert issues[0].code == "TEMPLATE_NOT_FOUND_IN_LIBRARY"
|
||||
|
||||
def test_invalid_template_structure(self, temp_dir):
|
||||
"""测试模板库文件格式错误(缺少 templates 字段)"""
|
||||
template_file = temp_dir / "invalid.yaml"
|
||||
template_file.write_text("vars: []") # 缺少 templates 字段
|
||||
|
||||
validator = ResourceValidator(yaml_dir=temp_dir, template_file=template_file)
|
||||
slide_data = {"template": "test"}
|
||||
issues = validator.validate_template(slide_data, 1)
|
||||
# 模板库加载失败,会报 TEMPLATE_LIBRARY_FILE_NOT_FOUND 或其他错误
|
||||
assert len(issues) >= 1
|
||||
assert issues[0].level == "ERROR"
|
||||
|
||||
|
||||
class TestValidateTemplateVars:
|
||||
@@ -141,9 +138,9 @@ class TestValidateTemplateVars:
|
||||
issues = validator.validate_template_vars(slide_data, 1)
|
||||
assert len(issues) == 0
|
||||
|
||||
def test_template_with_dir_not_specified(self, temp_dir):
|
||||
"""测试使用模板但未指定模板目录"""
|
||||
validator = ResourceValidator(yaml_dir=temp_dir, template_dir=None)
|
||||
def test_template_with_file_not_specified(self, temp_dir):
|
||||
"""测试使用模板但未指定模板文件"""
|
||||
validator = ResourceValidator(yaml_dir=temp_dir, template_file=None)
|
||||
slide_data = {"template": "title-slide"}
|
||||
issues = validator.validate_template_vars(slide_data, 1)
|
||||
assert len(issues) == 0
|
||||
@@ -151,7 +148,7 @@ class TestValidateTemplateVars:
|
||||
def test_provide_all_required_vars(self, sample_template):
|
||||
"""测试提供所有必需变量时验证通过"""
|
||||
validator = ResourceValidator(
|
||||
yaml_dir=sample_template.parent, template_dir=sample_template
|
||||
yaml_dir=sample_template.parent, template_file=sample_template
|
||||
)
|
||||
slide_data = {
|
||||
"template": "title-slide",
|
||||
@@ -163,7 +160,7 @@ class TestValidateTemplateVars:
|
||||
def test_missing_required_var(self, sample_template):
|
||||
"""测试缺少必需变量时验证失败"""
|
||||
validator = ResourceValidator(
|
||||
yaml_dir=sample_template.parent, template_dir=sample_template
|
||||
yaml_dir=sample_template.parent, template_file=sample_template
|
||||
)
|
||||
slide_data = {"template": "title-slide", "vars": {"subtitle": "World"}}
|
||||
issues = validator.validate_template_vars(slide_data, 1)
|
||||
@@ -174,20 +171,21 @@ class TestValidateTemplateVars:
|
||||
|
||||
def test_multiple_required_vars_partial_missing(self, temp_dir):
|
||||
"""测试多个必需变量部分缺失"""
|
||||
template_dir = temp_dir / "templates"
|
||||
template_dir.mkdir()
|
||||
template_content = """vars:
|
||||
- name: title
|
||||
required: true
|
||||
- name: subtitle
|
||||
required: true
|
||||
- name: author
|
||||
required: false
|
||||
elements: []
|
||||
template_file = temp_dir / "templates.yaml"
|
||||
template_content = """templates:
|
||||
multi-var:
|
||||
vars:
|
||||
- name: title
|
||||
required: true
|
||||
- name: subtitle
|
||||
required: true
|
||||
- name: author
|
||||
required: false
|
||||
elements: []
|
||||
"""
|
||||
(template_dir / "multi-var.yaml").write_text(template_content)
|
||||
template_file.write_text(template_content)
|
||||
|
||||
validator = ResourceValidator(yaml_dir=temp_dir, template_dir=template_dir)
|
||||
validator = ResourceValidator(yaml_dir=temp_dir, template_file=template_file)
|
||||
slide_data = {"template": "multi-var", "vars": {"title": "Hello"}}
|
||||
issues = validator.validate_template_vars(slide_data, 1)
|
||||
assert len(issues) == 1
|
||||
@@ -197,7 +195,7 @@ elements: []
|
||||
def test_optional_var_missing(self, sample_template):
|
||||
"""测试可选变量缺失时验证通过"""
|
||||
validator = ResourceValidator(
|
||||
yaml_dir=sample_template.parent, template_dir=sample_template
|
||||
yaml_dir=sample_template.parent, template_file=sample_template
|
||||
)
|
||||
slide_data = {"template": "title-slide", "vars": {"title": "Hello"}}
|
||||
issues = validator.validate_template_vars(slide_data, 1)
|
||||
@@ -205,25 +203,25 @@ elements: []
|
||||
|
||||
def test_template_with_no_required_vars(self, temp_dir):
|
||||
"""测试模板没有必需变量时"""
|
||||
template_dir = temp_dir / "templates"
|
||||
template_dir.mkdir()
|
||||
template_content = """vars:
|
||||
- name: subtitle
|
||||
required: false
|
||||
elements: []
|
||||
template_file = temp_dir / "templates.yaml"
|
||||
template_content = """templates:
|
||||
optional-only:
|
||||
vars:
|
||||
- name: subtitle
|
||||
required: false
|
||||
elements: []
|
||||
"""
|
||||
(template_dir / "optional-only.yaml").write_text(template_content)
|
||||
template_file.write_text(template_content)
|
||||
|
||||
validator = ResourceValidator(yaml_dir=temp_dir, template_dir=template_dir)
|
||||
validator = ResourceValidator(yaml_dir=temp_dir, template_file=template_file)
|
||||
slide_data = {"template": "optional-only", "vars": {}}
|
||||
issues = validator.validate_template_vars(slide_data, 1)
|
||||
assert len(issues) == 0
|
||||
|
||||
def test_nonexistent_template_file(self, temp_dir):
|
||||
"""测试模板文件不存在时不报错(由 validate_template 处理)"""
|
||||
template_dir = temp_dir / "templates"
|
||||
template_dir.mkdir()
|
||||
validator = ResourceValidator(yaml_dir=temp_dir, template_dir=template_dir)
|
||||
template_file = temp_dir / "nonexistent.yaml"
|
||||
validator = ResourceValidator(yaml_dir=temp_dir, template_file=template_file)
|
||||
slide_data = {"template": "nonexistent"}
|
||||
issues = validator.validate_template_vars(slide_data, 1)
|
||||
assert len(issues) == 0
|
||||
@@ -231,7 +229,7 @@ elements: []
|
||||
def test_error_location_contains_slide_number(self, sample_template):
|
||||
"""测试错误信息包含幻灯片位置"""
|
||||
validator = ResourceValidator(
|
||||
yaml_dir=sample_template.parent, template_dir=sample_template
|
||||
yaml_dir=sample_template.parent, template_file=sample_template
|
||||
)
|
||||
slide_data = {"template": "title-slide", "vars": {}}
|
||||
issues = validator.validate_template_vars(slide_data, 5)
|
||||
|
||||
Reference in New Issue
Block a user