feat: +unit test

This commit is contained in:
莘权 马 2023-12-26 13:31:50 +08:00
parent 339714261a
commit 6512f40ddd
27 changed files with 333 additions and 151 deletions

View file

@ -5,6 +5,7 @@
@Author : mashenquan
@File : test_hello.py
"""
import asyncio
import subprocess
from pathlib import Path
@ -14,10 +15,11 @@ import requests
@pytest.mark.asyncio
async def test_hello():
script_pathname = Path(__file__).resolve()
script_pathname = Path(__file__).parent / "../../../metagpt/tools/hello.py"
process = subprocess.Popen(["python", str(script_pathname)])
await asyncio.sleep(5)
url = "http://localhost:8080/openapi/greeting/dave"
url = "http://localhost:8082/openapi/greeting/dave"
headers = {"accept": "text/plain", "Content-Type": "application/json"}
data = {}
response = requests.post(url, headers=headers, json=data)

View file

@ -0,0 +1,31 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
@Time : 2023/12/26
@Author : mashenquan
@File : test_iflytek_tts.py
"""
import pytest
from metagpt.config import CONFIG
from metagpt.tools.iflytek_tts import oas3_iflytek_tts
@pytest.mark.asyncio
async def test_tts():
# Prerequisites
assert CONFIG.IFLYTEK_APP_ID
assert CONFIG.IFLYTEK_API_KEY
assert CONFIG.IFLYTEK_API_SECRET
result = await oas3_iflytek_tts(
text="你好hello",
app_id=CONFIG.IFLYTEK_APP_ID,
api_key=CONFIG.IFLYTEK_API_KEY,
api_secret=CONFIG.IFLYTEK_API_SECRET,
)
assert result
if __name__ == "__main__":
pytest.main([__file__, "-s"])

View file

@ -0,0 +1,32 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
@Time : 2023/12/26
@Author : mashenquan
@File : test_metagpt_oas3_api_svc.py
"""
import asyncio
import subprocess
from pathlib import Path
import pytest
import requests
@pytest.mark.asyncio
async def test_oas2_svc():
script_pathname = Path(__file__).parent / "../../../metagpt/tools/metagpt_oas3_api_svc.py"
process = subprocess.Popen(["python", str(script_pathname)])
await asyncio.sleep(5)
url = "http://localhost:8080/openapi/greeting/dave"
headers = {"accept": "text/plain", "Content-Type": "application/json"}
data = {}
response = requests.post(url, headers=headers, json=data)
assert response.text == "Hello dave\n"
process.terminate()
if __name__ == "__main__":
pytest.main([__file__, "-s"])

View file

@ -0,0 +1,25 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
@Time : 2023/12/26
@Author : mashenquan
@File : test_metagpt_text_to_image.py
"""
import pytest
from metagpt.config import CONFIG
from metagpt.tools.metagpt_text_to_image import oas3_metagpt_text_to_image
@pytest.mark.asyncio
async def test_draw():
# Prerequisites
assert CONFIG.METAGPT_TEXT_TO_IMAGE_MODEL_URL
binary_data = await oas3_metagpt_text_to_image("Panda emoji")
assert binary_data
if __name__ == "__main__":
pytest.main([__file__, "-s"])

View file

@ -8,6 +8,7 @@
import pytest
from metagpt.config import CONFIG
from metagpt.tools.moderation import Moderation
@ -20,11 +21,23 @@ from metagpt.tools.moderation import Moderation
],
)
def test_moderation(content):
# Prerequisites
assert CONFIG.OPENAI_API_KEY and CONFIG.OPENAI_API_KEY != "YOUR_API_KEY"
assert not CONFIG.OPENAI_API_TYPE
assert CONFIG.OPENAI_API_MODEL
moderation = Moderation()
results = moderation.moderation(content=content)
assert isinstance(results, list)
assert len(results) == len(content)
results = moderation.moderation_with_categories(content=content)
assert isinstance(results, list)
assert results
for m in results:
assert "flagged" in m
assert "true_categories" in m
@pytest.mark.asyncio
@pytest.mark.parametrize(
@ -36,7 +49,23 @@ def test_moderation(content):
],
)
async def test_amoderation(content):
# Prerequisites
assert CONFIG.OPENAI_API_KEY and CONFIG.OPENAI_API_KEY != "YOUR_API_KEY"
assert not CONFIG.OPENAI_API_TYPE
assert CONFIG.OPENAI_API_MODEL
moderation = Moderation()
results = await moderation.amoderation(content=content)
assert isinstance(results, list)
assert len(results) == len(content)
results = await moderation.amoderation_with_categories(content=content)
assert isinstance(results, list)
assert results
for m in results:
assert "flagged" in m
assert "true_categories" in m
if __name__ == "__main__":
pytest.main([__file__, "-s"])

