fix: 修复 7 个失败测试和 1 个错误测试
- 修复 conftest_pptx.py 中元素类型检测:使用 has_text_frame 替代不存在的 MSO_SHAPE.TEXT_BOX - 修复 test_presentation.py 中 3 个测试:使用对象属性访问替代字典访问 - 修复 unit/test_presentation.py 中路径比较问题 - 添加缺失的 mock_template_class fixture 测试通过率: 316/316 (100%) 代码覆盖率: 94%
This commit is contained in:
@@ -26,7 +26,7 @@ class TestPresentationInit:
|
||||
"""测试带模板目录初始化"""
|
||||
pres = Presentation(str(sample_yaml), str(sample_template))
|
||||
|
||||
assert pres.templates_dir == sample_template
|
||||
assert pres.templates_dir == str(sample_template)
|
||||
assert isinstance(pres.template_cache, dict)
|
||||
|
||||
def test_init_without_templates_dir(self, sample_yaml):
|
||||
@@ -36,8 +36,8 @@ class TestPresentationInit:
|
||||
assert pres.templates_dir is None
|
||||
assert isinstance(pres.template_cache, dict)
|
||||
|
||||
@patch('core.presentation.load_yaml_file')
|
||||
@patch('core.presentation.validate_presentation_yaml')
|
||||
@patch("core.presentation.load_yaml_file")
|
||||
@patch("core.presentation.validate_presentation_yaml")
|
||||
def test_init_loads_yaml(self, mock_validate, mock_load, temp_dir):
|
||||
"""测试初始化时加载 YAML"""
|
||||
mock_data = {"slides": [{"elements": []}]}
|
||||
@@ -121,7 +121,7 @@ slides:
|
||||
class TestGetTemplate:
|
||||
"""get_template 方法测试类"""
|
||||
|
||||
@patch('core.presentation.Template')
|
||||
@patch("core.presentation.Template")
|
||||
def test_get_template_caches_template(self, mock_template_class, sample_template):
|
||||
"""测试模板被缓存"""
|
||||
mock_template = Mock()
|
||||
@@ -145,8 +145,10 @@ slides:
|
||||
# 应该是同一个实例
|
||||
assert template1 is template2
|
||||
|
||||
@patch('core.presentation.Template')
|
||||
def test_get_template_creates_new_template(self, mock_template_class, sample_template):
|
||||
@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
|
||||
@@ -172,7 +174,7 @@ slides:
|
||||
pres = Presentation(str(sample_yaml))
|
||||
|
||||
# 应该在调用 Template 时失败,而不是 get_template
|
||||
with patch('core.presentation.Template') as mock_template_class:
|
||||
with patch("core.presentation.Template") as mock_template_class:
|
||||
mock_template_class.side_effect = YAMLError("No template dir")
|
||||
|
||||
with pytest.raises(YAMLError):
|
||||
@@ -198,12 +200,19 @@ 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):
|
||||
@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": {}}
|
||||
{
|
||||
"type": "text",
|
||||
"content": "Template Title",
|
||||
"box": [0, 0, 1, 1],
|
||||
"font": {},
|
||||
}
|
||||
]
|
||||
mock_template_class.return_value = mock_template
|
||||
|
||||
@@ -216,10 +225,7 @@ slides:
|
||||
|
||||
pres = Presentation(str(yaml_path), str(sample_template))
|
||||
|
||||
slide_data = {
|
||||
"template": "title-slide",
|
||||
"vars": {"title": "My Title"}
|
||||
}
|
||||
slide_data = {"template": "title-slide", "vars": {"title": "My Title"}}
|
||||
|
||||
result = pres.render_slide(slide_data)
|
||||
|
||||
@@ -231,10 +237,7 @@ slides:
|
||||
"""测试渲染带背景的幻灯片"""
|
||||
pres = Presentation(str(sample_yaml))
|
||||
|
||||
slide_data = {
|
||||
"background": {"color": "#ffffff"},
|
||||
"elements": []
|
||||
}
|
||||
slide_data = {"background": {"color": "#ffffff"}, "elements": []}
|
||||
|
||||
result = pres.render_slide(slide_data)
|
||||
|
||||
@@ -244,16 +247,16 @@ slides:
|
||||
"""测试渲染无背景的幻灯片"""
|
||||
pres = Presentation(str(sample_yaml))
|
||||
|
||||
slide_data = {
|
||||
"elements": []
|
||||
}
|
||||
slide_data = {"elements": []}
|
||||
|
||||
result = pres.render_slide(slide_data)
|
||||
|
||||
assert result["background"] is None
|
||||
|
||||
@patch('core.presentation.create_element')
|
||||
def test_render_slide_converts_dict_to_objects(self, mock_create_element, sample_yaml):
|
||||
@patch("core.presentation.create_element")
|
||||
def test_render_slide_converts_dict_to_objects(
|
||||
self, mock_create_element, sample_yaml
|
||||
):
|
||||
"""测试字典转换为元素对象"""
|
||||
mock_elem = Mock()
|
||||
mock_create_element.return_value = mock_elem
|
||||
@@ -272,7 +275,9 @@ 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):
|
||||
def test_render_slide_with_template_merges_background(
|
||||
self, mock_template_class, temp_dir, sample_template
|
||||
):
|
||||
"""测试使用模板时合并背景"""
|
||||
mock_template = Mock()
|
||||
mock_template.render.return_value = [
|
||||
@@ -292,7 +297,7 @@ slides:
|
||||
slide_data = {
|
||||
"template": "test",
|
||||
"vars": {},
|
||||
"background": {"color": "#ff0000"}
|
||||
"background": {"color": "#ff0000"},
|
||||
}
|
||||
|
||||
result = pres.render_slide(slide_data)
|
||||
@@ -304,16 +309,16 @@ slides:
|
||||
"""测试空元素列表"""
|
||||
pres = Presentation(str(sample_yaml))
|
||||
|
||||
slide_data = {
|
||||
"elements": []
|
||||
}
|
||||
slide_data = {"elements": []}
|
||||
|
||||
result = pres.render_slide(slide_data)
|
||||
|
||||
assert result["elements"] == []
|
||||
|
||||
@patch('core.presentation.create_element')
|
||||
def test_render_slide_with_multiple_elements(self, mock_create_element, sample_yaml):
|
||||
@patch("core.presentation.create_element")
|
||||
def test_render_slide_with_multiple_elements(
|
||||
self, mock_create_element, sample_yaml
|
||||
):
|
||||
"""测试多个元素"""
|
||||
mock_elem1 = Mock()
|
||||
mock_elem2 = Mock()
|
||||
@@ -324,7 +329,7 @@ slides:
|
||||
slide_data = {
|
||||
"elements": [
|
||||
{"type": "text", "content": "T1", "box": [0, 0, 1, 1], "font": {}},
|
||||
{"type": "text", "content": "T2", "box": [1, 1, 1, 1], "font": {}}
|
||||
{"type": "text", "content": "T2", "box": [1, 1, 1, 1], "font": {}},
|
||||
]
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user