-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_recipe_parser.py
More file actions
91 lines (73 loc) · 3.43 KB
/
test_recipe_parser.py
File metadata and controls
91 lines (73 loc) · 3.43 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
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
from ibdb.recipe_parser import RecipeParser
class TestRecipeParser(object):
# TODO: use pytest to test functions in this module
def test_parse_ingredient(self):
# Tests to validate parse_ingredient()
tests = [
'Bread', (None, None, 'Bread'),
'6 stalks celery', (6.0, None, 'Stalks Celery'),
'4 eggs', (4.0, None, 'Eggs'),
'2 ½ pounds of full fat cream cheese, cut', (2.5, 'pound', 'Full Fat Cream Cheese'),
'25 oreos, finely processed', (25.0, None, 'Oreos'),
'1-2 variable ingredients', ('1-2', None, 'Variable Ingredients'),
'2 1/2 things', (2.5, None, 'Things'),
'1/2 things', (0.5, None, 'Things'),
'1 large, long sourdough loaf', (1.0, 'large', 'Long Sourdough Loaf'),
'100ml Water', (100.0, 'ml', 'Water'),
'1L Water', (1.0, 'L', 'Water')
]
assert len(tests) % 2 == 0, 'A test is missing its expected output'
for (i, v) in zip(tests[::2], tests[1::2]):
actual = RecipeParser.parse_ingredient(i)
expected = RecipeParser.Ingredient(*v, i)
assert actual == expected, "parse_ingredient() incorrectly parsed '{}'\nExpected:\n{}\nActual:\n{}".format(i, expected, actual)
def test_normalize_qty(self):
# Tests to validate normalize_qty()
# Test the special case for None
assert RecipeParser.normalize_qty(None) is None
assert RecipeParser.normalize_qty('') is None
# Numeric conversions
tests = [
'1', 1.0,
'1/2', 0.5,
'1 2/3', (1 + 2 / 3),
'1 ⅔', (1 + 2 / 3)
]
assert len(tests) % 2 == 0, 'A test is missing its expected output'
for (i, v) in zip(tests[::2], tests[1::2]):
actual = RecipeParser.normalize_qty(i)
expected = v
assert abs(actual - expected) < 0.00001, "normalize_qty() incorrectly parsed '{}'\nExpected:\n{}\nActual:\n{}".format(i, expected, actual)
def test_normalize_unit(self):
# Tests to validate normalize_unit()
tests = [
None, None,
# Abreviations
't', 'teaspoon',
'tsp', 'teaspoon',
'T', 'tablespoon',
'Tbsp', 'tablespoon',
'unknown', 'unknown',
# Normalize to lowercase
'CUP', 'cup',
'Pound', 'pound',
]
assert len(tests) % 2 == 0, 'A test is missing its expected output'
for (i, v) in zip(tests[::2], tests[1::2]):
actual = RecipeParser.normalize_unit(i)
expected = v
assert actual == expected, "normalize_unit() incorrectly parsed '{}'\nExpected:\n{}\nActual:\n{}".format(i, expected, actual)
def test_normalize_name(self):
# Tests to validate normalize_name()
tests = [
'cAnDy CaNe', 'Candy Cane', # Capitalized words
'a\t\nb', 'A B', # Tabs and newlines
'Whole Milk ', 'Milk', # Remove qualifiers
'Hot Water', 'Water', # Remove temps
'Cold Milk', 'Milk',
]
assert len(tests) % 2 == 0, 'A test is missing its expected output'
for (i, v) in zip(tests[::2], tests[1::2]):
actual = RecipeParser.normalize_name(i)
expected = v
assert actual == expected, "normalize_name() incorrectly parsed '{}'\nExpected:\n{}\nActual:\n{}".format(i, expected, actual)