update dataclass encoder tests

This commit is contained in:
yanyongyu 2022-02-17 15:35:39 +08:00
parent 9c0e05c615
commit 1d6a333b49
No known key found for this signature in database
GPG Key ID: 796D8A7FB73396EB
2 changed files with 9 additions and 15 deletions

View File

@ -6,14 +6,14 @@ from utils import make_fake_message
def test_dataclass_encoder():
from nonebot.utils import DataclassEncoder
MessageSegment = make_fake_message().get_segment_class()
ms = MessageSegment.node_custom(
"1234", "testtest", "test" + MessageSegment.image("url")
)
simple = json.dumps("123", cls=DataclassEncoder)
assert simple == '"123"'
Message = make_fake_message()
MessageSegment = Message.get_segment_class()
ms = MessageSegment.nested(Message(MessageSegment.text("text")))
s = json.dumps(ms, cls=DataclassEncoder)
assert (
s
== '{"type": "node", "data": {"user_id": "1234", "nickname": "testtest", "content": [{"type": "text", "data": {"text": "test"}}, {"type": "image", "data": {"url": "url"}}]}}'
== '{"type": "node", "data": {"content": [{"type": "text", "data": {"text": "text"}}]}}'
)
s1 = json.dumps("123", cls=DataclassEncoder)
assert s1 == '"123"'

View File

@ -33,14 +33,8 @@ def make_fake_message():
return FakeMessageSegment("image", {"url": url})
@staticmethod
def node_custom(
user_id: str,
nickname: str,
content: Union["FakeMessage", "FakeMessageSegment", str],
):
return FakeMessageSegment(
"node", {"user_id": user_id, "nickname": nickname, "content": content}
)
def nested(content: "FakeMessage"):
return FakeMessageSegment("node", {"content": content})
def is_text(self) -> bool:
return self.type == "text"