bots: Add complete tests for offline testing of yoda bot.
Since yoda api returns response of text form, made changes in test_lib.py to handle responses that can either be json or plain text.
This commit is contained in:
parent
1e84e2eb5e
commit
29d33e86fe
14
zulip_bots/zulip_bots/bots/yoda/fixtures/test_1.json
Normal file
14
zulip_bots/zulip_bots/bots/yoda/fixtures/test_1.json
Normal file
|
@ -0,0 +1,14 @@
|
|||
{
|
||||
"request": {
|
||||
"api_url": "https://yoda.p.mashape.com/yoda?sentence=You+will+learn+how+to+speak+like+me+someday.",
|
||||
"headers": {
|
||||
"X-Mashape-Key": "12345678",
|
||||
"Accept": "text/plain"
|
||||
}
|
||||
},
|
||||
"response": {"text": "Learn how to speak like me someday, you will. Yes, hmmm."},
|
||||
"response-headers": {
|
||||
"status": 200,
|
||||
"content-type": "text/html; charset=utf-8"
|
||||
}
|
||||
}
|
23
zulip_bots/zulip_bots/bots/yoda/test_yoda.py
Normal file
23
zulip_bots/zulip_bots/bots/yoda/test_yoda.py
Normal file
|
@ -0,0 +1,23 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
from __future__ import absolute_import
|
||||
from __future__ import print_function
|
||||
|
||||
import json
|
||||
|
||||
from zulip_bots.test_lib import BotTestCase
|
||||
|
||||
class TestYodaBot(BotTestCase):
|
||||
bot_name = "yoda"
|
||||
|
||||
def test_bot(self):
|
||||
bot_response = "Learn how to speak like me someday, you will. Yes, hmmm."
|
||||
|
||||
with self.mock_config_info({'api_key': '12345678'}), \
|
||||
self.mock_http_conversation('test_1'):
|
||||
self.initialize_bot()
|
||||
self.assert_bot_response(
|
||||
message = {'content': 'You will learn how to speak like me someday.'},
|
||||
response = {'content': bot_response},
|
||||
expected_method='send_reply'
|
||||
)
|
|
@ -58,7 +58,6 @@ class YodaSpeakHandler(object):
|
|||
|
||||
def send_to_yoda_api(self, sentence):
|
||||
# function for sending sentence to api
|
||||
|
||||
response = requests.get("https://yoda.p.mashape.com/yoda?sentence=" + sentence,
|
||||
headers={
|
||||
"X-Mashape-Key": self.api_key,
|
||||
|
|
|
@ -122,15 +122,20 @@ class BotTestCase(TestCase):
|
|||
with patch('requests.get') as mock_get:
|
||||
mock_result = mock.MagicMock()
|
||||
mock_result.json.return_value = http_response
|
||||
if 'text' in http_response:
|
||||
mock_result.text = http_response.get('text', None)
|
||||
mock_result.status_code = http_headers.get('status', 200)
|
||||
mock_result.ok.return_value = http_headers.get('ok', True)
|
||||
mock_get.return_value = mock_result
|
||||
yield
|
||||
params = http_request.get('params', None)
|
||||
if params is None:
|
||||
mock_get.assert_called_with(http_request['api_url'])
|
||||
else:
|
||||
if 'params' in http_request:
|
||||
params = http_request.get('params', None)
|
||||
mock_get.assert_called_with(http_request['api_url'], params=params)
|
||||
elif 'headers' in http_request:
|
||||
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'])
|
||||
|
||||
def assert_bot_response(self, message, response, expected_method, state_handler = None):
|
||||
# type: (Dict[str, Any], Dict[str, Any], str, Optional[StateHandler]) -> None
|
||||
|
|
Loading…
Reference in a new issue