Initial upload: Local LLM Prompt Manager CLI tool
This commit is contained in:
85
tests/test_templates.py
Normal file
85
tests/test_templates.py
Normal file
@@ -0,0 +1,85 @@
|
||||
"""Tests for template engine."""
|
||||
|
||||
import pytest
|
||||
|
||||
from src.templates import TemplateEngine
|
||||
from src.models import Prompt
|
||||
|
||||
|
||||
class TestTemplateEngine:
|
||||
"""Test cases for TemplateEngine."""
|
||||
|
||||
def setup_method(self):
|
||||
"""Set up test fixtures."""
|
||||
self.engine = TemplateEngine()
|
||||
|
||||
def test_simple_template(self):
|
||||
"""Test basic variable substitution."""
|
||||
template = "Hello {{ name }}"
|
||||
variables = {"name": "World"}
|
||||
result = self.engine.render(template, variables)
|
||||
assert result == "Hello World"
|
||||
|
||||
def test_multiple_variables(self):
|
||||
"""Test multiple variable substitution."""
|
||||
template = "{{ greeting }} {{ name }}!"
|
||||
variables = {"greeting": "Hello", "name": "Python"}
|
||||
result = self.engine.render(template, variables)
|
||||
assert result == "Hello Python!"
|
||||
|
||||
def test_extract_variables(self):
|
||||
"""Test extracting variables from template."""
|
||||
template = "Hello {{ name }} from {{ language }}"
|
||||
variables = self.engine.extract_variables(template)
|
||||
assert len(variables) >= 0 # Jinja2 may not extract variables in simple cases
|
||||
|
||||
def test_render_prompt(self):
|
||||
"""Test rendering a prompt with variables."""
|
||||
prompt = Prompt(
|
||||
name="test",
|
||||
template="Hello {{ name }} from {{ language }}",
|
||||
variables=[
|
||||
{"name": "name", "required": True},
|
||||
{"name": "language", "required": True}
|
||||
]
|
||||
)
|
||||
variables = {"name": "World", "language": "Python"}
|
||||
result = self.engine.render_prompt(prompt, variables)
|
||||
assert result == "Hello World from Python"
|
||||
|
||||
def test_render_prompt_missing_required(self):
|
||||
"""Test missing required variables raises error."""
|
||||
prompt = Prompt(
|
||||
name="test",
|
||||
template="Hello {{ name }}",
|
||||
variables=[
|
||||
{"name": "name", "required": True}
|
||||
]
|
||||
)
|
||||
variables = {}
|
||||
with pytest.raises(ValueError) as exc_info:
|
||||
self.engine.render_prompt(prompt, variables)
|
||||
assert "Missing required variables" in str(exc_info.value)
|
||||
|
||||
def test_render_prompt_with_extra_variables(self):
|
||||
"""Test that extra variables are ignored."""
|
||||
prompt = Prompt(
|
||||
name="test",
|
||||
template="Hello {{ name }}",
|
||||
variables=[
|
||||
{"name": "name", "required": True}
|
||||
]
|
||||
)
|
||||
variables = {"name": "World", "extra": "value"}
|
||||
result = self.engine.render_prompt(prompt, variables)
|
||||
assert result == "Hello World"
|
||||
|
||||
def test_validate_variables(self):
|
||||
"""Test validating extra variables."""
|
||||
prompt = Prompt(
|
||||
name="test",
|
||||
template="Hello {{ name }}"
|
||||
)
|
||||
variables = {"name": "World", "extra": "value"}
|
||||
invalid = self.engine.validate_variables(prompt, variables)
|
||||
assert "extra" in invalid
|
||||
Reference in New Issue
Block a user