mirror of
https://github.com/nonebot/nonebot2.git
synced 2025-09-06 20:16:47 +00:00
⚡ improve dependency cache
This commit is contained in:
@ -1,6 +1,8 @@
|
||||
[report]
|
||||
exclude_lines =
|
||||
def __repr__
|
||||
pragma: no cover
|
||||
if TYPE_CHECKING:
|
||||
@(abc\.)?abstractmethod
|
||||
raise NotImplementedError
|
||||
if __name__ == .__main__.:
|
||||
|
8
tests/.isort.cfg
Normal file
8
tests/.isort.cfg
Normal file
@ -0,0 +1,8 @@
|
||||
[settings]
|
||||
profile=black
|
||||
line_length=80
|
||||
length_sort=true
|
||||
skip_gitignore=true
|
||||
force_sort_within_sections=true
|
||||
known_local_folder=plugins
|
||||
extra_standard_library=typing_extensions
|
@ -2,22 +2,23 @@ from nonebot import on_message
|
||||
from nonebot.adapters import Event
|
||||
from nonebot.params import Depends
|
||||
|
||||
test = on_message()
|
||||
test2 = on_message()
|
||||
test_depends = on_message()
|
||||
|
||||
runned = False
|
||||
runned = []
|
||||
|
||||
|
||||
def dependency(event: Event):
|
||||
# test cache
|
||||
global runned
|
||||
assert not runned
|
||||
runned = True
|
||||
runned.append(event)
|
||||
return event
|
||||
|
||||
|
||||
@test.handle()
|
||||
@test2.handle()
|
||||
async def handle(x: Event = Depends(dependency, use_cache=True)):
|
||||
@test_depends.handle()
|
||||
async def depends(x: Event = Depends(dependency)):
|
||||
# test dependency
|
||||
return x
|
||||
|
||||
|
||||
@test_depends.handle()
|
||||
async def depends_cache(y: Event = Depends(dependency, use_cache=True)):
|
||||
return y
|
||||
|
@ -1,2 +0,0 @@
|
||||
[tool.isort]
|
||||
known_local_folder = ["plugins"]
|
@ -1,6 +1,5 @@
|
||||
import os
|
||||
import sys
|
||||
from re import A
|
||||
from typing import TYPE_CHECKING, Set
|
||||
|
||||
import pytest
|
||||
|
29
tests/test_param.py
Normal file
29
tests/test_param.py
Normal file
@ -0,0 +1,29 @@
|
||||
import pytest
|
||||
from nonebug import App
|
||||
|
||||
from utils import load_plugin, make_fake_event
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_depends(app: App, load_plugin):
|
||||
from nonebot.params import EventParam, DependParam
|
||||
|
||||
from plugins.depends import runned, depends, test_depends
|
||||
|
||||
async with app.test_dependent(
|
||||
depends, allow_types=[EventParam, DependParam]
|
||||
) as ctx:
|
||||
event = make_fake_event()()
|
||||
ctx.pass_params(event=event)
|
||||
ctx.should_return(event)
|
||||
|
||||
assert len(runned) == 1 and runned[0] == event
|
||||
|
||||
runned.clear()
|
||||
|
||||
async with app.test_matcher(test_depends) as ctx:
|
||||
bot = ctx.create_bot()
|
||||
event_next = make_fake_event()()
|
||||
ctx.receive_event(bot, event_next)
|
||||
|
||||
assert len(runned) == 1 and runned[0] == event_next
|
@ -1,10 +1,56 @@
|
||||
from pathlib import Path
|
||||
from typing import TYPE_CHECKING, Set
|
||||
from typing import TYPE_CHECKING, Set, Type, Optional
|
||||
|
||||
import pytest
|
||||
from pydantic import create_model
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from nonebot.plugin import Plugin
|
||||
from nonebot.adapters import Event, Message
|
||||
|
||||
|
||||
def make_fake_event(
|
||||
_type: str = "message",
|
||||
_name: str = "test",
|
||||
_description: str = "test",
|
||||
_user_id: str = "test",
|
||||
_session_id: str = "test",
|
||||
_message: Optional["Message"] = None,
|
||||
_to_me: bool = True,
|
||||
**fields,
|
||||
) -> Type["Event"]:
|
||||
from nonebot.adapters import Event
|
||||
|
||||
_Fake = create_model("_Fake", __base__=Event, **fields)
|
||||
|
||||
class FakeEvent(_Fake):
|
||||
def get_type(self) -> str:
|
||||
return _type
|
||||
|
||||
def get_event_name(self) -> str:
|
||||
return _name
|
||||
|
||||
def get_event_description(self) -> str:
|
||||
return _description
|
||||
|
||||
def get_user_id(self) -> str:
|
||||
return _user_id
|
||||
|
||||
def get_session_id(self) -> str:
|
||||
return _session_id
|
||||
|
||||
def get_message(self) -> "Message":
|
||||
if _message is not None:
|
||||
return _message
|
||||
raise NotImplementedError
|
||||
|
||||
def is_tome(self) -> bool:
|
||||
return _to_me
|
||||
|
||||
class Config:
|
||||
extra = "forbid"
|
||||
|
||||
return FakeEvent
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
|
Reference in New Issue
Block a user