Skip to content

Commit 01a59f2

Browse files
committed
added unittest folder and tests, added factory pattern, added results in html format
1 parent bc2562c commit 01a59f2

7 files changed

+890
-10
lines changed

Test Results - .html

+767
Large diffs are not rendered by default.

factory.py

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# Define a Pizza interface
2+
from abc import abstractmethod
3+
4+
5+
class Pizza:
6+
@abstractmethod
7+
def prepare(self):
8+
return "Concrete implementation required"
9+
10+
11+
@abstractmethod
12+
def bake(self):
13+
pass
14+
15+
@abstractmethod
16+
def cut(self):
17+
pass
18+
19+
@abstractmethod
20+
def box(self):
21+
pass
22+
23+
# Concrete Pizza classes
24+
class MargheritaPizza(Pizza):
25+
26+
def prepare(self):
27+
print("Preparing Margherita Pizza")
28+
29+
def bake(self):
30+
print("Baking Margherita Pizza")
31+
32+
def cut(self):
33+
print("Cutting Margherita Pizza")
34+
35+
def box(self):
36+
print("Boxing Margherita Pizza")
37+
38+
class PepperoniPizza(Pizza):
39+
def prepare(self):
40+
print("Preparing Pepperoni Pizza")
41+
42+
def bake(self):
43+
print("Baking Pepperoni Pizza")
44+
45+
def cut(self):
46+
print("Cutting Pepperoni Pizza")
47+
48+
def box(self):
49+
print("Boxing Pepperoni Pizza")
50+
51+
# Pizza Factory
52+
class PizzaFactory:
53+
"""It creates and returns an instance of the appropriate subclass based
54+
on the pizza_type parameter."""
55+
def create_pizza(self, pizza_type):
56+
if pizza_type == 'Margherita':
57+
return MargheritaPizza()
58+
elif pizza_type == 'Pepperoni':
59+
return PepperoniPizza()
60+
else:
61+
raise ValueError("Invalid pizza type")
62+
63+
# Client code
64+
if __name__ == "__main__":
65+
# Creating a Margherita Pizza using the factory
66+
factory = PizzaFactory()
67+
margherita_pizza = factory.create_pizza('Margherita')
68+
69+
# Using the created Margherita Pizza
70+
margherita_pizza.prepare()
71+
margherita_pizza.bake()
72+
margherita_pizza.cut()
73+
margherita_pizza.box()
74+
75+
# Creating a Pepperoni Pizza using the factory
76+
pepperoni_pizza = factory.create_pizza('Pepperoni')
77+
78+
# Using the created Pepperoni Pizza
79+
pepperoni_pizza.prepare()
80+
pepperoni_pizza.bake()
81+
pepperoni_pizza.cut()
82+
pepperoni_pizza.box()

readme.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Using Classes for encapsulation compared to global variables
22

3-
This is quick reference for using alternatives to global variables.
4-
It is not a guide as to which pattern is the best.
3+
This is my quick reference for using alternatives to global variables.
4+
It is not by any means a guide as to which pattern is the best.
55

66
The choice between using classes and encapsulation versus global variables depends on the specific requirements and design principles of your code.
77

tests/test_factory.py

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import unittest
2+
3+
from factory import PizzaFactory, MargheritaPizza, PepperoniPizza, Pizza
4+
5+
6+
# Assume you have a PizzaFactory class and different Pizza subclasses (e.g.,
7+
# MargheritaPizza, PepperoniPizza)
8+
9+
class TestPizzaFactory(unittest.TestCase):
10+
def test_create_margherita_pizza(self):
11+
pizza_factory = PizzaFactory()
12+
pizza = pizza_factory.create_pizza("Margherita")
13+
self.assertIsInstance(pizza, MargheritaPizza)
14+
# Add more specific assertions related to MargheritaPizza creation if needed
15+
16+
def test_create_pepperoni_pizza(self):
17+
pizza_factory = PizzaFactory()
18+
pizza = pizza_factory.create_pizza("Pepperoni")
19+
self.assertIsInstance(pizza, PepperoniPizza)
20+
# Add more specific assertions related to PepperoniPizza creation if needed
21+
22+
23+
def test_abstract_class_instantiation(self):
24+
with self.assertRaises(Exception) as context:
25+
abstract_instance= Pizza()
26+
self.assertEqual(str(context.exception), "Concrete "
27+
"implementation required")
28+
29+
30+
if __name__ == '__main__':
31+
unittest.main()

tests/test_monostate.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from monostate import Monostate
33

44

5-
class MyTestCase(unittest.TestCase):
5+
class Borg_TestCase(unittest.TestCase):
66
@classmethod
77
def setUpClass(cls):
88
cls.borg1 = Monostate()

tests/test_observer.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from observer import Subscriber
44

55

6-
class MyTestCase(unittest.TestCase):
6+
class Observer_TestCase(unittest.TestCase):
77
@classmethod
88
def setUpClass(cls):
99
print("setUpClass")
@@ -21,11 +21,13 @@ def test_register_2_observers(self):
2121
self.assertEqual(len(self.channelPublisher.subscribers), 2)
2222

2323
def test_sequence(self):
24-
""" register , unregister, notify"""
24+
""" notify,
25+
unregister ,
26+
unregister,
27+
notify"""
2528
self.channelPublisher.notify()
2629
self.channelPublisher.unregister(self.bob)
2730
self.assertEqual(len(self.channelPublisher.subscribers), 1)
28-
subscribers = self.channelPublisher.subscribers
2931

3032
for subscriber in self.channelPublisher.subscribers:
3133
self.assertEqual(subscriber.name, "FooBar")
@@ -38,7 +40,5 @@ def test_sequence(self):
3840
self.channelPublisher.unregister(self.FooBar)
3941
self.assertNotIn(self.FooBar, self.channelPublisher.subscribers)
4042

41-
# assert that no more subscibers are left
43+
# verify that no more subscribers are left
4244
self.assertEqual(len(self.channelPublisher.subscribers), 0)
43-
44-

tests/test_singleton.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from singleton import Singleton
33

44

5-
class MyTestCase(unittest.TestCase):
5+
class Singleton_TestCase(unittest.TestCase):
66
@classmethod
77
def setUpClass(cls):
88
cls.s1 = Singleton.get_instance()

0 commit comments

Comments
 (0)