Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions tests/test_abstract_factory.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import unittest
from creational.abstract_factory import PetShop,\
Dog, Cat, DogFactory, CatFactory
try:
from unittest.mock import patch
except ImportError:
from mock import patch
from creational.abstract_factory import PetShop,\
Dog, Cat, DogFactory, CatFactory


class TestPetShop(unittest.TestCase):
Expand Down
12 changes: 4 additions & 8 deletions tests/test_adapter.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import unittest
from structural.adapter import Dog, Cat, Human, Car, Adapter
import sys

if sys.version_info < (2, 7):
import unittest2 as unittest
else:
import unittest

class ClassTest(unittest.TestCase):

Expand Down Expand Up @@ -40,6 +37,7 @@ def test_car_shall_make_very_loud_noise(self):
expected_noise = "vroom!!!!!!!!!!"
self.assertEqual(noise, expected_noise)


class AdapterTest(unittest.TestCase):

def test_dog_adapter_shall_make_noise(self):
Expand Down Expand Up @@ -75,7 +73,5 @@ def test_car_adapter_shall_make_very_loud_noise(self):
car_adapter = Adapter(car, make_noise=car.make_noise)
noise = car_adapter.make_noise(10)
expected_noise = "vroom!!!!!!!!!!"
self.assertEqual(noise, expected_noise)

if __name__ == "__main__":
unittest.main()
self.assertEqual(noise, expected_noise)
13 changes: 3 additions & 10 deletions tests/test_borg.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import unittest
from creational.borg import Borg, YourBorg
import sys

if sys.version_info < (2, 7):
import unittest2 as unittest
else:
import unittest

class BorgTest(unittest.TestCase):

@classmethod
def setUpClass(self):
def setUp(self):
self.b1 = Borg()
self.b2 = Borg()
self.ib1 = YourBorg()
Expand All @@ -27,6 +23,3 @@ def test_changing_instance_attribute_shall_change_borg_state(self):

def test_instances_shall_have_own_ids(self):
self.assertNotEqual(id(self.b1), id(self.b2), id(self.ib1))

if __name__ == "__main__":
unittest.main()
16 changes: 3 additions & 13 deletions tests/test_bridge.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import unittest
from structural.bridge import DrawingAPI1, DrawingAPI2, CircleShape
from sys import version_info

if version_info < (2, 7): # pragma: no cover
import unittest2 as unittest
else:
import unittest

try:
from unittest.mock import patch
except ImportError:
Expand All @@ -21,7 +14,7 @@ def test_bridge_shall_draw_with_concrete_api_implementation(cls):
ci1 = DrawingAPI1()
ci2 = DrawingAPI2()
with patch.object(ci1, 'draw_circle') as mock_ci1_draw_circle,\
patch.object(ci2, 'draw_circle') as mock_ci2_draw_circle:
patch.object(ci2, 'draw_circle') as mock_ci2_draw_circle:
sh1 = CircleShape(1, 2, 3, ci1)
sh1.draw()
cls.assertEqual(mock_ci1_draw_circle.call_count, 1)
Expand All @@ -45,11 +38,8 @@ def test_bridge_shall_scale_both_api_circles_with_own_implementation(cls):
cls.assertEqual(sh1._radius, EXPECTED_CIRCLE1_RADIUS)
cls.assertEqual(sh2._radius, EXPECTED_CIRCLE2_RADIUS)
with patch.object(sh1, 'scale') as mock_sh1_scale_circle,\
patch.object(sh2, 'scale') as mock_sh2_scale_circle:
patch.object(sh2, 'scale') as mock_sh2_scale_circle:
sh1.scale(2)
sh2.scale(2)
cls.assertEqual(mock_sh1_scale_circle.call_count, 1)
cls.assertEqual(mock_sh2_scale_circle.call_count, 1)

if __name__ == "__main__":
unittest.main()
21 changes: 10 additions & 11 deletions tests/test_command.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import shutil
import unittest
from behavioral.command import MoveFileCommand
import os, shutil, subprocess, sys

if sys.version_info < (2, 7):
import unittest2 as unittest
else:
import unittest

class CommandTest(unittest.TestCase):

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

