From 2b9e9b752c35fa32212f91d736e3f757f15c75a6 Mon Sep 17 00:00:00 2001 From: Jeff Ammons Date: Fri, 27 May 2016 17:59:40 -0700 Subject: [PATCH] Add tests and bugfix for b'' messages appearing in python3. I added unicode and non-unicode message testing for the output() method on RTMBot. I'm concerned about why the encode('ascii', 'ignore') was there in the first place, but not having it seems to do the right thing... --- requirements-dev.txt | 1 + rtmbot/core.py | 2 +- tests/test_example.py | 2 -- tests/test_rtmbot_core.py | 51 ++++++++++++++++++++++++++++++++++----- 4 files changed, 47 insertions(+), 9 deletions(-) delete mode 100644 tests/test_example.py diff --git a/requirements-dev.txt b/requirements-dev.txt index 8e69fc4..b011dfe 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -1,6 +1,7 @@ coveralls==1.1 ipdb==0.9.3 ipython==4.1.2 +mock==2.0.0 pdbpp==0.8.3 pytest>=2.8.2 pytest-cov==2.2.1 diff --git a/rtmbot/core.py b/rtmbot/core.py index 049bb77..9cc507d 100755 --- a/rtmbot/core.py +++ b/rtmbot/core.py @@ -101,7 +101,7 @@ class RtmBot(object): if limiter: time.sleep(.1) limiter = False - message = output[1].encode('ascii', 'ignore') + message = output[1] channel.send_message("{}".format(message)) limiter = True diff --git a/tests/test_example.py b/tests/test_example.py deleted file mode 100644 index c8d72d0..0000000 --- a/tests/test_example.py +++ /dev/null @@ -1,2 +0,0 @@ -def test_example(): - assert True \ No newline at end of file diff --git a/tests/test_rtmbot_core.py b/tests/test_rtmbot_core.py index 0353147..85d7563 100644 --- a/tests/test_rtmbot_core.py +++ b/tests/test_rtmbot_core.py @@ -1,15 +1,25 @@ +# -*- coding: utf-8 -*- +try: + from unittest.mock import Mock +except ImportError: + from mock import Mock + from testfixtures import LogCapture from rtmbot.core import RtmBot +def init_rtmbot(): + ''' Initializes an instance of RTMBot with some default values ''' + rtmbot = RtmBot({ + 'SLACK_TOKEN': 'test-12345', + 'BASE_PATH': '/tmp/', + 'LOGFILE': '/tmp/rtmbot.log', + 'DEBUG': True + }) + return rtmbot def test_init(): with LogCapture() as l: - rtmbot = RtmBot({ - 'SLACK_TOKEN': 'test-12345', - 'BASE_PATH': '/tmp/', - 'LOGFILE': '/tmp/rtmbot.log', - 'DEBUG': True - }) + rtmbot = init_rtmbot() assert rtmbot.token == 'test-12345' assert rtmbot.directory == '/tmp/' @@ -18,3 +28,32 @@ def test_init(): l.check( ('root', 'INFO', 'Initialized in: /tmp/') ) + +def test_output(): + ''' Test that sending a message behaves as expected ''' + rtmbot = init_rtmbot() + + # Mock the slack_client object + slackclient_mock = Mock() + channel_mock = Mock() + slackclient_mock.server.channels.find.return_value = channel_mock + rtmbot.slack_client = slackclient_mock + + # mock the plugin object to return a sample response + plugin_mock = Mock() + plugin_mock.do_output.return_value = [['C12345678', 'test message']] + rtmbot.bot_plugins.append(plugin_mock) + + rtmbot.output() + + + # test that the output matches the expected value + channel_mock.send_message.assert_called_with('test message') + + # test that unicode messages work as expected + channel_mock.reset_mock() + plugin_mock.reset_mock() + plugin_mock.do_output.return_value = [['C12345678', '🚀 testing']] + rtmbot.output() + + channel_mock.send_message.assert_called_with('🚀 testing')