forked from faif/python-patterns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_abstract_factory.py
67 lines (47 loc) · 1.65 KB
/
test_abstract_factory.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from abstract_factory import PetShop, Dog, Cat, DogFactory, CatFactory
import sys
if sys.version_info < (2, 7):
import unittest2 as unittest
else:
import unittest
try:
from unittest.mock import patch
except ImportError:
from mock import patch
class TestPetShop(unittest.TestCase):
def test_dog_pet_shop_shall_show_dog_instance(self):
f = DogFactory()
with patch.object(f, 'get_pet') as mock_f_get_pet,\
patch.object(f, 'get_food') as mock_f_get_food:
ps = PetShop(f)
ps.show_pet()
self.assertEqual(mock_f_get_pet.call_count, 1)
self.assertEqual(mock_f_get_food.call_count, 1)
def test_cat_pet_shop_shall_show_cat_instance(self):
f = CatFactory()
with patch.object(f, 'get_pet') as mock_f_get_pet,\
patch.object(f, 'get_food') as mock_f_get_food:
ps = PetShop(f)
ps.show_pet()
self.assertEqual(mock_f_get_pet.call_count, 1)
self.assertEqual(mock_f_get_food.call_count, 1)
class TestCat(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.c = Cat()
def test_cat_shall_meow(cls):
cls.assertEqual(cls.c.speak(), 'meow')
def test_cat_shall_be_printable(cls):
cls.assertEqual(str(cls.c), 'Cat')
class TestDog(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.d = Dog()
def test_dog_shall_woof(cls):
cls.assertEqual(cls.d.speak(), 'woof')
def test_dog_shall_be_printable(cls):
cls.assertEqual(str(cls.d), 'Dog')
if __name__ == "__main__":
unittest.main()