From 95ef3bd85f0f1384f8e122bb8ffd1c4ff21f1385 Mon Sep 17 00:00:00 2001 From: felinae98 <731499577@qq.com> Date: Mon, 6 Jun 2022 10:31:32 +0800 Subject: [PATCH] fix test --- src/plugins/nonebot_bison/config_manager.py | 18 +++-- tests/platforms/test_platform.py | 74 ++++++++++++--------- tests/test_config_manager_abort.py | 2 +- tests/test_config_manager_add.py | 18 ++--- tests/test_config_manager_query_del.py | 10 ++- 5 files changed, 67 insertions(+), 55 deletions(-) diff --git a/src/plugins/nonebot_bison/config_manager.py b/src/plugins/nonebot_bison/config_manager.py index 83a29ca..771e48b 100644 --- a/src/plugins/nonebot_bison/config_manager.py +++ b/src/plugins/nonebot_bison/config_manager.py @@ -266,21 +266,27 @@ def do_del_sub(del_sub: Type[Matcher]): state["sub_table"] = {} for index, sub in enumerate(sub_list, 1): state["sub_table"][index] = { - "target_type": sub["target_type"], - "target": sub["target"], + "platform_name": sub.target.platform_name, + "target": sub.target.target, } res += "{} {} {} {}\n".format( - index, sub["target_type"], sub["target_name"], sub["target"] + index, + sub.target.platform_name, + sub.target.target_name, + sub.target.target, ) - platform = platform_manager[sub["target_type"]] + platform = platform_manager[sub.target.platform_name] if platform.categories: res += " [{}]".format( ", ".join( - map(lambda x: platform.categories[Category(x)], sub["cats"]) + map( + lambda x: platform.categories[Category(x)], + sub.categories, + ) ) ) if platform.enable_tag: - res += " {}".format(", ".join(sub["tags"])) + res += " {}".format(", ".join(sub.tags)) res += "\n" res += "请输入要删除的订阅的序号\n输入'取消'中止" await bot.send(event=event, message=Message(await parse_text(res))) diff --git a/tests/platforms/test_platform.py b/tests/platforms/test_platform.py index 7bf0578..d140de1 100644 --- a/tests/platforms/test_platform.py +++ b/tests/platforms/test_platform.py @@ -22,7 +22,7 @@ raw_post_list_2 = raw_post_list_1 + [ def dummy_user(app: App): from nonebot_bison.types import User - user = User("123", "group") + user = User(123, "group") return user @@ -90,6 +90,12 @@ def mock_platform(app: App): from nonebot_bison.platform.platform import NewMessage from nonebot_bison.post import Post from nonebot_bison.types import Category, RawPost, Tag, Target + from nonebot_bison.utils import SchedulerConfig + + class MockPlatformSchedConf(SchedulerConfig, name="mock"): + + schedule_type = "interval" + schedule_setting = {"seconds": 100} class MockPlatform(NewMessage): @@ -97,9 +103,9 @@ def mock_platform(app: App): name = "Mock Platform" enabled = True is_common = True - schedule_interval = 10 enable_tag = True has_target = True + scheduler_class = "mock" categories = { Category(1): "转发", Category(2): "视频", @@ -148,6 +154,12 @@ def mock_platform_no_target(app: App): from nonebot_bison.platform.platform import CategoryNotSupport, NewMessage from nonebot_bison.post import Post from nonebot_bison.types import Category, RawPost, Tag, Target + from nonebot_bison.utils import SchedulerConfig + + class MockPlatformSchedConf(SchedulerConfig, name="mock"): + + schedule_type = "interval" + schedule_setting = {"seconds": 100} class MockPlatform(NewMessage): @@ -155,8 +167,7 @@ def mock_platform_no_target(app: App): name = "Mock Platform" enabled = True is_common = True - schedule_type = "interval" - schedule_kw = {"seconds": 30} + scheduler_class = "mock" enable_tag = True has_target = False categories = {Category(1): "转发", Category(2): "视频", Category(3): "不支持"} @@ -206,14 +217,19 @@ def mock_platform_no_target_2(app: App): from nonebot_bison.platform.platform import NewMessage from nonebot_bison.post import Post from nonebot_bison.types import Category, RawPost, Tag, Target + from nonebot_bison.utils import SchedulerConfig + + class MockPlatformSchedConf(SchedulerConfig, name="mock"): + + schedule_type = "interval" + schedule_setting = {"seconds": 100} class MockPlatform(NewMessage): platform_name = "mock_platform" name = "Mock Platform" enabled = True - schedule_type = "interval" - schedule_kw = {"seconds": 30} + scheduler_class = "mock" is_common = True enable_tag = True has_target = False @@ -324,13 +340,13 @@ async def test_new_message_target_without_cats_tags( mock_platform_without_cats_tags, user_info_factory ): res1 = await mock_platform_without_cats_tags.fetch_new_post( - "dummy", [user_info_factory(lambda _: [1, 2], lambda _: [])] + "dummy", [user_info_factory([1, 2], [])] ) assert len(res1) == 0 res2 = await mock_platform_without_cats_tags.fetch_new_post( "dummy", [ - user_info_factory(lambda _: [], lambda _: []), + user_info_factory([], []), ], ) assert len(res2) == 1 @@ -342,16 +358,14 @@ async def test_new_message_target_without_cats_tags( @pytest.mark.asyncio async def test_new_message_target(mock_platform, user_info_factory): - res1 = await mock_platform.fetch_new_post( - "dummy", [user_info_factory(lambda _: [1, 2], lambda _: [])] - ) + res1 = await mock_platform.fetch_new_post("dummy", [user_info_factory([1, 2], [])]) assert len(res1) == 0 res2 = await mock_platform.fetch_new_post( "dummy", [ - user_info_factory(lambda _: [1, 2], lambda _: []), - user_info_factory(lambda _: [1], lambda _: []), - user_info_factory(lambda _: [1, 2], lambda _: ["tag1"]), + user_info_factory([1, 2], []), + user_info_factory([1], []), + user_info_factory([1, 2], ["tag1"]), ], ) assert len(res2) == 3 @@ -372,15 +386,15 @@ async def test_new_message_target(mock_platform, user_info_factory): @pytest.mark.asyncio async def test_new_message_no_target(mock_platform_no_target, user_info_factory): res1 = await mock_platform_no_target.fetch_new_post( - "dummy", [user_info_factory(lambda _: [1, 2], lambda _: [])] + "dummy", [user_info_factory([1, 2], [])] ) assert len(res1) == 0 res2 = await mock_platform_no_target.fetch_new_post( "dummy", [ - user_info_factory(lambda _: [1, 2], lambda _: []), - user_info_factory(lambda _: [1], lambda _: []), - user_info_factory(lambda _: [1, 2], lambda _: ["tag1"]), + user_info_factory([1, 2], []), + user_info_factory([1], []), + user_info_factory([1, 2], ["tag1"]), ], ) assert len(res2) == 3 @@ -397,7 +411,7 @@ async def test_new_message_no_target(mock_platform_no_target, user_info_factory) assert "p2" in id_set_2 assert "p2" in id_set_3 res3 = await mock_platform_no_target.fetch_new_post( - "dummy", [user_info_factory(lambda _: [1, 2], lambda _: [])] + "dummy", [user_info_factory([1, 2], [])] ) assert len(res3) == 0 @@ -405,11 +419,11 @@ async def test_new_message_no_target(mock_platform_no_target, user_info_factory) @pytest.mark.asyncio async def test_status_change(mock_status_change, user_info_factory): res1 = await mock_status_change.fetch_new_post( - "dummy", [user_info_factory(lambda _: [1, 2], lambda _: [])] + "dummy", [user_info_factory([1, 2], [])] ) assert len(res1) == 0 res2 = await mock_status_change.fetch_new_post( - "dummy", [user_info_factory(lambda _: [1, 2], lambda _: [])] + "dummy", [user_info_factory([1, 2], [])] ) assert len(res2) == 1 posts = res2[0][1] @@ -418,8 +432,8 @@ async def test_status_change(mock_status_change, user_info_factory): res3 = await mock_status_change.fetch_new_post( "dummy", [ - user_info_factory(lambda _: [1, 2], lambda _: []), - user_info_factory(lambda _: [1], lambda _: []), + user_info_factory([1, 2], []), + user_info_factory([1], []), ], ) assert len(res3) == 2 @@ -427,7 +441,7 @@ async def test_status_change(mock_status_change, user_info_factory): assert res3[0][1][0].text == "off" assert len(res3[1][1]) == 0 res4 = await mock_status_change.fetch_new_post( - "dummy", [user_info_factory(lambda _: [1, 2], lambda _: [])] + "dummy", [user_info_factory([1, 2], [])] ) assert len(res4) == 0 @@ -445,19 +459,13 @@ async def test_group( from nonebot_bison.types import Category, RawPost, Tag, Target group_platform = NoTargetGroup([mock_platform_no_target, mock_platform_no_target_2]) - res1 = await group_platform.fetch_new_post( - "dummy", [user_info_factory(lambda _: [1, 4], lambda _: [])] - ) + res1 = await group_platform.fetch_new_post("dummy", [user_info_factory([1, 4], [])]) assert len(res1) == 0 - res2 = await group_platform.fetch_new_post( - "dummy", [user_info_factory(lambda _: [1, 4], lambda _: [])] - ) + res2 = await group_platform.fetch_new_post("dummy", [user_info_factory([1, 4], [])]) assert len(res2) == 1 posts = res2[0][1] assert len(posts) == 2 id_set_2 = set(map(lambda x: x.text, posts)) assert "p2" in id_set_2 and "p6" in id_set_2 - res3 = await group_platform.fetch_new_post( - "dummy", [user_info_factory(lambda _: [1, 4], lambda _: [])] - ) + res3 = await group_platform.fetch_new_post("dummy", [user_info_factory([1, 4], [])]) assert len(res3) == 0 diff --git a/tests/test_config_manager_abort.py b/tests/test_config_manager_abort.py index 453af95..e01fc32 100644 --- a/tests/test_config_manager_abort.py +++ b/tests/test_config_manager_abort.py @@ -272,7 +272,7 @@ async def test_abort_add_on_tag(app: App, db_migration): # 删除订阅阶段中止 @pytest.mark.asyncio -async def test_abort_del_sub(app: App): +async def test_abort_del_sub(app: App, init_scheduler): from nonebot.adapters.onebot.v11.bot import Bot from nonebot.adapters.onebot.v11.message import Message from nonebot_bison.config import config diff --git a/tests/test_config_manager_add.py b/tests/test_config_manager_add.py index 7f5f9d1..b75385b 100644 --- a/tests/test_config_manager_add.py +++ b/tests/test_config_manager_add.py @@ -58,7 +58,7 @@ async def test_configurable_at_me_false(app: App): @pytest.mark.asyncio @respx.mock -async def test_add_with_target(app: App, db_migration): +async def test_add_with_target(app: App, init_scheduler): from nonebot.adapters.onebot.v11.event import Sender from nonebot.adapters.onebot.v11.message import Message from nonebot_bison.config import config @@ -173,7 +173,7 @@ async def test_add_with_target(app: App, db_migration): @pytest.mark.asyncio @respx.mock -async def test_add_with_target_no_cat(app: App, db_migration): +async def test_add_with_target_no_cat(app: App, init_scheduler): from nonebot.adapters.onebot.v11.event import Sender from nonebot.adapters.onebot.v11.message import Message from nonebot_bison.config import config @@ -232,7 +232,7 @@ async def test_add_with_target_no_cat(app: App, db_migration): @pytest.mark.asyncio @respx.mock -async def test_add_no_target(app: App, db_migration): +async def test_add_no_target(app: App, init_scheduler): from nonebot.adapters.onebot.v11.event import Sender from nonebot.adapters.onebot.v11.message import Message from nonebot_bison.config import config @@ -397,7 +397,7 @@ async def test_add_with_get_id(app: App, db_migration): @pytest.mark.asyncio @respx.mock -async def test_add_with_bilibili_target_parser(app: App, db_migration): +async def test_add_with_bilibili_target_parser(app: App, init_scheduler): from nonebot.adapters.onebot.v11.event import Sender from nonebot.adapters.onebot.v11.message import Message from nonebot_bison.config import config @@ -507,8 +507,8 @@ async def test_add_with_bilibili_target_parser(app: App, db_migration): subs = await config.list_subscribe(10000, "group") assert len(subs) == 1 sub = subs[0] - assert sub["target"] == "161775300" - assert sub["tags"] == [] - assert sub["cats"] == [platform_manager["bilibili"].reverse_category["视频"]] - assert sub["target_type"] == "bilibili" - assert sub["target_name"] == "明日方舟" + assert sub.target.target == "161775300" + assert sub.tags == [] + assert sub.categories == [platform_manager["bilibili"].reverse_category["视频"]] + assert sub.target.platform_name == "bilibili" + assert sub.target.target_name == "明日方舟" diff --git a/tests/test_config_manager_query_del.py b/tests/test_config_manager_query_del.py index 6c6a26f..c1a2f70 100644 --- a/tests/test_config_manager_query_del.py +++ b/tests/test_config_manager_query_del.py @@ -8,7 +8,7 @@ from .utils import fake_admin_user, fake_group_message_event @pytest.mark.asyncio -async def test_query_sub(app: App, db_migration): +async def test_query_sub(app: App, init_scheduler): from nonebot.adapters.onebot.v11.message import Message from nonebot_bison.config import config from nonebot_bison.config_manager import query_sub_matcher @@ -36,7 +36,7 @@ async def test_query_sub(app: App, db_migration): @pytest.mark.asyncio -async def test_del_sub(app: App, db_migration): +async def test_del_sub(app: App, init_scheduler): from nonebot.adapters.onebot.v11.bot import Bot from nonebot.adapters.onebot.v11.message import Message from nonebot_bison.config import config @@ -86,15 +86,13 @@ async def test_del_sub(app: App, db_migration): @pytest.mark.asyncio -async def test_del_empty_sub(app: App): +async def test_del_empty_sub(app: App, init_scheduler): from nonebot.adapters.onebot.v11.bot import Bot from nonebot.adapters.onebot.v11.message import Message - from nonebot_bison.config import Config + from nonebot_bison.config import config from nonebot_bison.config_manager import del_sub_matcher from nonebot_bison.platform import platform_manager - config = Config() - config.user_target.truncate() async with app.test_matcher(del_sub_matcher) as ctx: bot = ctx.create_bot(base=Bot) assert isinstance(bot, Bot)