2022-01-30 00:05:01 +08:00
|
|
|
from pydantic import ValidationError, parse_obj_as
|
|
|
|
|
2022-01-17 00:29:09 +08:00
|
|
|
from utils import make_fake_message
|
|
|
|
|
|
|
|
|
2022-01-29 23:39:13 +08:00
|
|
|
def test_segment_add():
|
|
|
|
Message = make_fake_message()
|
|
|
|
MessageSegment = Message.get_segment_class()
|
|
|
|
|
|
|
|
assert MessageSegment.text("text") + MessageSegment.text("text") == Message(
|
|
|
|
[MessageSegment.text("text"), MessageSegment.text("text")]
|
|
|
|
)
|
|
|
|
|
|
|
|
assert MessageSegment.text("text") + "text" == Message(
|
|
|
|
[MessageSegment.text("text"), MessageSegment.text("text")]
|
|
|
|
)
|
|
|
|
|
2022-02-07 17:23:24 +08:00
|
|
|
assert (
|
|
|
|
MessageSegment.text("text") + Message([MessageSegment.text("text")])
|
2022-01-29 23:39:13 +08:00
|
|
|
) == Message([MessageSegment.text("text"), MessageSegment.text("text")])
|
2022-01-17 00:29:09 +08:00
|
|
|
|
2022-01-29 23:39:13 +08:00
|
|
|
assert "text" + MessageSegment.text("text") == Message(
|
|
|
|
[MessageSegment.text("text"), MessageSegment.text("text")]
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2022-01-30 00:05:01 +08:00
|
|
|
def test_segment_validate():
|
|
|
|
Message = make_fake_message()
|
|
|
|
MessageSegment = Message.get_segment_class()
|
|
|
|
|
|
|
|
assert parse_obj_as(
|
|
|
|
MessageSegment, {"type": "text", "data": {"text": "text"}}
|
|
|
|
) == MessageSegment.text("text")
|
|
|
|
|
|
|
|
try:
|
|
|
|
parse_obj_as(MessageSegment, "some str")
|
|
|
|
assert False
|
|
|
|
except ValidationError:
|
|
|
|
assert True
|
|
|
|
|
|
|
|
|
2022-01-30 11:04:02 +08:00
|
|
|
def test_segment():
|
|
|
|
Message = make_fake_message()
|
|
|
|
MessageSegment = Message.get_segment_class()
|
|
|
|
|
|
|
|
assert len(MessageSegment.text("text")) == 4
|
|
|
|
assert MessageSegment.text("text") != MessageSegment.text("other")
|
|
|
|
assert MessageSegment.text("text").get("data") == {"text": "text"}
|
|
|
|
assert list(MessageSegment.text("text").keys()) == ["type", "data"]
|
|
|
|
assert list(MessageSegment.text("text").values()) == ["text", {"text": "text"}]
|
|
|
|
assert list(MessageSegment.text("text").items()) == [
|
|
|
|
("type", "text"),
|
|
|
|
("data", {"text": "text"}),
|
|
|
|
]
|
|
|
|
|
|
|
|
origin = MessageSegment.text("text")
|
|
|
|
copy = origin.copy()
|
|
|
|
assert origin is not copy
|
|
|
|
assert origin == copy
|
|
|
|
|
|
|
|
|
2022-01-29 23:39:13 +08:00
|
|
|
def test_message_add():
|
2022-01-17 00:29:09 +08:00
|
|
|
Message = make_fake_message()
|
2022-01-29 23:39:13 +08:00
|
|
|
MessageSegment = Message.get_segment_class()
|
|
|
|
|
2022-02-07 17:23:24 +08:00
|
|
|
assert (
|
|
|
|
Message([MessageSegment.text("text")]) + MessageSegment.text("text")
|
2022-01-29 23:39:13 +08:00
|
|
|
) == Message([MessageSegment.text("text"), MessageSegment.text("text")])
|
2022-01-17 00:29:09 +08:00
|
|
|
|
2022-01-29 23:39:13 +08:00
|
|
|
assert Message([MessageSegment.text("text")]) + "text" == Message(
|
|
|
|
[MessageSegment.text("text"), MessageSegment.text("text")]
|
|
|
|
)
|
2022-01-17 00:29:09 +08:00
|
|
|
|
2022-02-07 17:23:24 +08:00
|
|
|
assert (
|
|
|
|
Message([MessageSegment.text("text")]) + Message([MessageSegment.text("text")])
|
2022-01-29 23:39:13 +08:00
|
|
|
) == Message([MessageSegment.text("text"), MessageSegment.text("text")])
|
2022-01-17 00:29:09 +08:00
|
|
|
|
2022-01-30 11:04:02 +08:00
|
|
|
assert "text" + Message([MessageSegment.text("text")]) == Message(
|
|
|
|
[MessageSegment.text("text"), MessageSegment.text("text")]
|
|
|
|
)
|
|
|
|
|
2022-01-29 23:39:13 +08:00
|
|
|
msg = Message([MessageSegment.text("text")])
|
|
|
|
msg += MessageSegment.text("text")
|
|
|
|
assert msg == Message([MessageSegment.text("text"), MessageSegment.text("text")])
|
2022-01-17 00:29:09 +08:00
|
|
|
|
|
|
|
|
2022-01-29 23:39:13 +08:00
|
|
|
def test_message_getitem():
|
2022-01-17 00:29:09 +08:00
|
|
|
|
|
|
|
Message = make_fake_message()
|
|
|
|
MessageSegment = Message.get_segment_class()
|
|
|
|
|
|
|
|
message = Message(
|
|
|
|
[
|
|
|
|
MessageSegment.text("test"),
|
|
|
|
MessageSegment.image("test2"),
|
|
|
|
MessageSegment.image("test3"),
|
|
|
|
MessageSegment.text("test4"),
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
|
|
|
assert message[0] == MessageSegment.text("test")
|
|
|
|
|
|
|
|
assert message[0:2] == Message(
|
|
|
|
[MessageSegment.text("test"), MessageSegment.image("test2")]
|
|
|
|
)
|
|
|
|
|
|
|
|
assert message["image"] == Message(
|
|
|
|
[MessageSegment.image("test2"), MessageSegment.image("test3")]
|
|
|
|
)
|
|
|
|
|
|
|
|
assert message["image", 0] == MessageSegment.image("test2")
|
|
|
|
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"]
|
|
|
|
assert message.get("image", 1) == Message([message["image", 0]])
|
|
|
|
|
|
|
|
assert message.count("image") == 2
|
2022-01-30 00:05:01 +08:00
|
|
|
|
|
|
|
|
|
|
|
def test_message_validate():
|
|
|
|
Message = make_fake_message()
|
|
|
|
MessageSegment = Message.get_segment_class()
|
|
|
|
|
2022-01-30 11:04:02 +08:00
|
|
|
Message_ = make_fake_message()
|
|
|
|
|
|
|
|
assert parse_obj_as(Message, Message([])) == Message([])
|
|
|
|
|
|
|
|
try:
|
|
|
|
parse_obj_as(Message, Message_([]))
|
|
|
|
assert False
|
|
|
|
except ValidationError:
|
|
|
|
assert True
|
|
|
|
|
2022-01-30 00:05:01 +08:00
|
|
|
assert parse_obj_as(Message, "text") == Message([MessageSegment.text("text")])
|
|
|
|
|
|
|
|
assert parse_obj_as(Message, {"type": "text", "data": {"text": "text"}}) == Message(
|
|
|
|
[MessageSegment.text("text")]
|
|
|
|
)
|
|
|
|
|
2022-02-12 16:15:06 +08:00
|
|
|
assert parse_obj_as(
|
|
|
|
Message,
|
|
|
|
[MessageSegment.text("text"), {"type": "text", "data": {"text": "text"}}],
|
|
|
|
) == Message([MessageSegment.text("text"), MessageSegment.text("text")])
|
2022-01-30 00:05:01 +08:00
|
|
|
|
|
|
|
try:
|
|
|
|
parse_obj_as(Message, object())
|
|
|
|
assert False
|
|
|
|
except ValidationError:
|
|
|
|
assert True
|