View file

@ -0,0 +1,30 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
@Time : 2023/12/26
@Author : mashenquan
@File : test_openai_text_to_embedding.py
"""
import pytest
from metagpt.config import CONFIG
from metagpt.tools.openai_text_to_embedding import oas3_openai_text_to_embedding
@pytest.mark.asyncio
async def test_embedding():
# Prerequisites
assert CONFIG.OPENAI_API_KEY and CONFIG.OPENAI_API_KEY != "YOUR_API_KEY"
assert not CONFIG.OPENAI_API_TYPE
assert CONFIG.OPENAI_API_MODEL
result = await oas3_openai_text_to_embedding("Panda emoji")
assert result
assert result.model
assert len(result.data) > 0
assert len(result.data[0].embedding) > 0
if __name__ == "__main__":
pytest.main([__file__, "-s"])

View file

@ -0,0 +1,27 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
@Time : 2023/12/26
@Author : mashenquan
@File : test_openai_text_to_image.py
"""
import pytest
from metagpt.config import CONFIG
from metagpt.tools.openai_text_to_image import oas3_openai_text_to_image
@pytest.mark.asyncio
async def test_draw():
# Prerequisites
assert CONFIG.OPENAI_API_KEY and CONFIG.OPENAI_API_KEY != "YOUR_API_KEY"
assert not CONFIG.OPENAI_API_TYPE
assert CONFIG.OPENAI_API_MODEL
binary_data = await oas3_openai_text_to_image("Panda emoji")
assert binary_data
if __name__ == "__main__":
pytest.main([__file__, "-s"])

View file

@ -3,7 +3,7 @@
"""
@Time : 2023/5/2 17:46
@Author : alexanderwu
@File : test_prompt_generator.py
@File : test_prompt_writer.py
"""
import pytest

View file

@ -9,6 +9,7 @@ from __future__ import annotations
import pytest
from metagpt.config import CONFIG
from metagpt.logs import logger
from metagpt.tools import SearchEngineType
from metagpt.tools.search_engine import SearchEngine
@ -44,6 +45,15 @@ async def test_search_engine(
max_results,
as_string,
):
# Prerequisites
if search_engine_typpe is SearchEngineType.SERPAPI_GOOGLE:
assert CONFIG.SERPAPI_API_KEY and CONFIG.SERPAPI_API_KEY != "YOUR_API_KEY"
elif search_engine_typpe is SearchEngineType.DIRECT_GOOGLE:
assert CONFIG.GOOGLE_API_KEY and CONFIG.GOOGLE_API_KEY != "YOUR_API_KEY"
assert CONFIG.GOOGLE_CSE_ID and CONFIG.GOOGLE_CSE_ID != "YOUR_CSE_ID"
elif search_engine_typpe is SearchEngineType.SERPER_GOOGLE:
assert CONFIG.SERPER_API_KEY and CONFIG.SERPER_API_KEY != "YOUR_API_KEY"
search_engine = SearchEngine(search_engine_typpe, run_func)
rsp = await search_engine.run("metagpt", max_results=max_results, as_string=as_string)
logger.info(rsp)
@ -52,3 +62,7 @@ async def test_search_engine(
else:
assert isinstance(rsp, list)
assert len(rsp) == max_results
if __name__ == "__main__":
pytest.main([__file__, "-s"])

View file

@ -18,6 +18,10 @@ MASTER_KEY = "116Qavl2qpCYNEJNv5-e0RC9kncev1nr1gt7ybEGVLk"
@pytest.fixture()
def search_engine_server():
# Prerequisites
# https://www.meilisearch.com/docs/learn/getting_started/installation
# brew update && brew install meilisearch
meilisearch_process = subprocess.Popen(["meilisearch", "--master-key", f"{MASTER_KEY}"], stdout=subprocess.PIPE)
time.sleep(3)
yield
@ -26,6 +30,10 @@ def search_engine_server():
def test_meilisearch(search_engine_server):
# Prerequisites
# https://www.meilisearch.com/docs/learn/getting_started/installation
# brew update && brew install meilisearch
search_engine = MeilisearchEngine(url="http://localhost:7700", token=MASTER_KEY)
# 假设有一个名为"books"的数据源,包含要添加的文档库
@ -44,3 +52,7 @@ def test_meilisearch(search_engine_server):
# 添加文档库到搜索引擎
search_engine.add_documents(books_data_source, documents)
logger.info(search_engine.search("Book 1"))
if __name__ == "__main__":
pytest.main([__file__, "-s"])

View file

@ -4,8 +4,8 @@
import pytest
from metagpt.config import Config
from metagpt.tools import WebBrowserEngineType, web_browser_engine
from metagpt.utils.parse_html import WebPage
@pytest.mark.asyncio
@ -18,14 +18,17 @@ from metagpt.tools import WebBrowserEngineType, web_browser_engine
ids=["playwright", "selenium"],
)
async def test_scrape_web_page(browser_type, url, urls):
conf = Config()
browser = web_browser_engine.WebBrowserEngine(options=conf.runtime_options, engine=browser_type)
browser = web_browser_engine.WebBrowserEngine(engine=browser_type)
result = await browser.run(url)
assert isinstance(result, str)
assert "深度赋智" in result
assert isinstance(result, WebPage)
assert "MetaGPT" in result.inner_text
if urls:
results = await browser.run(url, *urls)
assert isinstance(results, list)
assert len(results) == len(urls) + 1
assert all(("深度赋智" in i) for i in results)
assert all(("MetaGPT" in i.inner_text) for i in results)
if __name__ == "__main__":
pytest.main([__file__, "-s"])

