""" YAML 加载器单元测试 测试 load_yaml_file、validate_presentation_yaml 和 validate_template_yaml 函数 """ import pytest from pathlib import Path from loaders.yaml_loader import ( load_yaml_file, validate_presentation_yaml, validate_template_yaml, YAMLError ) class TestLoadYamlFile: """load_yaml_file 函数测试类""" def test_load_valid_yaml(self, sample_yaml): """测试加载有效的 YAML 文件""" data = load_yaml_file(sample_yaml) assert isinstance(data, dict) assert "slides" in data def test_load_nonexistent_file(self, temp_dir): """测试加载不存在的文件""" nonexistent = temp_dir / "nonexistent.yaml" with pytest.raises(YAMLError, match="文件不存在"): load_yaml_file(nonexistent) def test_load_directory_raises_error(self, temp_dir): """测试加载目录会引发错误""" with pytest.raises(YAMLError, match="不是有效的文件"): load_yaml_file(temp_dir) def test_load_yaml_with_syntax_error(self, temp_dir): """测试加载包含语法错误的 YAML""" invalid_file = temp_dir / "invalid.yaml" invalid_file.write_text("key: [unclosed list") with pytest.raises(YAMLError, match="YAML 语法错误"): load_yaml_file(invalid_file) def test_load_utf8_content(self, temp_dir): """测试加载 UTF-8 编码的内容""" utf8_file = temp_dir / "utf8.yaml" utf8_file.write_text("key: '中文内容'", encoding='utf-8') data = load_yaml_file(utf8_file) assert data["key"] == "中文内容" class TestValidatePresentationYaml: """validate_presentation_yaml 函数测试类""" def test_valid_presentation_structure(self): """测试有效的演示文稿结构""" data = { "metadata": {"size": "16:9"}, "slides": [{"elements": []}] } # 不应该引发错误 validate_presentation_yaml(data) def test_missing_slides_field(self): """测试缺少 slides 字段""" data = {"metadata": {"size": "16:9"}} with pytest.raises(YAMLError, match="缺少必需字段 'slides'"): validate_presentation_yaml(data) def test_slides_not_a_list(self): """测试 slides 不是列表""" data = {"slides": "not a list"} with pytest.raises(YAMLError, match="'slides' 必须是一个列表"): validate_presentation_yaml(data) def test_not_a_dict(self): """测试不是字典""" data = "not a dict" with pytest.raises(YAMLError, match="必须是一个字典对象"): validate_presentation_yaml(data) def test_empty_slides_list(self): """测试空的 slides 列表""" data = {"slides": []} # 空列表是有效的 validate_presentation_yaml(data) def test_with_file_path_in_error(self): """测试错误消息包含文件路径""" data = {"not_slides": []} with pytest.raises(YAMLError) as exc_info: validate_presentation_yaml(data, "test.yaml") assert "test.yaml" in str(exc_info.value) class TestValidateTemplateYaml: """validate_template_yaml 函数测试类""" def test_valid_template_structure(self): """测试有效的模板结构""" data = { "vars": [ {"name": "title", "required": True} ], "elements": [] } # 不应该引发错误 validate_template_yaml(data) def test_missing_elements_field(self): """测试缺少 elements 字段""" data = {"vars": []} with pytest.raises(YAMLError, match="缺少必需字段 'elements'"): validate_template_yaml(data) def test_elements_not_a_list(self): """测试 elements 不是列表""" data = {"elements": "not a list"} with pytest.raises(YAMLError, match="'elements' 必须是一个列表"): validate_template_yaml(data) def test_vars_not_a_list(self): """测试 vars 不是列表""" data = { "vars": "not a list", "elements": [] } with pytest.raises(YAMLError, match="'vars' 必须是一个列表"): validate_template_yaml(data) def test_var_item_not_a_dict(self): """测试变量项不是字典""" data = { "vars": ["not a dict"], "elements": [] } with pytest.raises(YAMLError, match="必须是一个字典对象"): validate_template_yaml(data) def test_var_item_missing_name(self): """测试变量项缺少 name 字段""" data = { "vars": [{"required": True}], "elements": [] } with pytest.raises(YAMLError, match="缺少必需字段 'name'"): validate_template_yaml(data) def test_template_without_vars(self): """测试没有 vars 的模板""" data = {"elements": []} # vars 是可选的 validate_template_yaml(data) def test_not_a_dict(self): """测试不是字典""" data = "not a dict" with pytest.raises(YAMLError, match="必须是一个字典对象"): validate_template_yaml(data) class TestYAMLError: """YAMLError 异常测试类""" def test_is_an_exception(self): """测试 YAMLError 是异常类""" error = YAMLError("Test error") assert isinstance(error, Exception) def test_can_be_raised_and_caught(self): """测试可以被引发和捕获""" with pytest.raises(YAMLError): raise YAMLError("Test")