Skip to content

Commit

Permalink
Tests (#38)
Browse files Browse the repository at this point in the history
* fix : test_custom_prompt_template.py updated

* fix : title test cases added to test_custom_prompt_template.py

* fix : copy/str test cases added to test_custom_prompt_template.py

* fix : save/load test cases added to test_custom_prompt_template.py

* fix : complete template tests

* fix : complete response tests

* fix : complete prompt tests

* fix : minor bug in render method fixed

* fix : prompt render test cases added

* fix : autopep8

* doc : TODOs added

* fix : codecov script added

* fix : test cases name updated

* fix : template title updated
  • Loading branch information
sepandhaghighi authored Feb 9, 2025
1 parent 761e7c7 commit 5e22bce
Show file tree
Hide file tree
Showing 7 changed files with 481 additions and 31 deletions.
6 changes: 6 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ jobs:
- name: Test with pytest
run: |
python -m pytest . --cov=memor --cov-report=term
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v4
with:
fail_ci_if_error: true
token: ${{ secrets.CODECOV_TOKEN }}
if: matrix.python-version == env.TEST_PYTHON_VERSION && matrix.os == env.TEST_OS
- name: Vulture, Bandit and Pydocstyle tests
run: |
python -m vulture memor/ otherfiles/ setup.py --min-confidence 65 --exclude=__init__.py --sort-by-size
Expand Down
1 change: 1 addition & 0 deletions memor/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
from .template import CustomPromptTemplate, PresetPromptTemplate
from .prompt import Prompt, Role
from .response import Response
from .errors import MemorRenderError, MemorValidationError

__version__ = MEMOR_VERSION
6 changes: 3 additions & 3 deletions memor/prompt.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def __eq__(self, other_prompt):
:type other_prompt: Prompt
:return: result as bool
"""
return self._message == other_prompt._message and self._responses == other_prompt._responses and self._role == other_prompt._role
return self._message == other_prompt._message and self._responses == other_prompt._responses and self._role == other_prompt._role and self._template == other_prompt._template

def __str__(self):
"""Return string representation of Prompt."""
Expand Down Expand Up @@ -406,15 +406,15 @@ def render(self, render_format=PromptRenderFormat.DEFAULT):
if isinstance(self._selected_response, Response):
format_kwargs.update({"response_message": self._selected_response._message})
format_kwargs.update({"response_score": self._selected_response._score})
format_kwargs.update({"response_role": self._selected_response._role})
format_kwargs.update({"response_role": self._selected_response._role.value})
format_kwargs.update({"response_temperature": self._selected_response._temperature})
format_kwargs.update({"response_model": self._selected_response._model})
format_kwargs.update({"response_date": datetime.datetime.strftime(
self._selected_response._date_created, DATE_TIME_FORMAT)})
for index, response in enumerate(self._responses):
format_kwargs.update({"response_{index}_message".format(index=index): response._message})
format_kwargs.update({"response_{index}_score".format(index=index): response._score})
format_kwargs.update({"response_{index}_role".format(index=index): response._role})
format_kwargs.update({"response_{index}_role".format(index=index): response._role.value})
format_kwargs.update({"response_{index}_temperature".format(index=index): response._temperature})
format_kwargs.update({"response_{index}_model".format(index=index): response._model})
format_kwargs.update({"response_{index}_date".format(index=index): datetime.datetime.strftime(response._date_created, DATE_TIME_FORMAT)})
Expand Down
6 changes: 3 additions & 3 deletions memor/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@
from .functions import _validate_string


class CustomPromptTemplate:
class CustomPromptTemplate: # TODO: We can change this class name to PromptTemplate
r"""
Prompt template.
>>> template = CustomPromptTemplate(content="Take a deep breath\n{message}!", title="Greeting")
>>> template = CustomPromptTemplate(content="Take a deep breath\n{prompt_message}!", title="Greeting")
>>> template.title
'Greeting'
"""
Expand All @@ -26,7 +26,7 @@ def __init__(
self,
content=None,
file_path=None,
title="unknown",
title=None,
custom_map=None):
"""
Prompt template object initiator.
Expand Down
202 changes: 186 additions & 16 deletions tests/test_custom_prompt_template.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,201 @@
from memor import CustomPromptTemplate
import datetime
import json
import copy
import pytest
from memor import CustomPromptTemplate, MemorValidationError

TEST_CASE_NAME = "CustomPromptTemplate tests"


def test_title1():
template = CustomPromptTemplate(
content="Act as a {language} developer and respond to this question:\n{prompt_message}",
custom_map={
"language": "Python"})
assert template.title == None


def test_title2():
template = CustomPromptTemplate(
content="Act as a {language} developer and respond to this question:\n{prompt_message}",
custom_map={
"language": "Python"})
template.update_title("template1")
assert template.title == "template1"


def test_title3():
template = CustomPromptTemplate(
content="Act as a {language} developer and respond to this question:\n{prompt_message}",
custom_map={
"language": "Python"},
title=None)
assert template.title == None


def test_content1():
template = CustomPromptTemplate(
content="Act as a {language} developer and respond to this question:\n{prompt_message}",
custom_map={
"language": "Python"})
assert template.content == "Act as a {language} developer and respond to this question:\n{prompt_message}"


def test_content2():
template = CustomPromptTemplate(
content="Act as a {language} developer and respond to this question:\n{prompt_message}",
custom_map={
"language": "Python"})
template.update_content(content="Act as a {language} developer and respond to this query:\n{prompt_message}")
assert template.content == "Act as a {language} developer and respond to this query:\n{prompt_message}"


def test_custom_map1():
template = CustomPromptTemplate(
content="Act as a {language} developer and respond to this question:\n{prompt_message}",
custom_map={
"language": "Python"})
assert template.custom_map == {"language": "Python"}


def test_custom_map2():
template = CustomPromptTemplate(
content="Act as a {language} developer and respond to this question:\n{prompt_message}",
custom_map={
"language": "Python"})
template.update_map({"language": "C++"})
assert template.custom_map == {"language": "C++"}


def test_date_modified():
template = CustomPromptTemplate(
content="Act as a {language} developer and respond to this question:\n{prompt_message}",
custom_map={
"language": "Python"})
assert isinstance(template.date_modified, datetime.datetime)


def test_date_created():
template = CustomPromptTemplate(
content="Act as a {language} developer and respond to this question:\n{prompt_message}",
custom_map={
"language": "Python"})
assert isinstance(template.date_created, datetime.datetime)


def test_json1():
template1 = CustomPromptTemplate(
content="Act as a {language} developer and respond to this question:\n{prompt_message}",
custom_map={
"language": "Python"})
template1_json = template1.to_json()
template2 = CustomPromptTemplate()
template2.from_json(template1_json)
assert template1 == template2


def test_json2():
template = CustomPromptTemplate()
with pytest.raises(MemorValidationError, match=r"Invalid template structure. It should be a JSON object with proper fields."):
template.from_json("{}")


def test_save1():
template = CustomPromptTemplate(
content="Act as a {language} developer and respond to this question:\n{prompt_message}",
custom_map={
"language": "Python"})
result = template.save("template_test1.json")
with open("template_test1.json", "r") as file:
saved_template = json.loads(file.read())
assert result["status"] and json.loads(template.to_json()) == saved_template


def test_save2():
template = CustomPromptTemplate(
content="Act as a {language} developer and respond to this question:\n{prompt_message}",
custom_map={
"language": "Python"})
result = template.save("f:/")
assert result["status"] == False


def test_load():
template1 = CustomPromptTemplate(
content="Act as a {language} developer and respond to this question:\n{prompt_message}",
custom_map={
"language": "Python"})
result = template1.save("template_test2.json")
template2 = CustomPromptTemplate(file_path="template_test2.json")
assert result["status"] and template1 == template2


def test_copy1():
template1 = CustomPromptTemplate(
content="Act as a {language} developer and respond to this question:\n{prompt_message}",
custom_map={
"language": "Python"})
template2 = copy.copy(template1)
assert id(template1) != id(template2)


def test_copy2():
template1 = CustomPromptTemplate(
content="Act as a {language} developer and respond to this question:\n{prompt_message}",
custom_map={
"language": "Python"})
template2 = template1.copy()
assert id(template1) != id(template2)


def test_str():
template = CustomPromptTemplate(
content="Act as a {language} developer and respond to this question:\n{prompt_message}",
custom_map={
"language": "Python"})
assert str(template) == template.content


def test_repr():
template = CustomPromptTemplate(
content="Act as a {language} developer and respond to this question:\n{prompt_message}",
custom_map={
"language": "Python"})
assert repr(template) == "CustomPromptTemplate(content={content})".format(content=template.content)


def test_equality1():
template1 = CustomPromptTemplate(content="What is your name?", custom_map={"name": "John"})
template1 = CustomPromptTemplate(
content="Act as a {language} developer and respond to this question:\n{prompt_message}",
custom_map={
"language": "Python"})
template2 = template1.copy()
assert template1 == template2


def test_equality2():
template1 = CustomPromptTemplate(content="What is your name?", custom_map={"name": "John"}, title="template1")
template2 = CustomPromptTemplate(content="What is your name?", custom_map={"name": "John"}, title="template2")
template1 = CustomPromptTemplate(
content="Act as a {language} developer and respond to this question:\n{prompt_message}",
custom_map={
"language": "Python"},
title="template1")
template2 = CustomPromptTemplate(
content="Act as a {language} developer and respond to this question:\n{prompt_message}",
custom_map={
"language": "Python"},
title="template2")
assert template1 != template2


def test_equality3():
template1 = CustomPromptTemplate(content="What is your name?", custom_map={"name": "John"}, title="template1")
template2 = CustomPromptTemplate(content="What is your name?", custom_map={"name": "John"}, title="template1")
template1 = CustomPromptTemplate(
content="Act as a {language} developer and respond to this question:\n{prompt_message}",
custom_map={
"language": "Python"},
title="template1")
template2 = CustomPromptTemplate(
content="Act as a {language} developer and respond to this question:\n{prompt_message}",
custom_map={
"language": "Python"},
title="template1")
assert template1 == template2


def test_custom_prompt_template_content():
template = CustomPromptTemplate(content="What is your name?", custom_map={"name": "John"})
assert template.content == "What is your name?"


def test_custom_prompt_template_custom_map():
template = CustomPromptTemplate(content="What is your name?", custom_map={"name": "John"})
assert template.custom_map == {"name": "John"}
Loading

0 comments on commit 5e22bce

Please sign in to comment.