Skip to content

Commit

Permalink
feat: dq_kcal_does_not_match_exclude_more (#9339)
Browse files Browse the repository at this point in the history
  • Loading branch information
benbenben2 authored Nov 28, 2023
1 parent 187902b commit 1d3d352
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 2 deletions.
14 changes: 13 additions & 1 deletion lib/ProductOpener/DataQualityFood.pm
Original file line number Diff line number Diff line change
Expand Up @@ -939,7 +939,19 @@ sub check_nutrition_data_energy_computation ($product_ref) {
my ($ignore_energy_calculated_error, $category_id)
= get_inherited_property_from_categories_tags($product_ref, "ignore_energy_calculated_error:en");

if (not((defined $ignore_energy_calculated_error) and ($ignore_energy_calculated_error eq 'yes'))) {
if (
(
not((defined $ignore_energy_calculated_error) and ($ignore_energy_calculated_error eq 'yes'))
# consider only when energy is high enough to minimize false positives (issue #7789)
# consider either computed_energy or energy input by contributor, to avoid when the energy is 5, but it should be 1500
and (
(($unit eq "kj") and (($specified_energy > 55) or ($computed_energy > 55)))
or ( ($unit eq "kcal")
and (($specified_energy > 13) or ($computed_energy > 13)))
)
)
)
{
# Compare to specified energy value with a tolerance of 30% + an additiontal tolerance of 5
if ( ($computed_energy < ($specified_energy * 0.7 - 5))
or ($computed_energy > ($specified_energy * 1.3 + 5)))
Expand Down
1 change: 1 addition & 0 deletions taxonomies/categories.txt
Original file line number Diff line number Diff line change
Expand Up @@ -92520,6 +92520,7 @@ th:ใช้น้ำตาลเทียม
tr:Tatlandırıcı, Tatlandırıcılar
zh:糖精
wikidata:en:Q4368298
ignore_energy_calculated_error:en:yes

<en:Sweeteners
en:Sugars
Expand Down
36 changes: 35 additions & 1 deletion tests/unit/dataqualityfood.t
Original file line number Diff line number Diff line change
Expand Up @@ -374,12 +374,46 @@ $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 nutrients but category possesses ignore_energy_calculated_error:en:yes tag'
) or diag explain $product_ref;

$product_ref = {
categories_tags => ['en:sweeteners'],
nutriments => {
"energy-kj_value" => 550,
"carbohydrates_value" => 10,
"fat_value" => 20,
"proteins_value" => 30,
"fiber_value" => 2,
}
};
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 nutrients but category possesses ignore_energy_calculated_error:en:yes tag'
) or diag explain $product_ref;

$product_ref = {
categories_tags => ['en:sweet-spreads'],
nutriments => {
"energy-kj_value" => 8,
"fat_value" => 0.5,
"saturated-fat_value" => 0.1,
"carbohydrates_value" => 0.5,
"sugars_value" => 0.5,
"proteins_value" => 0.5,
"salt_value" => 0.01,
}
};
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 but energy is lower than 55 kj'
) or diag explain $product_ref;

# energy matches nutrients
$product_ref = {
nutriments => {
Expand Down Expand Up @@ -462,7 +496,7 @@ $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 not matching nutrient but lower than 55 kj')
or diag explain $product_ref;

# Erythritol is a polyol which does not contribute to energy
Expand Down

0 comments on commit 1d3d352

Please sign in to comment.