📝 add examples

This commit is contained in:
yanyongyu
2022-01-07 18:38:04 +08:00
parent 6b2d8d2674
commit 0a10a3c18b
13 changed files with 167 additions and 36 deletions

21
tests/conftest.py Normal file
View File

@ -0,0 +1,21 @@
from pathlib import Path
from typing import TYPE_CHECKING, Set
import pytest
if TYPE_CHECKING:
from nonebot.plugin import Plugin
@pytest.fixture
def load_plugin(nonebug_init: None) -> Set["Plugin"]:
import nonebot
return nonebot.load_plugins(str(Path(__file__).parent / "plugins"))
@pytest.fixture
def load_example(nonebug_init: None) -> Set["Plugin"]:
import nonebot
return nonebot.load_plugins(str(Path(__file__).parent / "examples"))

29
tests/examples/weather.py Normal file
View File

@ -0,0 +1,29 @@
from nonebot import on_command
from nonebot.rule import to_me
from nonebot.matcher import Matcher
from nonebot.adapters import Message
from nonebot.params import Arg, CommandArg, ArgPlainText
weather = on_command("weather", rule=to_me(), aliases={"天气", "天气预报"}, priority=5)
@weather.handle()
async def handle_first_receive(matcher: Matcher, args: Message = CommandArg()):
plain_text = args.extract_plain_text() # 首次发送命令时跟随的参数,例:/天气 上海则args为上海
if plain_text:
matcher.set_arg("city", args) # 如果用户发送了参数则直接赋值
@weather.got("city", prompt="你想查询哪个城市的天气呢?")
async def handle_city(city: Message = Arg(), city_name: str = ArgPlainText("city")):
print(city_name)
if city_name not in ["北京", "上海"]: # 如果参数不符合要求,则提示用户重新输入
await weather.reject(city.template("你想查询的城市 {city} 暂不支持,请重新输入!"))
city_weather = await get_weather(city_name)
await weather.finish(city_weather)
# 在这里编写获取天气信息的函数
async def get_weather(city: str) -> str:
return f"{city}的天气是..."

View File

@ -0,0 +1,64 @@
import pytest
from nonebug import App
@pytest.mark.asyncio
async def test_weather(app: App):
from examples.weather import weather
from utils import make_fake_event, make_fake_message
# 将此处的 make_fake_message() 替换为你要发送的平台消息 Message 类型
Message = make_fake_message()
async with app.test_matcher(weather) as ctx:
bot = ctx.create_bot()
msg = Message("/天气 上海")
# 将此处的 make_fake_event() 替换为你要发送的平台事件 Event 类型
event = make_fake_event(_message=msg, _to_me=True)()
ctx.receive_event(bot, event)
ctx.should_call_send(event, "上海的天气是...", True)
ctx.should_finished()
async with app.test_matcher(weather) as ctx:
bot = ctx.create_bot()
msg = Message("/天气 南京")
# 将此处的 make_fake_event() 替换为你要发送的平台事件 Event 类型
event = make_fake_event(_message=msg, _to_me=True)()
ctx.receive_event(bot, event)
ctx.should_call_send(event, Message("你想查询的城市 南京 暂不支持,请重新输入!"), True)
ctx.should_rejected()
msg = Message("北京")
event = make_fake_event(_message=msg)()
ctx.receive_event(bot, event)
ctx.should_call_send(event, "北京的天气是...", True)
ctx.should_finished()
async with app.test_matcher(weather) as ctx:
bot = ctx.create_bot()
msg = Message("/天气")
# 将此处的 make_fake_event() 替换为你要发送的平台事件 Event 类型
event = make_fake_event(_message=msg, _to_me=True)()
ctx.receive_event(bot, event)
ctx.should_call_send(event, "你想查询哪个城市的天气呢?", True)
msg = Message("杭州")
event = make_fake_event(_message=msg)()
ctx.receive_event(bot, event)
ctx.should_call_send(event, Message("你想查询的城市 杭州 暂不支持,请重新输入!"), True)
ctx.should_rejected()
msg = Message("北京")
event = make_fake_event(_message=msg)()
ctx.receive_event(bot, event)
ctx.should_call_send(event, "北京的天气是...", True)
ctx.should_finished()

View File

@ -4,8 +4,6 @@ from typing import TYPE_CHECKING, Set
import pytest
from utils import load_plugin
if TYPE_CHECKING:
from nonebot.plugin import Plugin

View File

@ -1,7 +1,7 @@
import pytest
from nonebug import App
from utils import load_plugin, make_fake_event, make_fake_message
from utils import make_fake_event, make_fake_message
@pytest.mark.asyncio

View File

@ -1,7 +1,7 @@
import pytest
from nonebug import App
from utils import load_plugin, make_fake_event, make_fake_message
from utils import make_fake_event, make_fake_message
@pytest.mark.asyncio

View File

@ -1,11 +1,8 @@
from pathlib import Path
from typing import TYPE_CHECKING, Set, Type, Optional
from typing import TYPE_CHECKING, Type, Optional
import pytest
from pydantic import create_model
if TYPE_CHECKING:
from nonebot.plugin import Plugin
from nonebot.adapters import Event, Message
@ -85,10 +82,3 @@ def make_fake_event(
extra = "forbid"
return FakeEvent
@pytest.fixture
def load_plugin(nonebug_init: None) -> Set["Plugin"]:
import nonebot
return nonebot.load_plugins(str(Path(__file__).parent / "plugins"))