-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_diets.py
88 lines (68 loc) · 4.3 KB
/
test_diets.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
from pprint import pprint
import pytest
from mcdiet import DietProblem, InfeasibleError
def test_no_purchase():
menu = {'Big Mac': {'price': 5, 'foods': {'big-mac': 1}}}
nutr = {'big-mac': {'cals': 100, 'protein': 25, 'carbs': 50, 'fat': 10, 'sat': 5, 'sugar': 0, 'sodium': 0}}
diet = DietProblem(menu=menu, nutr=nutr)
# No purchase
result = diet.solve(nutrition={'protein': (None, None)})
expected = {'cost': 0.0, 'purchase': []}
assert result == expected
def test_infeasible():
menu = {'Big Mac': {'price': 5, 'foods': {'big-mac': 1}}}
nutr = {'big-mac': {'cals': 100, 'protein': 25, 'carbs': 50, 'fat': 10, 'sat': 5, 'sugar': 0, 'sodium': 0}}
diet = DietProblem(menu=menu, nutr=nutr)
with pytest.raises(InfeasibleError):
# can never have 1 <= protein <= 2
result = diet.solve(nutrition={'protein': (2, 1)})
with pytest.raises(InfeasibleError):
# cannot fufill requirements
result = diet.solve(nutrition={'protein': (None, 30), 'cals': (200, None)})
def test_single_meal_single_food():
menu = {'Big Mac': {'price': 5, 'foods': {'big-mac': 1}}}
nutr = {'big-mac': {'cals': 100, 'protein': 25, 'carbs': 50, 'fat': 10, 'sat': 5, 'sugar': 0, 'sodium': 0}}
diet = DietProblem(menu=menu, nutr=nutr)
# Single big mac
result = diet.solve(nutrition={'protein': (20, None)})
expected = {'cost': 5.0, 'purchase': [{'meal': 'Big Mac', 'bought': 1, 'foods': [{'food': 'big-mac', 'available': 1, 'eat': 1}]}]}
assert result == expected
# Two big macs
result = diet.solve(nutrition={'protein': (40, None)})
expected = {'cost': 10.0, 'purchase': [{'meal': 'Big Mac', 'bought': 2, 'foods': [{'food': 'big-mac', 'available': 2, 'eat': 2}]}]}
assert result == expected
def test_single_meal_combo():
menu = {'Big Mac Combo': {'price': 5, 'foods': {'big-mac': 1, 'coke': 1}}}
nutr = {'big-mac': {'cals': 100, 'protein': 25, 'carbs': 50, 'fat': 10, 'sat': 5, 'sugar': 0, 'sodium': 0},
'coke': {'cals': 100, 'protein': 0, 'carbs': 30, 'fat': 0, 'sat': 0, 'sugar': 30, 'sodium': 0}}
diet = DietProblem(menu=menu, nutr=nutr)
# Single big mac- don't drink coke
result = diet.solve(nutrition={'protein': (20, None), 'sugar': (None, 10)})
expected = {'cost': 5.0, 'purchase': [{'meal': 'Big Mac Combo', 'bought': 1,
'foods': [{'food': 'big-mac', 'available': 1, 'eat': 1},
{'food': 'coke', 'available': 1, 'eat': 0}]}]}
assert result == expected
# Two big macs, one coke
result = diet.solve(nutrition={'protein': (40, None), 'sugar': (30, 40)})
expected = {'cost': 10.0, 'purchase': [{'meal': 'Big Mac Combo', 'bought': 2,
'foods': [{'food': 'big-mac', 'available': 2, 'eat': 2},
{'food': 'coke', 'available': 2, 'eat': 1}]}]}
assert result == expected
def test_two_meal_combo():
menu = {'Big Mac Coke': {'price': 5, 'foods': {'big-mac': 1, 'coke': 1}},
'Double Big Mac Fries': {'price': 8, 'foods': {'big-mac': 2, 'fries': 1}}}
nutr = {'big-mac': {'cals': 100, 'protein': 25, 'carbs': 50, 'fat': 10, 'sat': 5, 'sugar': 0, 'sodium': 0},
'coke': {'cals': 100, 'protein': 0, 'carbs': 30, 'fat': 0, 'sat': 0, 'sugar': 30, 'sodium': 0},
'fries': {'cals': 100, 'protein': 0, 'carbs': 20, 'fat': 10, 'sat': 5, 'sugar': 0, 'sodium': 200},}
diet = DietProblem(menu=menu, nutr=nutr)
# Two big macs, one coke, one fries
# WARNING- there are 2 equivalent solutions here- eat both big macs from the Double Big Mac Fries meal, or one from each
result = diet.solve(nutrition={'protein': (40, 60), 'sugar': (30, 40), 'sodium': (200, 300)})
expected = {'cost': 13.0,
'purchase': [{'meal': 'Big Mac Coke', 'bought': 1,
'foods': [{'food': 'big-mac', 'available': 1, 'eat': 1},
{'food': 'coke', 'available': 1, 'eat': 1}]},
{'meal': 'Double Big Mac Fries', 'bought': 1,
'foods': [{'food': 'big-mac', 'available': 2, 'eat': 1},
{'food': 'fries', 'available': 1, 'eat': 1}]}]}
assert result == expected