bots: Simplify test_googletranslate.py.
Also add sender_full_name parameter in StubBotTestCase.verify_reply().
MODIFY
zulip_bots/zulip_bots/bots/googletranslate/test_googletranslate.py
zulip_bots/zulip_bots/bots/googletranslate/test_googletranslate.py
-#!/usr/bin/env python
-
-from __future__ import absolute_import
-from __future__ import print_function
-
-import json
-
from unittest.mock import patch
-from requests.exceptions import HTTPError, ConnectionError
+from requests.exceptions import ConnectionError
-from zulip_bots.test_lib import BotTestCase
+from zulip_bots.test_lib import StubBotTestCase
from zulip_bots.bots.googletranslate.googletranslate import TranslateError
help_text = '''
@@ -18,129 +11,68 @@ Please format your message like:
Visit [here](https://cloud.google.com/translate/docs/languages) for all languages
'''
-class TestGoogleTranslateBot(BotTestCase):
+class TestGoogleTranslateBot(StubBotTestCase):
bot_name = "googletranslate"
- def test_normal(self):
+ def _test(self, message, response, http_config_fixture, http_fixture=None):
with self.mock_config_info({'key': 'abcdefg'}), \
- self.mock_http_conversation('test_normal'):
- with self.mock_http_conversation('test_languages'):
- self.initialize_bot()
- self.assert_bot_response(
- message = {'content': '"hello" de', 'sender_full_name': 'tester'},
- response = {'content': 'Hallo (from tester)'},
- expected_method = 'send_reply'
- )
+ self.mock_http_conversation(http_config_fixture):
+ if http_fixture:
+ with self.mock_http_conversation(http_fixture):
+ self.verify_reply(message, response)
+ else:
+ self.verify_reply(message, response)
+
+ def test_normal(self):
+ self._test('"hello" de', 'Hallo (from Foo Test User)', 'test_languages', 'test_normal')
def test_source_language_not_found(self):
- with self.mock_config_info({'key': 'abcdefg'}), \
- self.mock_http_conversation('test_languages'):
- self.initialize_bot()
- self.assert_bot_response(
- message = {'content': '"hello" german foo', 'sender_full_name': 'tester'},
- response = {'content': 'Source language not found. Visit [here](https://cloud.google.com/translate/docs/languages) for all languages'},
- expected_method = 'send_reply'
- )
+ self._test('"hello" german foo',
+ ('Source language not found. Visit [here]'
+ '(https://cloud.google.com/translate/docs/languages) for all languages'),
+ 'test_languages')
def test_target_language_not_found(self):
- with self.mock_config_info({'key': 'abcdefg'}), \
- self.mock_http_conversation('test_languages'):
- self.initialize_bot()
- self.assert_bot_response(
- message = {'content': '"hello" bar english', 'sender_full_name': 'tester'},
- response = {'content': 'Target language not found. Visit [here](https://cloud.google.com/translate/docs/languages) for all languages'},
- expected_method = 'send_reply'
- )
+ self._test('"hello" bar english',
+ ('Target language not found. Visit [here]'
+ '(https://cloud.google.com/translate/docs/languages) for all languages'),
+ 'test_languages')
def test_403(self):
- with self.mock_config_info({'key': 'abcdefg'}), \
- self.mock_http_conversation('test_403'):
- with self.mock_http_conversation('test_languages'):
- self.initialize_bot()
- self.assert_bot_response(
- message = {'content': '"hello" german english', 'sender_full_name': 'tester'},
- response = {'content': 'Translate Error. Invalid API Key..'},
- expected_method = 'send_reply'
- )
+ self._test('"hello" german english',
+ 'Translate Error. Invalid API Key..',
+ 'test_languages', 'test_403')
# Override default function in StubBotTestCase
def test_bot_responds_to_empty_message(self):
- with self.mock_config_info({'key': 'abcdefg'}), \
- self.mock_http_conversation('test_languages'):
- self.initialize_bot()
- self.assert_bot_response(
- message = {'content': '', 'sender_full_name': 'tester'},
- response = {'content': help_text},
- expected_method = 'send_reply'
- )
+ self._test('', help_text, 'test_languages')
def test_help_command(self):
- with self.mock_config_info({'key': 'abcdefg'}), \
- self.mock_http_conversation('test_languages'):
- self.initialize_bot()
- self.assert_bot_response(
- message = {'content': 'help', 'sender_full_name': 'tester'},
- response = {'content': help_text},
- expected_method = 'send_reply'
- )
+ self._test('help', help_text, 'test_languages')
def test_help_too_many_args(self):
- with self.mock_config_info({'key': 'abcdefg'}), \
- self.mock_http_conversation('test_languages'):
- self.initialize_bot()
- self.assert_bot_response(
- message = {'content': '"hello" de english foo bar', 'sender_full_name': 'tester'},
- response = {'content': help_text},
- expected_method = 'send_reply'
- )
+ self._test('"hello" de english foo bar', help_text, 'test_languages')
def test_help_no_langs(self):
- with self.mock_config_info({'key': 'abcdefg'}), \
- self.mock_http_conversation('test_languages'):
- self.initialize_bot()
- self.assert_bot_response(
- message = {'content': '"hello"', 'sender_full_name': 'tester'},
- response = {'content': help_text},
- expected_method = 'send_reply'
- )
+ self._test('"hello"', help_text, 'test_languages')
def test_quotation_in_text(self):
- with self.mock_config_info({'key': 'abcdefg'}), \
- self.mock_http_conversation('test_quotation'):
- with self.mock_http_conversation('test_languages'):
- self.initialize_bot()
- self.assert_bot_response(
- message = {'content': '"this has "quotation" marks in" english', 'sender_full_name': 'tester'},
- response = {'content': 'this has "quotation" marks in (from tester)'},
- expected_method = 'send_reply'
- )
+ self._test('"this has "quotation" marks in" english',
+ 'this has "quotation" marks in (from Foo Test User)',
+ 'test_languages', 'test_quotation')
def test_exception(self):
- with self.mock_config_info({'key': 'abcdefg'}), \
- patch('zulip_bots.bots.googletranslate.googletranslate.translate', side_effect=Exception):
- with self.mock_http_conversation('test_languages'):
- self.initialize_bot()
- self.assertRaises(Exception)
- self.assert_bot_response(
- message = {'content': '"hello" de', 'sender_full_name': 'tester'},
- response = {'content': 'Error. .'},
- expected_method = 'send_reply'
- )
+ with patch('zulip_bots.bots.googletranslate.googletranslate.translate',
+ side_effect=Exception):
+ self._test('"hello" de', 'Error. .', 'test_languages')
def test_get_language_403(self):
- with self.mock_config_info({'key': 'abcdefg'}), \
- self.mock_http_conversation('test_language_403'), \
- self.assertRaises(TranslateError):
- self.initialize_bot()
+ with self.assertRaises(TranslateError):
+ self._test(None, None, 'test_language_403')
def test_connection_error(self):
- with self.mock_config_info({'key': 'abcdefg'}), \
- patch('requests.post', side_effect=ConnectionError()), \
+ with patch('requests.post', side_effect=ConnectionError()), \
patch('logging.warning'):
- with self.mock_http_conversation('test_languages'):
- self.initialize_bot()
- self.assert_bot_response(
- message = {'content': '"test" en', 'sender_full_name': 'tester'},
- response = {'content': 'Could not connect to Google Translate. .'},
- expected_method = 'send_reply'
- )
+ self._test('"test" en',
+ 'Could not connect to Google Translate. .',
+ 'test_languages')

MODIFY
zulip_bots/zulip_bots/test_lib.py
zulip_bots/zulip_bots/test_lib.py
@@ -124,6 +124,7 @@ class StubBotTestCase(TestCase):
message = dict(
sender_email='foo@example.com',
+ sender_full_name='Foo Test User',
content=request,
)
bot_handler.reset_transcript()

bots: Simplify test_googletranslate.py.

Also add sender_full_name parameter in StubBotTestCase.verify_reply().