2022-06-20 15:52:12 +08:00
|
|
|
import pytest
|
2024-01-26 11:12:57 +08:00
|
|
|
from pydantic import ValidationError
|
2022-01-30 00:05:01 +08:00
|
|
|
|
2024-01-26 11:12:57 +08:00
|
|
|
from nonebot.compat import type_validate_python
|
2023-06-19 17:48:59 +08:00
|
|
|
from utils import FakeMessage, FakeMessageSegment
|
2024-01-26 11:12:57 +08:00
|
|
|
from nonebot.adapters import Message, MessageSegment
|
2022-01-17 00:29:09 +08:00
|
|
|
|
|
|
|
|
2023-06-19 17:48:59 +08:00
|
|
|
def test_segment_data():
|
|
|
|
assert len(FakeMessageSegment.text("text")) == 4
|
|
|
|
assert FakeMessageSegment.text("text").get("data") == {"text": "text"}
|
|
|
|
assert list(FakeMessageSegment.text("text").keys()) == ["type", "data"]
|
|
|
|
assert list(FakeMessageSegment.text("text").values()) == ["text", {"text": "text"}]
|
|
|
|
assert list(FakeMessageSegment.text("text").items()) == [
|
|
|
|
("type", "text"),
|
|
|
|
("data", {"text": "text"}),
|
|
|
|
]
|
2022-01-29 23:39:13 +08:00
|
|
|
|
2023-06-19 17:48:59 +08:00
|
|
|
|
|
|
|
def test_segment_equal():
|
|
|
|
assert FakeMessageSegment("text", {"text": "text"}) == FakeMessageSegment(
|
|
|
|
"text", {"text": "text"}
|
|
|
|
)
|
|
|
|
assert FakeMessageSegment("text", {"text": "text"}) != FakeMessageSegment(
|
|
|
|
"text", {"text": "other"}
|
2022-01-29 23:39:13 +08:00
|
|
|
)
|
2023-06-19 17:48:59 +08:00
|
|
|
assert FakeMessageSegment("text", {"text": "text"}) != FakeMessageSegment(
|
|
|
|
"other", {"text": "text"}
|
|
|
|
)
|
|
|
|
|
2022-01-29 23:39:13 +08:00
|
|
|
|
2023-06-19 17:48:59 +08:00
|
|
|
def test_segment_add():
|
|
|
|
assert FakeMessageSegment.text("text") + FakeMessageSegment.text(
|
|
|
|
"text"
|
|
|
|
) == FakeMessage([FakeMessageSegment.text("text"), FakeMessageSegment.text("text")])
|
|
|
|
|
|
|
|
assert FakeMessageSegment.text("text") + "text" == FakeMessage(
|
|
|
|
[FakeMessageSegment.text("text"), FakeMessageSegment.text("text")]
|
2022-01-29 23:39:13 +08:00
|
|
|
)
|
|
|
|
|
2022-02-07 17:23:24 +08:00
|
|
|
assert (
|
2023-06-19 17:48:59 +08:00
|
|
|
FakeMessageSegment.text("text") + FakeMessage([FakeMessageSegment.text("text")])
|
|
|
|
) == FakeMessage([FakeMessageSegment.text("text"), FakeMessageSegment.text("text")])
|
2022-01-17 00:29:09 +08:00
|
|
|
|
2023-06-19 17:48:59 +08:00
|
|
|
assert "text" + FakeMessageSegment.text("text") == FakeMessage(
|
|
|
|
[FakeMessageSegment.text("text"), FakeMessageSegment.text("text")]
|
2022-01-29 23:39:13 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2022-01-30 00:05:01 +08:00
|
|
|
def test_segment_validate():
|
2024-01-26 11:12:57 +08:00
|
|
|
assert type_validate_python(
|
2023-06-19 17:48:59 +08:00
|
|
|
FakeMessageSegment,
|
2022-06-20 15:52:12 +08:00
|
|
|
{"type": "text", "data": {"text": "text"}, "extra": "should be ignored"},
|
2023-06-19 17:48:59 +08:00
|
|
|
) == FakeMessageSegment.text("text")
|
2024-01-26 11:12:57 +08:00
|
|
|
with pytest.raises(ValidationError):
|
|
|
|
type_validate_python(
|
|
|
|
type("FakeMessageSegment2", (MessageSegment,), {}),
|
|
|
|
FakeMessageSegment.text("text"),
|
|
|
|
)
|
2022-01-30 00:05:01 +08:00
|
|
|
|
2022-06-20 15:52:12 +08:00
|
|
|
with pytest.raises(ValidationError):
|
2024-01-26 11:12:57 +08:00
|
|
|
type_validate_python(FakeMessageSegment, "some str")
|
2022-06-20 15:52:12 +08:00
|
|
|
|
|
|
|
with pytest.raises(ValidationError):
|
2024-01-26 11:12:57 +08:00
|
|
|
type_validate_python(FakeMessageSegment, {"data": {}})
|
2022-01-30 00:05:01 +08:00
|
|
|
|
|
|
|
|
2023-04-04 21:42:01 +08:00
|
|
|
def test_segment_join():
|
2023-06-19 17:48:59 +08:00
|
|
|
seg = FakeMessageSegment.text("test")
|
2023-04-04 21:42:01 +08:00
|
|
|
iterable = [
|
2023-06-19 17:48:59 +08:00
|
|
|
FakeMessageSegment.text("first"),
|
|
|
|
FakeMessage(
|
|
|
|
[FakeMessageSegment.text("second"), FakeMessageSegment.text("third")]
|
|
|
|
),
|
2023-04-04 21:42:01 +08:00
|
|
|
]
|
|
|
|
|
2023-06-19 17:48:59 +08:00
|
|
|
assert seg.join(iterable) == FakeMessage(
|
2023-04-04 21:42:01 +08:00
|
|
|
[
|
2023-06-19 17:48:59 +08:00
|
|
|
FakeMessageSegment.text("first"),
|
|
|
|
FakeMessageSegment.text("test"),
|
|
|
|
FakeMessageSegment.text("second"),
|
|
|
|
FakeMessageSegment.text("third"),
|
2023-04-04 21:42:01 +08:00
|
|
|
]
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2023-06-19 17:48:59 +08:00
|
|
|
def test_segment_copy():
|
|
|
|
origin = FakeMessageSegment.text("text")
|
2022-01-30 11:04:02 +08:00
|
|
|
copy = origin.copy()
|
|
|
|
assert origin is not copy
|
|
|
|
assert origin == copy
|
|
|
|
|
|
|
|
|
2022-01-29 23:39:13 +08:00
|
|
|
def test_message_add():
|
2022-02-07 17:23:24 +08:00
|
|
|
assert (
|
2023-06-19 17:48:59 +08:00
|
|
|
FakeMessage([FakeMessageSegment.text("text")]) + FakeMessageSegment.text("text")
|
|
|
|
) == FakeMessage([FakeMessageSegment.text("text"), FakeMessageSegment.text("text")])
|
2022-01-17 00:29:09 +08:00
|
|
|
|
2023-06-19 17:48:59 +08:00
|
|
|
assert FakeMessage([FakeMessageSegment.text("text")]) + "text" == FakeMessage(
|
|
|
|
[FakeMessageSegment.text("text"), FakeMessageSegment.text("text")]
|
2022-01-29 23:39:13 +08:00
|
|
|
)
|
2022-01-17 00:29:09 +08:00
|
|
|
|
2022-02-07 17:23:24 +08:00
|
|
|
assert (
|
2023-06-19 17:48:59 +08:00
|
|
|
FakeMessage([FakeMessageSegment.text("text")])
|
|
|
|
+ FakeMessage([FakeMessageSegment.text("text")])
|
|
|
|
) == FakeMessage([FakeMessageSegment.text("text"), FakeMessageSegment.text("text")])
|
2022-01-17 00:29:09 +08:00
|
|
|
|
2023-06-19 17:48:59 +08:00
|
|
|
assert "text" + FakeMessage([FakeMessageSegment.text("text")]) == FakeMessage(
|
|
|
|
[FakeMessageSegment.text("text"), FakeMessageSegment.text("text")]
|
2022-01-30 11:04:02 +08:00
|
|
|
)
|
|
|
|
|
2023-06-19 17:48:59 +08:00
|
|
|
msg = FakeMessage([FakeMessageSegment.text("text")])
|
|
|
|
msg += FakeMessageSegment.text("text")
|
|
|
|
assert msg == FakeMessage(
|
|
|
|
[FakeMessageSegment.text("text"), FakeMessageSegment.text("text")]
|
|
|
|
)
|
2022-01-17 00:29:09 +08:00
|
|
|
|
|
|
|
|
2022-01-29 23:39:13 +08:00
|
|
|
def test_message_getitem():
|
2023-06-19 17:48:59 +08:00
|
|
|
message = FakeMessage(
|
2022-01-17 00:29:09 +08:00
|
|
|
[
|
2023-06-19 17:48:59 +08:00
|
|
|
FakeMessageSegment.text("test"),
|
|
|
|
FakeMessageSegment.image("test2"),
|
|
|
|
FakeMessageSegment.image("test3"),
|
|
|
|
FakeMessageSegment.text("test4"),
|
2022-01-17 00:29:09 +08:00
|
|
|
]
|
|
|
|
)
|
|
|
|
|
2023-06-19 17:48:59 +08:00
|
|
|
assert message[0] == FakeMessageSegment.text("test")
|
2022-01-17 00:29:09 +08:00
|
|
|
|
2023-06-19 17:48:59 +08:00
|
|
|
assert message[:2] == FakeMessage(
|
|
|
|
[FakeMessageSegment.text("test"), FakeMessageSegment.image("test2")]
|
2022-01-17 00:29:09 +08:00
|
|
|
)
|
|
|
|
|
2023-06-19 17:48:59 +08:00
|
|
|
assert message["image"] == FakeMessage(
|
|
|
|
[FakeMessageSegment.image("test2"), FakeMessageSegment.image("test3")]
|
2022-01-17 00:29:09 +08:00
|
|
|
)
|
|
|
|
|
2023-06-19 17:48:59 +08:00
|
|
|
assert message["image", 0] == FakeMessageSegment.image("test2")
|
2022-01-17 00:29:09 +08:00
|
|
|
assert message["image", 0:2] == message["image"]
|
|
|
|
|
|
|
|
assert message.index(message[0]) == 0
|
|
|
|
assert message.index("image") == 1
|
|
|
|
|
|
|
|
assert message.get("image") == message["image"]
|
|
|
|
assert message.get("image", 114514) == message["image"]
|
2023-06-19 17:48:59 +08:00
|
|
|
assert message.get("image", 1) == FakeMessage([message["image", 0]])
|
2022-01-17 00:29:09 +08:00
|
|
|
|
|
|
|
assert message.count("image") == 2
|
2022-01-30 00:05:01 +08:00
|
|
|
|
|
|
|
|
|
|
|
def test_message_validate():
|
2024-01-26 11:12:57 +08:00
|
|
|
assert type_validate_python(FakeMessage, FakeMessage([])) == FakeMessage([])
|
2022-01-30 11:04:02 +08:00
|
|
|
|
2022-06-20 15:52:12 +08:00
|
|
|
with pytest.raises(ValidationError):
|
2024-01-26 11:12:57 +08:00
|
|
|
type_validate_python(type("FakeMessage2", (Message,), {}), FakeMessage([]))
|
2022-01-30 00:05:01 +08:00
|
|
|
|
2024-01-26 11:12:57 +08:00
|
|
|
assert type_validate_python(FakeMessage, "text") == FakeMessage(
|
2023-06-19 17:48:59 +08:00
|
|
|
[FakeMessageSegment.text("text")]
|
2022-01-30 00:05:01 +08:00
|
|
|
)
|
|
|
|
|
2024-01-26 11:12:57 +08:00
|
|
|
assert type_validate_python(
|
2023-06-19 17:48:59 +08:00
|
|
|
FakeMessage, {"type": "text", "data": {"text": "text"}}
|
|
|
|
) == FakeMessage([FakeMessageSegment.text("text")])
|
|
|
|
|
2024-01-26 11:12:57 +08:00
|
|
|
assert type_validate_python(
|
2023-06-19 17:48:59 +08:00
|
|
|
FakeMessage,
|
|
|
|
[FakeMessageSegment.text("text"), {"type": "text", "data": {"text": "text"}}],
|
|
|
|
) == FakeMessage([FakeMessageSegment.text("text"), FakeMessageSegment.text("text")])
|
2022-01-30 00:05:01 +08:00
|
|
|
|
2022-06-20 15:52:12 +08:00
|
|
|
with pytest.raises(ValidationError):
|
2024-01-26 11:12:57 +08:00
|
|
|
type_validate_python(FakeMessage, object())
|
2023-04-04 21:42:01 +08:00
|
|
|
|
|
|
|
|
|
|
|
def test_message_contains():
|
2023-06-19 17:48:59 +08:00
|
|
|
message = FakeMessage(
|
2023-04-04 21:42:01 +08:00
|
|
|
[
|
2023-06-19 17:48:59 +08:00
|
|
|
FakeMessageSegment.text("test"),
|
|
|
|
FakeMessageSegment.image("test2"),
|
|
|
|
FakeMessageSegment.image("test3"),
|
|
|
|
FakeMessageSegment.text("test4"),
|
2023-04-04 21:42:01 +08:00
|
|
|
]
|
|
|
|
)
|
|
|
|
|
2023-06-19 17:48:59 +08:00
|
|
|
assert message.has(FakeMessageSegment.text("test")) is True
|
|
|
|
assert FakeMessageSegment.text("test") in message
|
2023-04-04 21:42:01 +08:00
|
|
|
assert message.has("image") is True
|
|
|
|
assert "image" in message
|
|
|
|
|
2023-06-19 17:48:59 +08:00
|
|
|
assert message.has(FakeMessageSegment.text("foo")) is False
|
|
|
|
assert FakeMessageSegment.text("foo") not in message
|
2023-04-04 21:42:01 +08:00
|
|
|
assert message.has("foo") is False
|
|
|
|
assert "foo" not in message
|
|
|
|
|
2024-02-12 17:53:50 +08:00
|
|
|
assert not bool(FakeMessageSegment.text(""))
|
|
|
|
msg_with_empty_seg = FakeMessage([FakeMessageSegment.text("")])
|
|
|
|
assert msg_with_empty_seg.has("text") is True
|
|
|
|
assert "text" in msg_with_empty_seg
|
|
|
|
|
2023-04-04 21:42:01 +08:00
|
|
|
|
|
|
|
def test_message_only():
|
2023-06-19 17:48:59 +08:00
|
|
|
message = FakeMessage(
|
2023-04-04 21:42:01 +08:00
|
|
|
[
|
2023-06-19 17:48:59 +08:00
|
|
|
FakeMessageSegment.text("test"),
|
|
|
|
FakeMessageSegment.text("test2"),
|
2023-04-04 21:42:01 +08:00
|
|
|
]
|
|
|
|
)
|
|
|
|
|
|
|
|
assert message.only("text") is True
|
2023-06-19 17:48:59 +08:00
|
|
|
assert message.only(FakeMessageSegment.text("test")) is False
|
2023-04-04 21:42:01 +08:00
|
|
|
|
2023-06-19 17:48:59 +08:00
|
|
|
message = FakeMessage(
|
2023-04-04 21:42:01 +08:00
|
|
|
[
|
2023-06-19 17:48:59 +08:00
|
|
|
FakeMessageSegment.text("test"),
|
|
|
|
FakeMessageSegment.image("test2"),
|
|
|
|
FakeMessageSegment.image("test3"),
|
|
|
|
FakeMessageSegment.text("test4"),
|
2023-04-04 21:42:01 +08:00
|
|
|
]
|
|
|
|
)
|
|
|
|
|
|
|
|
assert message.only("text") is False
|
|
|
|
|
2023-06-19 17:48:59 +08:00
|
|
|
message = FakeMessage(
|
2023-04-04 21:42:01 +08:00
|
|
|
[
|
2023-06-19 17:48:59 +08:00
|
|
|
FakeMessageSegment.text("test"),
|
|
|
|
FakeMessageSegment.text("test"),
|
2023-04-04 21:42:01 +08:00
|
|
|
]
|
|
|
|
)
|
|
|
|
|
2023-06-19 17:48:59 +08:00
|
|
|
assert message.only(FakeMessageSegment.text("test")) is True
|
2023-04-04 21:42:01 +08:00
|
|
|
|
|
|
|
|
|
|
|
def test_message_join():
|
2023-06-19 17:48:59 +08:00
|
|
|
msg = FakeMessage([FakeMessageSegment.text("test")])
|
2023-04-04 21:42:01 +08:00
|
|
|
iterable = [
|
2023-06-19 17:48:59 +08:00
|
|
|
FakeMessageSegment.text("first"),
|
|
|
|
FakeMessage(
|
|
|
|
[FakeMessageSegment.text("second"), FakeMessageSegment.text("third")]
|
|
|
|
),
|
2023-04-04 21:42:01 +08:00
|
|
|
]
|
|
|
|
|
2023-06-19 17:48:59 +08:00
|
|
|
assert msg.join(iterable) == FakeMessage(
|
2023-04-04 21:42:01 +08:00
|
|
|
[
|
2023-06-19 17:48:59 +08:00
|
|
|
FakeMessageSegment.text("first"),
|
|
|
|
FakeMessageSegment.text("test"),
|
|
|
|
FakeMessageSegment.text("second"),
|
|
|
|
FakeMessageSegment.text("third"),
|
2023-04-04 21:42:01 +08:00
|
|
|
]
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def test_message_include():
|
2023-06-19 17:48:59 +08:00
|
|
|
message = FakeMessage(
|
2023-04-04 21:42:01 +08:00
|
|
|
[
|
2023-06-19 17:48:59 +08:00
|
|
|
FakeMessageSegment.text("test"),
|
|
|
|
FakeMessageSegment.image("test2"),
|
|
|
|
FakeMessageSegment.image("test3"),
|
|
|
|
FakeMessageSegment.text("test4"),
|
2023-04-04 21:42:01 +08:00
|
|
|
]
|
|
|
|
)
|
|
|
|
|
2023-06-19 17:48:59 +08:00
|
|
|
assert message.include("text") == FakeMessage(
|
2023-04-04 21:42:01 +08:00
|
|
|
[
|
2023-06-19 17:48:59 +08:00
|
|
|
FakeMessageSegment.text("test"),
|
|
|
|
FakeMessageSegment.text("test4"),
|
2023-04-04 21:42:01 +08:00
|
|
|
]
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def test_message_exclude():
|
2023-06-19 17:48:59 +08:00
|
|
|
message = FakeMessage(
|
2023-04-04 21:42:01 +08:00
|
|
|
[
|
2023-06-19 17:48:59 +08:00
|
|
|
FakeMessageSegment.text("test"),
|
|
|
|
FakeMessageSegment.image("test2"),
|
|
|
|
FakeMessageSegment.image("test3"),
|
|
|
|
FakeMessageSegment.text("test4"),
|
2023-04-04 21:42:01 +08:00
|
|
|
]
|
|
|
|
)
|
|
|
|
|
2023-06-19 17:48:59 +08:00
|
|
|
assert message.exclude("image") == FakeMessage(
|
2023-04-04 21:42:01 +08:00
|
|
|
[
|
2023-06-19 17:48:59 +08:00
|
|
|
FakeMessageSegment.text("test"),
|
|
|
|
FakeMessageSegment.text("test4"),
|
2023-04-04 21:42:01 +08:00
|
|
|
]
|
|
|
|
)
|