Skip to content

Commit 4afb883

Browse files
committed
Unit-tests set elaborating with PEP8
1 parent f1e7886 commit 4afb883

12 files changed

+45
-132
lines changed

tests/test_abstract_factory.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
#!/usr/bin/env python
22
# -*- coding: utf-8 -*-
33
import unittest
4+
from creational.abstract_factory import PetShop,\
5+
Dog, Cat, DogFactory, CatFactory
46
try:
57
from unittest.mock import patch
68
except ImportError:
79
from mock import patch
8-
from creational.abstract_factory import PetShop,\
9-
Dog, Cat, DogFactory, CatFactory
1010

1111

1212
class TestPetShop(unittest.TestCase):

tests/test_adapter.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
#!/usr/bin/env python
2+
import unittest
23
from structural.adapter import Dog, Cat, Human, Car, Adapter
3-
import sys
44

5-
if sys.version_info < (2, 7):
6-
import unittest2 as unittest
7-
else:
8-
import unittest
95

106
class ClassTest(unittest.TestCase):
117

@@ -40,6 +36,7 @@ def test_car_shall_make_very_loud_noise(self):
4036
expected_noise = "vroom!!!!!!!!!!"
4137
self.assertEqual(noise, expected_noise)
4238

39+
4340
class AdapterTest(unittest.TestCase):
4441

4542
def test_dog_adapter_shall_make_noise(self):
@@ -75,7 +72,5 @@ def test_car_adapter_shall_make_very_loud_noise(self):
7572
car_adapter = Adapter(car, make_noise=car.make_noise)
7673
noise = car_adapter.make_noise(10)
7774
expected_noise = "vroom!!!!!!!!!!"
78-
self.assertEqual(noise, expected_noise)
7975

80-
if __name__ == "__main__":
81-
unittest.main()
76+
self.assertEqual(noise, expected_noise)

tests/test_borg.py

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
11
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
import unittest
24
from creational.borg import Borg, YourBorg
3-
import sys
45

5-
if sys.version_info < (2, 7):
6-
import unittest2 as unittest
7-
else:
8-
import unittest
96

107
class BorgTest(unittest.TestCase):
118

12-
@classmethod
13-
def setUpClass(self):
9+
def setUp(self):
1410
self.b1 = Borg()
1511
self.b2 = Borg()
1612
self.ib1 = YourBorg()
@@ -27,6 +23,3 @@ def test_changing_instance_attribute_shall_change_borg_state(self):
2723

2824
def test_instances_shall_have_own_ids(self):
2925
self.assertNotEqual(id(self.b1), id(self.b2), id(self.ib1))
30-
31-
if __name__ == "__main__":
32-
unittest.main()

tests/test_bridge.py

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,7 @@
11
#!/usr/bin/env python
22
# -*- coding: utf-8 -*-
3-
3+
import unittest
44
from structural.bridge import DrawingAPI1, DrawingAPI2, CircleShape
5-
from sys import version_info
6-
7-
if version_info < (2, 7): # pragma: no cover
8-
import unittest2 as unittest
9-
else:
10-
import unittest
11-
125
try:
136
from unittest.mock import patch
147
except ImportError:
@@ -21,7 +14,7 @@ def test_bridge_shall_draw_with_concrete_api_implementation(cls):
2114
ci1 = DrawingAPI1()
2215
ci2 = DrawingAPI2()
2316
with patch.object(ci1, 'draw_circle') as mock_ci1_draw_circle,\
24-
patch.object(ci2, 'draw_circle') as mock_ci2_draw_circle:
17+
patch.object(ci2, 'draw_circle') as mock_ci2_draw_circle:
2518
sh1 = CircleShape(1, 2, 3, ci1)
2619
sh1.draw()
2720
cls.assertEqual(mock_ci1_draw_circle.call_count, 1)
@@ -45,11 +38,8 @@ def test_bridge_shall_scale_both_api_circles_with_own_implementation(cls):
4538
cls.assertEqual(sh1._radius, EXPECTED_CIRCLE1_RADIUS)
4639
cls.assertEqual(sh2._radius, EXPECTED_CIRCLE2_RADIUS)
4740
with patch.object(sh1, 'scale') as mock_sh1_scale_circle,\
48-
patch.object(sh2, 'scale') as mock_sh2_scale_circle:
41+
patch.object(sh2, 'scale') as mock_sh2_scale_circle:
4942
sh1.scale(2)
5043
sh2.scale(2)
5144
cls.assertEqual(mock_sh1_scale_circle.call_count, 1)
5245
cls.assertEqual(mock_sh2_scale_circle.call_count, 1)
53-
54-
if __name__ == "__main__":
55-
unittest.main()

tests/test_command.py

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
import os
4+
import shutil
5+
import unittest
26
from behavioral.command import MoveFileCommand
3-
import os, shutil, subprocess, sys
47

5-
if sys.version_info < (2, 7):
6-
import unittest2 as unittest
7-
else:
8-
import unittest
98

