forked from faif/python-patterns
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_abstract_factory.py
More file actions
65 lines (48 loc) · 1.81 KB
/
test_abstract_factory.py
File metadata and controls
65 lines (48 loc) · 1.81 KB
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import unittest
from creational.abstract_factory import PetShop,\
Dog, Cat, DogFactory, CatFactory, Pet
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')
class PetTest(unittest.TestCase):
def test_from_name(self):
test_cases = [("kitty", "Miao"), ("duck", "Quak")]
for name, expected_speech in test_cases:
pet = Pet.from_name(name)
self.assertEqual(pet.speak(), expected_speech)