bots: Add complete test coverage for yoda bot.
Add error handling support for empty messages and invalid input to yoda bot, which was previously making the bot crash. Add comments to describe tests.
This commit is contained in:
parent
efb5335b2f
commit
f3d839fd47
|
@ -0,0 +1,14 @@
|
||||||
|
{
|
||||||
|
"request": {
|
||||||
|
"api_url": "https://yoda.p.mashape.com/yoda?sentence=@#$%^&*",
|
||||||
|
"headers": {
|
||||||
|
"X-Mashape-Key": "12345678",
|
||||||
|
"Accept": "text/plain"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"response": {"text": ""},
|
||||||
|
"response-headers": {
|
||||||
|
"status": 200,
|
||||||
|
"content-type": "text/html; charset=utf-8"
|
||||||
|
}
|
||||||
|
}
|
|
@ -12,6 +12,7 @@ class TestYodaBot(BotTestCase):
|
||||||
|
|
||||||
def test_bot(self):
|
def test_bot(self):
|
||||||
|
|
||||||
|
# Test normal sentence (1).
|
||||||
bot_response = "Learn how to speak like me someday, you will. Yes, hmmm."
|
bot_response = "Learn how to speak like me someday, you will. Yes, hmmm."
|
||||||
|
|
||||||
with self.mock_config_info({'api_key': '12345678'}), \
|
with self.mock_config_info({'api_key': '12345678'}), \
|
||||||
|
@ -23,6 +24,7 @@ class TestYodaBot(BotTestCase):
|
||||||
expected_method='send_reply'
|
expected_method='send_reply'
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# Test normal sentence (2).
|
||||||
bot_response = "Much to learn, you still have."
|
bot_response = "Much to learn, you still have."
|
||||||
|
|
||||||
with self.mock_config_info({'api_key': '12345678'}), \
|
with self.mock_config_info({'api_key': '12345678'}), \
|
||||||
|
@ -34,6 +36,7 @@ class TestYodaBot(BotTestCase):
|
||||||
expected_method='send_reply'
|
expected_method='send_reply'
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# Test only numbers.
|
||||||
bot_response = "23456. Herh herh herh."
|
bot_response = "23456. Herh herh herh."
|
||||||
|
|
||||||
with self.mock_config_info({'api_key': '12345678'}), \
|
with self.mock_config_info({'api_key': '12345678'}), \
|
||||||
|
@ -45,6 +48,7 @@ class TestYodaBot(BotTestCase):
|
||||||
expected_method='send_reply'
|
expected_method='send_reply'
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# Test help.
|
||||||
bot_response = '''
|
bot_response = '''
|
||||||
This bot allows users to translate a sentence into
|
This bot allows users to translate a sentence into
|
||||||
'Yoda speak'.
|
'Yoda speak'.
|
||||||
|
@ -62,3 +66,33 @@ class TestYodaBot(BotTestCase):
|
||||||
response = {'content': bot_response},
|
response = {'content': bot_response},
|
||||||
expected_method='send_reply'
|
expected_method='send_reply'
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# Test empty message.
|
||||||
|
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.config' file.
|
||||||
|
The 'yoda.config' file should be located at '~/yoda.config'.
|
||||||
|
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'
|
||||||
|
)
|
||||||
|
|
||||||
|
# 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'
|
||||||
|
)
|
||||||
|
|
|
@ -91,7 +91,8 @@ class YodaSpeakHandler(object):
|
||||||
def handle_input(self, message, bot_handler):
|
def handle_input(self, message, bot_handler):
|
||||||
|
|
||||||
original_content = message['content']
|
original_content = message['content']
|
||||||
if self.is_help(original_content):
|
|
||||||
|
if self.is_help(original_content) or (original_content == ""):
|
||||||
bot_handler.send_reply(message, HELP_MESSAGE)
|
bot_handler.send_reply(message, HELP_MESSAGE)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
|
@ -99,6 +100,9 @@ class YodaSpeakHandler(object):
|
||||||
try:
|
try:
|
||||||
reply_message = self.send_to_yoda_api(sentence)
|
reply_message = self.send_to_yoda_api(sentence)
|
||||||
|
|
||||||
|
if len(reply_message) == 0:
|
||||||
|
reply_message = 'Invalid input, please check the sentence you have entered.'
|
||||||
|
|
||||||
except ssl.SSLError or TypeError:
|
except ssl.SSLError or TypeError:
|
||||||
reply_message = 'The service is temporarily unavailable, please try again.'
|
reply_message = 'The service is temporarily unavailable, please try again.'
|
||||||
logging.error(reply_message)
|
logging.error(reply_message)
|
||||||
|
|
Loading…
Reference in a new issue