1
1
import sys
2
2
import unittest
3
3
from io import StringIO
4
- from time import time
5
4
6
- from patterns .structural .proxy import NoTalkProxy , Proxy
5
+ from patterns .structural .proxy import Proxy , client
7
6
8
7
9
8
class ProxyTest (unittest .TestCase ):
10
9
@classmethod
11
10
def setUpClass (cls ):
12
11
""" Class scope setup. """
13
- cls .p = Proxy ()
12
+ cls .proxy = Proxy ()
14
13
15
14
def setUp (cls ):
16
15
""" Function/test case scope setup. """
@@ -23,72 +22,16 @@ def tearDown(cls):
23
22
cls .output .close ()
24
23
sys .stdout = cls .saved_stdout
25
24
26
- def test_sales_manager_shall_talk_through_proxy_with_delay (cls ):
27
- cls .p .busy = "No"
28
- start_time = time ()
29
- cls .p .talk ()
30
- end_time = time ()
31
- execution_time = end_time - start_time
32
- print_output = cls .output .getvalue ()
33
- expected_print_output = "Proxy checking for Sales Manager availability\n \
34
- Sales Manager ready to talk\n "
35
- cls .assertEqual (print_output , expected_print_output )
36
- expected_execution_time = 1
37
- cls .assertEqual (int (execution_time * 10 ), expected_execution_time )
38
-
39
- def test_sales_manager_shall_respond_through_proxy_with_delay (cls ):
40
- cls .p .busy = "Yes"
41
- start_time = time ()
42
- cls .p .talk ()
43
- end_time = time ()
44
- execution_time = end_time - start_time
45
- print_output = cls .output .getvalue ()
46
- expected_print_output = "Proxy checking for Sales Manager availability\n \
47
- Sales Manager is busy\n "
48
- cls .assertEqual (print_output , expected_print_output )
49
- expected_execution_time = 1
50
- cls .assertEqual (int (execution_time * 10 ), expected_execution_time )
51
-
52
-
53
- class NoTalkProxyTest (unittest .TestCase ):
54
- @classmethod
55
- def setUpClass (cls ):
56
- """ Class scope setup. """
57
- cls .ntp = NoTalkProxy ()
58
-
59
- def setUp (cls ):
60
- """ Function/test case scope setup. """
61
- cls .output = StringIO ()
62
- cls .saved_stdout = sys .stdout
63
- sys .stdout = cls .output
64
-
65
- def tearDown (cls ):
66
- """ Function/test case scope teardown. """
67
- cls .output .close ()
68
- sys .stdout = cls .saved_stdout
69
-
70
- def test_sales_manager_shall_not_talk_through_proxy_with_delay (cls ):
71
- cls .ntp .busy = "No"
72
- start_time = time ()
73
- cls .ntp .talk ()
74
- end_time = time ()
75
- execution_time = end_time - start_time
76
- print_output = cls .output .getvalue ()
77
- expected_print_output = "Proxy checking for Sales Manager availability\n \
78
- This Sales Manager will not talk to you whether he/she is busy or not\n "
79
- cls .assertEqual (print_output , expected_print_output )
80
- expected_execution_time = 1
81
- cls .assertEqual (int (execution_time * 10 ), expected_execution_time )
82
-
83
- def test_sales_manager_shall_not_respond_through_proxy_with_delay (cls ):
84
- cls .ntp .busy = "Yes"
85
- start_time = time ()
86
- cls .ntp .talk ()
87
- end_time = time ()
88
- execution_time = end_time - start_time
89
- print_output = cls .output .getvalue ()
90
- expected_print_output = "Proxy checking for Sales Manager availability\n \
91
- This Sales Manager will not talk to you whether he/she is busy or not\n "
92
- cls .assertEqual (print_output , expected_print_output )
93
- expected_execution_time = 1
94
- cls .assertEqual (int (execution_time * 10 ), expected_execution_time )
25
+ def test_do_the_job_for_admin_shall_pass (self ):
26
+ client (self .proxy , "admin" )
27
+ assert self .output .getvalue () == (
28
+ "[log] Doing the job for admin is requested.\n "
29
+ "I am doing the job for admin\n "
30
+ )
31
+
32
+ def test_do_the_job_for_anonymous_shall_reject (self ):
33
+ client (self .proxy , "anonymous" )
34
+ assert self .output .getvalue () == (
35
+ "[log] Doing the job for anonymous is requested.\n "
36
+ "[log] I can do the job just for `admins`.\n "
37
+ )
0 commit comments