kotaemon/tests/test_template.py
ian_Cin 08b6e5d3fb [AUR-390] Add prompt template and prompt component (#24)
* Export pipeline to config

* Export the input to config

* Preliminary creating UI dynamically

* Add test for config export

* Try out prompt UI

* Add example projects

* Fix test errors

* Standardize interface for retrieval

* Finalize the UI demo

* Update README.md

* Update README

* Refactor according to main

* Fix typing issue

* Add openai key to git-secret

* Add prompt template and prompt component

* Update test

* update tests

* revert docstring

---------

Co-authored-by: trducng <trungduc1992@gmail.com>
Co-authored-by: Nguyen Trung Duc (john) <john@cinnamon.is>
2023-09-25 14:38:22 +07:00

54 lines
1.9 KiB
Python

import pytest
from kotaemon.prompt.template import PromptTemplate
def test_prompt_template_creation():
# Test case 1: Ensure the PromptTemplate object is created correctly
template_string = "This is a template"
template = PromptTemplate(template_string)
assert template.template == template_string
template_string = "Hello, {name}! Today is {day}."
template = PromptTemplate(template_string)
assert template.template == template_string
assert template.placeholders == {"name", "day"}
def test_prompt_template_addition():
# Test case 2: Ensure the __add__ method concatenates the templates correctly
template1 = PromptTemplate("Hello, ")
template2 = PromptTemplate("world!")
result = template1 + template2
assert result.template == "Hello, \nworld!"
template1 = PromptTemplate("Hello, {name}!")
template2 = PromptTemplate("Today is {day}.")
result = template1 + template2
assert result.template == "Hello, {name}!\nToday is {day}."
def test_prompt_template_extract_placeholders():
# Test case 3: Ensure the extract_placeholders method extracts placeholders
# correctly
template_string = "Hello, {name}! Today is {day}."
result = PromptTemplate.extract_placeholders(template_string)
assert result == {"name", "day"}
def test_prompt_template_populate():
# Test case 4: Ensure the populate method populates the template correctly
template_string = "Hello, {name}! Today is {day}."
template = PromptTemplate(template_string)
result = template.populate(name="John", day="Monday")
assert result == "Hello, John! Today is Monday."
def test_prompt_template_unknown_placeholder():
# Test case 5: Ensure the populate method raises an exception for unknown
# placeholders
template_string = "Hello, {name}! Today is {day}."
template = PromptTemplate(template_string)
with pytest.raises(ValueError):
template.populate(name="John", month="January")