Skip to content

Commit 31853cf

Browse files
neiljpshowell
authored andcommitted
Add test_bot_responds_to_empty_message to StubBotTestCase.
1 parent 381401b commit 31853cf

File tree

7 files changed

+87
-49
lines changed

7 files changed

+87
-49
lines changed

zulip_bots/zulip_bots/bots/giphy/test_giphy.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@
99
class TestGiphyBot(StubBotTestCase):
1010
bot_name = "giphy"
1111

12+
# Override default function in StubBotTestCase
13+
def test_bot_responds_to_empty_message(self) -> None:
14+
# FIXME?: Giphy does not respond to empty messages
15+
pass
16+
1217
def test_normal(self) -> None:
1318
bot_response = '[Click to enlarge]' \
1419
'(https://media4.giphy.com/media/3o6ZtpxSZbQRRnwCKQ/giphy.gif)' \

zulip_bots/zulip_bots/bots/github_detail/test_github_detail.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ def test_bot_usage(self) -> None:
2222

2323
self.assertIn('displays details on github issues', bot.usage())
2424

25+
# Override default function in StubBotTestCase
26+
def test_bot_responds_to_empty_message(self) -> None:
27+
with self.mock_config_info(self.mock_config):
28+
self.verify_reply('', 'Failed to find any issue or PR.')
29+
2530
def test_issue(self) -> None:
2631
request = 'zulip/zulip#5365'
2732
bot_response = '**[zulip/zulip#5365](https://github.com/zulip/zulip/issues/5365)'\

zulip_bots/zulip_bots/bots/googletranslate/test_googletranslate.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ def test_403(self):
6363
expected_method = 'send_reply'
6464
)
6565

66-
def test_help_empty(self):
66+
# Override default function in StubBotTestCase
67+
def test_bot_responds_to_empty_message(self):
6768
with self.mock_config_info({'key': 'abcdefg'}), \
6869
self.mock_http_conversation('test_languages'):
6970
self.initialize_bot()

zulip_bots/zulip_bots/bots/weather/test_weather.py

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,25 @@
1010
class TestWeatherBot(BotTestCase):
1111
bot_name = "weather"
1212

13+
help_content = '''
14+
This bot returns weather info for specified city.
15+
You specify city in the following format:
16+
city, state/country
17+
state and country parameter is optional(useful when there are many cities with the same name)
18+
For example:
19+
@**Weather Bot** Portland
20+
@**Weather Bot** Portland, Me
21+
'''.strip()
22+
23+
# Override default function in StubBotTestCase
24+
def test_bot_responds_to_empty_message(self) -> None:
25+
bot_response = self.help_content
26+
self.assert_bot_response(
27+
message = {'content': ''},
28+
response = {'content': bot_response},
29+
expected_method='send_reply'
30+
)
31+
1332
def test_bot(self) -> None:
1433

1534
# City query
@@ -55,28 +74,11 @@ def test_bot(self) -> None:
5574
response = {'content': bot_response},
5675
expected_method='send_reply'
5776
)
58-
help_content = '''
59-
This bot returns weather info for specified city.
60-
You specify city in the following format:
61-
city, state/country
62-
state and country parameter is optional(useful when there are many cities with the same name)
63-
For example:
64-
@**Weather Bot** Portland
65-
@**Weather Bot** Portland, Me
66-
'''.strip()
6777

6878
# help message
69-
bot_response = help_content
79+
bot_response = self.help_content
7080
self.assert_bot_response(
7181
message = {'content': 'help'},
7282
response = {'content': bot_response},
7383
expected_method='send_reply'
7484
)
75-
76-
# empty message
77-
bot_response = help_content
78-
self.assert_bot_response(
79-
message = {'content': ''},
80-
response = {'content': bot_response},
81-
expected_method='send_reply'
82-
)

zulip_bots/zulip_bots/bots/yoda/test_yoda.py

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,27 @@
1010
class TestYodaBot(BotTestCase):
1111
bot_name = "yoda"
1212

13+
# Override default function in StubBotTestCase
14+
def test_bot_responds_to_empty_message(self):
15+
bot_response = '''
16+
This bot allows users to translate a sentence into
17+
'Yoda speak'.
18+
Users should preface messages with '@mention-bot'.
19+
20+
Before running this, make sure to get a Mashape Api token.
21+
Instructions are in the 'readme.md' file.
22+
Store it in the 'yoda.conf' file.
23+
The 'yoda.conf' file should be located in this bot's (zulip_bots/bots/yoda/yoda)
24+
directory.
25+
Example input:
26+
@mention-bot You will learn how to speak like me someday.
27+
'''
28+
self.assert_bot_response(
29+
message = {'content': ''},
30+
response = {'content': bot_response},
31+
expected_method='send_reply'
32+
)
33+
1334
def test_bot(self):
1435

