Skip to content

Commit 7447a4f

Browse files
authored
Merge pull request faif#348 from imankulov/fix_tests
Fix tests
2 parents fb35b1e + b1da067 commit 7447a4f

File tree

6 files changed

+24
-76
lines changed

6 files changed

+24
-76
lines changed

patterns/behavioral/chain_of_responsibility.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"""
2020

2121
from abc import ABC, abstractmethod
22-
from typing import Callable, Optional, Tuple, TypeVar
22+
from typing import Optional, Tuple, TypeVar
2323

2424
T = TypeVar("T")
2525

patterns/structural/3-tier.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
Separates presentation, application processing, and data management functions.
44
"""
55

6-
from typing import Dict, KeysView, Optional, Type, TypeVar, Union
6+
from typing import Dict, KeysView, Optional, Union
77

88

99
class Data:

patterns/structural/proxy.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ def do_the_job(self, user: str) -> None:
5656
if user == "admin":
5757
self._real_subject.do_the_job(user)
5858
else:
59-
print(f"[log] I can do the job just for `admins`.")
59+
print("[log] I can do the job just for `admins`.")
6060

6161

6262
def client(job_doer: Union[RealSubject, Proxy], user: str) -> None:

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[flake8]
22
max-line-length = 120
3-
ignore = E266 E731
3+
ignore = E266 E731 W503
44
exclude = .venv*
55

66
[tool:pytest]

tests/creational/test_borg.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,13 @@ class BorgTest(unittest.TestCase):
77
def setUp(self):
88
self.b1 = Borg()
99
self.b2 = Borg()
10+
# creating YourBorg instance implicitly sets the state attribute
11+
# for all borg instances.
1012
self.ib1 = YourBorg()
1113

14+
def tearDown(self):
15+
self.ib1.state = "Init"
16+
1217
def test_initial_borg_state_shall_be_init(self):
1318
b = Borg()
1419
self.assertEqual(b.state, "Init")

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)