Skip to content

Commit

Permalink
fix: data quality - increase threshold for comparison between fiber a…
Browse files Browse the repository at this point in the history
…nd its subnutriments (#11145)

Increase the threshold from 0.001 to 0.01

Soluble fiber + insoluble fiber > fiber + 0.01

- Fixes #10491
  • Loading branch information
benbenben2 authored Jan 2, 2025
1 parent 7287d8b commit f0a2682
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/ProductOpener/DataQualityFood.pm
Original file line number Diff line number Diff line change
Expand Up @@ -1341,7 +1341,9 @@ sub check_nutrition_data ($product_ref) {

my $total_fiber = $soluble_fiber + $insoluble_fiber;

if ($total_fiber > $product_ref->{nutriments}{fiber_100g} + 0.001) {
# increased threshold from 0.001 to 0.01 (see issue #10491)
# make sure that floats stop after 2 decimals
if (sprintf("%.2f", $total_fiber) > sprintf("%.2f", $product_ref->{nutriments}{fiber_100g} + 0.01)) {
push @{$product_ref->{data_quality_errors_tags}},
"en:nutrition-soluble-fiber-plus-insoluble-fiber-greater-than-fiber";
}
Expand Down
34 changes: 34 additions & 0 deletions tests/unit/dataqualityfood.t
Original file line number Diff line number Diff line change
Expand Up @@ -1983,4 +1983,38 @@ ok(
'insoluble-fiber_100g larger than fiber_100g'
) or diag Dumper $product_ref;

# Test case for sum fiber subnutriment comparison with fiber (0.01 difference should be fine)
$product_ref = {
nutriments => {
fiber_100g => 3.57,
'soluble-fiber_100g' => 1.79,
'insoluble-fiber_100g' => 1.79,
},
data_quality_errors_tags => [],
};

ProductOpener::DataQuality::check_quality($product_ref);

ok(
!has_tag($product_ref, 'data_quality_errors', 'en:nutrition-soluble-fiber-plus-insoluble-fiber-greater-than-fiber'),
'insoluble-fiber_100g larger than fiber_100g'
) or diag Dumper $product_ref;

# Test case for sum fiber subnutriment comparison with fiber (0.02 difference should be raise error)
$product_ref = {
nutriments => {
fiber_100g => 3.57,
'soluble-fiber_100g' => 1.79,
'insoluble-fiber_100g' => 1.80,
},
data_quality_errors_tags => [],
};

ProductOpener::DataQuality::check_quality($product_ref);

ok(
has_tag($product_ref, 'data_quality_errors', 'en:nutrition-soluble-fiber-plus-insoluble-fiber-greater-than-fiber'),
'insoluble-fiber_100g larger than fiber_100g'
) or diag Dumper $product_ref;

done_testing();

0 comments on commit f0a2682

Please sign in to comment.