1536
# Test normal sentence (1).
@@ -68,26 +89,6 @@ def test_bot(self):
6889
expected_method='send_reply'
6990
)
7091

71-
# Test empty message.
72-
bot_response = '''
73-
This bot allows users to translate a sentence into
74-
'Yoda speak'.
75-
Users should preface messages with '@mention-bot'.
76-
77-
Before running this, make sure to get a Mashape Api token.
78-
Instructions are in the 'readme.md' file.
79-
Store it in the 'yoda.conf' file.
80-
The 'yoda.conf' file should be located in this bot's (zulip_bots/bots/yoda/yoda)
81-
directory.
82-
Example input:
83-
@mention-bot You will learn how to speak like me someday.
84-
'''
85-
self.assert_bot_response(
86-
message = {'content': ''},
87-
response = {'content': bot_response},
88-
expected_method='send_reply'
89-
)
90-
9192
# Test invalid input.
9293
bot_response = "Invalid input, please check the sentence you have entered."
9394
with self.mock_config_info({'api_key': '12345678'}), \

zulip_bots/zulip_bots/bots/youtube/test_youtube.py

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,21 @@ class TestYoutubeBot(StubBotTestCase):
1313
'number_of_results': '5',
1414
'video_region': 'US'} # type: Dict[str,str]
1515

16+
help_content = "*Help for YouTube bot* :robot_face: : \n\n" \
17+
"The bot responds to messages starting with @mention-bot.\n\n" \
18+
"`@mention-bot <search terms>` will return top Youtube video for the given `<search term>`.\n" \
19+
"`@mention-bot top <search terms>` also returns the top Youtube result.\n" \
20+
"`@mention-bot list <search terms>` will return a list Youtube videos for the given <search term>.\n \n" \
21+
"Example:\n" \
22+
" * @mention-bot funny cats\n" \
23+
" * @mention-bot list funny dogs"
24+
25+
# Override default function in StubBotTestCase
26+
def test_bot_responds_to_empty_message(self) -> None:
27+
with self.mock_config_info(self.normal_config), \
28+
self.mock_http_conversation('test_keyok'):
29+
self.verify_reply('', self.help_content)
30+
1631
def test_single(self) -> None:
1732
bot_response = 'Here is what I found for `funny cats` : \n'\
1833
'Cats are so funny you will die laughing - ' \
@@ -55,22 +70,13 @@ def test_noresult(self) -> None:
5570
self.verify_reply('somethingrandomwithnoresult', bot_response,)
5671

5772
def test_help(self) -> None:
58-
help_content = "*Help for YouTube bot* :robot_face: : \n\n" \
59-
"The bot responds to messages starting with @mention-bot.\n\n" \
60-
"`@mention-bot <search terms>` will return top Youtube video for the given `<search term>`.\n" \
61-
"`@mention-bot top <search terms>` also returns the top Youtube result.\n" \
62-
"`@mention-bot list <search terms>` will return a list Youtube videos for the given <search term>.\n \n" \
63-
"Example:\n" \
64-
" * @mention-bot funny cats\n" \
65-
" * @mention-bot list funny dogs"
66-
73+
help_content = self.help_content
6774
with self.mock_config_info(self.normal_config), \
6875
self.mock_http_conversation('test_keyok'):
6976
self.verify_reply('help', help_content)
7077
self.verify_reply('list', help_content)
7178
self.verify_reply('help list', help_content)
7279
self.verify_reply('top', help_content)
73-
self.verify_reply('', help_content)
7480

7581
def test_connection_error(self) -> None:
7682
with self.mock_config_info(self.normal_config), \

zulip_bots/zulip_bots/test_lib.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,24 @@ def test_bot_usage(self):
142142
bot = get_bot_message_handler(self.bot_name)
143143
self.assertNotEqual(bot.usage(), '')
144144

145+
def test_bot_responds_to_empty_message(self) -> None:
146+
# TODO get_response should be usable here once it's merged?
147+
bot = get_bot_message_handler(self.bot_name)
148+
bot_handler = StubBotHandler()
149+
150+
if hasattr(bot, 'initialize'):
151+
bot.initialize(bot_handler)
152+
153+
message = dict(
154+
sender_email='[email protected]',
155+
display_recipient='foo',
156+
content='',
157+
)
158+
bot_handler.reset_transcript()
159+
bot.handle_message(message, bot_handler)
160+
empty_response = bot_handler.unique_response()
161+
self.assertIsNotNone(empty_response)
162+
145163
def mock_http_conversation(self, test_name):
146164
# type: (str) -> Any
147165
assert test_name is not None

0 commit comments

Comments
 (0)