zulip_bots: Reify StateHandler testing.

This simplifies testing stateful bots by integrating the StateHandler
into the test library. As a side-effect, the mock bot handler gets
reused during a test, making the tests more realistic. The
StateHandler now keeps its state during a call to check_expected_responses,
forcing some stateful tests to be more verbose and explicit.
This commit is contained in:
derAnfaenger 2017-10-23 20:37:39 +02:00
parent e331426c64
commit 8179b30873
4 changed files with 60 additions and 68 deletions

View file

@ -23,9 +23,8 @@ class TestIncrementorBot(BotTestCase):
'sender_email': 'foo_sender@zulip.com',
},
]
storage = StateHandler()
self.assert_bot_response(dict(messages[0], content=""), {'content': "1"},
'send_reply', storage)
'send_reply')
# Last test commented out since we don't have update_message
# support in the test framework yet.

View file

@ -86,11 +86,11 @@ class TestTictactoeBot(BotTestCase):
("1,1", msg['after_1_1']),
("2, 1", msg['after_2_1']),
("(1,3)", msg['after_1_3']),
("quit", msg['successful_quit']),
# Can't test 'after_3_2' as it's random!
]
for m in messages:
state = StateHandler()
for (mesg, resp) in expected_send_message:
self.assert_bot_response(dict(m, content=mesg),
dict(private_response, content=resp),
'send_message', state)
'send_message')

View file

@ -8,42 +8,41 @@ from zulip_bots.lib import StateHandler
class TestVirtualFsBot(BotTestCase):
bot_name = "virtual_fs"
help_txt = ('foo_sender@zulip.com:\n\nThis bot implements a virtual file system for a stream.\n'
'The locations of text are persisted for the lifetime of the bot\n'
'running, and if you rename a stream, you will lose the info.\n'
'Example commands:\n\n```\n'
'@mention-bot sample_conversation: sample conversation with the bot\n'
'@mention-bot mkdir: create a directory\n'
'@mention-bot ls: list a directory\n'
'@mention-bot cd: change directory\n'
'@mention-bot pwd: show current path\n'
'@mention-bot write: write text\n'
'@mention-bot read: read text\n'
'@mention-bot rm: remove a file\n'
'@mention-bot rmdir: remove a directory\n'
'```\n'
'Use commands like `@mention-bot help write` for more details on specific\ncommands.\n')
def test_bot(self):
help_txt = ('foo_sender@zulip.com:\n\nThis bot implements a virtual file system for a stream.\n'
'The locations of text are persisted for the lifetime of the bot\n'
'running, and if you rename a stream, you will lose the info.\n'
'Example commands:\n\n```\n'
'@mention-bot sample_conversation: sample conversation with the bot\n'
'@mention-bot mkdir: create a directory\n'
'@mention-bot ls: list a directory\n'
'@mention-bot cd: change directory\n'
'@mention-bot pwd: show current path\n'
'@mention-bot write: write text\n'
'@mention-bot read: read text\n'
'@mention-bot rm: remove a file\n'
'@mention-bot rmdir: remove a directory\n'
'```\n'
'Use commands like `@mention-bot help write` for more details on specific\ncommands.\n')
expected = {
"cd /home": "foo_sender@zulip.com:\nERROR: invalid path",
"mkdir home": "foo_sender@zulip.com:\ndirectory created",
"pwd": "foo_sender@zulip.com:\n/",
"help": help_txt,
"help ls": "foo_sender@zulip.com:\nsyntax: ls <optional_path>",
"": help_txt,
}
def test_commands_1(self):
expected = [
("cd /home", "foo_sender@zulip.com:\nERROR: invalid path"),
("mkdir home", "foo_sender@zulip.com:\ndirectory created"),
("pwd", "foo_sender@zulip.com:\n/"),
("help", self.help_txt),
("help ls", "foo_sender@zulip.com:\nsyntax: ls <optional_path>"),
("", self.help_txt),
]
self.check_expected_responses(expected)
def test_commands_2(self):
expected = [
("help", help_txt),
("help", self.help_txt),
("help ls", "foo_sender@zulip.com:\nsyntax: ls <optional_path>"),
("", help_txt),
("", self.help_txt),
("pwd", "foo_sender@zulip.com:\n/"),
("cd /home", "foo_sender@zulip.com:\nERROR: invalid path"),
("mkdir home", "foo_sender@zulip.com:\ndirectory created"),
("cd /home", "foo_sender@zulip.com:\nCurrent path: /home/"),
]
storage = StateHandler()
self.check_expected_responses(expected, storage = storage)
self.check_expected_responses(expected)