From 014971b590b2ccb811b990d37181e2b123cb82c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Gigandet?= Date: Tue, 19 Sep 2023 14:38:40 +0200 Subject: [PATCH] fix: illegal division by zero in percent estimate (#9007) fix: illegal division by zero in percent estimate #8998 --- lib/ProductOpener/Ingredients.pm | 5 +- .../en-illegal-division-by-zero.json | 77 +++++++++++++++++++ .../fr-illegal-division-by-zero.json | 77 +++++++++++++++++++ tests/unit/ingredients.t | 15 ++++ 4 files changed, 172 insertions(+), 2 deletions(-) create mode 100644 tests/unit/expected_test_results/ingredients/en-illegal-division-by-zero.json create mode 100644 tests/unit/expected_test_results/ingredients/fr-illegal-division-by-zero.json diff --git a/lib/ProductOpener/Ingredients.pm b/lib/ProductOpener/Ingredients.pm index a1118bf15e8e5..c099a1b095b64 100644 --- a/lib/ProductOpener/Ingredients.pm +++ b/lib/ProductOpener/Ingredients.pm @@ -2878,10 +2878,11 @@ sub init_percent_values ($total_min, $total_max, $ingredients_ref) { # This is the case in particular for recipes that can be specified in grams with a total greater than 100g # So we start supposing it's grams (as if it's percent it will also work). - if (($total_min == $total_max) and $all_ingredients_have_a_set_percent) { + # In scale_percents or scale_grams mode, the percent/quantity sum must be greater than 0 + if (($total_min == $total_max) and ($all_ingredients_have_a_set_percent) and ($percent_sum > 0)) { $percent_mode = "scale_percents"; } - elsif (($total_min == $total_max) and $all_ingredients_have_a_set_quantity) { + elsif (($total_min == $total_max) and ($all_ingredients_have_a_set_quantity) and ($quantity_sum > 0)) { $percent_mode = "scale_grams"; } elsif ($percent_sum > $total_max) { diff --git a/tests/unit/expected_test_results/ingredients/en-illegal-division-by-zero.json b/tests/unit/expected_test_results/ingredients/en-illegal-division-by-zero.json new file mode 100644 index 0000000000000..b1f213f78bc66 --- /dev/null +++ b/tests/unit/expected_test_results/ingredients/en-illegal-division-by-zero.json @@ -0,0 +1,77 @@ +{ + "ingredients" : [ + { + "id" : "en:each-capsule-contains", + "ingredients" : [ + { + "id" : "en:paracetamol-500-m-5-060198-790", + "percent_estimate" : 100, + "percent_max" : 100, + "percent_min" : 100, + "quantity" : "0 mg", + "quantity_g" : 0, + "text" : "paracetamol 500 m 5 060198 790" + } + ], + "percent_estimate" : 100, + "percent_max" : 100, + "percent_min" : 100, + "text" : "each capsule contains" + } + ], + "ingredients_analysis" : { + "en:palm-oil-content-unknown" : [ + "en:each-capsule-contains", + "en:paracetamol-500-m-5-060198-790" + ], + "en:vegan-status-unknown" : [ + "en:each-capsule-contains", + "en:paracetamol-500-m-5-060198-790" + ], + "en:vegetarian-status-unknown" : [ + "en:each-capsule-contains", + "en:paracetamol-500-m-5-060198-790" + ] + }, + "ingredients_analysis_tags" : [ + "en:palm-oil-content-unknown", + "en:vegan-status-unknown", + "en:vegetarian-status-unknown" + ], + "ingredients_hierarchy" : [ + "en:each-capsule-contains", + "en:paracetamol-500-m-5-060198-790" + ], + "ingredients_n" : 2, + "ingredients_n_tags" : [ + "2", + "1-10" + ], + "ingredients_original_tags" : [ + "en:each-capsule-contains", + "en:paracetamol-500-m-5-060198-790" + ], + "ingredients_percent_analysis" : 1, + "ingredients_tags" : [ + "en:each-capsule-contains", + "en:paracetamol-500-m-5-060198-790" + ], + "ingredients_text" : "each capsule contains: paracetamol 500 m 5 060198 790 0 mg.", + "ingredients_with_specified_percent_n" : 0, + "ingredients_with_specified_percent_sum" : 0, + "ingredients_with_unspecified_percent_n" : 1, + "ingredients_with_unspecified_percent_sum" : 100, + "ingredients_without_ciqual_codes" : [ + "en:paracetamol-500-m-5-060198-790" + ], + "ingredients_without_ciqual_codes_n" : 1, + "known_ingredients_n" : 0, + "lc" : "en", + "nutriments" : { + "fruits-vegetables-legumes-estimate-from-ingredients_100g" : 0, + "fruits-vegetables-legumes-estimate-from-ingredients_serving" : 0, + "fruits-vegetables-nuts-estimate-from-ingredients_100g" : 0, + "fruits-vegetables-nuts-estimate-from-ingredients_serving" : 0 + }, + "unknown_ingredients_n" : 2 +} diff --git a/tests/unit/expected_test_results/ingredients/fr-illegal-division-by-zero.json b/tests/unit/expected_test_results/ingredients/fr-illegal-division-by-zero.json new file mode 100644 index 0000000000000..1829794e692ee --- /dev/null +++ b/tests/unit/expected_test_results/ingredients/fr-illegal-division-by-zero.json @@ -0,0 +1,77 @@ +{ + "ingredients" : [ + { + "id" : "fr:analyse-moyenne-pour-1", + "ingredients" : [ + { + "id" : "fr:1472-kj", + "percent_estimate" : 100, + "percent_max" : 100, + "percent_min" : 100, + "text" : "1472 kJ" + } + ], + "percent_estimate" : 100, + "percent_max" : 100, + "percent_min" : 100, + "quantity" : "00 g", + "quantity_g" : 0, + "text" : "Analyse moyenne pour 1" + } + ], + "ingredients_analysis" : { + "en:palm-oil-content-unknown" : [ + "fr:analyse-moyenne-pour-1", + "fr:1472-kj" + ], + "en:vegan-status-unknown" : [ + "fr:analyse-moyenne-pour-1", + "fr:1472-kj" + ], + "en:vegetarian-status-unknown" : [ + "fr:analyse-moyenne-pour-1", + "fr:1472-kj" + ] + }, + "ingredients_analysis_tags" : [ + "en:palm-oil-content-unknown", + "en:vegan-status-unknown", + "en:vegetarian-status-unknown" + ], + "ingredients_hierarchy" : [ + "fr:analyse-moyenne-pour-1", + "fr:1472-kj" + ], + "ingredients_n" : 2, + "ingredients_n_tags" : [ + "2", + "1-10" + ], + "ingredients_original_tags" : [ + "fr:analyse-moyenne-pour-1", + "fr:1472-kj" + ], + "ingredients_percent_analysis" : 1, + "ingredients_tags" : [ + "fr:analyse-moyenne-pour-1", + "fr:1472-kj" + ], + "ingredients_text" : "Analyse moyenne pour 1 00 g: 1472 kJ,", + "ingredients_with_specified_percent_n" : 0, + "ingredients_with_specified_percent_sum" : 0, + "ingredients_with_unspecified_percent_n" : 1, + "ingredients_with_unspecified_percent_sum" : 100, + "ingredients_without_ciqual_codes" : [ + "fr:1472-kj" + ], + "ingredients_without_ciqual_codes_n" : 1, + "known_ingredients_n" : 0, + "lc" : "fr", + "nutriments" : { + "fruits-vegetables-legumes-estimate-from-ingredients_100g" : 0, + "fruits-vegetables-legumes-estimate-from-ingredients_serving" : 0, + "fruits-vegetables-nuts-estimate-from-ingredients_100g" : 0, + "fruits-vegetables-nuts-estimate-from-ingredients_serving" : 0 + }, + "unknown_ingredients_n" : 2 +} diff --git a/tests/unit/ingredients.t b/tests/unit/ingredients.t index c794591f9fc5b..b1a506d038882 100755 --- a/tests/unit/ingredients.t +++ b/tests/unit/ingredients.t @@ -547,6 +547,21 @@ Origin of peaches: Spain. Origin of some unknown ingredient: France. origin of A ingredients_text => "farine demi-complète de riz, farine de blé demi complet", } ], + # illegal division by zero + [ + "fr-illegal-division-by-zero", + { + lc => "fr", + ingredients_text => "Analyse moyenne pour 1 00 g: 1472 kJ,", + } + ], + [ + "en-illegal-division-by-zero", + { + lc => "en", + ingredients_text => "each capsule contains: paracetamol 500 m 5 060198 790 0 mg.", + } + ], ); foreach my $test_ref (@tests) {