-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_scaler.py
125 lines (94 loc) · 5.42 KB
/
test_scaler.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
import scaler
class TestGetScaleFactor:
@staticmethod
def make_param(value):
req_params = {
'scale' : value
}
return req_params
def test_return_nothing_if_no_param(self):
assert scaler.get_scale_factor({}) == None
def test_return_nothing_if_non_numeric(self):
req_params = TestGetScaleFactor.make_param('lol')
assert scaler.get_scale_factor(req_params) == None
def test_return_number_if_numeric_scale(self):
assert scaler.get_scale_factor(TestGetScaleFactor.make_param('2')) == 2
assert scaler.get_scale_factor(TestGetScaleFactor.make_param('5')) == 5
assert scaler.get_scale_factor(TestGetScaleFactor.make_param('20')) == 20
def test_return_number_if_decimal_number_scale(self):
assert scaler.get_scale_factor(TestGetScaleFactor.make_param('0.5')) == 0.5
assert scaler.get_scale_factor(TestGetScaleFactor.make_param('.5')) == 0.5
assert scaler.get_scale_factor(TestGetScaleFactor.make_param('1.5')) == 1.5
def test_ignore_silly_scale(self):
assert scaler.get_scale_factor(TestGetScaleFactor.make_param('1')) == None
assert scaler.get_scale_factor(TestGetScaleFactor.make_param('0')) == None
assert scaler.get_scale_factor(TestGetScaleFactor.make_param('1.0')) == None
assert scaler.get_scale_factor(TestGetScaleFactor.make_param('0.0')) == None
assert scaler.get_scale_factor(TestGetScaleFactor.make_param('-1')) == None
assert scaler.get_scale_factor(TestGetScaleFactor.make_param('-1.0')) == None
def test_ignore_large_scales(self):
assert scaler.get_scale_factor(TestGetScaleFactor.make_param('50')) == 50
assert scaler.get_scale_factor(TestGetScaleFactor.make_param('51')) == None
assert scaler.get_scale_factor(TestGetScaleFactor.make_param('1000')) == None
class TestScaleIngredient:
@staticmethod
def make_ingredient(quantity):
ingredient = {
'name' : 'Onion',
'prep' : 'chopped',
'notes' : None,
'quantity' : quantity
}
return ingredient
@staticmethod
def make_scaled_ingredient(quantity):
ingredient = TestScaleIngredient.make_ingredient(quantity)
ingredient['scaled'] = True
return ingredient
def test_ignore_ingredients_with_no_quantity(self):
ingredient = {'name': 'Onion', 'quantity': None}
assert scaler.scale_ingredient(ingredient, 2) == {'name': 'Onion', 'quantity': None}
def test_ignore_ingredients_with_unparseable_quantity(self):
ingredient = {'name': 'Onion', 'quantity': 'About 3'}
assert scaler.scale_ingredient(ingredient, 2) == {'name': 'Onion', 'quantity': 'About 3'}
def test_scale_ingredient(self):
ingredient = {'name': 'Onion', 'quantity': '2'}
scaled_ingredient = scaler.scale_ingredient(ingredient, 2)
assert scaled_ingredient['quantity'] == '4'
assert scaled_ingredient['scaled'] == True
def test_scale_ingredient_by_fraction(self):
ingredient = {'name': 'Onion', 'quantity': '1'}
scaled_ingredient = scaler.scale_ingredient(ingredient, 1.5)
assert scaled_ingredient['quantity'] == '1.50'
assert scaled_ingredient['scaled'] == True
def test_scale_ingredient_with_suffix(self):
ingredient = TestScaleIngredient.make_ingredient('4g')
scaled_ingredient = TestScaleIngredient.make_scaled_ingredient('8g')
assert scaler.scale_ingredient(ingredient, 2.0) == scaled_ingredient
ingredient = TestScaleIngredient.make_ingredient('2 tbsp')
scaled_ingredient = TestScaleIngredient.make_scaled_ingredient('4 tbsp')
assert scaler.scale_ingredient(ingredient, 2.0) == scaled_ingredient
def test_scale_ingredient_with_fraction(self):
ingredient = TestScaleIngredient.make_ingredient('1/4')
scaled_ingredient = TestScaleIngredient.make_scaled_ingredient('1/2')
assert scaler.scale_ingredient(ingredient, 2.0) == scaled_ingredient
def test_scale_ingredient_with_uneven_fraction(self):
ingredient = TestScaleIngredient.make_ingredient('1/4')
scaled_ingredient = TestScaleIngredient.make_scaled_ingredient('1 1/4')
assert scaler.scale_ingredient(ingredient, 5.0) == scaled_ingredient
def test_scale_fractional_ingredient_to_int(self):
ingredient = TestScaleIngredient.make_ingredient('1/2')
scaled_ingredient = TestScaleIngredient.make_scaled_ingredient('1')
assert scaler.scale_ingredient(ingredient, 2.0) == scaled_ingredient
def test_scale_messy_fraction_to_decimal(self):
ingredient = TestScaleIngredient.make_ingredient('1/2')
scaled_ingredient = TestScaleIngredient.make_scaled_ingredient('0.90')
assert scaler.scale_ingredient(ingredient, 1.8) == scaled_ingredient
def test_scale_ingredient_with_uneven_fraction_with_suffix(self):
ingredient = TestScaleIngredient.make_ingredient('1/4 tsp')
scaled_ingredient = TestScaleIngredient.make_scaled_ingredient('1 1/4 tsp')
assert scaler.scale_ingredient(ingredient, 5.0) == scaled_ingredient
def test_scale_ingredient_with_range(self):
ingredient = TestScaleIngredient.make_ingredient('2-3')
scaled_ingredient = TestScaleIngredient.make_scaled_ingredient('4-6')
assert scaler.scale_ingredient(ingredient, 2.0) == scaled_ingredient