mirror of
https://github.com/suyiiyii/nonebot-bison.git
synced 2025-06-07 20:33:01 +08:00
fix test
This commit is contained in:
parent
50b46c5f03
commit
95ef3bd85f
@ -266,21 +266,27 @@ def do_del_sub(del_sub: Type[Matcher]):
|
|||||||
state["sub_table"] = {}
|
state["sub_table"] = {}
|
||||||
for index, sub in enumerate(sub_list, 1):
|
for index, sub in enumerate(sub_list, 1):
|
||||||
state["sub_table"][index] = {
|
state["sub_table"][index] = {
|
||||||
"target_type": sub["target_type"],
|
"platform_name": sub.target.platform_name,
|
||||||
"target": sub["target"],
|
"target": sub.target.target,
|
||||||
}
|
}
|
||||||
res += "{} {} {} {}\n".format(
|
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:
|
if platform.categories:
|
||||||
res += " [{}]".format(
|
res += " [{}]".format(
|
||||||
", ".join(
|
", ".join(
|
||||||
map(lambda x: platform.categories[Category(x)], sub["cats"])
|
map(
|
||||||
|
lambda x: platform.categories[Category(x)],
|
||||||
|
sub.categories,
|
||||||
|
)
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
if platform.enable_tag:
|
if platform.enable_tag:
|
||||||
res += " {}".format(", ".join(sub["tags"]))
|
res += " {}".format(", ".join(sub.tags))
|
||||||
res += "\n"
|
res += "\n"
|
||||||
res += "请输入要删除的订阅的序号\n输入'取消'中止"
|
res += "请输入要删除的订阅的序号\n输入'取消'中止"
|
||||||
await bot.send(event=event, message=Message(await parse_text(res)))
|
await bot.send(event=event, message=Message(await parse_text(res)))
|
||||||
|
@ -22,7 +22,7 @@ raw_post_list_2 = raw_post_list_1 + [
|
|||||||
def dummy_user(app: App):
|
def dummy_user(app: App):
|
||||||
from nonebot_bison.types import User
|
from nonebot_bison.types import User
|
||||||
|
|
||||||
user = User("123", "group")
|
user = User(123, "group")
|
||||||
return user
|
return user
|
||||||
|
|
||||||
|
|
||||||
@ -90,6 +90,12 @@ def mock_platform(app: App):
|
|||||||
from nonebot_bison.platform.platform import NewMessage
|
from nonebot_bison.platform.platform import NewMessage
|
||||||
from nonebot_bison.post import Post
|
from nonebot_bison.post import Post
|
||||||
from nonebot_bison.types import Category, RawPost, Tag, Target
|
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):
|
class MockPlatform(NewMessage):
|
||||||
|
|
||||||
@ -97,9 +103,9 @@ def mock_platform(app: App):
|
|||||||
name = "Mock Platform"
|
name = "Mock Platform"
|
||||||
enabled = True
|
enabled = True
|
||||||
is_common = True
|
is_common = True
|
||||||
schedule_interval = 10
|
|
||||||
enable_tag = True
|
enable_tag = True
|
||||||
has_target = True
|
has_target = True
|
||||||
|
scheduler_class = "mock"
|
||||||
categories = {
|
categories = {
|
||||||
Category(1): "转发",
|
Category(1): "转发",
|
||||||
Category(2): "视频",
|
Category(2): "视频",
|
||||||
@ -148,6 +154,12 @@ def mock_platform_no_target(app: App):
|
|||||||
from nonebot_bison.platform.platform import CategoryNotSupport, NewMessage
|
from nonebot_bison.platform.platform import CategoryNotSupport, NewMessage
|
||||||
from nonebot_bison.post import Post
|
from nonebot_bison.post import Post
|
||||||
from nonebot_bison.types import Category, RawPost, Tag, Target
|
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):
|
class MockPlatform(NewMessage):
|
||||||
|
|
||||||
@ -155,8 +167,7 @@ def mock_platform_no_target(app: App):
|
|||||||
name = "Mock Platform"
|
name = "Mock Platform"
|
||||||
enabled = True
|
enabled = True
|
||||||
is_common = True
|
is_common = True
|
||||||
schedule_type = "interval"
|
scheduler_class = "mock"
|
||||||
schedule_kw = {"seconds": 30}
|
|
||||||
enable_tag = True
|
enable_tag = True
|
||||||
has_target = False
|
has_target = False
|
||||||
categories = {Category(1): "转发", Category(2): "视频", Category(3): "不支持"}
|
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.platform.platform import NewMessage
|
||||||
from nonebot_bison.post import Post
|
from nonebot_bison.post import Post
|
||||||
from nonebot_bison.types import Category, RawPost, Tag, Target
|
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):
|
class MockPlatform(NewMessage):
|
||||||
|
|
||||||
platform_name = "mock_platform"
|
platform_name = "mock_platform"
|
||||||
name = "Mock Platform"
|
name = "Mock Platform"
|
||||||
enabled = True
|
enabled = True
|
||||||
schedule_type = "interval"
|
scheduler_class = "mock"
|
||||||
schedule_kw = {"seconds": 30}
|
|
||||||
is_common = True
|
is_common = True
|
||||||
enable_tag = True
|
enable_tag = True
|
||||||
has_target = False
|
has_target = False
|
||||||
@ -324,13 +340,13 @@ async def test_new_message_target_without_cats_tags(
|
|||||||
mock_platform_without_cats_tags, user_info_factory
|
mock_platform_without_cats_tags, user_info_factory
|
||||||
):
|
):
|
||||||
res1 = await mock_platform_without_cats_tags.fetch_new_post(
|
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
|
assert len(res1) == 0
|
||||||
res2 = await mock_platform_without_cats_tags.fetch_new_post(
|
res2 = await mock_platform_without_cats_tags.fetch_new_post(
|
||||||
"dummy",
|
"dummy",
|
||||||
[
|
[
|
||||||
user_info_factory(lambda _: [], lambda _: []),
|
user_info_factory([], []),
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
assert len(res2) == 1
|
assert len(res2) == 1
|
||||||
@ -342,16 +358,14 @@ async def test_new_message_target_without_cats_tags(
|
|||||||
|
|
||||||
@pytest.mark.asyncio
|
@pytest.mark.asyncio
|
||||||
async def test_new_message_target(mock_platform, user_info_factory):
|
async def test_new_message_target(mock_platform, user_info_factory):
|
||||||
res1 = await mock_platform.fetch_new_post(
|
res1 = await mock_platform.fetch_new_post("dummy", [user_info_factory([1, 2], [])])
|
||||||
"dummy", [user_info_factory(lambda _: [1, 2], lambda _: [])]
|
|
||||||
)
|
|
||||||
assert len(res1) == 0
|
assert len(res1) == 0
|
||||||
res2 = await mock_platform.fetch_new_post(
|
res2 = await mock_platform.fetch_new_post(
|
||||||
"dummy",
|
"dummy",
|
||||||
[
|
[
|
||||||
user_info_factory(lambda _: [1, 2], lambda _: []),
|
user_info_factory([1, 2], []),
|
||||||
user_info_factory(lambda _: [1], lambda _: []),
|
user_info_factory([1], []),
|
||||||
user_info_factory(lambda _: [1, 2], lambda _: ["tag1"]),
|
user_info_factory([1, 2], ["tag1"]),
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
assert len(res2) == 3
|
assert len(res2) == 3
|
||||||
@ -372,15 +386,15 @@ async def test_new_message_target(mock_platform, user_info_factory):
|
|||||||
@pytest.mark.asyncio
|
@pytest.mark.asyncio
|
||||||
async def test_new_message_no_target(mock_platform_no_target, user_info_factory):
|
async def test_new_message_no_target(mock_platform_no_target, user_info_factory):
|
||||||
res1 = await mock_platform_no_target.fetch_new_post(
|
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
|
assert len(res1) == 0
|
||||||
res2 = await mock_platform_no_target.fetch_new_post(
|
res2 = await mock_platform_no_target.fetch_new_post(
|
||||||
"dummy",
|
"dummy",
|
||||||
[
|
[
|
||||||
user_info_factory(lambda _: [1, 2], lambda _: []),
|
user_info_factory([1, 2], []),
|
||||||
user_info_factory(lambda _: [1], lambda _: []),
|
user_info_factory([1], []),
|
||||||
user_info_factory(lambda _: [1, 2], lambda _: ["tag1"]),
|
user_info_factory([1, 2], ["tag1"]),
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
assert len(res2) == 3
|
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_2
|
||||||
assert "p2" in id_set_3
|
assert "p2" in id_set_3
|
||||||
res3 = await mock_platform_no_target.fetch_new_post(
|
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
|
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
|
@pytest.mark.asyncio
|
||||||
async def test_status_change(mock_status_change, user_info_factory):
|
async def test_status_change(mock_status_change, user_info_factory):
|
||||||
res1 = await mock_status_change.fetch_new_post(
|
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
|
assert len(res1) == 0
|
||||||
res2 = await mock_status_change.fetch_new_post(
|
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
|
assert len(res2) == 1
|
||||||
posts = res2[0][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(
|
res3 = await mock_status_change.fetch_new_post(
|
||||||
"dummy",
|
"dummy",
|
||||||
[
|
[
|
||||||
user_info_factory(lambda _: [1, 2], lambda _: []),
|
user_info_factory([1, 2], []),
|
||||||
user_info_factory(lambda _: [1], lambda _: []),
|
user_info_factory([1], []),
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
assert len(res3) == 2
|
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 res3[0][1][0].text == "off"
|
||||||
assert len(res3[1][1]) == 0
|
assert len(res3[1][1]) == 0
|
||||||
res4 = await mock_status_change.fetch_new_post(
|
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
|
assert len(res4) == 0
|
||||||
|
|
||||||
@ -445,19 +459,13 @@ async def test_group(
|
|||||||
from nonebot_bison.types import Category, RawPost, Tag, Target
|
from nonebot_bison.types import Category, RawPost, Tag, Target
|
||||||
|
|
||||||
group_platform = NoTargetGroup([mock_platform_no_target, mock_platform_no_target_2])
|
group_platform = NoTargetGroup([mock_platform_no_target, mock_platform_no_target_2])
|
||||||
res1 = await group_platform.fetch_new_post(
|
res1 = await group_platform.fetch_new_post("dummy", [user_info_factory([1, 4], [])])
|
||||||
"dummy", [user_info_factory(lambda _: [1, 4], lambda _: [])]
|
|
||||||
)
|
|
||||||
assert len(res1) == 0
|
assert len(res1) == 0
|
||||||
res2 = await group_platform.fetch_new_post(
|
res2 = await group_platform.fetch_new_post("dummy", [user_info_factory([1, 4], [])])
|
||||||
"dummy", [user_info_factory(lambda _: [1, 4], lambda _: [])]
|
|
||||||
)
|
|
||||||
assert len(res2) == 1
|
assert len(res2) == 1
|
||||||
posts = res2[0][1]
|
posts = res2[0][1]
|
||||||
assert len(posts) == 2
|
assert len(posts) == 2
|
||||||
id_set_2 = set(map(lambda x: x.text, posts))
|
id_set_2 = set(map(lambda x: x.text, posts))
|
||||||
assert "p2" in id_set_2 and "p6" in id_set_2
|
assert "p2" in id_set_2 and "p6" in id_set_2
|
||||||
res3 = await group_platform.fetch_new_post(
|
res3 = await group_platform.fetch_new_post("dummy", [user_info_factory([1, 4], [])])
|
||||||
"dummy", [user_info_factory(lambda _: [1, 4], lambda _: [])]
|
|
||||||
)
|
|
||||||
assert len(res3) == 0
|
assert len(res3) == 0
|
||||||
|
@ -272,7 +272,7 @@ async def test_abort_add_on_tag(app: App, db_migration):
|
|||||||
|
|
||||||
# 删除订阅阶段中止
|
# 删除订阅阶段中止
|
||||||
@pytest.mark.asyncio
|
@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.bot import Bot
|
||||||
from nonebot.adapters.onebot.v11.message import Message
|
from nonebot.adapters.onebot.v11.message import Message
|
||||||
from nonebot_bison.config import config
|
from nonebot_bison.config import config
|
||||||
|
@ -58,7 +58,7 @@ async def test_configurable_at_me_false(app: App):
|
|||||||
|
|
||||||
@pytest.mark.asyncio
|
@pytest.mark.asyncio
|
||||||
@respx.mock
|
@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.event import Sender
|
||||||
from nonebot.adapters.onebot.v11.message import Message
|
from nonebot.adapters.onebot.v11.message import Message
|
||||||
from nonebot_bison.config import config
|
from nonebot_bison.config import config
|
||||||
@ -173,7 +173,7 @@ async def test_add_with_target(app: App, db_migration):
|
|||||||
|
|
||||||
@pytest.mark.asyncio
|
@pytest.mark.asyncio
|
||||||
@respx.mock
|
@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.event import Sender
|
||||||
from nonebot.adapters.onebot.v11.message import Message
|
from nonebot.adapters.onebot.v11.message import Message
|
||||||
from nonebot_bison.config import config
|
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
|
@pytest.mark.asyncio
|
||||||
@respx.mock
|
@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.event import Sender
|
||||||
from nonebot.adapters.onebot.v11.message import Message
|
from nonebot.adapters.onebot.v11.message import Message
|
||||||
from nonebot_bison.config import config
|
from nonebot_bison.config import config
|
||||||
@ -397,7 +397,7 @@ async def test_add_with_get_id(app: App, db_migration):
|
|||||||
|
|
||||||
@pytest.mark.asyncio
|
@pytest.mark.asyncio
|
||||||
@respx.mock
|
@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.event import Sender
|
||||||
from nonebot.adapters.onebot.v11.message import Message
|
from nonebot.adapters.onebot.v11.message import Message
|
||||||
from nonebot_bison.config import config
|
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")
|
subs = await config.list_subscribe(10000, "group")
|
||||||
assert len(subs) == 1
|
assert len(subs) == 1
|
||||||
sub = subs[0]
|
sub = subs[0]
|
||||||
assert sub["target"] == "161775300"
|
assert sub.target.target == "161775300"
|
||||||
assert sub["tags"] == []
|
assert sub.tags == []
|
||||||
assert sub["cats"] == [platform_manager["bilibili"].reverse_category["视频"]]
|
assert sub.categories == [platform_manager["bilibili"].reverse_category["视频"]]
|
||||||
assert sub["target_type"] == "bilibili"
|
assert sub.target.platform_name == "bilibili"
|
||||||
assert sub["target_name"] == "明日方舟"
|
assert sub.target.target_name == "明日方舟"
|
||||||
|
@ -8,7 +8,7 @@ from .utils import fake_admin_user, fake_group_message_event
|
|||||||
|
|
||||||
|
|
||||||
@pytest.mark.asyncio
|
@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.adapters.onebot.v11.message import Message
|
||||||
from nonebot_bison.config import config
|
from nonebot_bison.config import config
|
||||||
from nonebot_bison.config_manager import query_sub_matcher
|
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
|
@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.bot import Bot
|
||||||
from nonebot.adapters.onebot.v11.message import Message
|
from nonebot.adapters.onebot.v11.message import Message
|
||||||
from nonebot_bison.config import config
|
from nonebot_bison.config import config
|
||||||
@ -86,15 +86,13 @@ async def test_del_sub(app: App, db_migration):
|
|||||||
|
|
||||||
|
|
||||||
@pytest.mark.asyncio
|
@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.bot import Bot
|
||||||
from nonebot.adapters.onebot.v11.message import Message
|
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.config_manager import del_sub_matcher
|
||||||
from nonebot_bison.platform import platform_manager
|
from nonebot_bison.platform import platform_manager
|
||||||
|
|
||||||
config = Config()
|
|
||||||
config.user_target.truncate()
|
|
||||||
async with app.test_matcher(del_sub_matcher) as ctx:
|
async with app.test_matcher(del_sub_matcher) as ctx:
|
||||||
bot = ctx.create_bot(base=Bot)
|
bot = ctx.create_bot(base=Bot)
|
||||||
assert isinstance(bot, Bot)
|
assert isinstance(bot, Bot)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user