feat: merge rfc116

This commit is contained in:
莘权 马 2023-11-22 17:29:34 +08:00
commit e9471ed206

View file

@ -4,13 +4,20 @@
@Time : 2023/4/29 16:19
@Author : alexanderwu
@File : test_common.py
@Modified by: mashenquan, 2023/11/21. Add unit tests.
"""
import os
from typing import Any, Set
import pytest
from pydantic import BaseModel
from metagpt.actions import RunCode
from metagpt.const import get_project_root
from metagpt.roles.tutorial_assistant import TutorialAssistant
from metagpt.schema import Message
from metagpt.utils.common import any_to_str, any_to_str_set
class TestGetProjectRoot:
@ -21,10 +28,55 @@ class TestGetProjectRoot:
def test_get_project_root(self):
project_root = get_project_root()
assert project_root.name == "metagpt"
assert project_root.name == "MetaGPT"
def test_get_root_exception(self):
with pytest.raises(Exception) as exc_info:
self.change_etc_dir()
get_project_root()
assert str(exc_info.value) == "Project root not found."
def test_any_to_str(self):
class Input(BaseModel):
x: Any
want: str
inputs = [
Input(x=TutorialAssistant, want="metagpt.roles.tutorial_assistant.TutorialAssistant"),
Input(x=TutorialAssistant(), want="metagpt.roles.tutorial_assistant.TutorialAssistant"),
Input(x=RunCode, want="metagpt.actions.run_code.RunCode"),
Input(x=RunCode(), want="metagpt.actions.run_code.RunCode"),
Input(x=Message, want="metagpt.schema.Message"),
Input(x=Message(""), want="metagpt.schema.Message"),
Input(x="A", want="A"),
]
for i in inputs:
v = any_to_str(i.x)
assert v == i.want
def test_any_to_str_set(self):
class Input(BaseModel):
x: Any
want: Set
inputs = [
Input(
x=[TutorialAssistant, RunCode(), "a"],
want={"metagpt.roles.tutorial_assistant.TutorialAssistant", "metagpt.actions.run_code.RunCode", "a"},
),
Input(
x={TutorialAssistant, RunCode(), "a"},
want={"metagpt.roles.tutorial_assistant.TutorialAssistant", "metagpt.actions.run_code.RunCode", "a"},
),
Input(
x=(TutorialAssistant, RunCode(), "a"),
want={"metagpt.roles.tutorial_assistant.TutorialAssistant", "metagpt.actions.run_code.RunCode", "a"},
),
]
for i in inputs:
v = any_to_str_set(i.x)
assert v == i.want
if __name__ == "__main__":
pytest.main([__file__, "-s"])