-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_create_menu.py
176 lines (146 loc) · 5.22 KB
/
test_create_menu.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import unittest
from unittest import mock
from create_menu import (
select_recipes_brute_force,
CreateMenuUseCase,
DietaryPreferenceNotValid,
MenuRepository,
CannotCreateMenu,
)
from create_recipes import (
Recipe,
RecipeRepository,
Ingredient,
RecipeId,
MacroNutrients,
)
test_ingredient = Ingredient(
id="Test",
macronutrients=MacroNutrients(
carbohydrates=0,
proteins=0,
fats=0,
),
kilocalories=0,
)
test_recipe = Recipe(
id=RecipeId("12345678123456781234567812345678"),
name="test",
ingredients=[(1, test_ingredient)],
yield_=1,
)
class BruteForceTestCase(unittest.TestCase):
def test_invalid_size_raises_error(self):
recipes = [test_recipe]
size = 0
self.assertRaises(ValueError, select_recipes_brute_force, recipes, size)
def test_empty_recipes_raises_error(self):
recipes = []
size = 1
self.assertRaises(ValueError, select_recipes_brute_force, recipes, size)
def test_single_recipe_size_1(self):
recipes = [test_recipe]
size = 1
menu = select_recipes_brute_force(recipes, size)
self.assertEqual(len(menu), 1)
self.assertEqual(menu[0], test_recipe)
def test_single_recipe_size_3(self):
recipes = [test_recipe]
size = 3
menu = select_recipes_brute_force(recipes, size)
self.assertEqual(len(menu), 3)
self.assertEqual(menu[0], test_recipe)
self.assertEqual(menu[1], test_recipe)
self.assertEqual(menu[2], test_recipe)
def test_2_recipes_size_3(self):
recipes = [
Recipe(
id=RecipeId("12345678123456781234567812345678"),
name="test",
ingredients=[(1, test_ingredient)],
yield_=1,
),
Recipe(
id=RecipeId("23456781234567812345678123456781"),
name="test",
ingredients=[
(1, test_ingredient),
(2, test_ingredient),
],
yield_=1,
),
]
size = 3
menu = select_recipes_brute_force(recipes, size)
self.assertEqual(len(menu), 3)
for recipe in menu:
self.assertTrue(recipe in recipes)
def test_ingredient_preferences(self):
allowed_ingredient = Ingredient(
id="Allowed",
macronutrients=MacroNutrients(
carbohydrates=10,
proteins=10,
fats=10,
),
kilocalories=10,
)
forbidden_ingredient = Ingredient(
id="Forbidden",
macronutrients=MacroNutrients(
carbohydrates=10,
proteins=10,
fats=10,
),
kilocalories=10,
)
def forbid_ingredient(menu):
for recipe in menu:
if recipe.contains(forbidden_ingredient):
return float("inf")
return 0
# Having forbidden recipe first, forces it to be checked first.
recipes = [
# Forbidden.
Recipe(
id=RecipeId("12345678123456781234567812345678"),
name="forbidden recipe",
ingredients=[(1, forbidden_ingredient)],
yield_=1,
),
# Allowed.
Recipe(
id=RecipeId("23456781234567812345678123456781"),
name="allowed recipe",
ingredients=[(1, allowed_ingredient)],
yield_=1,
),
]
menu = select_recipes_brute_force(recipes, 1, forbid_ingredient)
self.assertEqual(len(menu), 1)
self.assertTrue(menu[0] == recipes[1])
class TestCreateMenuUseCase(unittest.TestCase):
def test_no_recipes(self):
mock_recipe_repo = mock.create_autospec(RecipeRepository)
mock_menu_repo = mock.create_autospec(MenuRepository)
mock_recipe_repo.all.return_value = []
use_case = CreateMenuUseCase(mock_recipe_repo, mock_menu_repo)
with mock.patch("create_menu.create_preferences", return_value=[]):
self.assertRaises(CannotCreateMenu, use_case, 1, [])
mock_menu_repo.add.assert_not_called()
def test_preference_value_error(self):
mock_recipe_repo = mock.create_autospec(RecipeRepository)
mock_menu_repo = mock.create_autospec(MenuRepository)
mock_recipe_repo.all.return_value = []
use_case = CreateMenuUseCase(mock_recipe_repo, mock_menu_repo)
with mock.patch("create_menu.create_preferences", side_effect=ValueError):
self.assertRaises(DietaryPreferenceNotValid, use_case, 1, [])
mock_menu_repo.add.assert_not_called()
def test_preference_type_error(self):
mock_recipe_repo = mock.create_autospec(RecipeRepository)
mock_menu_repo = mock.create_autospec(MenuRepository)
mock_recipe_repo.all.return_value = []
use_case = CreateMenuUseCase(mock_recipe_repo, mock_menu_repo)
with mock.patch("create_menu.create_preferences", side_effect=TypeError):
self.assertRaises(DietaryPreferenceNotValid, use_case, 1, [])
mock_menu_repo.add.assert_not_called()