bots: Simplify test_googletranslate.py.

Also add sender_full_name parameter in StubBotTestCase.verify_reply().
This commit is contained in:
neiljp (Neil Pilgrim) 2017-12-10 15:25:57 -08:00
parent c8824cb2e5
commit 84d92337be
2 changed files with 41 additions and 108 deletions

View file

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

View file

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