mirror of
https://github.com/FoundationAgents/MetaGPT.git
synced 2026-05-21 14:05:17 +02:00
Merge pull request #791 from iorisa/fixbug/unittest
fixbug: Replace `mock` with `pytest-mock`
This commit is contained in:
commit
59b2e5e002
6 changed files with 23 additions and 24 deletions
1
setup.py
1
setup.py
|
|
@ -46,7 +46,6 @@ extras_require["test"] = [
|
|||
"chromadb==0.4.14",
|
||||
"gradio==3.0.0",
|
||||
"grpcio-status==1.48.2",
|
||||
"mock==5.1.0",
|
||||
"pylint==3.0.3",
|
||||
"pybrowsers",
|
||||
]
|
||||
|
|
|
|||
|
|
@ -37,7 +37,7 @@ async def test_project_management_an(mocker):
|
|||
root.instruct_content.model_dump = mock_refined_tasks_json
|
||||
mocker.patch("metagpt.actions.project_management_an.REFINED_PM_NODE.fill", return_value=root)
|
||||
|
||||
prompt = NEW_REQ_TEMPLATE.format(old_tasks=TASKS_SAMPLE, context=dict_to_markdown(REFINED_DESIGN_JSON))
|
||||
prompt = NEW_REQ_TEMPLATE.format(old_task=TASKS_SAMPLE, context=dict_to_markdown(REFINED_DESIGN_JSON))
|
||||
node = await REFINED_PM_NODE.fill(prompt, llm)
|
||||
|
||||
assert "Refined Logic Analysis" in node.instruct_content.model_dump()
|
||||
|
|
|
|||
|
|
@ -17,6 +17,7 @@ from metagpt.utils.git_repository import ChangeType
|
|||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@pytest.mark.skip
|
||||
async def test_rebuild(context):
|
||||
# Mock
|
||||
data = await aread(filename=Path(__file__).parent / "../../data/graph_db/networkx.json")
|
||||
|
|
|
|||
|
|
@ -55,7 +55,7 @@ async def test_write_code_plan_and_change_an(mocker):
|
|||
|
||||
@pytest.mark.asyncio
|
||||
async def test_refine_code(mocker):
|
||||
mocker.patch("metagpt.actions.write_code.WriteCodePlanAndChange.write_code", return_value=REFINED_CODE_SAMPLE)
|
||||
mocker.patch.object(WriteCode, "_aask", return_value=REFINED_CODE_SAMPLE)
|
||||
prompt = REFINED_TEMPLATE.format(
|
||||
user_requirement=NEW_REQUIREMENT_SAMPLE,
|
||||
code_plan_and_change=CODE_PLAN_AND_CHANGE_SAMPLE,
|
||||
|
|
|
|||
|
|
@ -9,23 +9,25 @@ from unittest.mock import AsyncMock
|
|||
|
||||
import pytest
|
||||
|
||||
from metagpt.config2 import Config
|
||||
from metagpt.utils.redis import Redis
|
||||
|
||||
|
||||
async def async_mock_from_url(*args, **kwargs):
|
||||
mock_client = AsyncMock()
|
||||
mock_client.set.return_value = None
|
||||
mock_client.get.side_effect = [b"test", b""]
|
||||
return mock_client
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_redis(mocker):
|
||||
redis = Config.default().redis
|
||||
mocker.patch("aioredis.from_url", return_value=async_mock_from_url())
|
||||
async def async_mock_from_url(*args, **kwargs):
|
||||
mock_client = AsyncMock()
|
||||
mock_client.set.return_value = None
|
||||
mock_client.get.return_value = b"test"
|
||||
return mock_client
|
||||
|
||||
conn = Redis(redis)
|
||||
mocker.patch("aioredis.from_url", return_value=async_mock_from_url())
|
||||
mock_config = mocker.Mock()
|
||||
mock_config.to_url.return_value = "http://mock.com"
|
||||
mock_config.username = "mockusername"
|
||||
mock_config.password = "mockpwd"
|
||||
mock_config.db = "0"
|
||||
|
||||
conn = Redis(mock_config)
|
||||
await conn.set("test", "test", timeout_sec=0)
|
||||
assert await conn.get("test") == b"test"
|
||||
await conn.close()
|
||||
|
|
|
|||
|
|
@ -8,8 +8,8 @@
|
|||
import uuid
|
||||
from pathlib import Path
|
||||
|
||||
import aioboto3
|
||||
import aiofiles
|
||||
import mock
|
||||
import pytest
|
||||
|
||||
from metagpt.config2 import Config
|
||||
|
|
@ -18,21 +18,18 @@ from metagpt.utils.s3 import S3
|
|||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@mock.patch("aioboto3.Session")
|
||||
async def test_s3(mock_session_class):
|
||||
async def test_s3(mocker):
|
||||
# Set up the mock response
|
||||
data = await aread(__file__, "utf-8")
|
||||
mock_session_object = mock.Mock()
|
||||
reader_mock = mock.AsyncMock()
|
||||
reader_mock = mocker.AsyncMock()
|
||||
reader_mock.read.side_effect = [data.encode("utf-8"), b"", data.encode("utf-8")]
|
||||
type(reader_mock).url = mock.PropertyMock(return_value="https://mock")
|
||||
mock_client = mock.AsyncMock()
|
||||
type(reader_mock).url = mocker.PropertyMock(return_value="https://mock")
|
||||
mock_client = mocker.AsyncMock()
|
||||
mock_client.put_object.return_value = None
|
||||
mock_client.get_object.return_value = {"Body": reader_mock}
|
||||
mock_client.__aenter__.return_value = mock_client
|
||||
mock_client.__aexit__.return_value = None
|
||||
mock_session_object.client.return_value = mock_client
|
||||
mock_session_class.return_value = mock_session_object
|
||||
mocker.patch.object(aioboto3.Session, "client", return_value=mock_client)
|
||||
|
||||
# Prerequisites
|
||||
s3 = Config.default().s3
|
||||
|
|
@ -55,7 +52,7 @@ async def test_s3(mock_session_class):
|
|||
|
||||
# Mock session env
|
||||
s3.access_key = "ABC"
|
||||
type(reader_mock).url = mock.PropertyMock(return_value="")
|
||||
type(reader_mock).url = mocker.PropertyMock(return_value="")
|
||||
try:
|
||||
conn = S3(s3)
|
||||
res = await conn.cache("ABC", ".bak", "script")
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue