mypy: Improve typing of dialogflow and salesforce tests.

This commit is contained in:
neiljp (Neil Pilgrim) 2018-06-09 20:25:28 -07:00 committed by showell
parent 509991f26f
commit cd76a3cf5a
2 changed files with 15 additions and 15 deletions

View file

@ -4,19 +4,10 @@ from contextlib import contextmanager
from unittest.mock import patch from unittest.mock import patch
from typing import Any, ByteString from typing import Iterator, ByteString
import json import json
class MockTextRequest():
def __init__(self) -> None:
self.session_id = ""
self.query = ""
self.response = ""
def getresponse(self) -> Any:
return MockHttplibRequest(self.response)
class MockHttplibRequest(): class MockHttplibRequest():
def __init__(self, response: str) -> None: def __init__(self, response: str) -> None:
self.response = response self.response = response
@ -24,8 +15,17 @@ class MockHttplibRequest():
def read(self) -> ByteString: def read(self) -> ByteString:
return json.dumps(self.response).encode() return json.dumps(self.response).encode()
class MockTextRequest():
def __init__(self) -> None:
self.session_id = ""
self.query = ""
self.response = ""
def getresponse(self) -> MockHttplibRequest:
return MockHttplibRequest(self.response)
@contextmanager @contextmanager
def mock_dialogflow(test_name: str, bot_name: str) -> Any: def mock_dialogflow(test_name: str, bot_name: str) -> Iterator[None]:
response_data = read_bot_fixture_data(bot_name, test_name) response_data = read_bot_fixture_data(bot_name, test_name)
try: try:
df_request = response_data['request'] df_request = response_data['request']

View file

@ -2,11 +2,11 @@ from zulip_bots.test_lib import BotTestCase, DefaultTests, StubBotHandler, read_
from simple_salesforce.exceptions import SalesforceAuthenticationFailed from simple_salesforce.exceptions import SalesforceAuthenticationFailed
from contextlib import contextmanager from contextlib import contextmanager
from unittest.mock import patch from unittest.mock import patch
from typing import Any, Dict from typing import Any, Dict, Iterator
@contextmanager @contextmanager
def mock_salesforce_query(test_name: str, bot_name: str) -> Any: def mock_salesforce_query(test_name: str, bot_name: str) -> Iterator[None]:
response_data = read_bot_fixture_data(bot_name, test_name) response_data = read_bot_fixture_data(bot_name, test_name)
sf_response = response_data.get('response') sf_response = response_data.get('response')
@ -16,7 +16,7 @@ def mock_salesforce_query(test_name: str, bot_name: str) -> Any:
@contextmanager @contextmanager
def mock_salesforce_auth(is_success: bool) -> Any: def mock_salesforce_auth(is_success: bool) -> Iterator[None]:
if is_success: if is_success:
with patch('simple_salesforce.api.Salesforce.__init__') as mock_sf_init: with patch('simple_salesforce.api.Salesforce.__init__') as mock_sf_init:
mock_sf_init.return_value = None mock_sf_init.return_value = None
@ -31,7 +31,7 @@ def mock_salesforce_auth(is_success: bool) -> Any:
@contextmanager @contextmanager
def mock_salesforce_commands_types() -> Any: def mock_salesforce_commands_types() -> Iterator[None]:
with patch('zulip_bots.bots.salesforce.utils.commands', mock_commands), \ with patch('zulip_bots.bots.salesforce.utils.commands', mock_commands), \
patch('zulip_bots.bots.salesforce.utils.object_types', mock_object_types): patch('zulip_bots.bots.salesforce.utils.object_types', mock_object_types):
yield yield