From e857a97427c31f0885f7ccfb122015cd25ce5a09 Mon Sep 17 00:00:00 2001 From: Abhijeet Kaur Date: Mon, 29 May 2017 05:24:32 +0530 Subject: [PATCH] bots: Add mock test for 'send_message' function in 'bots_test_lib' file. Since few bots directly call 'send_message' function of 'BotHandlerApi' class instead of calling 'send_reply' function first, add 'mock_test_send_message' to check for 'send_message' function. All test_.py files now need to specify which function the bot will be sending the response to, for each particular message. Make 'test_virtual_fs.py' and 'test_thesaurus.py' test files consistent with other bots. --- bots/thesaurus/test_thesaurus.py | 43 +++++---------- bots/virtual_fs/test_virtual_fs.py | 89 +++++++++++++----------------- bots_api/bots_test_lib.py | 72 +++++++++++++++--------- 3 files changed, 98 insertions(+), 106 deletions(-) diff --git a/bots/thesaurus/test_thesaurus.py b/bots/thesaurus/test_thesaurus.py index bb485a9..533b2f1 100644 --- a/bots/thesaurus/test_thesaurus.py +++ b/bots/thesaurus/test_thesaurus.py @@ -17,32 +17,17 @@ class TestThesaurusBot(BotTestCase): bot_name = "thesaurus" def test_bot(self): - self.assert_bot_output( - {'content': "synonym good", 'type': "private", 'sender_email': "foo"}, - "great, satisfying, exceptional, positive, acceptable" - ) - self.assert_bot_output( - {'content': "synonym nice", 'type': "stream", 'display_recipient': "foo", 'subject': "foo"}, - "cordial, kind, good, okay, fair" - ) - self.assert_bot_output( - {'content': "synonym foo", 'type': "stream", 'display_recipient': "foo", 'subject': "foo"}, - "bar, thud, X, baz, corge" - ) - self.assert_bot_output( - {'content': "antonym dirty", 'type': "stream", 'display_recipient': "foo", 'subject': "foo"}, - "ordered, sterile, spotless, moral, clean" - ) - self.assert_bot_output( - {'content': "antonym bar", 'type': "stream", 'display_recipient': "foo", 'subject': "foo"}, - "loss, whole, advantage, aid, failure" - ) - self.assert_bot_output( - {'content': "", 'type': "stream", 'display_recipient': "foo", 'subject': "foo"}, - ("To use this bot, start messages with either " - "@mention-bot synonym (to get the synonyms of a given word) " - "or @mention-bot antonym (to get the antonyms of a given word). " - "Phrases are not accepted so only use single words " - "to search. For example you could search '@mention-bot synonym hello' " - "or '@mention-bot antonym goodbye'."), - ) + expected = { + "synonym good": "great, satisfying, exceptional, positive, acceptable", + "synonym nice": "cordial, kind, good, okay, fair", + "synonym foo": "bar, thud, X, baz, corge", + "antonym dirty": "ordered, sterile, spotless, moral, clean", + "antonym bar": "loss, whole, advantage, aid, failure", + "": ("To use this bot, start messages with either " + "@mention-bot synonym (to get the synonyms of a given word) " + "or @mention-bot antonym (to get the antonyms of a given word). " + "Phrases are not accepted so only use single words " + "to search. For example you could search '@mention-bot synonym hello' " + "or '@mention-bot antonym goodbye'."), + } + self.check_expected_responses(expected) diff --git a/bots/virtual_fs/test_virtual_fs.py b/bots/virtual_fs/test_virtual_fs.py index f7b60ea..644f340 100644 --- a/bots/virtual_fs/test_virtual_fs.py +++ b/bots/virtual_fs/test_virtual_fs.py @@ -17,55 +17,40 @@ class TestVirtualFsBot(BotTestCase): bot_name = "virtual_fs" def test_bot(self): - self.assert_bot_output( - {'content': "cd /home", 'type': "private", 'display_recipient': "foo", 'sender_email': "foo_sender@zulip.com"}, - "foo_sender@zulip.com:\nERROR: invalid path" - ) - self.assert_bot_output( - {'content': "mkdir home", 'type': "stream", 'display_recipient': "foo", 'subject': "foo", 'sender_email': "foo_sender@zulip.com"}, - "foo_sender@zulip.com:\ndirectory created" - ) - self.assert_bot_output( - {'content': "pwd", 'type': "stream", 'display_recipient': "foo", 'subject': "foo", 'sender_email': "foo_sender@zulip.com"}, - "foo_sender@zulip.com:\n/" - ) - self.assert_bot_output( - {'content': "help", 'type': "stream", 'display_recipient': "foo", 'subject': "foo", 'sender_email': "foo_sender@zulip.com"}, - ('foo_sender@zulip.com:\n\nThis bot implements a virtual file system for a stream.\n' - 'The locations of text are persisted for the lifetime of the bot\n' - 'running, and if you rename a stream, you will lose the info.\n' - 'Example commands:\n\n```\n' - '@mention-bot sample_conversation: sample conversation with the bot\n' - '@mention-bot mkdir: create a directory\n' - '@mention-bot ls: list a directory\n' - '@mention-bot cd: change directory\n' - '@mention-bot pwd: show current path\n' - '@mention-bot write: write text\n' - '@mention-bot read: read text\n' - '@mention-bot rm: remove a file\n' - '@mention-bot rmdir: remove a directory\n' - '```\n' - 'Use commands like `@mention-bot help write` for more details on specific\ncommands.\n'), - ) - self.assert_bot_output( - {'content': "help ls", 'type': "stream", 'display_recipient': "foo", 'subject': "foo", 'sender_email': "foo_sender@zulip.com"}, - "foo_sender@zulip.com:\nsyntax: ls " - ) - self.assert_bot_output( - {'content': "", 'type': "stream", 'display_recipient': "foo", 'subject': "foo", 'sender_email': "foo_sender@zulip.com"}, - ('foo_sender@zulip.com:\n\nThis bot implements a virtual file system for a stream.\n' - 'The locations of text are persisted for the lifetime of the bot\n' - 'running, and if you rename a stream, you will lose the info.\n' - 'Example commands:\n\n```\n' - '@mention-bot sample_conversation: sample conversation with the bot\n' - '@mention-bot mkdir: create a directory\n' - '@mention-bot ls: list a directory\n' - '@mention-bot cd: change directory\n' - '@mention-bot pwd: show current path\n' - '@mention-bot write: write text\n' - '@mention-bot read: read text\n' - '@mention-bot rm: remove a file\n' - '@mention-bot rmdir: remove a directory\n' - '```\n' - 'Use commands like `@mention-bot help write` for more details on specific\ncommands.\n'), - ) + expected = { + "cd /home": "foo_sender@zulip.com:\nERROR: invalid path", + "mkdir home": "foo_sender@zulip.com:\ndirectory created", + "pwd": "foo_sender@zulip.com:\n/", + "help": ('foo_sender@zulip.com:\n\nThis bot implements a virtual file system for a stream.\n' + 'The locations of text are persisted for the lifetime of the bot\n' + 'running, and if you rename a stream, you will lose the info.\n' + 'Example commands:\n\n```\n' + '@mention-bot sample_conversation: sample conversation with the bot\n' + '@mention-bot mkdir: create a directory\n' + '@mention-bot ls: list a directory\n' + '@mention-bot cd: change directory\n' + '@mention-bot pwd: show current path\n' + '@mention-bot write: write text\n' + '@mention-bot read: read text\n' + '@mention-bot rm: remove a file\n' + '@mention-bot rmdir: remove a directory\n' + '```\n' + 'Use commands like `@mention-bot help write` for more details on specific\ncommands.\n'), + "help ls": "foo_sender@zulip.com:\nsyntax: ls ", + "": ('foo_sender@zulip.com:\n\nThis bot implements a virtual file system for a stream.\n' + 'The locations of text are persisted for the lifetime of the bot\n' + 'running, and if you rename a stream, you will lose the info.\n' + 'Example commands:\n\n```\n' + '@mention-bot sample_conversation: sample conversation with the bot\n' + '@mention-bot mkdir: create a directory\n' + '@mention-bot ls: list a directory\n' + '@mention-bot cd: change directory\n' + '@mention-bot pwd: show current path\n' + '@mention-bot write: write text\n' + '@mention-bot read: read text\n' + '@mention-bot rm: remove a file\n' + '@mention-bot rmdir: remove a directory\n' + '```\n' + 'Use commands like `@mention-bot help write` for more details on specific\ncommands.\n'), + } + self.check_expected_responses(expected) diff --git a/bots_api/bots_test_lib.py b/bots_api/bots_test_lib.py index 8555a6b..09c66bf 100644 --- a/bots_api/bots_test_lib.py +++ b/bots_api/bots_test_lib.py @@ -25,31 +25,56 @@ current_dir = os.path.dirname(os.path.abspath(__file__)) class BotTestCase(TestCase): bot_name = '' # type: str - def assert_bot_output(self, request, response): - # type: (Dict[str, Any], str) -> None - bot_module = os.path.normpath(os.path.join(current_dir, "../bots", self.bot_name, self.bot_name + ".py")) - self.bot_test(messages=[request], bot_module=bot_module, - bot_response=[response]) - - def check_expected_responses(self, expectations, email="foo", recipient="foo", subject="foo", type="all"): - # type: (Dict[str, str], str, str, str, str) -> None + def check_expected_responses(self, expectations, expected_method='send_reply', email="foo_sender@zulip.com", recipient="foo", subject="foo", type="all"): + # type: (Dict[str, Any], str, str, str, str, str) -> None + # To test send_message, Any would be a Dict type, + # to test send_reply, Any would be a str type. if type not in ["private", "stream", "all"]: logging.exception("check_expected_response expects type to be 'private', 'stream' or 'all'") for m, r in expectations.items(): if type != "stream": - self.assert_bot_output( - {'content': m, 'type': "private", 'sender_email': email}, r) + self.mock_test( + {'content': m, 'type': "private", 'display_recipient': recipient, + 'sender_email': email}, r, expected_method) if type != "private": - self.assert_bot_output( + self.mock_test( {'content': m, 'type': "stream", 'display_recipient': recipient, - 'subject': subject}, r) + 'subject': subject, 'sender_email': email}, r, expected_method) - def mock_test(self, messages, message_handler, bot_response): - # message_handler is of type Any, since it can contain any bot's + def mock_test(self, messages, bot_response, expected_method): + # type: (Dict[str, str], Any, str) -> None + if expected_method == "send_reply": + self.mock_test_send_reply(messages, bot_response, expected_method) + else: + self.mock_test_send_message(messages, bot_response, expected_method) + + def mock_test_send_message(self, messages, bot_response, expected_method): + # type: (Dict[str, str], Dict[str, str], str) -> None + # Since send_message function uses bot_response of type Dict, no + # further changes required. + self.assert_bot_output([messages], [bot_response], expected_method) + + def mock_test_send_reply(self, messages, bot_response, expected_method): + # type: (Dict[str, str], str, str) -> None + # Since send_reply function uses bot_response of type str, we + # do convert the str type to a Dict type to have the same assert_bot_output function. + bot_response_type_dict = {'content': bot_response} + self.assert_bot_output([messages], [bot_response_type_dict], expected_method) + + def get_bot_message_handler(self): + # type: () -> Any + # message_handler is of type 'Any', since it can contain any bot's # handler class. Eventually, we want bot's handler classes to # inherit from a common prototype specifying the handle_message # function. - # type: (List[Dict[str, Any]], Any, List[str]) -> None + bot_module = os.path.join(current_dir, "bots", + self.bot_name, self.bot_name + ".py") + message_handler = self.bot_to_run(bot_module) + return message_handler + + def assert_bot_output(self, messages, bot_response, expected_method): + # type: (List[Dict[str, Any]], List[Dict[str, str]], str) -> None + message_handler = self.get_bot_message_handler() # Mocking BotHandlerApi with patch('bots_api.bot_lib.BotHandlerApi') as MockClass: instance = MockClass.return_value @@ -57,19 +82,16 @@ class BotTestCase(TestCase): for (message, response) in zip(messages, bot_response): # Send message to the concerned bot message_handler.handle_message(message, MockClass(), StateHandler()) - - # Check if BotHandlerApi is sending a reply message. - # This can later be modified to assert the contents of BotHandlerApi.send_message - instance.send_reply.assert_called_with(message, response) + # Check if the bot is sending a message via `send_message` function. + # Where response is a dictionary here. + if expected_method == "send_message": + instance.send_message.assert_called_with(response) + else: + instance.send_reply.assert_called_with(message, response['content']) def bot_to_run(self, bot_module): - # Returning Any, same argument as in mock_test function. + # Returning Any, same argument as in get_bot_message_handler function. # type: (str) -> Any lib_module = get_lib_module(bot_module) message_handler = lib_module.handler_class() return message_handler - - def bot_test(self, messages, bot_module, bot_response): - # type: (List[Dict[str, Any]], str, List[str]) -> None - message_handler = self.bot_to_run(bot_module) - self.mock_test(messages=messages, message_handler=message_handler, bot_response=bot_response)