bots: Simplify test_yoda.py.

This commit is contained in:
neiljp (Neil Pilgrim) 2017-12-10 18:05:20 -08:00
parent 20cb236fdd
commit 060211a131

View file

@ -1,18 +1,9 @@
#!/usr/bin/env python
from zulip_bots.test_lib import StubBotTestCase
from __future__ import absolute_import
from __future__ import print_function
import json
from zulip_bots.test_lib import BotTestCase
class TestYodaBot(BotTestCase):
class TestYodaBot(StubBotTestCase):
bot_name = "yoda"
# Override default function in StubBotTestCase
def test_bot_responds_to_empty_message(self):
bot_response = '''
help_text = '''
This bot allows users to translate a sentence into
'Yoda speak'.
Users should preface messages with '@mention-bot'.
@ -25,77 +16,39 @@ class TestYodaBot(BotTestCase):
Example input:
@mention-bot You will learn how to speak like me someday.
'''
self.assert_bot_response(
message = {'content': ''},
response = {'content': bot_response},
expected_method='send_reply'
)
def _test(self, message, response, fixture=None):
with self.mock_config_info({'api_key': '12345678'}):
if fixture is not None:
with self.mock_http_conversation(fixture):
self.verify_reply(message, response)
else:
self.verify_reply(message, response)
# Override default function in StubBotTestCase
def test_bot_responds_to_empty_message(self):
self._test('', self.help_text)
def test_bot(self):
# Test normal sentence (1).
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'
)
self._test('You will learn how to speak like me someday.',
"Learn how to speak like me someday, you will. Yes, hmmm.",
'test_1')
# Test normal sentence (2).
bot_response = "Much to learn, you still have."
with self.mock_config_info({'api_key': '12345678'}), \
self.mock_http_conversation('test_2'):
self.initialize_bot()
self.assert_bot_response(
message = {'content': 'you still have much to learn'},
response = {'content': bot_response},
expected_method='send_reply'
)
self._test('you still have much to learn',
"Much to learn, you still have.",
'test_2')
# Test only numbers.
bot_response = "23456. Herh herh herh."
with self.mock_config_info({'api_key': '12345678'}), \
self.mock_http_conversation('test_only_numbers'):
self.initialize_bot()
self.assert_bot_response(
message = {'content': '23456'},
response = {'content': bot_response},
expected_method='send_reply'
)
self._test('23456', "23456. Herh herh herh.",
'test_only_numbers')
# Test help.
bot_response = '''
This bot allows users to translate a sentence into
'Yoda speak'.
Users should preface messages with '@mention-bot'.
Before running this, make sure to get a Mashape Api token.
Instructions are in the 'readme.md' file.
Store it in the 'yoda.conf' file.
The 'yoda.conf' file should be located in this bot's (zulip_bots/bots/yoda/yoda)
directory.
Example input:
@mention-bot You will learn how to speak like me someday.
'''
self.assert_bot_response(
message = {'content': 'help'},
response = {'content': bot_response},
expected_method='send_reply'
)
self._test('help', self.help_text)
# Test invalid input.
bot_response = "Invalid input, please check the sentence you have entered."
with self.mock_config_info({'api_key': '12345678'}), \
self.mock_http_conversation('test_invalid_input'):
self.initialize_bot()
self.assert_bot_response(
message = {'content': '@#$%^&*'},
response = {'content': bot_response},
expected_method='send_reply'
)
self._test('@#$%^&*',
"Invalid input, please check the sentence you have entered.",
'test_invalid_input')