Skip to content

Commit

Permalink
Merge pull request #791 from iorisa/fixbug/unittest
Browse files Browse the repository at this point in the history
fixbug: Replace `mock` with `pytest-mock`
  • Loading branch information
garylin2099 authored Jan 24, 2024
2 parents ac98df9 + d51d262 commit 59b2e5e
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 23 deletions.
1 change: 0 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ def run(self):
"chromadb==0.4.14",
"gradio==3.0.0",
"grpcio-status==1.48.2",
"mock==5.1.0",
"pylint==3.0.3",
"pybrowsers",
]
Expand Down
2 changes: 1 addition & 1 deletion tests/metagpt/actions/test_project_management_an.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
1 change: 1 addition & 0 deletions tests/metagpt/actions/test_rebuild_sequence_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@


@pytest.mark.asyncio
@pytest.mark.skip
async def test_rebuild(context):
# Mock
data = await aread(filename=Path(__file__).parent / "../../data/graph_db/networkx.json")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
22 changes: 12 additions & 10 deletions tests/metagpt/utils/test_redis.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,25 @@

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
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

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(redis)
conn = Redis(mock_config)
await conn.set("test", "test", timeout_sec=0)
assert await conn.get("test") == b"test"
await conn.close()
Expand Down
17 changes: 7 additions & 10 deletions tests/metagpt/utils/test_s3.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
import uuid
from pathlib import Path

import aioboto3
import aiofiles
import mock
import pytest

from metagpt.config2 import Config
Expand All @@ -18,21 +18,18 @@


@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
Expand All @@ -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")
Expand Down

0 comments on commit 59b2e5e

Please sign in to comment.