tests: Refactor mock_http_conversation
with deduplicating.
Move `get_response` inside of `mock_http_conversation`, as it is not used anywhere else. Also create `assert_called_with_fields`. `assert_called_with_fields` calls the `assert_called_with` method of a mock object by using an HTTP request and a list of fields to look for.
This commit is contained in:
parent
bd0462f7b5
commit
c2c79cf989
|
@ -159,13 +159,6 @@ def read_bot_fixture_data(bot_name, test_name):
|
||||||
http_data = json.loads(content)
|
http_data = json.loads(content)
|
||||||
return http_data
|
return http_data
|
||||||
|
|
||||||
def get_response(http_response, http_headers):
|
|
||||||
# type: (Dict[str, Any], Dict[str, Any]) -> Any
|
|
||||||
mock_result = requests.Response()
|
|
||||||
mock_result._content = json.dumps(http_response).encode() # type: ignore # This modifies a "hidden" attribute.
|
|
||||||
mock_result.status_code = http_headers.get('status', 200)
|
|
||||||
return mock_result
|
|
||||||
|
|
||||||
@contextmanager
|
@contextmanager
|
||||||
def mock_http_conversation(http_data):
|
def mock_http_conversation(http_data):
|
||||||
# type: (Dict[str, Any]) -> Any
|
# type: (Dict[str, Any]) -> Any
|
||||||
|
@ -178,6 +171,31 @@ def mock_http_conversation(http_data):
|
||||||
http_data should be fixtures data formatted like the data
|
http_data should be fixtures data formatted like the data
|
||||||
in zulip_bots/zulip_bots/bots/giphy/fixtures/test_normal.json
|
in zulip_bots/zulip_bots/bots/giphy/fixtures/test_normal.json
|
||||||
"""
|
"""
|
||||||
|
def get_response(http_response, http_headers):
|
||||||
|
# type: (Dict[str, Any], Dict[str, Any]) -> Any
|
||||||
|
"""Creates a fake `requests` Response with a desired HTTP response and
|
||||||
|
response headers.
|
||||||
|
"""
|
||||||
|
mock_result = requests.Response()
|
||||||
|
mock_result._content = json.dumps(http_response).encode() # type: ignore # This modifies a "hidden" attribute.
|
||||||
|
mock_result.status_code = http_headers.get('status', 200)
|
||||||
|
return mock_result
|
||||||
|
|
||||||
|
def assert_called_with_fields(mock_result, http_request, fields):
|
||||||
|
# type: (Any, Dict[str, Any], List[str]) -> None
|
||||||
|
"""Calls `assert_called_with` on a mock object using an HTTP request.
|
||||||
|
Uses `fields` to determine which keys to look for in HTTP request and
|
||||||
|
to test; if a key is in `fields`, e.g., 'headers', it will be used in
|
||||||
|
the assertion.
|
||||||
|
"""
|
||||||
|
args = {}
|
||||||
|
|
||||||
|
for field in fields:
|
||||||
|
if field in http_request:
|
||||||
|
args[field] = http_request[field]
|
||||||
|
|
||||||
|
mock_result.assert_called_with(http_request['api_url'], **args)
|
||||||
|
|
||||||
http_request = http_data.get('request')
|
http_request = http_data.get('request')
|
||||||
http_response = http_data.get('response')
|
http_response = http_data.get('response')
|
||||||
http_headers = http_data.get('response-headers')
|
http_headers = http_data.get('response-headers')
|
||||||
|
@ -187,30 +205,20 @@ def mock_http_conversation(http_data):
|
||||||
with patch('requests.get') as mock_get:
|
with patch('requests.get') as mock_get:
|
||||||
mock_get.return_value = get_response(http_response, http_headers)
|
mock_get.return_value = get_response(http_response, http_headers)
|
||||||
yield
|
yield
|
||||||
if 'params' in http_request:
|
assert_called_with_fields(
|
||||||
params = http_request.get('params', None)
|
mock_get,
|
||||||
mock_get.assert_called_with(http_request['api_url'], params=params)
|
http_request,
|
||||||
elif 'headers' in http_request:
|
['params', 'headers']
|
||||||
headers = http_request.get('headers', None)
|
)
|
||||||
mock_get.assert_called_with(http_request['api_url'], headers=headers)
|
|
||||||
else:
|
|
||||||
mock_get.assert_called_with(http_request['api_url'])
|
|
||||||
else:
|
else:
|
||||||
with patch('requests.post') as mock_post:
|
with patch('requests.post') as mock_post:
|
||||||
mock_post.return_value = get_response(http_response, http_headers)
|
mock_post.return_value = get_response(http_response, http_headers)
|
||||||
yield
|
yield
|
||||||
if 'json' in http_request:
|
assert_called_with_fields(
|
||||||
json = http_request.get('json', None)
|
mock_post,
|
||||||
if 'params' in http_request:
|
http_request,
|
||||||
params = http_request.get('params', None)
|
['params', 'headers', 'json']
|
||||||
mock_post.assert_called_with(http_request['api_url'], json=json, params=params)
|
)
|
||||||
else:
|
|
||||||
mock_post.assert_called_with(http_request['api_url'], json=json)
|
|
||||||
elif 'params' in http_request:
|
|
||||||
params = http_request.get('params', None)
|
|
||||||
mock_post.assert_called_with(http_request['api_url'], params=params)
|
|
||||||
else:
|
|
||||||
mock_post.assert_called_with(http_request['api_url'])
|
|
||||||
|
|
||||||
class BotTestCase(StubBotTestCase):
|
class BotTestCase(StubBotTestCase):
|
||||||
"""Test class for common Bot test helper methods"""
|
"""Test class for common Bot test helper methods"""
|
||||||
|
|
Loading…
Reference in a new issue