add more tests

This commit is contained in:
yanyongyu 2022-01-30 11:04:02 +08:00
parent f3cc93c699
commit 2cd6867bd1
No known key found for this signature in database
GPG Key ID: 796D8A7FB73396EB
2 changed files with 47 additions and 7 deletions

View File

@ -115,7 +115,7 @@ class Message(List[TMS], abc.ABC):
elif isinstance(message, Iterable):
self.extend(message)
else:
self.extend(self._construct(message))
self.extend(self._construct(message)) # pragma: no cover
@classmethod
def template(cls: Type[TM], format_string: Union[str, TM]) -> MessageTemplate[TM]:
@ -166,6 +166,8 @@ class Message(List[TMS], abc.ABC):
def _validate(cls, value):
if isinstance(value, cls):
return value
elif isinstance(value, Message):
raise ValueError(f"Type {type(value)} can not be converted to {cls}")
elif isinstance(value, str):
pass
elif isinstance(value, dict):
@ -201,7 +203,7 @@ class Message(List[TMS], abc.ABC):
elif isinstance(other, Iterable):
self.extend(other)
else:
raise ValueError(f"Unsupported type: {type(other)}")
raise ValueError(f"Unsupported type: {type(other)}") # pragma: no cover
return self
@overload
@ -276,7 +278,7 @@ class Message(List[TMS], abc.ABC):
elif isinstance(arg1, str) and isinstance(arg2, slice):
return self.__class__([seg for seg in self if seg.type == arg1][arg2])
else:
raise ValueError("Incorrect arguments to slice")
raise ValueError("Incorrect arguments to slice") # pragma: no cover
def index(self, value: Union[TMS, str], *args) -> int:
if isinstance(value, str):
@ -314,7 +316,7 @@ class Message(List[TMS], abc.ABC):
elif isinstance(obj, str):
self.extend(self._construct(obj))
else:
raise ValueError(f"Unexpected type: {type(obj)} {obj}")
raise ValueError(f"Unexpected type: {type(obj)} {obj}") # pragma: no cover
return self
def extend(self: TM, obj: Union[TM, Iterable[TMS]]) -> TM:

View File

@ -39,6 +39,26 @@ def test_segment_validate():
assert True
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
def test_message_add():
Message = make_fake_message()
MessageSegment = Message.get_segment_class()
@ -55,6 +75,10 @@ def test_message_add():
[MessageSegment.text("text")]
) == Message([MessageSegment.text("text"), MessageSegment.text("text")])
assert "text" + Message([MessageSegment.text("text")]) == Message(
[MessageSegment.text("text"), MessageSegment.text("text")]
)
msg = Message([MessageSegment.text("text")])
msg += MessageSegment.text("text")
assert msg == Message([MessageSegment.text("text"), MessageSegment.text("text")])
@ -101,15 +125,29 @@ def test_message_validate():
Message = make_fake_message()
MessageSegment = Message.get_segment_class()
Message_ = make_fake_message()
assert parse_obj_as(Message, Message([])) == Message([])
try:
parse_obj_as(Message, Message_([]))
assert False
except ValidationError:
assert True
assert parse_obj_as(Message, "text") == Message([MessageSegment.text("text")])
assert parse_obj_as(Message, {"type": "text", "data": {"text": "text"}}) == Message(
[MessageSegment.text("text")]
)
assert parse_obj_as(
Message, [{"type": "text", "data": {"text": "text"}}]
) == Message([MessageSegment.text("text")])
assert (
parse_obj_as(
Message,
[MessageSegment.text("text"), {"type": "text", "data": {"text": "text"}}],
)
== Message([MessageSegment.text("text"), MessageSegment.text("text")])
)
try:
parse_obj_as(Message, object())