From 7bdaf963b424884381a3ae1f91ff9ec051847a8c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Thu, 4 Jan 2024 00:19:28 +0800 Subject: [PATCH] feat: +mock --- .well-known/openapi.yaml | 2 +- ...test_hello.py => test_openapi_v3_hello.py} | 2 +- tests/metagpt/utils/test_redis.py | 26 ++++++++++++---- tests/metagpt/utils/test_s3.py | 31 +++++++++++++++---- tests/metagpt/utils/test_session.py | 13 ++++++++ 5 files changed, 60 insertions(+), 14 deletions(-) rename tests/metagpt/tools/{test_hello.py => test_openapi_v3_hello.py} (96%) create mode 100644 tests/metagpt/utils/test_session.py diff --git a/.well-known/openapi.yaml b/.well-known/openapi.yaml index bc291b7db..47ca04b23 100644 --- a/.well-known/openapi.yaml +++ b/.well-known/openapi.yaml @@ -11,7 +11,7 @@ paths: post: summary: Generate greeting description: Generates a greeting message. - operationId: hello.post_greeting + operationId: openapi_v3_hello.post_greeting responses: 200: description: greeting response diff --git a/tests/metagpt/tools/test_hello.py b/tests/metagpt/tools/test_openapi_v3_hello.py similarity index 96% rename from tests/metagpt/tools/test_hello.py rename to tests/metagpt/tools/test_openapi_v3_hello.py index 7e61532ab..3f3f79829 100644 --- a/tests/metagpt/tools/test_hello.py +++ b/tests/metagpt/tools/test_openapi_v3_hello.py @@ -3,7 +3,7 @@ """ @Time : 2023/12/26 @Author : mashenquan -@File : test_hello.py +@File : test_openapi_v3_hello.py """ import asyncio import subprocess diff --git a/tests/metagpt/utils/test_redis.py b/tests/metagpt/utils/test_redis.py index b93ff0cdb..d499418ac 100644 --- a/tests/metagpt/utils/test_redis.py +++ b/tests/metagpt/utils/test_redis.py @@ -6,20 +6,34 @@ @File : test_redis.py """ +import mock import pytest from metagpt.config import CONFIG from metagpt.utils.redis import Redis +async def async_mock_from_url(*args, **kwargs): + mock_client = mock.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(): +@mock.patch("aioredis.from_url", return_value=async_mock_from_url()) +async def test_redis(mock_from_url): + # Mock + # mock_client = mock.AsyncMock() + # mock_client.set.return_value=None + # mock_client.get.side_effect = [b'test', b''] + # mock_from_url.return_value = mock_client + # Prerequisites - assert CONFIG.REDIS_HOST and CONFIG.REDIS_HOST != "YOUR_REDIS_HOST" - assert CONFIG.REDIS_PORT and CONFIG.REDIS_PORT != "YOUR_REDIS_PORT" - # assert CONFIG.REDIS_USER - assert CONFIG.REDIS_PASSWORD is not None and CONFIG.REDIS_PASSWORD != "YOUR_REDIS_PASSWORD" - assert CONFIG.REDIS_DB is not None and CONFIG.REDIS_DB != "YOUR_REDIS_DB_INDEX, str, 0-based" + CONFIG.REDIS_HOST = "MOCK_REDIS_HOST" + CONFIG.REDIS_PORT = "MOCK_REDIS_PORT" + CONFIG.REDIS_PASSWORD = "MOCK_REDIS_PASSWORD" + CONFIG.REDIS_DB = 0 conn = Redis() assert not conn.is_valid diff --git a/tests/metagpt/utils/test_s3.py b/tests/metagpt/utils/test_s3.py index f74e7b52a..132aa0635 100644 --- a/tests/metagpt/utils/test_s3.py +++ b/tests/metagpt/utils/test_s3.py @@ -9,20 +9,36 @@ import uuid from pathlib import Path import aiofiles +import mock import pytest from metagpt.config import CONFIG +from metagpt.utils.common import aread from metagpt.utils.s3 import S3 @pytest.mark.asyncio -async def test_s3(): +@mock.patch("aioboto3.Session") +async def test_s3(mock_session_class): + # Set up the mock response + data = await aread(__file__, "utf-8") + mock_session_object = mock.Mock() + reader_mock = mock.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() + 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 + # Prerequisites - assert CONFIG.S3_ACCESS_KEY and CONFIG.S3_ACCESS_KEY != "YOUR_S3_ACCESS_KEY" - assert CONFIG.S3_SECRET_KEY and CONFIG.S3_SECRET_KEY != "YOUR_S3_SECRET_KEY" - assert CONFIG.S3_ENDPOINT_URL and CONFIG.S3_ENDPOINT_URL != "YOUR_S3_ENDPOINT_URL" - # assert CONFIG.S3_SECURE: true # true/false - assert CONFIG.S3_BUCKET and CONFIG.S3_BUCKET != "YOUR_S3_BUCKET" + # assert CONFIG.S3_ACCESS_KEY and CONFIG.S3_ACCESS_KEY != "YOUR_S3_ACCESS_KEY" + # assert CONFIG.S3_SECRET_KEY and CONFIG.S3_SECRET_KEY != "YOUR_S3_SECRET_KEY" + # assert CONFIG.S3_ENDPOINT_URL and CONFIG.S3_ENDPOINT_URL != "YOUR_S3_ENDPOINT_URL" + # assert CONFIG.S3_BUCKET and CONFIG.S3_BUCKET != "YOUR_S3_BUCKET" conn = S3() assert conn.is_valid @@ -42,6 +58,7 @@ async def test_s3(): assert "http" in res # Mock session env + type(reader_mock).url = mock.PropertyMock(return_value="") old_options = CONFIG.options.copy() new_options = old_options.copy() new_options["S3_ACCESS_KEY"] = "YOUR_S3_ACCESS_KEY" @@ -54,6 +71,8 @@ async def test_s3(): finally: CONFIG.set_context(old_options) + await reader.close() + if __name__ == "__main__": pytest.main([__file__, "-s"]) diff --git a/tests/metagpt/utils/test_session.py b/tests/metagpt/utils/test_session.py new file mode 100644 index 000000000..eab2587a2 --- /dev/null +++ b/tests/metagpt/utils/test_session.py @@ -0,0 +1,13 @@ +#!/usr/bin/env python3 +# _*_ coding: utf-8 _*_ + +import pytest + + +def test_nodeid(request): + print(request.node.nodeid) + assert request.node.nodeid + + +if __name__ == "__main__": + pytest.main([__file__, "-s"])