contrib_bots: Add check_expected_responses() test helper and use it.
This further simplifies the logic for testing a contrib_bots bot to require very little code per test in the common case.
This commit is contained in:
parent
536ab436ed
commit
1991e0128b
|
@ -16,24 +16,14 @@ class TestConverterBot(BotTestCase):
|
||||||
bot_name = "converter"
|
bot_name = "converter"
|
||||||
|
|
||||||
def test_bot(self):
|
def test_bot(self):
|
||||||
self.assert_bot_output(
|
expected = {
|
||||||
{'content': "2 m cm", 'type': "private", 'sender_email': "foo@gmail.com"},
|
"": ('Too few arguments given. Enter `@convert help` '
|
||||||
"2.0 m = 200.0 cm\n"
|
'for help on using the converter.\n'),
|
||||||
)
|
"foo bar": ('Too few arguments given. Enter `@convert help` '
|
||||||
self.assert_bot_output(
|
'for help on using the converter.\n'),
|
||||||
{'content': "12 celsius fahrenheit", 'type': "stream", 'display_recipient': "foo", 'subject': "foo"},
|
"2 m cm": "2.0 m = 200.0 cm\n",
|
||||||
"12.0 celsius = 53.600054 fahrenheit\n"
|
"12.0 celsius fahrenheit": "12.0 celsius = 53.600054 fahrenheit\n",
|
||||||
)
|
"0.002 kilometer millimile": "0.002 kilometer = 1.2427424 millimile\n",
|
||||||
self.assert_bot_output(
|
"3 megabyte kilobit": "3.0 megabyte = 24576.0 kilobit\n",
|
||||||
{'content': "0.002 kilometer millimile", 'type': "stream", 'display_recipient': "foo", 'subject': "foo"},
|
}
|
||||||
"0.002 kilometer = 1.2427424 millimile\n"
|
self.check_expected_responses(expected)
|
||||||
)
|
|
||||||
self.assert_bot_output(
|
|
||||||
{'content': "3 megabyte kilobit", 'type': "stream", 'display_recipient': "foo", 'subject': "foo"},
|
|
||||||
"3.0 megabyte = 24576.0 kilobit\n"
|
|
||||||
)
|
|
||||||
self.assert_bot_output(
|
|
||||||
{'content': "foo bar", 'type': "stream", 'display_recipient': "foo", 'subject': "foo"},
|
|
||||||
('Too few arguments given. Enter `@convert help` '
|
|
||||||
'for help on using the converter.\n')
|
|
||||||
)
|
|
||||||
|
|
|
@ -16,14 +16,12 @@ class TestDefineBot(BotTestCase):
|
||||||
bot_name = "define"
|
bot_name = "define"
|
||||||
|
|
||||||
def test_bot(self):
|
def test_bot(self):
|
||||||
self.assert_bot_output(
|
expected = {
|
||||||
{'content': "foo", 'type': "private", 'sender_email': "foo"},
|
"": 'Please enter a word to define.',
|
||||||
"**foo**:\nDefinition not available."
|
"foo": "**foo**:\nDefinition not available.",
|
||||||
)
|
"cat": ("**cat**:\n\n* (**noun**) a small domesticated carnivorous mammal "
|
||||||
self.assert_bot_output(
|
|
||||||
{'content': "cat", 'type': "stream", 'display_recipient': "foo", 'subject': "foo"},
|
|
||||||
("**cat**:\n\n* (**noun**) a small domesticated carnivorous mammal "
|
|
||||||
"with soft fur, a short snout, and retractile claws. It is widely "
|
"with soft fur, a short snout, and retractile claws. It is widely "
|
||||||
"kept as a pet or for catching mice, and many breeds have been "
|
"kept as a pet or for catching mice, and many breeds have been "
|
||||||
"developed.\n their pet cat\n\n"),
|
"developed.\n their pet cat\n\n"),
|
||||||
)
|
}
|
||||||
|
self.check_expected_responses(expected)
|
||||||
|
|
|
@ -16,23 +16,11 @@ class TestEncryptBot(BotTestCase):
|
||||||
bot_name = "encrypt"
|
bot_name = "encrypt"
|
||||||
|
|
||||||
def test_bot(self):
|
def test_bot(self):
|
||||||
self.assert_bot_output(
|
expected = {
|
||||||
{'content': "Please encrypt this", 'type': "private", 'sender_email': "foo@gmail.com"},
|
"": "Encrypted/Decrypted text: ",
|
||||||
"Encrypted/Decrypted text: Cyrnfr rapelcg guvf"
|
"Let\'s Do It": "Encrypted/Decrypted text: Yrg\'f Qb Vg",
|
||||||
)
|
"me&mom together..!!": "Encrypted/Decrypted text: zr&zbz gbtrgure..!!",
|
||||||
self.assert_bot_output(
|
"foo bar": "Encrypted/Decrypted text: sbb one",
|
||||||
{'content': "Let\'s Do It", 'type': "stream", 'display_recipient': "foo", 'subject': "foo"},
|
"Please encrypt this": "Encrypted/Decrypted text: Cyrnfr rapelcg guvf",
|
||||||
"Encrypted/Decrypted text: Yrg\'f Qb Vg"
|
}
|
||||||
)
|
self.check_expected_responses(expected)
|
||||||
self.assert_bot_output(
|
|
||||||
{'content': "", 'type': "stream", 'display_recipient': "foo", 'subject': "foo"},
|
|
||||||
"Encrypted/Decrypted text: "
|
|
||||||
)
|
|
||||||
self.assert_bot_output(
|
|
||||||
{'content': "me&mom together..!!", 'type': "stream", 'display_recipient': "foo", 'subject': "foo"},
|
|
||||||
"Encrypted/Decrypted text: zr&zbz gbtrgure..!!"
|
|
||||||
)
|
|
||||||
self.assert_bot_output(
|
|
||||||
{'content': "foo bar", 'type': "stream", 'display_recipient': "foo", 'subject': "foo"},
|
|
||||||
"Encrypted/Decrypted text: sbb one"
|
|
||||||
)
|
|
||||||
|
|
|
@ -5,6 +5,7 @@ from __future__ import print_function
|
||||||
|
|
||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
|
from six.moves import zip
|
||||||
|
|
||||||
our_dir = os.path.dirname(os.path.abspath(__file__))
|
our_dir = os.path.dirname(os.path.abspath(__file__))
|
||||||
# For dev setups, we can find the API in the repo itself.
|
# For dev setups, we can find the API in the repo itself.
|
||||||
|
@ -16,15 +17,6 @@ class TestHelloWorldBot(BotTestCase):
|
||||||
bot_name = "helloworld"
|
bot_name = "helloworld"
|
||||||
|
|
||||||
def test_bot(self):
|
def test_bot(self):
|
||||||
self.assert_bot_output(
|
txt = "beep boop"
|
||||||
{'content': "foo", 'type': "private", 'sender_email': "foo"},
|
messages = ["", "foo", "Hi, my name is abc"]
|
||||||
"beep boop"
|
self.check_expected_responses(dict(list(zip(messages, len(messages)*[txt]))))
|
||||||
)
|
|
||||||
self.assert_bot_output(
|
|
||||||
{'content': "Hi, my name is abc", 'type': "stream", 'display_recipient': "foo", 'subject': "foo"},
|
|
||||||
"beep boop"
|
|
||||||
)
|
|
||||||
self.assert_bot_output(
|
|
||||||
{'content': "", 'type': "stream", 'display_recipient': "foo", 'subject': "foo"},
|
|
||||||
"beep boop"
|
|
||||||
)
|
|
||||||
|
|
|
@ -5,6 +5,7 @@ from __future__ import print_function
|
||||||
|
|
||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
|
from six.moves import zip
|
||||||
|
|
||||||
our_dir = os.path.dirname(os.path.abspath(__file__))
|
our_dir = os.path.dirname(os.path.abspath(__file__))
|
||||||
# For dev setups, we can find the API in the repo itself.
|
# For dev setups, we can find the API in the repo itself.
|
||||||
|
@ -16,15 +17,6 @@ class TestHelpBot(BotTestCase):
|
||||||
bot_name = "help"
|
bot_name = "help"
|
||||||
|
|
||||||
def test_bot(self):
|
def test_bot(self):
|
||||||
self.assert_bot_output(
|
txt = "Info on Zulip can be found here:\nhttps://github.com/zulip/zulip"
|
||||||
{'content': "help", 'type': "private", 'sender_email': "foo"},
|
messages = ["", "help", "Hi, my name is abc"]
|
||||||
"Info on Zulip can be found here:\nhttps://github.com/zulip/zulip"
|
self.check_expected_responses(dict(list(zip(messages, len(messages)*[txt]))))
|
||||||
)
|
|
||||||
self.assert_bot_output(
|
|
||||||
{'content': "Hi, my name is abc", 'type': "stream", 'display_recipient': "foo", 'subject': "foo"},
|
|
||||||
"Info on Zulip can be found here:\nhttps://github.com/zulip/zulip"
|
|
||||||
)
|
|
||||||
self.assert_bot_output(
|
|
||||||
{'content': "", 'type': "stream", 'display_recipient': "foo", 'subject': "foo"},
|
|
||||||
"Info on Zulip can be found here:\nhttps://github.com/zulip/zulip"
|
|
||||||
)
|
|
||||||
|
|
|
@ -16,19 +16,15 @@ class TestWikipediaBot(BotTestCase):
|
||||||
bot_name = "wikipedia"
|
bot_name = "wikipedia"
|
||||||
|
|
||||||
def test_bot(self):
|
def test_bot(self):
|
||||||
self.assert_bot_output(
|
expected = {
|
||||||
{'content': "foo", 'type': "private", 'sender_email': "foo"},
|
"": 'Please enter your message after @mention-bot',
|
||||||
'For search term "foo", https://en.wikipedia.org/wiki/Foobar'
|
"sssssss kkkkk": ('I am sorry. The search term you provided '
|
||||||
)
|
'is not found :slightly_frowning_face:'),
|
||||||
self.assert_bot_output(
|
"foo": ('For search term "foo", '
|
||||||
{'content': "", 'type': "stream", 'display_recipient': "foo", 'subject': "foo"},
|
'https://en.wikipedia.org/wiki/Foobar'),
|
||||||
'Please enter your message after @mention-bot'
|
"123": ('For search term "123", '
|
||||||
)
|
'https://en.wikipedia.org/wiki/123'),
|
||||||
self.assert_bot_output(
|
"laugh": ('For search term "laugh", '
|
||||||
{'content': "sssssss kkkkk", 'type': "stream", 'display_recipient': "foo", 'subject': "foo"},
|
'https://en.wikipedia.org/wiki/Laughter'),
|
||||||
'I am sorry. The search term you provided is not found :slightly_frowning_face:'
|
}
|
||||||
)
|
self.check_expected_responses(expected)
|
||||||
self.assert_bot_output(
|
|
||||||
{'content': "123", 'type': "stream", 'display_recipient': "foo", 'subject': "foo"},
|
|
||||||
'For search term "123", https://en.wikipedia.org/wiki/123'
|
|
||||||
)
|
|
||||||
|
|
|
@ -39,8 +39,4 @@ class TestXkcdBot(BotTestCase):
|
||||||
"(https://imgs.xkcd.com/comics/chess_notation.png)"),
|
"(https://imgs.xkcd.com/comics/chess_notation.png)"),
|
||||||
"999999999": invalid_id_txt + "999999999",
|
"999999999": invalid_id_txt + "999999999",
|
||||||
}
|
}
|
||||||
for m, r in expected.items():
|
self.check_expected_responses(expected)
|
||||||
self.assert_bot_output(
|
|
||||||
{'content': m, 'type': "private", 'sender_email': "foo"}, r)
|
|
||||||
self.assert_bot_output(
|
|
||||||
{'content': m, 'type': "stream", 'sender_email': "foo"}, r)
|
|
||||||
|
|
|
@ -6,6 +6,7 @@ from __future__ import print_function
|
||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
import unittest
|
import unittest
|
||||||
|
import logging
|
||||||
|
|
||||||
from mock import MagicMock, patch
|
from mock import MagicMock, patch
|
||||||
|
|
||||||
|
@ -31,6 +32,19 @@ class BotTestCase(TestCase):
|
||||||
self.bot_test(messages=[request], bot_module=bot_module,
|
self.bot_test(messages=[request], bot_module=bot_module,
|
||||||
bot_response=[response])
|
bot_response=[response])
|
||||||
|
|
||||||
|
def check_expected_responses(self, expectations, email="foo", recipient="foo", subject="foo", type="all"):
|
||||||
|
# type: (Dict[str, str], str, str, str, str) -> None
|
||||||
|
if type not in ["private", "stream", "all"]:
|
||||||
|
logging.exception("check_expected_response expects type to be 'private', 'stream' or 'all'")
|
||||||
|
for m, r in expectations.items():
|
||||||
|
if type != "stream":
|
||||||
|
self.assert_bot_output(
|
||||||
|
{'content': m, 'type': "private", 'sender_email': email}, r)
|
||||||
|
if type != "private":
|
||||||
|
self.assert_bot_output(
|
||||||
|
{'content': m, 'type': "stream", 'display_recipient': recipient,
|
||||||
|
'subject': subject}, r)
|
||||||
|
|
||||||
def mock_test(self, messages, message_handler, bot_response):
|
def mock_test(self, messages, message_handler, bot_response):
|
||||||
# message_handler is of type Any, since it can contain any bot's
|
# message_handler is of type Any, since it can contain any bot's
|
||||||
# handler class. Eventually, we want bot's handler classes to
|
# handler class. Eventually, we want bot's handler classes to
|
||||||
|
|
Loading…
Reference in a new issue