78 lines
2.3 KiB
Python
78 lines
2.3 KiB
Python
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" |