Skip to content

Commit 2ed51f8

Browse files
committed
Rewrite tests for the Proxy class
Remove outdated tests and replace them with ones that check the new behavior.
1 parent fb35b1e commit 2ed51f8

File tree

1 file changed

+15
-72
lines changed

1 file changed

+15
-72
lines changed

tests/structural/test_proxy.py

Lines changed: 15 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
import sys
22
import unittest
33
from io import StringIO
4-
from time import time
54

6-
from patterns.structural.proxy import NoTalkProxy, Proxy
5+
from patterns.structural.proxy import Proxy, client
76

87

98
class ProxyTest(unittest.TestCase):
109
@classmethod
1110
def setUpClass(cls):
1211
""" Class scope setup. """
13-
cls.p = Proxy()
12+
cls.proxy = Proxy()
1413

1514
def setUp(cls):
1615
""" Function/test case scope setup. """
@@ -23,72 +22,16 @@ def tearDown(cls):
2322
cls.output.close()
2423
sys.stdout = cls.saved_stdout
2524

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

Comments
 (0)