From 5e7f887a571f8ce0617c6b46e1401131d3208da9 Mon Sep 17 00:00:00 2001 From: 7000pctAUTO Date: Wed, 4 Feb 2026 12:33:46 +0000 Subject: [PATCH] Add test suite --- tests/test_core.py | 78 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 tests/test_core.py diff --git a/tests/test_core.py b/tests/test_core.py new file mode 100644 index 0000000..db17cc7 --- /dev/null +++ b/tests/test_core.py @@ -0,0 +1,78 @@ +import pytest +import sys +from pathlib import Path + +sys.path.insert(0, str(Path(__file__).parent.parent / "src")) + +from promptforge.core.prompt import Prompt, PromptVariable, VariableType +from promptforge.core.template import TemplateEngine +from promptforge.core.exceptions import MissingVariableError, InvalidPromptError + + +class TestPrompt: + def test_prompt_creation(self): + prompt = Prompt( + name="Test Prompt", + description="A test prompt", + content="Hello {{name}}!" + ) + assert prompt.name == "Test Prompt" + assert prompt.version == "1.0.0" + assert prompt.content == "Hello {{name}}!" + + def test_prompt_from_yaml(self): + yaml_content = """--- +name: YAML Prompt +description: Loaded from YAML +version: "2.0.0" +--- +Hello {{name}} +""" + prompt = Prompt.from_yaml(yaml_content) + assert prompt.name == "YAML Prompt" + assert prompt.version == "2.0.0" + assert prompt.content == "Hello {{name}}" + + def test_prompt_to_yaml(self): + prompt = Prompt( + name="Test Prompt", + content="Hello {{name}}" + ) + yaml_output = prompt.to_yaml() + assert "---" in yaml_output + assert "name: Test Prompt" in yaml_output + assert "Hello {{name}}" in yaml_output + + def test_prompt_save_and_load(self, tmp_path): + prompt = Prompt( + name="Save Test", + content="Content: {{text}}" + ) + filepath = prompt.save(tmp_path) + assert filepath.exists() + + loaded = Prompt.load(filepath) + assert loaded.name == "Save Test" + + +class TestTemplateEngine: + def test_render_simple(self): + engine = TemplateEngine() + result = engine.render("Hello {{name}}!", {"name": "World"}) + assert result == "Hello World!" + + def test_render_missing_variable(self): + engine = TemplateEngine() + with pytest.raises(MissingVariableError): + engine.render("Hello {{name}}!", {}) + + def test_get_variables(self): + engine = TemplateEngine() + vars = engine.get_variables("Hello {{name}}, your score is {{score}}") + assert "name" in vars + assert "score" in vars + + def test_boolean_conversion(self): + engine = TemplateEngine() + result = engine.render("Enabled: {{enabled}}", {"enabled": "true"}) + assert result == "Enabled: True" \ No newline at end of file