Fix research action bug && Optimize universal file operation tools

This commit is contained in:
Stitch-z 2023-09-19 16:26:33 +08:00
parent 1333738915
commit 97be431127
7 changed files with 155 additions and 43 deletions

View file

@ -7,7 +7,6 @@
"""
from pathlib import Path
import aiofiles
import pytest
from metagpt.utils.file import File
@ -18,10 +17,9 @@ from metagpt.utils.file import File
("root_path", "filename", "content"),
[(Path("/code/MetaGPT/data/tutorial_docx/2023-09-07_17-05-20"), "test.md", "Hello World!")]
)
async def test_write_file(root_path: Path, filename: str, content: bytes):
async def test_write_and_read_file(root_path: Path, filename: str, content: bytes):
full_file_name = await File.write(root_path=root_path, filename=filename, content=content.encode('utf-8'))
assert isinstance(full_file_name, Path)
assert root_path / filename == full_file_name
async with aiofiles.open(full_file_name, mode="r") as reader:
body = await reader.read()
assert body == content
file_data = await File.read(full_file_name)
assert file_data.decode("utf-8") == content

View file

@ -64,6 +64,59 @@ def test_parse_data():
assert OutputParser.parse_data(test_data) == expected_result
@pytest.mark.parametrize(
("text", "data_type", "parsed_data", "expected_exception"),
[
(
"""xxx [1, 2, ["a", "b", [3, 4]], {"x": 5, "y": [6, 7]}] xxx""",
"list",
[1, 2, ["a", "b", [3, 4]], {"x": 5, "y": [6, 7]}],
None,
),
(
"""xxx ["1", "2", "3"] xxx \n xxx \t xx""",
"list",
["1", "2", "3"],
None,
),
(
"""{"title": "a", "directory": {"sub_dir1": ["title1, title2"]}, "sub_dir2": [1, 2]}""",
"dict",
{"title": "a", "directory": {"sub_dir1": ["title1, title2"]}, "sub_dir2": [1, 2]},
None,
),
(
"""xxx {"title": "x", \n \t "directory": ["x", \n "y"]} xxx \n xxx \t xx""",
"dict",
{"title": "x", "directory": ["x", "y"]},
None,
),
(
"""xxx xx""",
"list",
None,
Exception,
),
(
"""xxx [1, 2, []xx""",
"list",
None,
Exception,
),
]
)
def test_extract_list_or_dict(text: str, data_type: str, parsed_data: list, expected_exception):
def case():
resp = OutputParser.extract_struct(text, data_type)
assert resp == parsed_data
if expected_exception:
with pytest.raises(expected_exception):
case()
else:
case()
if __name__ == '__main__':
t_text = '''
## Required Python third-party packages