Skip to content

Commit

Permalink
Copes with no nutriments
Browse files Browse the repository at this point in the history
  • Loading branch information
john-gom committed Nov 20, 2023
1 parent 915b606 commit ecee6c4
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 1 deletion.
2 changes: 1 addition & 1 deletion prepare_nutrients.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def count_ingredients(ingredients, nutrients):

def assign_weightings(product):
# Determine which nutrients will be used in the analysis by assigning a weighting
product_nutrients = product['nutriments']
product_nutrients = product.get('nutriments', {})
count = product['recipe_estimator']['ingredient_count']
computed_nutrients = product['recipe_estimator']['nutrients']

Expand Down
29 changes: 29 additions & 0 deletions prepare_nutrients_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,32 @@ def test_prepare_nutrients():
assert energy.get('weighting') is None


def test_prepare_nutrients_copes_with_no_product_nutrients():
product = {
'ingredients': [{
'id':'en:tomato',
'nutrients': {
'carbohydrates': {'percent_min': 2.5,'percent_max': 2.5},
'energy': {'percent_min': 80,'percent_max': 80},
'water': {'percent_min': 90,'percent_max': 90},
}
}]}

prepare_nutrients(product)

metrics = product.get('recipe_estimator')
assert metrics is not None

# Ingredient count is calculated
assert metrics['ingredient_count'] == 1

nutrients = metrics.get('nutrients')
assert nutrients is not None
nutrient = nutrients.get('carbohydrates')
assert nutrient is not None

# Nutrient information flagged
assert nutrient.get('notes') is not None
assert nutrient.get('weighting') is None


27 changes: 27 additions & 0 deletions recipe_estimator_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,3 +164,30 @@ def test_estimate_recipe_simple_recipe_with_no_matched_ingredients():
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

0 comments on commit ecee6c4

Please sign in to comment.