109
class CommandTest(unittest.TestCase):
1110

@@ -14,7 +13,8 @@ def __get_test_directory(self):
1413
"""
1514
Get the temporary directory for the tests.
1615
"""
17-
self.test_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'test_command')
16+
self.test_dir = os.path.join(os.path.dirname(
17+
os.path.realpath(__file__)), 'test_command')
1818

1919
@classmethod
2020
def setUpClass(self):
@@ -29,8 +29,10 @@ def setUpClass(self):
2929
open('tests/test_command/foo.txt', 'w').close()
3030
self.__get_test_directory()
3131
self.command_stack = []
32-
self.command_stack.append(MoveFileCommand(os.path.join(self.test_dir, 'foo.txt'), os.path.join(self.test_dir, 'bar.txt')))
33-
self.command_stack.append(MoveFileCommand(os.path.join(self.test_dir, 'bar.txt'), os.path.join(self.test_dir, 'baz.txt')))
32+
self.command_stack.append(MoveFileCommand(os.path.join(
33+
self.test_dir, 'foo.txt'), os.path.join(self.test_dir, 'bar.txt')))
34+
self.command_stack.append(MoveFileCommand(os.path.join(
35+
self.test_dir, 'bar.txt'), os.path.join(self.test_dir, 'baz.txt')))
3436

