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...
This commit is contained in:
Jeff Ammons 2016-05-27 17:59:40 -07:00
parent 1a4b96584e
commit 2b9e9b752c
4 changed files with 47 additions and 9 deletions

View file

@ -1,6 +1,7 @@
coveralls==1.1 coveralls==1.1
ipdb==0.9.3 ipdb==0.9.3
ipython==4.1.2 ipython==4.1.2
mock==2.0.0
pdbpp==0.8.3 pdbpp==0.8.3
pytest>=2.8.2 pytest>=2.8.2
pytest-cov==2.2.1 pytest-cov==2.2.1

View file

@ -101,7 +101,7 @@ class RtmBot(object):
if limiter: if limiter:
time.sleep(.1) time.sleep(.1)
limiter = False limiter = False
message = output[1].encode('ascii', 'ignore') message = output[1]
channel.send_message("{}".format(message)) channel.send_message("{}".format(message))
limiter = True limiter = True

View file

@ -1,2 +0,0 @@
def test_example():
assert True

View file

@ -1,15 +1,25 @@
# -*- coding: utf-8 -*-
try:
from unittest.mock import Mock
except ImportError:
from mock import Mock
from testfixtures import LogCapture from testfixtures import LogCapture
from rtmbot.core import RtmBot from rtmbot.core import RtmBot
def init_rtmbot():
def test_init(): ''' Initializes an instance of RTMBot with some default values '''
with LogCapture() as l:
rtmbot = RtmBot({ rtmbot = RtmBot({
'SLACK_TOKEN': 'test-12345', 'SLACK_TOKEN': 'test-12345',
'BASE_PATH': '/tmp/', 'BASE_PATH': '/tmp/',
'LOGFILE': '/tmp/rtmbot.log', 'LOGFILE': '/tmp/rtmbot.log',
'DEBUG': True 'DEBUG': True
}) })
return rtmbot
def test_init():
with LogCapture() as l:
rtmbot = init_rtmbot()
assert rtmbot.token == 'test-12345' assert rtmbot.token == 'test-12345'
assert rtmbot.directory == '/tmp/' assert rtmbot.directory == '/tmp/'
@ -18,3 +28,32 @@ def test_init():
l.check( l.check(
('root', 'INFO', 'Initialized in: /tmp/') ('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')