mirror of
https://github.com/FoundationAgents/MetaGPT.git
synced 2026-05-01 20:03:28 +02:00
Merge pull request #3 from Stitch-z/feature-tutorial-assistant
update: add unit test for the role tutorial assistant
This commit is contained in:
commit
ad691c62e5
2 changed files with 80 additions and 0 deletions
53
tests/metagpt/actions/test_write_tutorial.py
Normal file
53
tests/metagpt/actions/test_write_tutorial.py
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
#!/usr/bin/env python3
|
||||
# _*_ coding: utf-8 _*_
|
||||
"""
|
||||
@Time : 2023/9/6 21:41:34
|
||||
@Author : Stitch-z
|
||||
@File : test_write_tutorial.py
|
||||
"""
|
||||
from typing import Dict
|
||||
|
||||
import aiofiles
|
||||
import pytest
|
||||
|
||||
from metagpt.actions.write_tutorial import WriteDirectory, WriteContent, SaveDocx
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@pytest.mark.parametrize(
|
||||
("language", "topic"),
|
||||
[("English", "Write a tutorial about Python")]
|
||||
)
|
||||
async def test_write_directory(language: str, topic: str):
|
||||
ret = await WriteDirectory(language=language).run(topic=topic)
|
||||
assert isinstance(ret, dict)
|
||||
assert "title" in ret
|
||||
assert "directory" in ret
|
||||
assert isinstance(ret["directory"], list)
|
||||
assert len(ret["directory"])
|
||||
assert isinstance(ret["directory"][0], dict)
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@pytest.mark.parametrize(
|
||||
("language", "topic", "directory"),
|
||||
[("English", "Write a tutorial about Python", {"Introduction": ["What is Python?", "Why learn Python?"]})]
|
||||
)
|
||||
async def test_write_content(language: str, topic: str, directory: Dict):
|
||||
ret = await WriteContent(language=language, directory=directory).run(topic=topic)
|
||||
assert isinstance(ret, str)
|
||||
assert list(directory.keys())[0] in ret
|
||||
for value in list(directory.values())[0]:
|
||||
assert value in ret
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@pytest.mark.parametrize(
|
||||
("title", "content"),
|
||||
[("Python", "Write a tutorial about Python")]
|
||||
)
|
||||
async def test_save_docx(title: str, content: str):
|
||||
ret = await SaveDocx().run(title=title, content=content)
|
||||
assert isinstance(ret, str)
|
||||
assert title in ret
|
||||
async with aiofiles.open(ret, mode="r") as reader:
|
||||
body = await reader.read()
|
||||
assert body == content
|
||||
27
tests/metagpt/roles/test_tutorial_assistant.py
Normal file
27
tests/metagpt/roles/test_tutorial_assistant.py
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
#!/usr/bin/env python3
|
||||
# _*_ coding: utf-8 _*_
|
||||
"""
|
||||
@Time : 2023/9/6 23:11:27
|
||||
@Author : Stitch-z
|
||||
@File : test_tutorial_assistant.py
|
||||
"""
|
||||
import aiofiles
|
||||
import pytest
|
||||
|
||||
from metagpt.roles.tutorial_assistant import TutorialAssistant
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@pytest.mark.parametrize(
|
||||
("language", "topic"),
|
||||
[("Chinese", "Write a tutorial about Python")]
|
||||
)
|
||||
async def test_tutorial_assistant(language: str, topic: str):
|
||||
topic = "Write a tutorial about MySQL"
|
||||
role = TutorialAssistant(language=language)
|
||||
msg = await role.run(topic)
|
||||
filename = msg.content
|
||||
title = filename.split("/")[-1].split(".")[0]
|
||||
async with aiofiles.open(filename, mode="r") as reader:
|
||||
content = await reader.read()
|
||||
assert content.startswith(f"# {title}")
|
||||
Loading…
Add table
Add a link
Reference in a new issue