-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
recipe_estimator_test.py
193 lines (157 loc) · 5.52 KB
/
recipe_estimator_test.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
from recipe_estimator import estimate_recipe
def test_estimate_recipe_accounts_for_evaporation():
product = {
'ingredients': [{
'id':'en:tomato',
'nutrients': {
'carbohydrates': {'percent_min': 2.5,'percent_max': 2.5},
'water': {'percent_min': 90,'percent_max': 90},
}
}],
'nutriments': {
'carbohydrates': 5,
}}
estimate_recipe(product)
metrics = product.get('recipe_estimator')
assert metrics is not None
# Status is valid
assert metrics['status'] == 0
ingredient = product['ingredients'][0]
# Percent estimate is relative to total ingredient quantities
percent_estimate = ingredient.get('percent_estimate')
assert round(percent_estimate) == 100
# Quantity estimate gives original quantity of ingredient per 100g/ml of product
quantity_estimate = ingredient.get('quantity_estimate')
assert round(quantity_estimate) == 200
evaporation = ingredient.get('evaporation')
assert round(evaporation) == 100
def test_estimate_recipe_evaporation_is_constrained():
product = {
'ingredients': [{
'id':'en:tomato',
'nutrients': {
'carbohydrates': {'percent_min': 2.5,'percent_max': 2.5},
'water': {'percent_min': 10,'percent_max': 10},
}
}],
'nutriments': {
'carbohydrates': 5,
}}
estimate_recipe(product)
metrics = product.get('recipe_estimator')
assert metrics is not None
# Status is valid
assert metrics['status'] == 0
ingredient = product['ingredients'][0]
# If tomatoes have a maximum water content of 10% then an original quantity of N times 90% = 100%, so N = 100 / 90 = 111
quantity_estimate = ingredient.get('quantity_estimate')
assert round(quantity_estimate) == 111
evaporation = ingredient.get('evaporation')
assert round(evaporation) == 11
def test_estimate_recipe_simple_recipe():
# A x 15 + B x 3 = 10
# A + B = 1
# 15A + 3 - 3A = 10
# A = 7 / 12 = 58%
product = {
'ingredients': [
{
'id':'one',
'nutrients': {
'carbohydrates': {'percent_min': 15,'percent_max': 15},
}
},
{
'id':'two',
'nutrients': {
'carbohydrates': {'percent_min': 3,'percent_max': 3},
}
}
],
'nutriments': {
'carbohydrates': 10,
}}
estimate_recipe(product)
metrics = product.get('recipe_estimator')
assert metrics is not None
# Status is valid
assert metrics['status'] == 0
assert round(product['ingredients'][0]['percent_estimate']) == 58
assert round(product['ingredients'][1]['percent_estimate']) == 42
def test_estimate_recipe_simple_recipe_with_one_unmatched_ingredient():
product = {
'ingredients': [
{
'id':'one',
'nutrients': {
'carbohydrates': {'percent_min': 15,'percent_max': 15},
}
},
{
'id':'two',
'nutrients': {
'carbohydrates': {'percent_min': 0,'percent_max': 100},
}
}
],
'nutriments': {
'carbohydrates': 10,
}}
estimate_recipe(product)
metrics = product.get('recipe_estimator')
assert metrics is not None
# Status is valid
assert metrics['status'] == 0
assert round(product['ingredients'][0]['percent_estimate']) >= 50
assert round(product['ingredients'][1]['percent_estimate']) <= 50
assert round(product['ingredients'][0]['percent_estimate'] + product['ingredients'][1]['percent_estimate']) == 100
def test_estimate_recipe_simple_recipe_with_no_matched_ingredients():
product = {
'ingredients': [
{
'id':'one',
'nutrients': {
'carbohydrates': {'percent_min': 0,'percent_max': 100},
}
},
{
'id':'two',
'nutrients': {
'carbohydrates': {'percent_min': 0,'percent_max': 100},
}
}
],
'nutriments': {
'carbohydrates': 10,
}}
estimate_recipe(product)
metrics = product.get('recipe_estimator')
assert metrics is not None
# Status is valid
assert metrics['status'] == 0
assert round(product['ingredients'][0]['percent_estimate']) >= 50
assert round(product['ingredients'][1]['percent_estimate']) <= 50
assert round(product['ingredients'][0]['percent_estimate'] + product['ingredients'][1]['percent_estimate']) == 100
def test_estimate_recipe_simple_recipe_with_no_nutriments():
product = {
'ingredients': [
{
'id':'one',
'nutrients': {
'carbohydrates': {'percent_min': 15,'percent_max': 15},
}
},
{
'id':'two',
'nutrients': {
'carbohydrates': {'percent_min': 3,'percent_max': 3},
}
}
]}
estimate_recipe(product)
metrics = product.get('recipe_estimator')
assert metrics is not None
# Status is valid
assert metrics['status'] == 0
assert round(product['ingredients'][0]['percent_estimate']) >= 50
assert round(product['ingredients'][1]['percent_estimate']) <= 50