3537
def test_sequential_execution(self):
3638
self.command_stack[0].execute()
@@ -55,6 +57,3 @@ def tearDownClass(self):
5557
Remove the temporary directory /test_command and its content.
5658
"""
5759
shutil.rmtree('tests/test_command')
58-
59-
if __name__ == "__main__":
60-
unittest.main()

tests/test_flyweight.py

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,8 @@
11
#!/usr/bin/env python
22
# -*- coding: utf-8 -*-
3-
3+
import unittest
44
from structural.flyweight import Card
5-
from sys import version_info
65

7-
if version_info < (2, 7): # pragma: no cover
8-
import unittest2 as unittest
9-
else:
10-
import unittest
116

127
class TestCard(unittest.TestCase):
138

@@ -35,7 +30,3 @@ def test_instances_shall_share_additional_attributes(self):
3530
c2 = Card('9', 'h')
3631
self.assertEqual(hasattr(c2, expected_attribute_name), True)
3732
self.assertEqual(c2.attr, expected_attribute_value)
38-
39-
if __name__ == "__main__":
40-
unittest.main()
41-

tests/test_hsm.py

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
11
#!/usr/bin/env python
2-
from other.hsm.hsm import HierachicalStateMachine, UnsupportedMessageType,\
3-
UnsupportedState, UnsupportedTransition, Active, Standby, Suspect, Failed
4-
from sys import version_info
5-
6-
if version_info < (2, 7): # pragma: no cover
7-
import unittest2 as unittest
8-
else:
9-
import unittest
10-
2+
# -*- coding: utf-8 -*-
3+
import unittest
4+
from other.hsm.hsm import HierachicalStateMachine,\
5+
UnsupportedMessageType, UnsupportedState,\
6+
UnsupportedTransition, Active, Standby, Suspect
117
try:
128
from unittest.mock import patch
139
except ImportError:
@@ -62,9 +58,9 @@ def test_given_standby_on_message_switchover_shall_set_active(cls):
6258

6359
def test_given_standby_on_message_switchover_shall_call_hsm_methods(cls):
6460
with patch.object(cls.hsm, '_perform_switchover') as mock_perform_switchover,\
65-
patch.object(cls.hsm, '_check_mate_status') as mock_check_mate_status,\
66-
patch.object(cls.hsm, '_send_switchover_response') as mock_send_switchover_response,\
67-
patch.object(cls.hsm, '_next_state') as mock_next_state:
61+
patch.object(cls.hsm, '_check_mate_status') as mock_check_mate_status,\
62+
patch.object(cls.hsm, '_send_switchover_response') as mock_send_switchover_response,\
63+
patch.object(cls.hsm, '_next_state') as mock_next_state:
6864
cls.hsm.on_message('switchover')
6965
cls.assertEqual(mock_perform_switchover.call_count, 1)
7066
cls.assertEqual(mock_check_mate_status.call_count, 1)
@@ -89,7 +85,3 @@ def test_given_standby_on_message_operator_inservice_shall_raise_exception_and_k
8985
with cls.assertRaises(UnsupportedTransition) as context:
9086
cls.hsm.on_message('operator inservice')
9187
cls.assertEqual(isinstance(cls.hsm._current_state, Standby), True)
92-
93-
94-
if __name__ == "__main__":
95-
unittest.main()

tests/test_observer.py

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,7 @@
11
#!/usr/bin/env python
22
# -*- coding: utf-8 -*-
3-
4-
import sys
5-
from io import StringIO
6-
from behavioral.observer import Subject, Data, DecimalViewer, HexViewer
7-
8-
if sys.version_info < (2, 7):
9-
import unittest2 as unittest
10-
else:
11-
import unittest
12-
3+
import unittest
4+
from behavioral.observer import Subject, Data, DecimalViewer, HexViewer
135
try:
146
from unittest.mock import patch
157
except ImportError:
@@ -43,20 +35,21 @@ def test_c_observers_shall_be_detachable(cls):
4335
cls.s.detach(cls.hex_obs)
4436
cls.assertEqual(len(cls.s._observers), 0)
4537

38+
4639
class TestData(unittest.TestCase):
4740

4841
@classmethod
4942
def setUpClass(cls):
5043
cls.dec_obs = DecimalViewer()
5144
cls.hex_obs = HexViewer()
5245
cls.sub = Data('Data')
53-
#inherited behavior already tested with TestSubject
46+
# inherited behavior already tested with TestSubject
5447
cls.sub.attach(cls.dec_obs)
5548
cls.sub.attach(cls.hex_obs)
5649

5750
def test_data_change_shall_notify_all_observers_once(cls):
5851
with patch.object(cls.dec_obs, 'update') as mock_dec_obs_update,\
59-
patch.object(cls.hex_obs, 'update') as mock_hex_obs_update:
52+
patch.object(cls.hex_obs, 'update') as mock_hex_obs_update:
6053
cls.sub.data = 10
6154
cls.assertEqual(mock_dec_obs_update.call_count, 1)
6255
cls.assertEqual(mock_hex_obs_update.call_count, 1)
@@ -68,6 +61,3 @@ def test_data_value_shall_be_changeable(cls):
6861
def test_data_name_shall_be_changeable(cls):
6962
cls.sub.name = 'New Data Name'
7063
cls.assertEqual(cls.sub.name, 'New Data Name')
71-
72-
if __name__ == "__main__":
73-
unittest.main()

tests/test_proxy.py

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,14 @@
11
#!/usr/bin/env python
22
# -*- coding: utf-8 -*-
3-
4-
from structural.proxy import Proxy, NoTalkProxy
53
import sys
64
from time import time
7-
5+
import unittest
6+
from structural.proxy import Proxy, NoTalkProxy
87
if sys.version_info[0] == 2:
98
from StringIO import StringIO
109
else:
1110
from io import StringIO
1211

13-
if sys.version_info < (2, 7):
14-
import unittest2 as unittest
15-
else:
16-
import unittest
17-
1812

1913
class ProxyTest(unittest.TestCase):
2014

@@ -104,6 +98,3 @@ def test_sales_manager_shall_not_respond_through_proxy_with_delay(cls):
10498
cls.assertEqual(print_output, expected_print_output)
10599
expected_execution_time = 1
106100
cls.assertEqual(int(execution_time*10), expected_execution_time)
107-
108-
if __name__ == "__main__":
109-
unittest.main()

tests/test_publish_subscribe.py

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,7 @@
11
#!/usr/bin/env python
22
# -*- coding: utf-8 -*-
3-
4-
from sys import version_info
3+
import unittest
54
from behavioral.publish_subscribe import Provider, Publisher, Subscriber
6-
7-
if version_info < (2, 7): # pragma: no cover
8-
import unittest2 as unittest
9-
else:
10-
import unittest
11-
125
try:
136
from unittest.mock import patch, call
147
except ImportError:
@@ -57,7 +50,7 @@ def test_provider_shall_update_affected_subscribers_with_published_subscription(
5750
sub2.subscribe('sub 2 msg 1')
5851
sub2.subscribe('sub 2 msg 2')
5952
with patch.object(sub1, 'run') as mock_subscriber1_run,\
60-
patch.object(sub2, 'run') as mock_subscriber2_run:
53+
patch.object(sub2, 'run') as mock_subscriber2_run:
6154
pro.update()
6255
cls.assertEqual(mock_subscriber1_run.call_count, 0)
6356
cls.assertEqual(mock_subscriber2_run.call_count, 0)
@@ -66,13 +59,9 @@ def test_provider_shall_update_affected_subscribers_with_published_subscription(
6659
pub.publish('sub 2 msg 1')
6760
pub.publish('sub 2 msg 2')
6861
with patch.object(sub1, 'run') as mock_subscriber1_run,\
69-
patch.object(sub2, 'run') as mock_subscriber2_run:
62+
patch.object(sub2, 'run') as mock_subscriber2_run:
7063
pro.update()
7164
expected_sub1_calls = [call('sub 1 msg 1'), call('sub 1 msg 2')]
7265
mock_subscriber1_run.assert_has_calls(expected_sub1_calls)
7366
expected_sub2_calls = [call('sub 2 msg 1'), call('sub 2 msg 2')]
7467
mock_subscriber2_run.assert_has_calls(expected_sub2_calls)
75-
76-
if __name__ == "__main__":
77-
unittest.main()
78-

0 commit comments

Comments
 (0)