zulip_bots: Obey pep8, python3 type annotations.

This commit is contained in:
dkvasov 2018-05-17 17:49:16 +03:00
parent a06fb98f21
commit 0040182ec4

View file

@ -1,5 +1,4 @@
from unittest.mock import patch import unittest
from unittest import TestCase
from typing import List, Dict, Any, Tuple from typing import List, Dict, Any, Tuple
@ -22,49 +21,42 @@ from zulip_bots.test_file_utils import (
read_bot_fixture_data, read_bot_fixture_data,
) )
class StubBotHandler: class StubBotHandler:
def __init__(self): def __init__(self) -> None:
# type: () -> None
self.storage = SimpleStorage() self.storage = SimpleStorage()
self.full_name = 'test-bot' self.full_name = 'test-bot'
self.email = 'test-bot@example.com' self.email = 'test-bot@example.com'
self.message_server = SimpleMessageServer() self.message_server = SimpleMessageServer()
self.reset_transcript() self.reset_transcript()
def reset_transcript(self): def reset_transcript(self) -> None:
# type: () -> None
self.transcript = [] # type: List[Tuple[str, Dict[str, Any]]] self.transcript = [] # type: List[Tuple[str, Dict[str, Any]]]
def send_message(self, message): def send_message(self, message: Dict[str, Any]) -> Dict[str, Any]:
# type: (Dict[str, Any]) -> Dict[str, Any]
self.transcript.append(('send_message', message)) self.transcript.append(('send_message', message))
return self.message_server.send(message) return self.message_server.send(message)
def send_reply(self, message, response): def send_reply(self, message: Dict[str, Any], response: str) -> Dict[str, Any]:
# type: (Dict[str, Any], str) -> Dict[str, Any]
response_message = dict( response_message = dict(
content=response content=response
) )
self.transcript.append(('send_reply', response_message)) self.transcript.append(('send_reply', response_message))
return self.message_server.send(response_message) return self.message_server.send(response_message)
def update_message(self, message): def update_message(self, message: Dict[str, Any]) -> None:
# type: (Dict[str, Any]) -> None
self.message_server.update(message) self.message_server.update(message)
class BotQuitException(Exception): class BotQuitException(Exception):
pass pass
def quit(self, message = ""): def quit(self, message: str="") -> None:
# type: (str) -> None
raise self.BotQuitException() raise self.BotQuitException()
def get_config_info(self, bot_name, optional=False): def get_config_info(self, bot_name: str, optional: bool=False) -> Dict[str, Any]:
# type: (str, bool) -> Dict[str, Any]
return {} return {}
def unique_reply(self): def unique_reply(self) -> Dict[str, Any]:
# type: () -> Dict[str, Any]
responses = [ responses = [
message message
for (method, message) for (method, message)
@ -74,8 +66,7 @@ class StubBotHandler:
self.ensure_unique_response(responses) self.ensure_unique_response(responses)
return responses[0] return responses[0]
def unique_response(self): def unique_response(self) -> Dict[str, Any]:
# type: () -> Dict[str, Any]
responses = [ responses = [
message message
for (method, message) for (method, message)
@ -84,25 +75,24 @@ class StubBotHandler:
self.ensure_unique_response(responses) self.ensure_unique_response(responses)
return responses[0] return responses[0]
def ensure_unique_response(self, responses): def ensure_unique_response(self, responses: List[Dict[str, Any]]) -> None:
# type: (List[Dict[str, Any]]) -> None
if not responses: if not responses:
raise Exception('The bot is not responding for some reason.') raise Exception('The bot is not responding for some reason.')
if len(responses) > 1: if len(responses) > 1:
raise Exception('The bot is giving too many responses for some reason.') raise Exception('The bot is giving too many responses for some reason.')
class BotTestCase(TestCase):
class BotTestCase(unittest.TestCase):
bot_name = '' bot_name = ''
def _get_handlers(self): def _get_handlers(self) -> Tuple[Any, StubBotHandler]:
# 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) return bot, bot_handler
def get_response(self, message): def get_response(self, message):
# type: (Dict[str, Any]) -> Dict[str, Any] # type: (Dict[str, Any]) -> Dict[str, Any]
@ -111,13 +101,12 @@ class BotTestCase(TestCase):
bot.handle_message(message, bot_handler) bot.handle_message(message, bot_handler)
return bot_handler.unique_response() return bot_handler.unique_response()
def make_request_message(self, content): def make_request_message(self, content: str) -> Dict[str, Any]:
# type: (str) -> Dict[str, Any] """
'''
This is mostly used internally but This is mostly used internally but
tests can override this behavior by tests can override this behavior by
mocking/subclassing. mocking/subclassing.
''' """
message = dict( message = dict(
display_recipient='foo_stream', display_recipient='foo_stream',
sender_email='foo@example.com', sender_email='foo@example.com',
@ -127,8 +116,7 @@ class BotTestCase(TestCase):
) )
return message return message
def get_reply_dict(self, request): def get_reply_dict(self, request: str) -> Dict[str, Any]:
# type: (str) -> Dict[str, Any]
bot, bot_handler = self._get_handlers() bot, bot_handler = self._get_handlers()
message = self.make_request_message(request) message = self.make_request_message(request)
bot_handler.reset_transcript() bot_handler.reset_transcript()
@ -136,14 +124,11 @@ class BotTestCase(TestCase):
reply = bot_handler.unique_reply() reply = bot_handler.unique_reply()
return reply return reply
def verify_reply(self, request, response): def verify_reply(self, request: str, response: str) -> None:
# type: (str, str) -> None
reply = self.get_reply_dict(request) reply = self.get_reply_dict(request)
self.assertEqual(response, reply['content']) self.assertEqual(response, reply['content'])
def verify_dialog(self, conversation): def verify_dialog(self, conversation: 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, bot_handler = self._get_handlers() bot, bot_handler = self._get_handlers()
@ -163,8 +148,7 @@ class BotTestCase(TestCase):
bot_class = type(get_bot_message_handler(self.bot_name)) bot_class = type(get_bot_message_handler(self.bot_name))
bot_class.validate_config(config_data) bot_class.validate_config(config_data)
def test_bot_usage(self): def test_bot_usage(self) -> None:
# type: () -> None
bot = get_bot_message_handler(self.bot_name) bot = get_bot_message_handler(self.bot_name)
self.assertNotEqual(bot.usage(), '') self.assertNotEqual(bot.usage(), '')
@ -177,16 +161,13 @@ class BotTestCase(TestCase):
# we also want a non-blank response # we also want a non-blank response
self.assertTrue(len(response['content']) >= 1) self.assertTrue(len(response['content']) >= 1)
def mock_http_conversation(self, test_name): def mock_http_conversation(self, test_name: str) -> Any:
# type: (str) -> Any
assert test_name is not None assert test_name is not None
http_data = read_bot_fixture_data(self.bot_name, test_name) http_data = read_bot_fixture_data(self.bot_name, test_name)
return mock_http_conversation(http_data) return mock_http_conversation(http_data)
def mock_request_exception(self): def mock_request_exception(self) -> Any:
# type: () -> Any
return mock_request_exception() return mock_request_exception()
def mock_config_info(self, config_info): def mock_config_info(self, config_info: Dict[str, str]) -> Any:
# type: (Dict[str, str]) -> Any return unittest.mock.patch('zulip_bots.test_lib.StubBotHandler.get_config_info', return_value=config_info)
return patch('zulip_bots.test_lib.StubBotHandler.get_config_info', return_value=config_info)