From 96749bde8307a89f48d7a312227e460fc8fbeebf Mon Sep 17 00:00:00 2001 From: yanyongyu Date: Tue, 4 Jan 2022 15:19:59 +0800 Subject: [PATCH] :white_check_mark: add matcher updater tests --- tests/plugins/matcher/__init__.py | 7 +++ tests/plugins/matcher/matcher_permission.py | 13 +++++ .../matcher_process.py} | 0 tests/plugins/matcher/matcher_type.py | 10 ++++ tests/test_matcher.py | 57 ++++++++++++++++++- 5 files changed, 86 insertions(+), 1 deletion(-) create mode 100644 tests/plugins/matcher/__init__.py create mode 100644 tests/plugins/matcher/matcher_permission.py rename tests/plugins/{matcher.py => matcher/matcher_process.py} (100%) create mode 100644 tests/plugins/matcher/matcher_type.py diff --git a/tests/plugins/matcher/__init__.py b/tests/plugins/matcher/__init__.py new file mode 100644 index 00000000..f084d7f1 --- /dev/null +++ b/tests/plugins/matcher/__init__.py @@ -0,0 +1,7 @@ +from pathlib import Path + +from nonebot import load_plugins + +_sub_plugins = set() + +_sub_plugins |= load_plugins(str(Path(__file__).parent)) diff --git a/tests/plugins/matcher/matcher_permission.py b/tests/plugins/matcher/matcher_permission.py new file mode 100644 index 00000000..27a105b3 --- /dev/null +++ b/tests/plugins/matcher/matcher_permission.py @@ -0,0 +1,13 @@ +from nonebot.matcher import Matcher +from nonebot.permission import Permission + +default_permission = Permission() + +test_permission_updater = Matcher.new(permission=default_permission) + +test_custom_updater = Matcher.new(permission=default_permission) + + +@test_custom_updater.permission_updater +async def _() -> Permission: + return default_permission diff --git a/tests/plugins/matcher.py b/tests/plugins/matcher/matcher_process.py similarity index 100% rename from tests/plugins/matcher.py rename to tests/plugins/matcher/matcher_process.py diff --git a/tests/plugins/matcher/matcher_type.py b/tests/plugins/matcher/matcher_type.py new file mode 100644 index 00000000..76db3a6f --- /dev/null +++ b/tests/plugins/matcher/matcher_type.py @@ -0,0 +1,10 @@ +from nonebot.matcher import Matcher + +test_type_updater = Matcher.new(type_="test") + +test_custom_updater = Matcher.new(type_="test") + + +@test_custom_updater.type_updater +async def _() -> str: + return "custom" diff --git a/tests/test_matcher.py b/tests/test_matcher.py index 61f8c73c..32a0ed3d 100644 --- a/tests/test_matcher.py +++ b/tests/test_matcher.py @@ -6,7 +6,7 @@ from utils import load_plugin, make_fake_event, make_fake_message @pytest.mark.asyncio async def test_matcher(app: App, load_plugin): - from plugins.matcher import ( + from plugins.matcher.matcher_process import ( test_got, test_handle, test_preset, @@ -74,3 +74,58 @@ async def test_matcher(app: App, load_plugin): bot = ctx.create_bot() ctx.receive_event(bot, event) ctx.should_finished() + + +@pytest.mark.asyncio +async def test_type_updater(app: App, load_plugin): + from plugins.matcher.matcher_type import ( + test_type_updater, + test_custom_updater, + ) + + event = make_fake_event()() + + assert test_type_updater.type == "test" + async with app.test_api() as ctx: + bot = ctx.create_bot() + matcher = test_type_updater() + new_type = await matcher.update_type(bot, event) + assert new_type == "message" + + assert test_custom_updater.type == "test" + async with app.test_api() as ctx: + bot = ctx.create_bot() + matcher = test_custom_updater() + new_type = await matcher.update_type(bot, event) + assert new_type == "custom" + + +@pytest.mark.asyncio +async def test_permission_updater(app: App, load_plugin): + from nonebot.permission import User + + from plugins.matcher.matcher_permission import ( + default_permission, + test_custom_updater, + test_permission_updater, + ) + + event = make_fake_event(_session_id="test")() + + assert test_permission_updater.permission is default_permission + async with app.test_api() as ctx: + bot = ctx.create_bot() + matcher = test_permission_updater() + new_perm = await matcher.update_permission(bot, event) + assert len(new_perm.checkers) == 1 + checker = list(new_perm.checkers)[0].call + assert isinstance(checker, User) + assert checker.users == ("test",) + assert checker.perm is default_permission + + assert test_custom_updater.permission is default_permission + async with app.test_api() as ctx: + bot = ctx.create_bot() + matcher = test_custom_updater() + new_perm = await matcher.update_permission(bot, event) + assert new_perm is default_permission