added dummy class and began writing unit tests
This commit is contained in:
0
creational/__init__.py
Normal file
0
creational/__init__.py
Normal file
0
creational/singleton.py
Normal file
0
creational/singleton.py
Normal file
0
tests/__init__.py
Normal file
0
tests/__init__.py
Normal file
0
tests/creational_tests/__init__.py
Normal file
0
tests/creational_tests/__init__.py
Normal file
14
tests/creational_tests/test_singleton.py
Normal file
14
tests/creational_tests/test_singleton.py
Normal file
@@ -0,0 +1,14 @@
|
||||
from unittest import TestCase
|
||||
from creational import singleton
|
||||
|
||||
|
||||
class SingletonTestCase(TestCase):
|
||||
"""
|
||||
Unit testing class for the singleton design pattern.
|
||||
"""
|
||||
def setUp(self):
|
||||
"""
|
||||
Initialize testing data.
|
||||
|
||||
:return:
|
||||
"""
|
||||
63
tests/test_utils.py
Normal file
63
tests/test_utils.py
Normal file
@@ -0,0 +1,63 @@
|
||||
from unittest import TestCase
|
||||
from tests.utils.dummy_class import DummyClass
|
||||
|
||||
|
||||
class DummyClassTestCase(TestCase):
|
||||
"""
|
||||
Unit testing class for the DummyClass class.
|
||||
"""
|
||||
def setUp(self):
|
||||
"""
|
||||
Initialize testing data.
|
||||
"""
|
||||
self.a = 10
|
||||
self.b = 5
|
||||
self.add_function = lambda instance: instance.a + instance.b
|
||||
self.substract_function = lambda instance: instance.a - instance.b
|
||||
|
||||
def test_init(self):
|
||||
"""
|
||||
Test the __init__ method.
|
||||
|
||||
@raise AssertionError: If the test fails.
|
||||
"""
|
||||
dummy = DummyClass(attributes={'a': self.a, 'b': self.b},
|
||||
functions={'add': self.add_function, 'subtract': self.substract_function})
|
||||
|
||||
self.assertEquals(self.a, dummy.a)
|
||||
self.assertEquals(self.b, dummy.b)
|
||||
|
||||
def test_invalid_init(self):
|
||||
"""
|
||||
Test the __init__ method with invalid parameters.
|
||||
|
||||
@raise AssertionError if the test fails:
|
||||
"""
|
||||
with self.assertRaises(ValueError):
|
||||
DummyClass(attributes={'a': self.add_function}, functions={'add': self.a})
|
||||
|
||||
def test_add(self):
|
||||
"""
|
||||
Test a dummy class initialized with an add function.
|
||||
|
||||
@raise AssertionError: If the test fails.
|
||||
"""
|
||||
dummy = DummyClass(attributes={'a': self.a, 'b': self.b},
|
||||
functions={'add': self.add_function})
|
||||
|
||||
self.assertEquals(15, dummy.add())
|
||||
|
||||
def test_subtract(self):
|
||||
"""
|
||||
Test a dummy class initialized with a subtract function.
|
||||
|
||||
@raise AssertionError: If the test fails.
|
||||
"""
|
||||
dummy = DummyClass(attributes={'a': self.a, 'b': self.b},
|
||||
functions={'subtract': self.substract_function})
|
||||
|
||||
self.assertEquals(5, dummy.subtract())
|
||||
|
||||
|
||||
|
||||
|
||||
0
tests/utils/__init__.py
Normal file
0
tests/utils/__init__.py
Normal file
27
tests/utils/dummy_class.py
Normal file
27
tests/utils/dummy_class.py
Normal file
@@ -0,0 +1,27 @@
|
||||
from types import MethodType
|
||||
|
||||
|
||||
class DummyClass(object):
|
||||
"""
|
||||
Class representing dummy data.
|
||||
"""
|
||||
def __init__(self, attributes, functions):
|
||||
"""
|
||||
Initialize a new DummyClass instance.
|
||||
|
||||
@param attributes: A dictionary of instance variables for this dummy instance.
|
||||
@type attributes: dict
|
||||
@param functions: A dictionary of functions for this dummy instance.
|
||||
@type functions: dict
|
||||
"""
|
||||
for key, value in attributes.items():
|
||||
if callable(value):
|
||||
raise ValueError
|
||||
else:
|
||||
setattr(self, key, value)
|
||||
|
||||
for key, value in functions.items():
|
||||
if not callable(value):
|
||||
raise ValueError
|
||||
else:
|
||||
setattr(self, key, MethodType(value, self))
|
||||
Reference in New Issue
Block a user