|
| 1 | +#!/usr/bin/env python |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | + |
| 4 | +from proxy import Proxy, NoTalkProxy |
| 5 | +from io import StringIO |
| 6 | +import sys |
| 7 | +from time import time |
| 8 | + |
| 9 | +if sys.version_info < (2, 7): |
| 10 | + import unittest2 as unittest |
| 11 | +else: |
| 12 | + import unittest |
| 13 | + |
| 14 | + |
| 15 | +class ProxyTest(unittest.TestCase): |
| 16 | + |
| 17 | + @classmethod |
| 18 | + def setUpClass(cls): |
| 19 | + """ Class scope setup. """ |
| 20 | + cls.p = Proxy() |
| 21 | + |
| 22 | + def setUp(cls): |
| 23 | + """ Function/test case scope setup. """ |
| 24 | + cls.output = StringIO() |
| 25 | + cls.saved_stdout = sys.stdout |
| 26 | + sys.stdout = cls.output |
| 27 | + |
| 28 | + def tearDown(cls): |
| 29 | + """ Function/test case scope teardown. """ |
| 30 | + cls.output.close() |
| 31 | + sys.stdout = cls.saved_stdout |
| 32 | + |
| 33 | + def test_sales_manager_shall_talk_through_proxy_with_delay(cls): |
| 34 | + cls.p.busy = 'No' |
| 35 | + start_time = time() |
| 36 | + cls.p.talk() |
| 37 | + end_time = time() |
| 38 | + execution_time = end_time - start_time |
| 39 | + print_output = cls.output.getvalue() |
| 40 | + expected_print_output = 'Proxy checking for Sales Manager availability\n\ |
| 41 | +Sales Manager ready to talk\n' |
| 42 | + cls.assertEqual(print_output, expected_print_output) |
| 43 | + expected_execution_time = 2 |
| 44 | + cls.assertEqual(int(execution_time), expected_execution_time) |
| 45 | + |
| 46 | + def test_sales_manager_shall_respond_through_proxy_with_delay(cls): |
| 47 | + cls.p.busy = 'Yes' |
| 48 | + start_time = time() |
| 49 | + cls.p.talk() |
| 50 | + end_time = time() |
| 51 | + execution_time = end_time - start_time |
| 52 | + print_output = cls.output.getvalue() |
| 53 | + expected_print_output = 'Proxy checking for Sales Manager availability\n\ |
| 54 | +Sales Manager is busy\n' |
| 55 | + cls.assertEqual(print_output, expected_print_output) |
| 56 | + expected_execution_time = 2 |
| 57 | + cls.assertEqual(int(execution_time), expected_execution_time) |
| 58 | + |
| 59 | + |
| 60 | +class NoTalkProxyTest(unittest.TestCase): |
| 61 | + |
| 62 | + @classmethod |
| 63 | + def setUpClass(cls): |
| 64 | + """ Class scope setup. """ |
| 65 | + cls.ntp = NoTalkProxy() |
| 66 | + |
| 67 | + def setUp(cls): |
| 68 | + """ Function/test case scope setup. """ |
| 69 | + cls.output = StringIO() |
| 70 | + cls.saved_stdout = sys.stdout |
| 71 | + sys.stdout = cls.output |
| 72 | + |
| 73 | + def tearDown(cls): |
| 74 | + """ Function/test case scope teardown. """ |
| 75 | + cls.output.close() |
| 76 | + sys.stdout = cls.saved_stdout |
| 77 | + |
| 78 | + def test_sales_manager_shall_not_talk_through_proxy_with_delay(cls): |
| 79 | + cls.ntp.busy = 'No' |
| 80 | + start_time = time() |
| 81 | + cls.ntp.talk() |
| 82 | + end_time = time() |
| 83 | + execution_time = end_time - start_time |
| 84 | + print_output = cls.output.getvalue() |
| 85 | + expected_print_output = 'Proxy checking for Sales Manager availability\n\ |
| 86 | +This Sales Manager will not talk to you whether he/she is busy or not\n' |
| 87 | + cls.assertEqual(print_output, expected_print_output) |
| 88 | + expected_execution_time = 2 |
| 89 | + cls.assertEqual(int(execution_time), expected_execution_time) |
| 90 | + |
| 91 | + def test_sales_manager_shall_not_respond_through_proxy_with_delay(cls): |
| 92 | + cls.ntp.busy = 'Yes' |
| 93 | + start_time = time() |
| 94 | + cls.ntp.talk() |
| 95 | + end_time = time() |
| 96 | + execution_time = end_time - start_time |
| 97 | + print_output = cls.output.getvalue() |
| 98 | + expected_print_output = 'Proxy checking for Sales Manager availability\n\ |
| 99 | +This Sales Manager will not talk to you whether he/she is busy or not\n' |
| 100 | + cls.assertEqual(print_output, expected_print_output) |
| 101 | + expected_execution_time = 2 |
| 102 | + cls.assertEqual(int(execution_time), expected_execution_time) |
| 103 | + |
| 104 | +if __name__ == "__main__": |
| 105 | + unittest.main() |
0 commit comments