Skip to content

Commit

Permalink
fix: data quality, false positive, nutrition sum with lower symbol (#…
Browse files Browse the repository at this point in the history
…11076)

### What
Error is:
Nutrition-soluble-fiber-plus-insoluble-fiber-greater-than-fiber should
not be raised for

> Fiber = 5
> insoluble fiber < 1
> soluble fiber = 5

and similar cases.

The PR covers only Fibers, insoluble fiber and soluble fiber.
The PR covers only if symbol is "<"

### Related issue(s) and discussion
- Fixes #11075
  • Loading branch information
benbenben2 authored Dec 9, 2024
1 parent 6eaeb26 commit d389c87
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 19 deletions.
44 changes: 25 additions & 19 deletions lib/ProductOpener/DataQualityFood.pm
Original file line number Diff line number Diff line change
Expand Up @@ -1319,25 +1319,31 @@ sub check_nutrition_data ($product_ref) {
}

# sum of nutriments that compose fiber can not be greater than the value of fiber
if (
(defined $product_ref->{nutriments}{fiber_100g})
and (
(
(
(defined $product_ref->{nutriments}{'soluble-fiber_100g'})
? $product_ref->{nutriments}{'soluble-fiber_100g'}
: 0
) + (
(defined $product_ref->{nutriments}{'insoluble-fiber_100g'})
? $product_ref->{nutriments}{'insoluble-fiber_100g'}
: 0
)
) > ($product_ref->{nutriments}{fiber_100g}) + 0.001
)
)
{
push @{$product_ref->{data_quality_errors_tags}},
"en:nutrition-soluble-fiber-plus-insoluble-fiber-greater-than-fiber";
# ignore if there is "<" symbol (example: <1 + 5 = 5, issue #11075)
if (defined $product_ref->{nutriments}{fiber_100g}) {
my $soluble_fiber = 0;
my $insoluble_fiber = 0;

if (defined $product_ref->{nutriments}{'soluble-fiber_100g'}) {
my $soluble_modifier = $product_ref->{nutriments}{'soluble-fiber_modifier'};
if (!defined $soluble_modifier || $soluble_modifier ne '<') {
$soluble_fiber = $product_ref->{nutriments}{'soluble-fiber_100g'};
}
}

if (defined $product_ref->{nutriments}{'insoluble-fiber_100g'}) {
my $insoluble_modifier = $product_ref->{nutriments}{'insoluble-fiber_modifier'};
if (!defined $insoluble_modifier || $insoluble_modifier ne '<') {
$insoluble_fiber = $product_ref->{nutriments}{'insoluble-fiber_100g'};
}
}

my $total_fiber = $soluble_fiber + $insoluble_fiber;

if ($total_fiber > $product_ref->{nutriments}{fiber_100g} + 0.001) {
push @{$product_ref->{data_quality_errors_tags}},
"en:nutrition-soluble-fiber-plus-insoluble-fiber-greater-than-fiber";
}
}

# Too small salt value? (e.g. g entered in mg)
Expand Down
47 changes: 47 additions & 0 deletions tests/unit/dataqualityfood.t
Original file line number Diff line number Diff line change
Expand Up @@ -1907,4 +1907,51 @@ ok(
'Soluble fiber + Insoluble fiber exceeds total fiber'
) or diag Dumper $product_ref;

# Test case for fiber content having "<" symbol
$product_ref = {
nutriments => {
fiber_100g => 5,
'soluble-fiber_100g' => 1,
'soluble-fiber_modifier' => '<',
'insoluble-fiber_100g' => 5,
},
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'),
'Soluble fiber + Insoluble fiber exceeds total fiber but there is < symbol'
) or diag Dumper $product_ref;

# Test case for fiber content having ">" symbol
$product_ref = {
nutriments => {
fiber_100g => 5,
'soluble-fiber_100g' => 1,
'soluble-fiber_modifier' => '>',
'insoluble-fiber_100g' => 5,
},
data_quality_errors_tags => [],
};

# Test case for fiber content beside other element having "<"
$product_ref = {
nutriments => {
fiber_100g => 5,
'soluble-fiber_100g' => 1,
'soluble-fiber_modifier' => '<',
'insoluble-fiber_100g' => 10,
},
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 d389c87

Please sign in to comment.