From 1592ee82a687ed6500bad47bafa8eac285d400c7 Mon Sep 17 00:00:00 2001 From: UnCLAS-Prommer Date: Fri, 13 Mar 2026 16:19:49 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20pytest=E6=9B=B4=E6=96=B0=EF=BC=9Bsession?= =?UTF-8?q?=5Fmessage=E7=9B=B8=E5=85=B3test=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pytests/message_test/session_message_test.py | 24 ++++++++++---------- pytests/utils_test/message_utils_test.py | 20 ++++++---------- 2 files changed, 19 insertions(+), 25 deletions(-) diff --git a/pytests/message_test/session_message_test.py b/pytests/message_test/session_message_test.py index aab8971a..d30a4f0b 100644 --- a/pytests/message_test/session_message_test.py +++ b/pytests/message_test/session_message_test.py @@ -175,7 +175,7 @@ def load_message_via_file(monkeypatch): @pytest.mark.asyncio async def test_process(monkeypatch): load_message_via_file(monkeypatch) - msg = SessionMessage("msg123", datetime.now()) + msg = SessionMessage("msg123", datetime.now(), platform="test_platform") msg.session_id = "session123" msg.raw_message = MessageSequence(components=[]) msg.raw_message.components = [TextComponent("Hello, world!")] @@ -186,7 +186,7 @@ async def test_process(monkeypatch): @pytest.mark.asyncio async def test_multiple_text(monkeypatch): load_message_via_file(monkeypatch) - msg = SessionMessage("msg123", datetime.now()) + msg = SessionMessage("msg123", datetime.now(), platform="test_platform") msg.session_id = "session123" msg.raw_message = MessageSequence(components=[]) msg.raw_message.components = [TextComponent("Hello,"), TextComponent("world!")] @@ -197,7 +197,7 @@ async def test_multiple_text(monkeypatch): @pytest.mark.asyncio async def test_image(monkeypatch): load_message_via_file(monkeypatch) - msg = SessionMessage("msg123", datetime.now()) + msg = SessionMessage("msg123", datetime.now(), platform="test_platform") msg.session_id = "session123" msg.raw_message = MessageSequence(components=[]) msg.raw_message.components = [ImageComponent(binary_hash="image_hash"), TextComponent("Hello, world!")] @@ -208,7 +208,7 @@ async def test_image(monkeypatch): @pytest.mark.asyncio async def test_emoji(monkeypatch): load_message_via_file(monkeypatch) - msg = SessionMessage("msg123", datetime.now()) + msg = SessionMessage("msg123", datetime.now(), platform="test_platform") msg.session_id = "session123" msg.raw_message = MessageSequence(components=[]) msg.raw_message.components = [EmojiComponent(binary_hash="emoji_hash"), TextComponent("Hello, world!")] @@ -219,7 +219,7 @@ async def test_emoji(monkeypatch): @pytest.mark.asyncio async def test_voice(monkeypatch): load_message_via_file(monkeypatch) - msg = SessionMessage("msg123", datetime.now()) + msg = SessionMessage("msg123", datetime.now(), platform="test_platform") msg.session_id = "session123" msg.raw_message = MessageSequence(components=[]) msg.raw_message.components = [VoiceComponent(binary_hash="voice_hash"), TextComponent("Hello, world!")] @@ -230,7 +230,7 @@ async def test_voice(monkeypatch): @pytest.mark.asyncio async def test_at_component(monkeypatch): load_message_via_file(monkeypatch) - msg = SessionMessage("msg123", datetime.now()) + msg = SessionMessage("msg123", datetime.now(), platform="test_platform") msg.session_id = "session123" msg.platform = "test_platform" msg.raw_message = MessageSequence(components=[]) @@ -242,7 +242,7 @@ async def test_at_component(monkeypatch): @pytest.mark.asyncio async def test_reply_component_fail_to_fetch(monkeypatch): load_message_via_file(monkeypatch) - msg = SessionMessage("msg123", datetime.now()) + msg = SessionMessage("msg123", datetime.now(), platform="test_platform") msg.session_id = "session123" msg.platform = "test_platform" msg.raw_message = MessageSequence(components=[]) @@ -269,7 +269,7 @@ async def test_reply_component_success(monkeypatch): return DummyRecord() module_msg.get_db_session = lambda: DummyDBSessionWithReply() - msg = SessionMessage("msg123", datetime.now()) + msg = SessionMessage("msg123", datetime.now(), platform="test_platform") msg.session_id = "session123" msg.platform = "test_platform" msg.raw_message = MessageSequence(components=[]) @@ -287,7 +287,7 @@ async def test_reply_component_with_db_fail(monkeypatch): raise Exception("数据库查询失败") module_msg.get_db_session = lambda: DummyDBSessionWithError() - msg = SessionMessage("msg123", datetime.now()) + msg = SessionMessage("msg123", datetime.now(), platform="test_platform") msg.session_id = "session123" msg.platform = "test_platform" msg.raw_message = MessageSequence(components=[]) @@ -300,7 +300,7 @@ async def test_reply_component_with_db_fail(monkeypatch): @pytest.mark.asyncio async def test_forward_component(monkeypatch): load_message_via_file(monkeypatch) - msg = SessionMessage("msg123", datetime.now()) + msg = SessionMessage("msg123", datetime.now(), platform="test_platform") msg.session_id = "session123" msg.platform = "test_platform" msg.raw_message = MessageSequence(components=[]) @@ -337,7 +337,7 @@ async def test_forward_component(monkeypatch): @pytest.mark.asyncio async def test_forward_with_reply(monkeypatch): load_message_via_file(monkeypatch) - msg = SessionMessage("msg123", datetime.now()) + msg = SessionMessage("msg123", datetime.now(), platform="test_platform") msg.session_id = "session123" msg.platform = "test_platform" msg.raw_message = MessageSequence(components=[]) @@ -375,7 +375,7 @@ async def test_forward_with_reply(monkeypatch): @pytest.mark.asyncio async def test_multiple_reply_with_delay_in_forward(monkeypatch): load_message_via_file(monkeypatch) - msg = SessionMessage("msg123", datetime.now()) + msg = SessionMessage("msg123", datetime.now(), platform="test_platform") msg.session_id = "session123" msg.platform = "test_platform" msg.raw_message = MessageSequence(components=[]) diff --git a/pytests/utils_test/message_utils_test.py b/pytests/utils_test/message_utils_test.py index fba99324..19ab8ac3 100644 --- a/pytests/utils_test/message_utils_test.py +++ b/pytests/utils_test/message_utils_test.py @@ -250,7 +250,7 @@ async def test_build_readable_message_basic(monkeypatch): utils_module = load_utils_via_file(monkeypatch) MessageUtils = utils_module.MessageUtils - msg = SessionMessage("m1", datetime.now()) + msg = SessionMessage("m1", datetime.now(), platform="test") msg.platform = "test" msg.session_id = "s_test" user_info = UserInfo(user_id="u1", user_nickname="Alice") @@ -268,8 +268,7 @@ async def test_build_readable_message_anonymize(monkeypatch): utils_module = load_utils_via_file(monkeypatch) MessageUtils = utils_module.MessageUtils - msg = SessionMessage("m2", datetime.now()) - msg.platform = "test" + msg = SessionMessage("m2", datetime.now(), platform="test") msg.session_id = "s_test" user_info = UserInfo(user_id="u42", user_nickname="Bob") msg.message_info = MessageInfo(user_info=user_info) @@ -289,8 +288,7 @@ async def test_build_readable_message_replace_bot(monkeypatch): utils_module = load_utils_via_file(monkeypatch) MessageUtils = utils_module.MessageUtils - msg = SessionMessage("m3", datetime.now()) - msg.platform = "test" + msg = SessionMessage("m3", datetime.now(), platform="test") msg.session_id = "s_test" user_info = UserInfo(user_id="bot_self", user_nickname="SomeBot") msg.message_info = MessageInfo(user_info=user_info) @@ -308,8 +306,7 @@ async def test_build_readable_message_image_extraction(monkeypatch): # 构建包含图片组件的消息 img = ImageComponent(binary_hash="h", binary_data=b"\x01\x02", content="Img") - msg = SessionMessage("mi1", datetime.now()) - msg.platform = "test" + msg = SessionMessage("mi1", datetime.now(), platform="test") msg.session_id = "s_img" msg.raw_message = MessageSequence([img]) msg.message_info = MessageInfo(UserInfo(user_id="ui_img", user_nickname="ImgUser")) @@ -327,11 +324,9 @@ async def test_build_readable_message_anonymize_and_replace_bot_name_and_lineno( utils_module = load_utils_via_file(monkeypatch) MessageUtils = utils_module.MessageUtils # 构建多个消息 - msg1 = SessionMessage("m4", datetime.now()) - msg1.platform = "test" + msg1 = SessionMessage("m4", datetime.now(), platform="test") msg1.session_id = "s_comb" - msg2 = SessionMessage("m5", datetime.now()) - msg2.platform = "test" + msg2 = SessionMessage("m5", datetime.now(), platform="test") msg2.session_id = "s_comb" msg1.message_info = MessageInfo(UserInfo(user_id="u_comb", user_nickname="Charlie")) msg2.message_info = MessageInfo(UserInfo(user_id="bot_self", user_nickname="SomeBot")) @@ -361,8 +356,7 @@ async def test_build_readable_message_with_at(monkeypatch): # 构建包含回复组件的消息 at_comp = AtComponent(target_user_id="u_at", target_user_nickname="AtUser") - msg = SessionMessage("m_at", datetime.now()) - msg.platform = "test" + msg = SessionMessage("m_at", datetime.now(), platform="test") msg.session_id = "s_at" msg.raw_message = MessageSequence([at_comp]) msg.message_info = MessageInfo(UserInfo(user_id="u_main", user_nickname="MainUser"))