From 2fa677a3e09f80dff4260b07af4db9ec9b7839fa Mon Sep 17 00:00:00 2001 From: Steve Howell Date: Sun, 10 Dec 2017 06:35:51 -0800 Subject: [PATCH] bot tests: Extract StubBotTestCase._get_handlers(). This is mostly a pure refactoring, but it also ensures that `initialize` is called in a consistent way by most of our test helpers. (This didn't cause problems before, since some bots don't require initialization.) --- zulip_bots/zulip_bots/test_lib.py | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/zulip_bots/zulip_bots/test_lib.py b/zulip_bots/zulip_bots/test_lib.py index 3c8841b..e14127c 100755 --- a/zulip_bots/zulip_bots/test_lib.py +++ b/zulip_bots/zulip_bots/test_lib.py @@ -100,14 +100,19 @@ class StubBotTestCase(TestCase): bot_name = '' - def get_response(self, message): - # type: (Dict[str, Any]) -> Dict[str, Any] + def _get_handlers(self): + # type: () -> Tuple[Any, StubBotHandler] bot = get_bot_message_handler(self.bot_name) bot_handler = StubBotHandler() if hasattr(bot, 'initialize'): bot.initialize(bot_handler) + return (bot, bot_handler) + + def get_response(self, message): + # type: (Dict[str, Any]) -> Dict[str, Any] + bot, bot_handler = self._get_handlers() bot_handler.reset_transcript() bot.handle_message(message, bot_handler) return bot_handler.unique_response() @@ -115,12 +120,7 @@ class StubBotTestCase(TestCase): def verify_reply(self, request, response): # type: (str, str) -> None - # Start a new message handler for the full conversation. - bot = get_bot_message_handler(self.bot_name) - bot_handler = StubBotHandler() - - if hasattr(bot, 'initialize'): - bot.initialize(bot_handler) + bot, bot_handler = self._get_handlers() message = dict( sender_email='foo@example.com', @@ -135,8 +135,7 @@ class StubBotTestCase(TestCase): # type: (List[Tuple[str, str]]) -> None # Start a new message handler for the full conversation. - bot = get_bot_message_handler(self.bot_name) - bot_handler = StubBotHandler() + bot, bot_handler = self._get_handlers() for (request, expected_response) in conversation: message = dict(