View file

@ -4,8 +4,9 @@
import pytest
from metagpt.config import Config
from metagpt.config import CONFIG
from metagpt.tools import web_browser_engine_playwright
from metagpt.utils.parse_html import WebPage
@pytest.mark.asyncio
@ -19,25 +20,25 @@ from metagpt.tools import web_browser_engine_playwright
ids=["chromium-normal", "firefox-normal", "webkit-normal"],
)
async def test_scrape_web_page(browser_type, use_proxy, kwagrs, url, urls, proxy, capfd):
conf = Config()
global_proxy = conf.global_proxy
global_proxy = CONFIG.global_proxy
try:
if use_proxy:
conf.global_proxy = proxy
browser = web_browser_engine_playwright.PlaywrightWrapper(
options=conf.runtime_options, browser_type=browser_type, **kwagrs
)
CONFIG.global_proxy = proxy
browser = web_browser_engine_playwright.PlaywrightWrapper(browser_type=browser_type, **kwagrs)
result = await browser.run(url)
result = result.inner_text
assert isinstance(result, str)
assert "DeepWisdom" in result
assert isinstance(result, WebPage)
assert "MetaGPT" in result.inner_text
if urls:
results = await browser.run(url, *urls)
assert isinstance(results, list)
assert len(results) == len(urls) + 1
assert all(("DeepWisdom" in i) for i in results)
assert all(("MetaGPT" in i.inner_text) for i in results)
if use_proxy:
assert "Proxy:" in capfd.readouterr().out
finally:
conf.global_proxy = global_proxy
CONFIG.global_proxy = global_proxy
if __name__ == "__main__":
pytest.main([__file__, "-s"])

View file

@ -4,8 +4,9 @@
import pytest
from metagpt.config import Config
from metagpt.config import CONFIG
from metagpt.tools import web_browser_engine_selenium
from metagpt.utils.parse_html import WebPage
@pytest.mark.asyncio
@ -19,23 +20,28 @@ from metagpt.tools import web_browser_engine_selenium
ids=["chrome-normal", "firefox-normal", "edge-normal"],
)
async def test_scrape_web_page(browser_type, use_proxy, url, urls, proxy, capfd):
conf = Config()
global_proxy = conf.global_proxy
# Prerequisites
# firefox, chrome, Microsoft Edge
global_proxy = CONFIG.global_proxy
try:
if use_proxy:
conf.global_proxy = proxy
browser = web_browser_engine_selenium.SeleniumWrapper(options=conf.runtime_options, browser_type=browser_type)
CONFIG.global_proxy = proxy
browser = web_browser_engine_selenium.SeleniumWrapper(browser_type=browser_type)
result = await browser.run(url)
result = result.inner_text
assert isinstance(result, str)
assert "Deepwisdom" in result
assert isinstance(result, WebPage)
assert "MetaGPT" in result.inner_text
if urls:
results = await browser.run(url, *urls)
assert isinstance(results, list)
assert len(results) == len(urls) + 1
assert all(("Deepwisdom" in i.inner_text) for i in results)
assert all(("MetaGPT" in i.inner_text) for i in results)
if use_proxy:
assert "Proxy:" in capfd.readouterr().out
finally:
conf.global_proxy = global_proxy
CONFIG.global_proxy = global_proxy
if __name__ == "__main__":
pytest.main([__file__, "-s"])