Skip to content
This repository was archived by the owner on Aug 15, 2022. It is now read-only.

Commit 2b9e9b7

Browse files
author
Jeff Ammons
committed
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...
1 parent 1a4b965 commit 2b9e9b7

File tree

4 files changed

+47
-9
lines changed

4 files changed

+47
-9
lines changed

requirements-dev.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
coveralls==1.1
22
ipdb==0.9.3
33
ipython==4.1.2
4+
mock==2.0.0
45
pdbpp==0.8.3
56
pytest>=2.8.2
67
pytest-cov==2.2.1

rtmbot/core.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ def output(self):
101101
if limiter:
102102
time.sleep(.1)
103103
limiter = False
104-
message = output[1].encode('ascii', 'ignore')
104+
message = output[1]
105105
channel.send_message("{}".format(message))
106106
limiter = True
107107

tests/test_example.py

Lines changed: 0 additions & 2 deletions
This file was deleted.

tests/test_rtmbot_core.py

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,25 @@
1+
# -*- coding: utf-8 -*-
2+
try:
3+
from unittest.mock import Mock
4+
except ImportError:
5+
from mock import Mock
6+
17
from testfixtures import LogCapture
28
from rtmbot.core import RtmBot
39

10+
def init_rtmbot():
11+
''' Initializes an instance of RTMBot with some default values '''
12+
rtmbot = RtmBot({
13+
'SLACK_TOKEN': 'test-12345',
14+
'BASE_PATH': '/tmp/',
15+
'LOGFILE': '/tmp/rtmbot.log',
16+
'DEBUG': True
17+
})
18+
return rtmbot
419

520
def test_init():
621
with LogCapture() as l:
7-
rtmbot = RtmBot({
8-
'SLACK_TOKEN': 'test-12345',
9-
'BASE_PATH': '/tmp/',
10-
'LOGFILE': '/tmp/rtmbot.log',
11-
'DEBUG': True
12-
})
22+
rtmbot = init_rtmbot()
1323

1424
assert rtmbot.token == 'test-12345'
1525
assert rtmbot.directory == '/tmp/'
@@ -18,3 +28,32 @@ def test_init():
1828
l.check(
1929
('root', 'INFO', 'Initialized in: /tmp/')
2030
)
31+
32+
def test_output():
33+
''' Test that sending a message behaves as expected '''
34+
rtmbot = init_rtmbot()
35+
36+
# Mock the slack_client object
37+
slackclient_mock = Mock()
38+
channel_mock = Mock()
39+
slackclient_mock.server.channels.find.return_value = channel_mock
40+
rtmbot.slack_client = slackclient_mock
41+
42+
# mock the plugin object to return a sample response
43+
plugin_mock = Mock()
44+
plugin_mock.do_output.return_value = [['C12345678', 'test message']]
45+
rtmbot.bot_plugins.append(plugin_mock)
46+
47+
rtmbot.output()
48+
49+
50+
# test that the output matches the expected value
51+
channel_mock.send_message.assert_called_with('test message')
52+
53+
# test that unicode messages work as expected
54+
channel_mock.reset_mock()
55+
plugin_mock.reset_mock()
56+
plugin_mock.do_output.return_value = [['C12345678', '🚀 testing']]
57+
rtmbot.output()
58+
59+
channel_mock.send_message.assert_called_with('🚀 testing')

0 commit comments

Comments
 (0)