Skip to content

Commit

Permalink
Unit test WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
blueset committed Jul 11, 2018
1 parent 1b35bb6 commit bb84448
Show file tree
Hide file tree
Showing 6 changed files with 185 additions and 10 deletions.
2 changes: 1 addition & 1 deletion ehforwarderbot/chat.py
Expand Up @@ -54,7 +54,7 @@ def __init__(self, channel: Optional[EFBChannel]=None):
self.channel_id: str = channel.channel_id

self.chat_name: str = None
self.chat_type: ChatType = None
self.chat_type: ChatType = ChatType.Unknown
self.chat_alias: str = None
self.chat_uid: str = None
self.is_chat: bool = True
Expand Down
1 change: 1 addition & 0 deletions ehforwarderbot/constants.py
Expand Up @@ -53,6 +53,7 @@ class ChatType(Enum):
User = "User"
Group = "Group"
System = "System"
Unknown = "Unknown"


class TargetType(Enum):
Expand Down
19 changes: 11 additions & 8 deletions ehforwarderbot/message.py
Expand Up @@ -118,26 +118,29 @@ def verify(self):
raise ValueError("Author is not valid.")
else:
self.author.verify()
if self.author is None or not isinstance(self.author, EFBChat):
if self.chat is None or not isinstance(self.chat, EFBChat):
raise ValueError("Chat is not valid.")
else:
self.author.verify()
elif self.chat is not self.author: # Prevent repetitive verification
self.chat.verify()
if self.type is None or not isinstance(self.type, MsgType):
raise ValueError("Type is not valid.")
if self.deliver_to is None or not isinstance(self.deliver_to, EFBChannel):
raise ValueError("Deliver_to is not valid.")
if self.type in (MsgType.Audio, MsgType.File, MsgType.Image, MsgType.Sticker, MsgType.Video):
if self.file is None or not hasattr(self.file, "read") or not hasattr(self.file, "close"):
raise ValueError("File is not valid.")
if self.mime is None or not self.mime:
if self.mime is None or not self.mime or not isinstance(self.mime, str):
raise ValueError("MIME is not valid.")
if self.path is None or not self.path:
if self.path is None or not self.path or not isinstance(self.path, str):
raise ValueError("Path is not valid.")
if self.type == MsgType.Location and (self.attributes is None or not isinstance(self.attributes, EFBMsgLocationAttribute)):
if self.type == MsgType.Location and (self.attributes is None
or not isinstance(self.attributes, EFBMsgLocationAttribute)):
raise ValueError("Attribute of location message is invalid.")
if self.type == MsgType.Link and (self.attributes is None or not isinstance(self.attributes, EFBMsgLinkAttribute)):
if self.type == MsgType.Link and (self.attributes is None
or not isinstance(self.attributes, EFBMsgLinkAttribute)):
raise ValueError("Attribute of link message is invalid.")
if self.type == MsgType.Status and (self.attributes is None or not isinstance(self.attributes, EFBMsgStatusAttribute)):
if self.type == MsgType.Status and (self.attributes is None
or not isinstance(self.attributes, EFBMsgStatusAttribute)):
raise ValueError("Attribute of status message is invalid.")

if self.attributes:
Expand Down
33 changes: 33 additions & 0 deletions tests/test_chat.py
Expand Up @@ -39,3 +39,36 @@ def test_copy(self):
copy = chat.copy()
self.assertEqual(chat, copy)
self.assertIsNot(chat, copy)

def test_verify(self):
channel = MockMasterChannel()

with self.subTest("Valid chat"):
chat = EFBChat(channel)
chat.chat_uid = "00001"
chat.chat_name = "Chat"
chat.chat_alias = "chaT"
chat.chat_type = ChatType.User
chat.verify()

with self.subTest("Missing UID"):
chat = EFBChat(channel)
chat.chat_name = "Chat"
chat.chat_type = ChatType.User
with self.assertRaises(ValueError):
chat.verify()

with self.subTest("Missing name"):
chat = EFBChat(channel)
chat.chat_uid = "00001"
chat.chat_type = ChatType.User
with self.assertRaises(ValueError):
chat.verify()

with self.subTest("Wrong chat type"):
chat = EFBChat(channel)
chat.chat_uid = "00001"
chat.chat_name = "Chat"
chat.chat_type = "user"
with self.assertRaises(ValueError):
chat.verify()
2 changes: 1 addition & 1 deletion tests/test_extra_functions.py
Expand Up @@ -12,4 +12,4 @@ def test_get_extra_functions_list(self):
self.assertTrue(callable(echo))
self.assertTrue(hasattr(echo, 'name'))
self.assertTrue(hasattr(echo, 'desc'))
self.assertIn('{function_name}', echo.desc)
self.assertIn('{function_name}', echo.desc)
138 changes: 138 additions & 0 deletions tests/test_message.py
@@ -0,0 +1,138 @@
import unittest
from unittest import mock
from tempfile import NamedTemporaryFile

