feat: +unit test

fixbug: PYTHONPATH

fixbug: unit test
This commit is contained in:
莘权 马 2023-12-27 11:24:22 +08:00
parent 641c71bf18
commit 0adabfe53f
33 changed files with 1561 additions and 297 deletions

View file

@ -4,6 +4,8 @@
import json
from pathlib import Path
import aiofiles
from metagpt.provider.openai_api import OpenAILLM as GPTAPI
ICL_SAMPLE = """Interface definition:
@ -174,6 +176,9 @@ class UTGenerator:
return doc
for name, prop in node.items():
if not isinstance(prop, dict):
doc += f'{" " * level}{self._para_to_str(node)}\n'
break
doc += f'{" " * level}{self.para_to_str(name, prop, prop_object_required)}\n'
doc += dive_into_object(prop)
if prop["type"] == "array":
@ -202,12 +207,12 @@ class UTGenerator:
return tags
def generate_ut(self, include_tags) -> bool:
async def generate_ut(self, include_tags) -> bool:
"""Generate test case files"""
tags = self.get_tags_mapping()
for tag, paths in tags.items():
if include_tags is None or tag in include_tags:
self._generate_ut(tag, paths)
await self._generate_ut(tag, paths)
return True
def build_api_doc(self, node: dict, path: str, method: str) -> str:
@ -250,21 +255,22 @@ class UTGenerator:
return doc
def _store(self, data, base, folder, fname):
async def _store(self, data, base, folder, fname):
"""Store data in a file."""
file_path = self.get_file_path(Path(base) / folder, fname)
with open(file_path, "w", encoding="utf-8") as file:
file.write(data)
async with aiofiles.open(file_path, mode="w", encoding="utf-8") as file:
await file.write(data)
def ask_gpt_and_save(self, question: str, tag: str, fname: str):
async def ask_gpt_and_save(self, question: str, tag: str, fname: str):
"""Generate questions and store both questions and answers"""
messages = [self.icl_sample, question]
result = self.gpt_msgs_to_code(messages=messages)
result = await self.gpt_msgs_to_code(messages=messages)
self._store(question, self.questions_path, tag, f"{fname}.txt")
self._store(result, self.ut_py_path, tag, f"{fname}.py")
await self._store(question, self.questions_path, tag, f"{fname}.txt")
data = result.get("code", "") if result else ""
await self._store(data, self.ut_py_path, tag, f"{fname}.py")
def _generate_ut(self, tag, paths):
async def _generate_ut(self, tag, paths):
"""Process the structure under a data path
Args:
@ -276,13 +282,13 @@ class UTGenerator:
summary = node["summary"]
question = self.template_prefix
question += self.build_api_doc(node, path, method)
self.ask_gpt_and_save(question, tag, summary)
await self.ask_gpt_and_save(question, tag, summary)
async def gpt_msgs_to_code(self, messages: list) -> str:
"""Choose based on different calling methods"""
result = ""
if self.chatgpt_method == "API":
result = await GPTAPI().aask_code(msgs=messages)
result = await GPTAPI().aask_code(messages=messages)
return result

View file

@ -6,7 +6,7 @@
from __future__ import annotations
import importlib
from typing import Any, Callable, Coroutine, Literal, overload
from typing import Any, Callable, Coroutine, overload
from metagpt.config import CONFIG
from metagpt.tools import WebBrowserEngineType
@ -46,12 +46,3 @@ class WebBrowserEngine:
async def run(self, url: str, *urls: str) -> WebPage | list[WebPage]:
return await self.run_func(url, *urls)
if __name__ == "__main__":
import fire
async def main(url: str, *urls: str, engine_type: Literal["playwright", "selenium"] = "playwright", **kwargs):
return await WebBrowserEngine(engine=WebBrowserEngineType(engine_type), **kwargs).run(url, *urls)
fire.Fire(main)

View file

@ -142,12 +142,3 @@ async def _log_stream(sr, log_func):
_install_lock: asyncio.Lock = None
_install_cache = set()
if __name__ == "__main__":
import fire
async def main(url: str, *urls: str, browser_type: str = "chromium", **kwargs):
return await PlaywrightWrapper(browser_type=browser_type, **kwargs).run(url, *urls)
fire.Fire(main)

View file

@ -118,12 +118,3 @@ def _gen_get_driver_func(browser_type, *args, executable_path=None):
return WebDriver(options=deepcopy(options), service=Service(executable_path=executable_path))
return _get_driver
if __name__ == "__main__":
import fire
async def main(url: str, *urls: str, browser_type: str = "chrome", **kwargs):
return await SeleniumWrapper(browser_type=browser_type, **kwargs).run(url, *urls)
fire.Fire(main)