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