simplified
This commit is contained in:
125
test_smart_variables.py
Normal file
125
test_smart_variables.py
Normal file
@@ -0,0 +1,125 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Test the smart variable detection that distinguishes required from optional"""
|
||||
|
||||
from pathlib import Path
|
||||
from src.llmutils.prompt_manager import PromptManager
|
||||
|
||||
# Create test prompts directory
|
||||
test_dir = Path("test_prompts")
|
||||
test_dir.mkdir(exist_ok=True)
|
||||
|
||||
# Test 1: Simple required variable
|
||||
simple = test_dir / "simple.md"
|
||||
simple.write_text("Hello {{ name }}!")
|
||||
|
||||
# Test 2: Optional variable in conditional
|
||||
conditional = test_dir / "conditional.md"
|
||||
conditional.write_text("""Status: OK
|
||||
{% if error %}
|
||||
Error: {{ error }}
|
||||
{% endif %}""")
|
||||
|
||||
# Test 3: Variable with default filter
|
||||
default_filter = test_dir / "default.md"
|
||||
default_filter.write_text("""Hello {{ name | default('Guest') }}!
|
||||
Age: {{ age }}""")
|
||||
|
||||
# Test 4: Complex mix
|
||||
complex_template = test_dir / "complex.md"
|
||||
complex_template.write_text("""# Report for {{ title }}
|
||||
|
||||
Status: {{ status }}
|
||||
|
||||
{% if error %}
|
||||
⚠️ ERROR: {{ error }}
|
||||
{% endif %}
|
||||
|
||||
{% if warning %}
|
||||
⚠️ WARNING: {{ warning }}
|
||||
{% endif %}
|
||||
|
||||
Debug Level: {{ debug_level | default(0) }}
|
||||
|
||||
{% for item in items %}
|
||||
- {{ item }}
|
||||
{% endfor %}
|
||||
|
||||
Generated by: {{ author | default('System') }}""")
|
||||
|
||||
# Configure PromptManager
|
||||
PromptManager.configure(path=test_dir)
|
||||
|
||||
print("=" * 70)
|
||||
print("Testing Smart Variable Detection (Required vs Optional)")
|
||||
print("=" * 70)
|
||||
|
||||
# Test each template
|
||||
test_cases = [
|
||||
("simple", {"name": "Alice"}),
|
||||
("conditional", {"error": "Timeout"}),
|
||||
("default", {"age": 25}),
|
||||
("complex", {"title": "Daily Report", "status": "OK", "items": ["task1", "task2"]})
|
||||
]
|
||||
|
||||
for prompt_name, test_data in test_cases:
|
||||
print(f"\n{'='*50}")
|
||||
print(f"TESTING: {prompt_name}")
|
||||
print("=" * 50)
|
||||
|
||||
prompt = PromptManager.get_prompt(prompt_name)
|
||||
|
||||
print(f"\nAll variables: {prompt.variables}")
|
||||
print(f"Required variables: {prompt.required_variables}")
|
||||
print(f"Optional variables: {prompt.optional_variables}")
|
||||
|
||||
print(f"\nProviding: {test_data}")
|
||||
|
||||
# Test validation
|
||||
is_valid = prompt.validate(**test_data)
|
||||
missing = prompt.get_missing_variables(**test_data)
|
||||
|
||||
print(f"Valid: {is_valid}")
|
||||
if missing:
|
||||
print(f"Missing required: {missing}")
|
||||
|
||||
# Try to fill
|
||||
try:
|
||||
filled = prompt.fill(**test_data)
|
||||
print("\nFilled successfully (showing first 200 chars):")
|
||||
print(filled[:200])
|
||||
except ValueError as e:
|
||||
print(f"\nError filling: {e}")
|
||||
|
||||
# Test 5: Demonstrate that optional variables don't need to be provided
|
||||
print("\n" + "=" * 70)
|
||||
print("DEMONSTRATING OPTIONAL VARIABLES")
|
||||
print("=" * 70)
|
||||
|
||||
prompt = PromptManager.get_prompt("complex")
|
||||
print(f"\nRequired: {prompt.required_variables}")
|
||||
print(f"Optional: {prompt.optional_variables}")
|
||||
|
||||
# Provide only required variables
|
||||
minimal_data = {
|
||||
"title": "Test Report",
|
||||
"status": "Running",
|
||||
"items": ["item1", "item2", "item3"]
|
||||
}
|
||||
|
||||
print(f"\nProviding only required variables: {minimal_data}")
|
||||
try:
|
||||
filled = prompt.fill(**minimal_data)
|
||||
print("\n✓ Filled successfully with only required variables!")
|
||||
print("\nOutput:")
|
||||
print("-" * 40)
|
||||
print(filled)
|
||||
except ValueError as e:
|
||||
print(f"\n✗ Error: {e}")
|
||||
|
||||
print("\n" + "=" * 70)
|
||||
print("Tests completed!")
|
||||
print("=" * 70)
|
||||
|
||||
# Cleanup
|
||||
import shutil
|
||||
shutil.rmtree(test_dir)
|
||||
Reference in New Issue
Block a user