@classmethod
def setUpClass(self):
Expand All @@ -29,8 +29,10 @@ def setUpClass(self):
open('tests/test_command/foo.txt', 'w').close()
self.__get_test_directory()
self.command_stack = []
self.command_stack.append(MoveFileCommand(os.path.join(self.test_dir, 'foo.txt'), os.path.join(self.test_dir, 'bar.txt')))
self.command_stack.append(MoveFileCommand(os.path.join(self.test_dir, 'bar.txt'), os.path.join(self.test_dir, 'baz.txt')))
self.command_stack.append(MoveFileCommand(os.path.join(
self.test_dir, 'foo.txt'), os.path.join(self.test_dir, 'bar.txt')))
self.command_stack.append(MoveFileCommand(os.path.join(
self.test_dir, 'bar.txt'), os.path.join(self.test_dir, 'baz.txt')))

def test_sequential_execution(self):
self.command_stack[0].execute()
Expand All @@ -55,6 +57,3 @@ def tearDownClass(self):
Remove the temporary directory /test_command and its content.
"""
shutil.rmtree('tests/test_command')

if __name__ == "__main__":
unittest.main()
11 changes: 1 addition & 10 deletions tests/test_flyweight.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import unittest
from structural.flyweight import Card
from sys import version_info

if version_info < (2, 7): # pragma: no cover
import unittest2 as unittest
else:
import unittest

class TestCard(unittest.TestCase):

Expand Down Expand Up @@ -35,7 +30,3 @@ def test_instances_shall_share_additional_attributes(self):
c2 = Card('9', 'h')
self.assertEqual(hasattr(c2, expected_attribute_name), True)
self.assertEqual(c2.attr, expected_attribute_value)

if __name__ == "__main__":
unittest.main()

24 changes: 8 additions & 16 deletions tests/test_hsm.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
#!/usr/bin/env python
from other.hsm.hsm import HierachicalStateMachine, UnsupportedMessageType,\
UnsupportedState, UnsupportedTransition, Active, Standby, Suspect, Failed
from sys import version_info

if version_info < (2, 7): # pragma: no cover
import unittest2 as unittest
else:
import unittest

# -*- coding: utf-8 -*-
import unittest
from other.hsm.hsm import HierachicalStateMachine,\
UnsupportedMessageType, UnsupportedState,\
UnsupportedTransition, Active, Standby, Suspect
try:
from unittest.mock import patch
except ImportError:
Expand Down Expand Up @@ -62,9 +58,9 @@ def test_given_standby_on_message_switchover_shall_set_active(cls):

def test_given_standby_on_message_switchover_shall_call_hsm_methods(cls):
with patch.object(cls.hsm, '_perform_switchover') as mock_perform_switchover,\
patch.object(cls.hsm, '_check_mate_status') as mock_check_mate_status,\
patch.object(cls.hsm, '_send_switchover_response') as mock_send_switchover_response,\
patch.object(cls.hsm, '_next_state') as mock_next_state:
patch.object(cls.hsm, '_check_mate_status') as mock_check_mate_status,\
patch.object(cls.hsm, '_send_switchover_response') as mock_send_switchover_response,\
patch.object(cls.hsm, '_next_state') as mock_next_state:
cls.hsm.on_message('switchover')
cls.assertEqual(mock_perform_switchover.call_count, 1)
cls.assertEqual(mock_check_mate_status.call_count, 1)
Expand All @@ -89,7 +85,3 @@ def test_given_standby_on_message_operator_inservice_shall_raise_exception_and_k
with cls.assertRaises(UnsupportedTransition) as context:
cls.hsm.on_message('operator inservice')
cls.assertEqual(isinstance(cls.hsm._current_state, Standby), True)


if __name__ == "__main__":
unittest.main()
20 changes: 5 additions & 15 deletions tests/test_observer.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,7 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys
from io import StringIO
from behavioral.observer import Subject, Data, DecimalViewer, HexViewer

if sys.version_info < (2, 7):
import unittest2 as unittest
else:
import unittest

import unittest
from behavioral.observer import Subject, Data, DecimalViewer, HexViewer
try:
from unittest.mock import patch
except ImportError:
Expand Down Expand Up @@ -43,20 +35,21 @@ def test_c_observers_shall_be_detachable(cls):
cls.s.detach(cls.hex_obs)
cls.assertEqual(len(cls.s._observers), 0)


class TestData(unittest.TestCase):

@classmethod
def setUpClass(cls):
cls.dec_obs = DecimalViewer()
cls.hex_obs = HexViewer()
cls.sub = Data('Data')
#inherited behavior already tested with TestSubject
# inherited behavior already tested with TestSubject
cls.sub.attach(cls.dec_obs)
cls.sub.attach(cls.hex_obs)

def test_data_change_shall_notify_all_observers_once(cls):
with patch.object(cls.dec_obs, 'update') as mock_dec_obs_update,\
patch.object(cls.hex_obs, 'update') as mock_hex_obs_update:
patch.object(cls.hex_obs, 'update') as mock_hex_obs_update:
cls.sub.data = 10
cls.assertEqual(mock_dec_obs_update.call_count, 1)
cls.assertEqual(mock_hex_obs_update.call_count, 1)
Expand All @@ -68,6 +61,3 @@ def test_data_value_shall_be_changeable(cls):
def test_data_name_shall_be_changeable(cls):
cls.sub.name = 'New Data Name'
cls.assertEqual(cls.sub.name, 'New Data Name')

if __name__ == "__main__":
unittest.main()
13 changes: 2 additions & 11 deletions tests/test_proxy.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,14 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

from structural.proxy import Proxy, NoTalkProxy
import sys
from time import time

import unittest
from structural.proxy import Proxy, NoTalkProxy
if sys.version_info[0] == 2:
from StringIO import StringIO
else:
from io import StringIO

if sys.version_info < (2, 7):
import unittest2 as unittest
else:
import unittest


class ProxyTest(unittest.TestCase):

Expand Down Expand Up @@ -104,6 +98,3 @@ def test_sales_manager_shall_not_respond_through_proxy_with_delay(cls):
cls.assertEqual(print_output, expected_print_output)
expected_execution_time = 1
cls.assertEqual(int(execution_time*10), expected_execution_time)

if __name__ == "__main__":
unittest.main()
17 changes: 3 additions & 14 deletions tests/test_publish_subscribe.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

from sys import version_info
import unittest
from behavioral.publish_subscribe import Provider, Publisher, Subscriber

if version_info < (2, 7): # pragma: no cover
import unittest2 as unittest
else:
import unittest

try:
from unittest.mock import patch, call
except ImportError:
Expand Down Expand Up @@ -57,7 +50,7 @@ def test_provider_shall_update_affected_subscribers_with_published_subscription(
sub2.subscribe('sub 2 msg 1')
sub2.subscribe('sub 2 msg 2')
with patch.object(sub1, 'run') as mock_subscriber1_run,\
patch.object(sub2, 'run') as mock_subscriber2_run:
patch.object(sub2, 'run') as mock_subscriber2_run:
pro.update()
cls.assertEqual(mock_subscriber1_run.call_count, 0)
cls.assertEqual(mock_subscriber2_run.call_count, 0)
Expand All @@ -66,13 +59,9 @@ def test_provider_shall_update_affected_subscribers_with_published_subscription(
pub.publish('sub 2 msg 1')
pub.publish('sub 2 msg 2')
with patch.object(sub1, 'run') as mock_subscriber1_run,\
patch.object(sub2, 'run') as mock_subscriber2_run:
patch.object(sub2, 'run') as mock_subscriber2_run:
pro.update()
expected_sub1_calls = [call('sub 1 msg 1'), call('sub 1 msg 2')]
mock_subscriber1_run.assert_has_calls(expected_sub1_calls)
expected_sub2_calls = [call('sub 2 msg 1'), call('sub 2 msg 2')]
mock_subscriber2_run.assert_has_calls(expected_sub2_calls)

if __name__ == "__main__":
unittest.main()

12 changes: 3 additions & 9 deletions tests/test_state.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
#!/usr/bin/env python
from behavioral.state import Radio
# -*- coding: utf-8 -*-
import sys
import unittest
from behavioral.state import Radio

if sys.version_info < (2, 7):
import unittest2 as unittest
else:
import unittest

class RadioTest(unittest.TestCase):
"""
Expand Down Expand Up @@ -57,7 +55,3 @@ def test_shall_toggle_from_fm_to_am(self):
state = self.radio.state.name
expected_state_name = 'AM'
self.assertEqual(state, expected_state_name)

if __name__ == "__main__":
unittest.main()

15 changes: 2 additions & 13 deletions tests/test_strategy.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,8 @@
#!/usr/bin/env python
"""
Tests for strategy.py
"""

# -*- coding: utf-8 -*-
import os
import subprocess
import sys

if sys.version_info < (2, 7):
import unittest2 as unittest
else:
import unittest
import unittest


class StrategyTest(unittest.TestCase):
Expand All @@ -31,6 +23,3 @@ def test_print_output(self):
# byte representation required due to EOF returned subprocess
expected_output_as_bytes = expected_output.encode(encoding='UTF-8')
self.assertEqual(output, expected_output_as_bytes)

if __name__ == "__main__":
unittest.main()