Skip to content

Commit

Permalink
fix: quality check when we have erythritol without polyols (#7946)
Browse files Browse the repository at this point in the history
* feat: add erythritol as a nutrient #7837

* erythritol is now in the nutrients taxonomy

* fix: quality check when we have erythritol without polyols
  • Loading branch information
stephanegigandet authored Jan 5, 2023
1 parent b4b76ba commit ef47e9d
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 3 deletions.
13 changes: 12 additions & 1 deletion lib/ProductOpener/DataQualityFood.pm
Original file line number Diff line number Diff line change
Expand Up @@ -570,10 +570,21 @@ sub check_nutrition_data_energy_computation ($product_ref) {

my $energy_per_gram = $energy_from_nutrients{europe}{$nid}{$unit};
my $grams = 0;
# handles nutriment1__minus__numtriment2 case
# handles nutriment1__minus__nutriment2 case
if ($nid =~ /_minus_/) {
my $nid_minus = $';
$nid = $`;

# If we are computing carbohydrates minus polyols, and we do not have a value for polyols
# but we have a value for erythritol (which is a polyol), then we need to remove erythritol
if (($nid_minus eq "polyols") and (not defined $product_ref->{nutriments}{$nid_minus . "_value"})) {
$nid_minus = "erythritol";
}
# Similarly for polyols minus erythritol
if (($nid eq "polyols") and (not defined $product_ref->{nutriments}{$nid . "_value"})) {
$nid = "erythritol";
}

$grams -= $product_ref->{nutriments}{$nid_minus . "_value"} || 0;
}
$grams += $product_ref->{nutriments}{$nid . "_value"} || 0;
Expand Down
57 changes: 55 additions & 2 deletions tests/unit/dataqualityfood.t
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ $product_ref = {
ProductOpener::DataQuality::check_quality($product_ref);
is($product_ref->{nutriments}{"energy-kj_value_computed"}, 1436);
ok(has_tag($product_ref, 'data_quality', 'en:energy-value-in-kj-does-not-match-value-computed-from-other-nutrients'),
'energy not matching nutrient')
'energy not matching nutrients')
or diag explain $product_ref;

# energy does not match nutrients
Expand All @@ -372,7 +372,60 @@ $product_ref = {
ProductOpener::DataQuality::check_quality($product_ref);
ok(
!has_tag($product_ref, 'data_quality', 'en:energy-value-in-kj-does-not-match-value-computed-from-other-nutrients'),
'energy not matching nutrient'
'energy matching nutrients'
) or diag explain $product_ref;

# Polyols in general contribute energy
$product_ref = {
nutriments => {
"energy-kj_value" => 0,
"carbohydrates_value" => 100,
"polyols_value" => 100,
"fat_value" => 0,
"proteins_value" => 0,
"fiber_value" => 0,
}
};
ProductOpener::DataQuality::check_quality($product_ref);
ok(has_tag($product_ref, 'data_quality', 'en:energy-value-in-kj-does-not-match-value-computed-from-other-nutrients'),
'energy not matching nutrients - polyols')
or diag explain $product_ref;

# Erythritol is a polyol which does not contribute to energy
$product_ref = {
nutriments => {
"energy-kj_value" => 0,
"carbohydrates_value" => 100,
"polyols_value" => 100,
"erythritol_value" => 100,
"fat_value" => 0,
"proteins_value" => 0,
"fiber_value" => 0,
}
};
ProductOpener::DataQuality::check_quality($product_ref);
ok(
!has_tag($product_ref, 'data_quality', 'en:energy-value-in-kj-does-not-match-value-computed-from-other-nutrients'),
'energy matching nutrient - erythritol'
) or diag explain $product_ref;

# Erythritol is a polyol which does not contribute to energy
# If we do not have a value for polyols but we have a value for erythritol,
# we should assume that the polyols are equal to erythritol when we check the nutrients to energy computation
$product_ref = {
nutriments => {
"energy-kj_value" => 0,
"carbohydrates_value" => 100,
"erythritol_value" => 100,
"fat_value" => 0,
"proteins_value" => 0,
"fiber_value" => 0,
}
};
ProductOpener::DataQuality::check_quality($product_ref);
ok(
!has_tag($product_ref, 'data_quality', 'en:energy-value-in-kj-does-not-match-value-computed-from-other-nutrients'),
'energy matching nutrient - erythritol without polyols'
) or diag explain $product_ref;

# Polyols in general contribute energy
Expand Down

0 comments on commit ef47e9d

Please sign in to comment.