mirror of
https://github.com/FoundationAgents/MetaGPT.git
synced 2026-04-26 17:26:22 +02:00
fix ut of serialize_deserialize
This commit is contained in:
parent
2abe99cf45
commit
a01766ae72
8 changed files with 29 additions and 17 deletions
|
|
@ -13,14 +13,13 @@ def test_action_serialize():
|
|||
action = Action()
|
||||
ser_action_dict = action.dict()
|
||||
assert "name" in ser_action_dict
|
||||
assert "llm" in ser_action_dict
|
||||
assert "llm" not in ser_action_dict
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_action_deserialize():
|
||||
action = Action()
|
||||
serialized_data = action.dict()
|
||||
assert isinstance(serialized_data["llm"], OpenAIGPTAPI)
|
||||
|
||||
new_action = Action(**serialized_data)
|
||||
|
||||
|
|
|
|||
|
|
@ -14,7 +14,6 @@ async def test_product_manager_deserialize():
|
|||
role = ProductManager()
|
||||
ser_role_dict = role.dict(by_alias=True)
|
||||
new_role = ProductManager(**ser_role_dict)
|
||||
# new_role = ProductManager().deserialize(ser_role_dict)
|
||||
|
||||
assert new_role.name == "Alice"
|
||||
assert len(new_role._actions) == 1
|
||||
|
|
|
|||
|
|
@ -17,7 +17,15 @@ from metagpt.const import SERDESER_PATH
|
|||
from metagpt.roles.engineer import Engineer
|
||||
from metagpt.utils.utils import format_trackback_info
|
||||
|
||||
from tests.metagpt.serialize_deserialize.test_serdeser_base import RoleC, serdeser_path
|
||||
from tests.metagpt.serialize_deserialize.test_serdeser_base import RoleA, RoleB, RoleC, serdeser_path
|
||||
|
||||
|
||||
def test_roles():
|
||||
role_a = RoleA()
|
||||
assert len(role_a._rc.watch) == 1
|
||||
role_b = RoleB()
|
||||
assert len(role_a._rc.watch) == 1
|
||||
assert len(role_b._rc.watch) == 1
|
||||
|
||||
|
||||
def test_role_serialize():
|
||||
|
|
|
|||
|
|
@ -8,6 +8,7 @@ from pathlib import Path
|
|||
from metagpt.actions.action import Action
|
||||
from metagpt.roles.role import Role, RoleReactMode
|
||||
from metagpt.actions.add_requirement import BossRequirement
|
||||
from metagpt.actions.action_output import ActionOutput
|
||||
|
||||
|
||||
serdeser_path = Path(__file__).absolute().parent.joinpath("../../data/serdeser_storage")
|
||||
|
|
@ -22,21 +23,27 @@ class MockMessage(BaseModel):
|
|||
class ActionPass(Action):
|
||||
name: str = "ActionPass"
|
||||
|
||||
async def run(self, messages: list["Message"]):
|
||||
return "pass"
|
||||
async def run(self, messages: list["Message"]) -> ActionOutput:
|
||||
output_mapping = {
|
||||
"result": (str, ...)
|
||||
}
|
||||
pass_class = ActionOutput.create_model_class("pass", output_mapping)
|
||||
pass_output = ActionOutput("ActionPass run passed", pass_class(**{"result": "pass result"}))
|
||||
|
||||
return pass_output
|
||||
|
||||
|
||||
class ActionOK(Action):
|
||||
name: str = "ActionOK"
|
||||
|
||||
async def run(self, messages: list["Message"]):
|
||||
async def run(self, messages: list["Message"]) -> str:
|
||||
return "ok"
|
||||
|
||||
|
||||
class ActionRaise(Action):
|
||||
name: str = "ActionRaise"
|
||||
|
||||
async def run(self, messages: list["Message"]):
|
||||
async def run(self, messages: list["Message"]) -> str:
|
||||
raise RuntimeError("parse error in ActionRaise")
|
||||
|
||||
|
||||
|
|
@ -48,7 +55,8 @@ class RoleA(Role):
|
|||
constraints: str = "RoleA's constraints"
|
||||
|
||||
def __init__(self, **kwargs):
|
||||
super(RoleA, self).__init__(**kwargs)
|
||||
# super(RoleA, self).__init__(**kwargs)
|
||||
super().__init__(**kwargs)
|
||||
self._init_actions([ActionPass])
|
||||
self._watch([BossRequirement])
|
||||
|
||||
|
|
@ -63,7 +71,8 @@ class RoleB(Role):
|
|||
constraints: str = "RoleB's constraints"
|
||||
|
||||
def __init__(self, **kwargs):
|
||||
super(RoleB, self).__init__(**kwargs)
|
||||
# super(RoleB, self).__init__(**kwargs)
|
||||
super().__init__(**kwargs)
|
||||
self._init_actions([ActionOK, ActionRaise])
|
||||
self._watch([ActionPass])
|
||||
self._rc.react_mode = RoleReactMode.BY_ORDER
|
||||
|
|
|
|||
|
|
@ -11,7 +11,7 @@ from metagpt.roles import ProjectManager, ProductManager, Architect
|
|||
from metagpt.team import Team
|
||||
from metagpt.const import SERDESER_PATH
|
||||
|
||||
from tests.metagpt.serialize_deserialize.test_serdeser_base import RoleA, RoleB, RoleC, serdeser_path
|
||||
from tests.metagpt.serialize_deserialize.test_serdeser_base import RoleA, RoleB, RoleC, serdeser_path, ActionOK
|
||||
|
||||
|
||||
def test_team_deserialize():
|
||||
|
|
|
|||
|
|
@ -21,7 +21,7 @@ async def test_action_deserialize():
|
|||
action = WritePRD()
|
||||
serialized_data = action.dict()
|
||||
new_action = WritePRD(**serialized_data)
|
||||
# new_action = WritePRD().deserialize(serialized_data)
|
||||
assert new_action.name == ""
|
||||
assert new_action.llm == LLM()
|
||||
assert len(await new_action.run([Message(content="write a cli snake game")])) > 0
|
||||
action_output = await new_action.run([Message(content="write a cli snake game")])
|
||||
assert len(action_output.content) > 0
|
||||
|
|
|
|||
|
|
@ -27,7 +27,6 @@ async def test_write_code_deserialize():
|
|||
action = WriteCode()
|
||||
serialized_data = action.dict()
|
||||
new_action = WriteCode(**serialized_data)
|
||||
# new_action = WriteCode().deserialize(serialized_data)
|
||||
assert new_action.name == "WriteCode"
|
||||
assert new_action.llm == LLM()
|
||||
await new_action.run(context="write a cli snake game", filename="test_code")
|
||||
|
|
@ -38,7 +37,6 @@ async def test_write_code_review_deserialize():
|
|||
action = WriteCodeReview()
|
||||
serialized_data = action.dict()
|
||||
new_action = WriteCodeReview(**serialized_data)
|
||||
# new_action = WriteCodeReview().deserialize(serialized_data)
|
||||
code = await WriteCode().run(context="write a cli snake game", filename="test_code")
|
||||
|
||||
assert new_action.name == "WriteCodeReview"
|
||||
|
|
|
|||
|
|
@ -26,7 +26,7 @@ def test_write_task_serialize():
|
|||
async def test_write_design_deserialize():
|
||||
action = WriteDesign()
|
||||
serialized_data = action.dict()
|
||||
new_action = WriteDesign().deserialize(serialized_data)
|
||||
new_action = WriteDesign(**serialized_data)
|
||||
assert new_action.name == ""
|
||||
assert new_action.llm == LLM()
|
||||
await new_action.run(context="write a cli snake game")
|
||||
|
|
@ -37,7 +37,6 @@ async def test_write_task_deserialize():
|
|||
action = WriteTasks()
|
||||
serialized_data = action.dict()
|
||||
new_action = WriteTasks(**serialized_data)
|
||||
# new_action = WriteTasks().deserialize(serialized_data)
|
||||
assert new_action.name == "CreateTasks"
|
||||
assert new_action.llm == LLM()
|
||||
await new_action.run(context="write a cli snake game")
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue