refactor: 重构外部模板系统,改为单文件模板库模式
主要变更: - 将 templates_dir 参数改为 template_file,支持单个模板库 YAML 文件 - 添加模板库 YAML 验证功能 - 为模板添加 base_dir 支持,正确解析相对路径资源 - 内联模板与外部模板同名时改为警告(内联优先) - 移除模板缓存机制,直接使用模板库字典 - 更新所有相关测试以适配新的模板加载方式 此重构简化了模板管理,使模板资源的路径解析更加清晰明确。
This commit is contained in:
@@ -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()
|
||||
|
||||
Reference in New Issue
Block a user