tools image getter ut

This commit is contained in:
liuminhui 2025-01-16 17:48:35 +08:00
parent 1fd5ffc6ba
commit 320bd1f02e
2 changed files with 39 additions and 16 deletions

View file

@ -12,7 +12,6 @@ from pydantic import BaseModel
from metagpt.context import Context
from metagpt.roles.di.data_interpreter import DataInterpreter
from metagpt.schema import UserMessage
from metagpt.tools.libs.git import git_checkout, git_clone
from metagpt.utils.common import awrite
from metagpt.utils.git_repository import GitRepository
@ -26,21 +25,6 @@ async def get_env(key: str, app_name: str = ""):
return os.environ.get(key)
@pytest.mark.asyncio
@pytest.mark.parametrize(
["url", "commit_id"], [("https://github.com/sqlfluff/sqlfluff.git", "d19de0ecd16d298f9e3bfb91da122734c40c01e5")]
)
@pytest.mark.skip
async def test_git(url: str, commit_id: str):
repo_dir = await git_clone(url)
assert repo_dir
await git_checkout(repo_dir, commit_id)
repo = GitRepository(repo_dir, auto_init=False)
repo.delete_repository()
@pytest.mark.skip
@pytest.mark.asyncio
async def test_login():

View file

@ -0,0 +1,39 @@
from pathlib import Path
from unittest.mock import AsyncMock, patch
import pytest
import pytest_asyncio
from metagpt.tools.libs.image_getter import ImageGetter
@pytest.mark.asyncio
class TestImageGetter:
@pytest_asyncio.fixture(autouse=True)
async def image_getter_client(self):
"""Fixture to initialize the ImageGetter."""
self.image_getter = ImageGetter(headless=True)
await self.image_getter.start()
yield self.image_getter
if self.image_getter.browser_instance:
await self.image_getter.browser_instance.close()
@patch("metagpt.tools.libs.image_getter.decode_image")
async def test_get_image_success(self, mock_decode_image):
"""Test successfully retrieving and saving an image."""
search_term = "nature"
image_save_path = Path.cwd() / "test_image_getter.jpg"
# Mock the decode_image to avoid actual image decoding
mock_image = AsyncMock()
mock_decode_image.return_value = mock_image
# Mock the Playwright page evaluation result to return a dummy base64 image string
self.image_getter.page.goto = AsyncMock()
self.image_getter.page.wait_for_selector = AsyncMock()
self.image_getter.page.evaluate = AsyncMock(return_value="data:image/png;base64,FAKEBASE64STRING")
result = await self.image_getter.get_image(search_term, str(image_save_path))
assert f"{search_term} found." in result
mock_decode_image.assert_called_once()