Skip to content

Commit

Permalink
✅ add qqguild test
Browse files Browse the repository at this point in the history
  • Loading branch information
RF-Tar-Railt committed Oct 23, 2023
1 parent d9c1abe commit 7969458
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 58 deletions.
4 changes: 3 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import pytest
import nonebot
from nonebug import NONEBOT_INIT_KWARGS
from nonebot.adapters.satori import Adapter as SatoriAdapter

# 导入适配器
from nonebot.adapters.qq import Adapter as QQAdapter
from nonebot.adapters.satori import Adapter as SatoriAdapter
from nonebot.adapters.discord import Adapter as DiscordAdapter
from nonebot.adapters.onebot.v11 import Adapter as Onebot11Adapter

Expand All @@ -19,6 +20,7 @@ def pytest_configure(config: pytest.Config):
def load_bot():
# 加载适配器
driver = nonebot.get_driver()
driver.register_adapter(QQAdapter)
driver.register_adapter(DiscordAdapter)
driver.register_adapter(Onebot11Adapter)
driver.register_adapter(SatoriAdapter)
Expand Down
80 changes: 23 additions & 57 deletions tests/fake.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@
from typing import TYPE_CHECKING, Literal

if TYPE_CHECKING:
from nonebot.adapters.qq import MessageCreateEvent as MessageCreateEvent
from nonebot.adapters.satori.event import MessageEvent as SatoriMessageEvent
from nonebot.adapters.discord.event import ApplicationCommandInteractionEvent
from nonebot.adapters.onebot.v11 import GroupMessageEvent as GroupMessageEventV11
from nonebot.adapters.onebot.v12 import GroupMessageEvent as GroupMessageEventV12
from nonebot.adapters.onebot.v11 import PrivateMessageEvent as PrivateMessageEventV11
from nonebot.adapters.onebot.v12 import PrivateMessageEvent as PrivateMessageEventV12


def fake_group_message_event_v11(**field) -> "GroupMessageEventV11":
Expand Down Expand Up @@ -69,61 +68,6 @@ class Config:
return FakeEvent(**field)


def fake_group_message_event_v12(**field) -> "GroupMessageEventV12":
from pydantic import create_model
from nonebot.adapters.onebot.v12.event import BotSelf
from nonebot.adapters.onebot.v12 import Message, GroupMessageEvent

_Fake = create_model("_Fake", __base__=GroupMessageEvent)

class FakeEvent(_Fake):
self: BotSelf = BotSelf(platform="qq", user_id="test")
id: str = "1"
time: datetime = datetime.fromtimestamp(1000000)
type: Literal["message"] = "message"
detail_type: Literal["group"] = "group"
sub_type: str = ""
message_id: str = "10"
message: Message = Message("test")
original_message: Message = Message("test")
alt_message: str = "test"
user_id: str = "100"
group_id: str = "10000"
to_me: bool = False

class Config:
extra = "forbid"

return FakeEvent(**field)


def fake_private_message_event_v12(**field) -> "PrivateMessageEventV12":
from pydantic import create_model
from nonebot.adapters.onebot.v12.event import BotSelf
from nonebot.adapters.onebot.v12 import Message, PrivateMessageEvent

_Fake = create_model("_Fake", __base__=PrivateMessageEvent)

class FakeEvent(_Fake):
self: BotSelf = BotSelf(platform="qq", user_id="test")
id: str = "1"
time: datetime = datetime.fromtimestamp(1000000)
type: Literal["message"] = "message"
detail_type: Literal["private"] = "private"
sub_type: str = ""
message_id: str = "10"
message: Message = Message("test")
original_message: Message = Message("test")
alt_message: str = "test"
user_id: str = "100"
to_me: bool = False

class Config:
extra = "forbid"

return FakeEvent(**field)


def fake_discord_interaction_event(**field) -> "ApplicationCommandInteractionEvent":
from pydantic import create_model
from nonebot.adapters.discord.event import ApplicationCommandInteractionEvent
Expand Down Expand Up @@ -166,3 +110,25 @@ class Config:
extra = "forbid"

return FakeEvent(**field)


def fake_message_event_guild(**field) -> "MessageCreateEvent":
from pydantic import create_model
from nonebot.adapters.qq.message import Message
from nonebot.adapters.qq.models.guild import User
from nonebot.adapters.qq.event import MessageCreateEvent

_Fake = create_model("_Fake", __base__=MessageCreateEvent)

class FakeEvent(_Fake):
id: str = "1234"
channel_id: str = "abcd"
guild_id: str = "efgh"
content: str = "test"
author: User = User(id=field.pop("user_id", "123456789"), username=field.pop("username", "foobar"))
_message = field.pop("message", Message("test"))

class Config:
extra = "forbid"

return FakeEvent(**field)
37 changes: 37 additions & 0 deletions tests/test_qqguild.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import asyncio

import pytest
from nonebug import App
from nonebot import logger, get_adapter
from arclet.alconna import Args, Alconna
from nonebot.adapters.qq import Bot, Adapter, Message

from tests.fake import fake_message_event_guild


@pytest.mark.asyncio()
async def test_send(app: App):
from nonebot_plugin_alconna import on_alconna

def check(name: str):
print(">>>>>>>>>>>>> check running")
logger.warning(">>>>>>>>>>>>>>> check running")
asyncio.create_task(bot.send(event, "check running"))
if isinstance(name, str):
return name
return None

cmd = on_alconna(Alconna("test", Args["name", check]))

@cmd.handle()
async def _():
await cmd.finish("test!")

async with app.test_matcher(cmd) as ctx:
adapter = get_adapter(Adapter)
bot = ctx.create_bot(base=Bot, adapter=adapter, bot_info=None)
event = fake_message_event_guild(message=Message("test aaaa"), id="123")
ctx.receive_event(bot, event)
ctx.should_call_send(event, "check running")
ctx.should_call_send(event, "test!")
ctx.should_finished()

0 comments on commit 7969458

Please sign in to comment.