From d2e359e05c949171b1ef0c2a7d65d62771a7eead Mon Sep 17 00:00:00 2001 From: Abhijeet Kaur Date: Sat, 27 May 2017 23:09:43 +0530 Subject: [PATCH] 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. --- contrib_bots/bots/converter/converter.py | 2 +- contrib_bots/bots/converter/test_converter.py | 39 ++++++++++++++++ contrib_bots/bots/converter/tests.py | 45 ------------------- 3 files changed, 40 insertions(+), 46 deletions(-) create mode 100644 contrib_bots/bots/converter/test_converter.py delete mode 100644 contrib_bots/bots/converter/tests.py diff --git a/contrib_bots/bots/converter/converter.py b/contrib_bots/bots/converter/converter.py index 6b7fb40349..9658d75fe1 100644 --- a/contrib_bots/bots/converter/converter.py +++ b/contrib_bots/bots/converter/converter.py @@ -8,7 +8,7 @@ import importlib import sys from math import log10, floor -from . import utils +import utils import re def is_float(value): diff --git a/contrib_bots/bots/converter/test_converter.py b/contrib_bots/bots/converter/test_converter.py new file mode 100644 index 0000000000..9ee1e6a7e0 --- /dev/null +++ b/contrib_bots/bots/converter/test_converter.py @@ -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') + ) diff --git a/contrib_bots/bots/converter/tests.py b/contrib_bots/bots/converter/tests.py deleted file mode 100644 index e1506d06d7..0000000000 --- a/contrib_bots/bots/converter/tests.py +++ /dev/null @@ -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()