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.)
This commit is contained in:
Steve Howell 2017-12-10 06:35:51 -08:00
parent 01f8366bea
commit 2fa677a3e0

View file

@ -100,14 +100,19 @@ class StubBotTestCase(TestCase):
bot_name = '' bot_name = ''
def get_response(self, message): def _get_handlers(self):
# type: (Dict[str, Any]) -> Dict[str, Any] # type: () -> Tuple[Any, StubBotHandler]
bot = get_bot_message_handler(self.bot_name) bot = get_bot_message_handler(self.bot_name)
bot_handler = StubBotHandler() bot_handler = StubBotHandler()
if hasattr(bot, 'initialize'): if hasattr(bot, 'initialize'):
bot.initialize(bot_handler) 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_handler.reset_transcript()
bot.handle_message(message, bot_handler) bot.handle_message(message, bot_handler)
return bot_handler.unique_response() return bot_handler.unique_response()
@ -115,12 +120,7 @@ class StubBotTestCase(TestCase):
def verify_reply(self, request, response): def verify_reply(self, request, response):
# type: (str, str) -> None # type: (str, str) -> None
# Start a new message handler for the full conversation. bot, bot_handler = self._get_handlers()
bot = get_bot_message_handler(self.bot_name)
bot_handler = StubBotHandler()
if hasattr(bot, 'initialize'):
bot.initialize(bot_handler)
message = dict( message = dict(
sender_email='foo@example.com', sender_email='foo@example.com',
@ -135,8 +135,7 @@ class StubBotTestCase(TestCase):
# type: (List[Tuple[str, str]]) -> None # type: (List[Tuple[str, str]]) -> None
# Start a new message handler for the full conversation. # Start a new message handler for the full conversation.
bot = get_bot_message_handler(self.bot_name) bot, bot_handler = self._get_handlers()
bot_handler = StubBotHandler()
for (request, expected_response) in conversation: for (request, expected_response) in conversation:
message = dict( message = dict(