testsuite: Add tests for converter bot in contrib_bots.
Remove previous unittest file for converter bot. Add new test file which is in accordance with the test-suite famework developed for contrib_bots. Since 'coverter' folder is now a package (addition of __init__.py), modify converter.py to import utils.py from the same package.
This commit is contained in:
parent
5c18c75295
commit
a52269cbe2
|
@ -8,7 +8,7 @@ import importlib
|
||||||
import sys
|
import sys
|
||||||
from math import log10, floor
|
from math import log10, floor
|
||||||
|
|
||||||
from . import utils
|
import utils
|
||||||
import re
|
import re
|
||||||
|
|
||||||
def is_float(value):
|
def is_float(value):
|
||||||
|
|
39
contrib_bots/bots/converter/test_converter.py
Normal file
39
contrib_bots/bots/converter/test_converter.py
Normal file
|
@ -0,0 +1,39 @@
|
||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
from __future__ import absolute_import
|
||||||
|
from __future__ import print_function
|
||||||
|
|
||||||
|
import os
|
||||||
|
import sys
|
||||||
|
|
||||||
|
our_dir = os.path.dirname(os.path.abspath(__file__))
|
||||||
|
# For dev setups, we can find the API in the repo itself.
|
||||||
|
if os.path.exists(os.path.join(our_dir, '..')):
|
||||||
|
sys.path.insert(0, '..')
|
||||||
|
from bots_test_lib import BotTestCase
|
||||||
|
|
||||||
|
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')
|
||||||
|
)
|
|
@ -1,45 +0,0 @@
|
||||||
from __future__ import absolute_import
|
|
||||||
from . import converter
|
|
||||||
|
|
||||||
def test():
|
|
||||||
for cmd, expected_response in sample_conversation():
|
|
||||||
message = {'content': cmd, 'subject': 'foo',
|
|
||||||
'display_recipient': 'bar'}
|
|
||||||
|
|
||||||
class ClientDummy(object):
|
|
||||||
def __init__(self):
|
|
||||||
self.output = ''
|
|
||||||
|
|
||||||
def send_message(self, params):
|
|
||||||
self.output = params['content']
|
|
||||||
handler = converter.ConverterHandler()
|
|
||||||
client_dummy = ClientDummy()
|
|
||||||
handler.handle_message(message, client_dummy, '')
|
|
||||||
if client_dummy.output != expected_response:
|
|
||||||
raise AssertionError('''
|
|
||||||
cmd: %s
|
|
||||||
expected: %s
|
|
||||||
but got : %s
|
|
||||||
''' % (cmd, expected_response, client_dummy.output))
|
|
||||||
|
|
||||||
def sample_conversation():
|
|
||||||
return [
|
|
||||||
('@convert 2 m cm', '2.0 m = 200.0 cm\n'),
|
|
||||||
('@converter 2 m cm', ''),
|
|
||||||
('@convert 12 celsius fahrenheit',
|
|
||||||
'12.0 celsius = 53.600054 fahrenheit\n'),
|
|
||||||
('@convert 0.002 kilometer millimile',
|
|
||||||
'0.002 kilometer = 1.2427424 millimile\n'),
|
|
||||||
('@convert 3 megabyte kilobit',
|
|
||||||
'3.0 megabyte = 24576.0 kilobit\n'),
|
|
||||||
(('foo @convert 120.5 g lb bar baz.\n'
|
|
||||||
'baz bar bar @convert 22 k c lorem ipsum dolor'),
|
|
||||||
('1. conversion: 120.5 g = 0.26565703 lb\n'
|
|
||||||
'2. conversion: 22.0 k = -251.15 c\n')),
|
|
||||||
('@convert foo bar',
|
|
||||||
('Too few arguments given. Enter `@convert help` '
|
|
||||||
'for help on using the converter.\n')),
|
|
||||||
]
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
|
||||||
test()
|
|
Loading…
Reference in a new issue