from ehforwarderbot import EFBMsg, EFBChat, ChatType, MsgType
from ehforwarderbot.message import EFBMsgLinkAttribute, EFBMsgLocationAttribute, EFBMsgStatusAttribute, EFBMsgCommands, \
EFBMsgCommand

from .mocks.master import MockMasterChannel


class EFBMessageTest(unittest.TestCase):
def setUp(self):
self.channel = MockMasterChannel()
self.chat = EFBChat(self.channel)
self.chat.chat_name = "Chat 0"
self.chat.chat_uid = "0"
self.chat.chat_type = ChatType.User

def test_basic_verify(self):
with self.subTest("Valid text message"):
msg = EFBMsg()
msg.deliver_to = self.channel
msg.author = self.chat
msg.chat = self.chat
msg.type = MsgType.Text
msg.text = "Message"
msg.verify()

for i in (MsgType.Image, MsgType.Audio, MsgType.File, MsgType.Sticker):
with self.subTest(f"Valid {i} message"), NamedTemporaryFile() as f:
msg = EFBMsg()
msg.deliver_to = self.channel
msg.author = self.chat
msg.chat = self.chat
msg.type = i
msg.file = f
msg.filename = "test.bin"
msg.path = f.name
msg.mime = "application/octet-stream"
msg.verify()

with self.subTest("Missing deliver_to"), self.assertRaises(ValueError):
msg = EFBMsg()
msg.author = self.chat
msg.chat = self.chat
msg.type = MsgType.Text
msg.text = "Message"
msg.verify()

with self.subTest("Missing author"), self.assertRaises(ValueError):
msg = EFBMsg()
msg.deliver_to = self.channel
msg.chat = self.chat
msg.type = MsgType.Text
msg.text = "Message"
msg.verify()

with self.subTest("Missing chat"), self.assertRaises(ValueError):
msg = EFBMsg()
msg.deliver_to = self.channel
msg.author = self.chat
msg.type = MsgType.Text
msg.text = "Message"
msg.verify()

with self.subTest("Missing type"), self.assertRaises(ValueError):
msg = EFBMsg()
msg.deliver_to = self.channel
msg.author = self.chat
msg.chat = self.chat
msg.text = "Message"
msg.verify()

def test_chain_verify(self):
patch_chat_0 = self.chat.copy()
patch_chat_1 = self.chat.copy()
patch_chat_0.verify = mock.Mock()
patch_chat_1.verify = mock.Mock()

msg = EFBMsg()
msg.deliver_to = self.channel

with self.subTest("Different author and chat"):
msg.author = patch_chat_0
msg.chat = patch_chat_1
msg.text = "Message"
msg.verify()

patch_chat_0.verify.assert_called_once()
patch_chat_1.verify.assert_called_once()

patch_chat_0.verify.reset_mock()

with self.subTest("Same author and chat"):
msg.author = patch_chat_0
msg.chat = patch_chat_0
msg.text = "Message"
msg.verify()

patch_chat_0.verify.assert_called_once()

with self.subTest("Link message"):
msg.type = MsgType.Link
msg.attributes = EFBMsgLinkAttribute()
msg.attributes.verify = mock.Mock()
msg.verify()

msg.attributes.verify.assert_called_once()

with self.subTest("Location message"):
msg.type = MsgType.Location
msg.attributes = EFBMsgLocationAttribute(latitude=0.0, longitude=0.0)
msg.attributes.verify = mock.Mock()
msg.verify()

msg.attributes.verify.assert_called_once()

with self.subTest("Status message"):
msg.type = MsgType.Status
msg.attributes = EFBMsgStatusAttribute(status_type=EFBMsgStatusAttribute.Types.TYPING)
msg.attributes.verify = mock.Mock()
msg.verify()

msg.attributes.verify.assert_called_once()

with self.subTest("Message Command"):
msg.type = MsgType.Text
msg.attributes = None
msg.commands = EFBMsgCommands([
EFBMsgCommand(name="Command 1", callable_name="command_1")
])

msg.commands.commands[0].verify = mock.Mock()

msg.verify()

msg.commands.commands[0].verify.assert_called_once()

0 comments on commit bb84448

Please sign in to comment.