From 29d33e86fef8849be74be87a50ad421a71ee7c91 Mon Sep 17 00:00:00 2001 From: Abhijeet Kaur Date: Thu, 3 Aug 2017 19:09:03 +0530 Subject: [PATCH] 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. --- .../zulip_bots/bots/yoda/fixtures/test_1.json | 14 +++++++++++ zulip_bots/zulip_bots/bots/yoda/test_yoda.py | 23 +++++++++++++++++++ zulip_bots/zulip_bots/bots/yoda/yoda.py | 1 - zulip_bots/zulip_bots/test_lib.py | 13 +++++++---- 4 files changed, 46 insertions(+), 5 deletions(-) create mode 100644 zulip_bots/zulip_bots/bots/yoda/fixtures/test_1.json create mode 100644 zulip_bots/zulip_bots/bots/yoda/test_yoda.py diff --git a/zulip_bots/zulip_bots/bots/yoda/fixtures/test_1.json b/zulip_bots/zulip_bots/bots/yoda/fixtures/test_1.json new file mode 100644 index 0000000..3d0bf2e --- /dev/null +++ b/zulip_bots/zulip_bots/bots/yoda/fixtures/test_1.json @@ -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" + } +} diff --git a/zulip_bots/zulip_bots/bots/yoda/test_yoda.py b/zulip_bots/zulip_bots/bots/yoda/test_yoda.py new file mode 100644 index 0000000..9f04dea --- /dev/null +++ b/zulip_bots/zulip_bots/bots/yoda/test_yoda.py @@ -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' + ) diff --git a/zulip_bots/zulip_bots/bots/yoda/yoda.py b/zulip_bots/zulip_bots/bots/yoda/yoda.py index 5c11d5a..91d1678 100644 --- a/zulip_bots/zulip_bots/bots/yoda/yoda.py +++ b/zulip_bots/zulip_bots/bots/yoda/yoda.py @@ -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, diff --git a/zulip_bots/zulip_bots/test_lib.py b/zulip_bots/zulip_bots/test_lib.py index 61def47..648263b 100755 --- a/zulip_bots/zulip_bots/test_lib.py +++ b/zulip_bots/zulip_bots/test_lib.py @@ -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