From b73cbe73798647b7e1e69ff4c2c7f41ad9ec7c7a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Tue, 21 Nov 2023 14:00:09 +0800 Subject: [PATCH 1/2] feat: +unit test --- tests/metagpt/utils/test_common.py | 56 ++++++++++++++++++++++++++++-- 1 file changed, 54 insertions(+), 2 deletions(-) diff --git a/tests/metagpt/utils/test_common.py b/tests/metagpt/utils/test_common.py index ec4443175..6474b1233 100644 --- a/tests/metagpt/utils/test_common.py +++ b/tests/metagpt/utils/test_common.py @@ -4,27 +4,79 @@ @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: def change_etc_dir(self): # current_directory = Path.cwd() - abs_root = '/etc' + abs_root = "/etc" os.chdir(abs_root) 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"]) From 5142cb59f7120e564d014e8e3ab2e69698e0972e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Wed, 22 Nov 2023 15:59:35 +0800 Subject: [PATCH 2/2] refactor: consumers -> members --- metagpt/environment.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/metagpt/environment.py b/metagpt/environment.py index b3c296dac..02eb3d340 100644 --- a/metagpt/environment.py +++ b/metagpt/environment.py @@ -29,7 +29,7 @@ class Environment(BaseModel): """ roles: dict[str, Role] = Field(default_factory=dict) - consumers: dict[Role, Set] = Field(default_factory=dict) + members: dict[Role, Set] = Field(default_factory=dict) history: str = Field(default="") # For debug class Config: @@ -61,7 +61,7 @@ class Environment(BaseModel): logger.info(f"publish_message: {message.dump()}") found = False # According to the routing feature plan in Chapter 2.2.3.2 of RFC 113 - for role, subscription in self.consumers.items(): + for role, subscription in self.members.items(): if is_subscribed(message, subscription): role.put_message(message) found = True @@ -106,8 +106,8 @@ class Environment(BaseModel): def get_subscription(self, obj): """Get the labels for messages to be consumed by the object.""" - return self.consumers.get(obj, {}) + return self.members.get(obj, {}) def set_subscription(self, obj, tags): """Set the labels for message to be consumed by the object""" - self.consumers[obj] = tags + self.members[obj] = tags