fix tools ut

This commit is contained in:
liuminhui 2025-01-21 15:38:45 +08:00
parent 1cc1804637
commit ee05b41097
6 changed files with 34 additions and 44 deletions

View file

@ -37,7 +37,7 @@ class GPTvGenerator:
It utilizes a vision model to analyze the layout from an image and generate webpage codes accordingly.
"""
def __init__(self, config: Optional[Config]):
def __init__(self, config: Optional[Config] = None):
"""Initialize GPTvGenerator class with default values from the configuration."""
from metagpt.llm import LLM

View file

@ -130,12 +130,8 @@ def test_insert_content(temp_py_file):
@pytest.mark.parametrize(
"filename",
[
TEST_DATA_PATH / "requirements/1.txt",
TEST_DATA_PATH / "requirements/1.json",
TEST_DATA_PATH / "requirements/1.constraint.md",
TEST_DATA_PATH / "requirements/pic/1.png",
TEST_DATA_PATH / "docx_for_test.docx",
TEST_DATA_PATH / "requirements/2.pdf",
TEST_DATA_PATH / "output_parser/1.md",
TEST_DATA_PATH / "search/serper-metagpt-8.json",
TEST_DATA_PATH / "audio/hello.mp3",
TEST_DATA_PATH / "code/python/1.py",
TEST_DATA_PATH / "code/js/1.js",
@ -264,12 +260,6 @@ def test_open_file_long_with_lineno(temp_file_path):
assert result.split("\n") == expected.split("\n")
def test_create_file_unexist_path():
editor = Editor()
with pytest.raises(FileNotFoundError):
editor.create_file("/unexist/path/a.txt")
@pytest.mark.asyncio
async def test_create_file(temp_file_path):
editor = Editor()
@ -578,15 +568,16 @@ Pay attention to the new content. Ensure that it aligns with the new parameters.
def test_edit_file_by_replace_mismatch(temp_py_file):
editor = Editor()
output = editor.edit_file_by_replace(
file_name=str(temp_py_file),
first_replaced_line_number=5,
first_replaced_line_content="",
new_content=" b = 9",
last_replaced_line_number=5,
last_replaced_line_content="",
)
assert output.strip() == MISMATCH_ERROR.strip()
with pytest.raises(ValueError) as match_error:
editor.edit_file_by_replace(
file_name=str(temp_py_file),
first_replaced_line_number=5,
first_replaced_line_content="",
new_content=" b = 9",
last_replaced_line_number=5,
last_replaced_line_content="",
)
assert str(match_error.value).strip() == MISMATCH_ERROR.strip()
def test_append_file(temp_file_path):

View file

@ -46,7 +46,7 @@ async def test_index_repo(path, query):
],
)
def test_classify_path(paths, path_type, root):
result, result_root = IndexRepo.classify_path(paths)
result, result_root = IndexRepo.find_index_repo_path(paths)
assert path_type in set(result.keys())
assert root == result_root.get(path_type, "")

View file

@ -1,4 +1,7 @@
import tempfile
from pathlib import Path
import pytest
from metagpt.tools.libs.linter import Linter, LintResult
@ -20,7 +23,8 @@ def test_get_abs_fname():
def test_py_lint():
linter = Linter()
code = "print('Hello, World!')"
result = linter.py_lint("test_linter.py", "test_linter.py", code)
test_file_path = str(Path(__file__).resolve())
result = linter.py_lint(test_file_path, test_file_path, code)
assert result is None # No errors expected for valid Python code
@ -54,3 +58,7 @@ def test_run_cmd():
result = linter.run_cmd("flake8", temp_file.name, "print('Hello, World!')")
# Since flake8 might not be installed in the test environment, we just ensure no exception is raised
assert result is None or isinstance(result, LintResult)
if __name__ == "__main__":
pytest.main([__file__, "-s"])

View file

@ -14,7 +14,8 @@ from metagpt.tools.libs.shell import shell_execute
],
)
async def test_shell(command, expect_stdout, expect_stderr):
stdout, stderr = await shell_execute(command)
stdout, stderr, returncode = await shell_execute(command)
assert returncode == 0
assert expect_stdout in stdout
assert stderr == expect_stderr

View file

@ -1,24 +1,14 @@
import pytest
from metagpt.tools.libs.web_scraping import scrape_web_playwright
from metagpt.tools.libs.web_scraping import view_page_element_to_scrape
@pytest.mark.asyncio
async def test_scrape_web_playwright(http_server):
server, test_url = await http_server()
result = await scrape_web_playwright(test_url)
# Assert that the result is a dictionary
assert isinstance(result, dict)
# Assert that the result contains 'inner_text' and 'html' keys
assert "inner_text" in result
assert "html" in result
# Assert startswith and endswith
assert not result["inner_text"].startswith(" ")
assert not result["inner_text"].endswith(" ")
assert not result["html"].startswith(" ")
assert not result["html"].endswith(" ")
await server.stop()
async def test_view_page_element_to_scrape():
# Define the test URL and parameters
test_url = "https://docs.deepwisdom.ai/main/zh/"
test_requirement = "Retrieve all paragraph texts"
test_keep_links = True
test_page = await view_page_element_to_scrape(test_url, test_requirement, test_keep_links)
assert isinstance(test_page, str)
assert "html" in test_page