Skip to content

Commit

Permalink
Merge pull request #10 from openfoodfacts/ciqual-proxy-food-code
Browse files Browse the repository at this point in the history
fix: use ciqual_proxy_food_code
  • Loading branch information
stephanegigandet authored Dec 13, 2023
2 parents 145946d + 3471864 commit 568420d
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 6 deletions.
7 changes: 4 additions & 3 deletions ciqual/nutrients.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ def parse_value(ciqual_nutrient):
def get_ciqual_code(ingredient_id):
ingredient = ingredients_taxonomy.get(ingredient_id, None)
if ingredient is None:
print(ingredient_id + ' not found')
print(ingredient_id + ' not found')
return None

ciqual_code = ingredient.get('ciqual_food_code', None)
ciqual_code = ingredient.get('ciqual_food_code', ingredient.get('ciqual_proxy_food_code', None))
if ciqual_code:
return ciqual_code['en']

Expand All @@ -77,7 +77,7 @@ def setup_ingredients(ingredients):
setup_ingredients(ingredient['ingredients'])

else:
ciqual_code = ingredient.get('ciqual_food_code')
ciqual_code = ingredient.get('ciqual_food_code', ingredient.get('ciqual_proxy_food_code', None))
if (ciqual_code is None):
ciqual_code = get_ciqual_code(ingredient['id'])

Expand All @@ -98,6 +98,7 @@ def setup_ingredients(ingredients):
ingredient_nutrients[nutrient['off_id']] = {'percent_min': value, 'percent_max': value}

ingredient['nutrients'] = ingredient_nutrients
ingredient['ciqual_food_code_used'] = ciqual_code


def prepare_product(product):
Expand Down
7 changes: 4 additions & 3 deletions ciqual/nutrients_test.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import pytest
from ciqual.nutrients import prepare_product
from ciqual.nutrients import get_ciqual_code, prepare_product

def test_prepare_product_populates_nutrients():
product = {'ingredients': [{'id':'en:tomato', 'ciqual_food_code': '20047'}]}
Expand Down Expand Up @@ -39,4 +38,6 @@ def test_prepare_product_creates_a_max_range_entry_if_ingredient_not_found():
assert carbs['percent_min'] >= 0
assert carbs['percent_max'] <= 100


def test_get_ciqual_code_should_use_proxy_if_no_main_code():
ciqual_code = get_ciqual_code('en:tomato-sauce')
assert ciqual_code == '11107'
Binary file modified requirements.txt
Binary file not shown.

0 comments on commit 568420d

Please sign in to comment.