-
-
Notifications
You must be signed in to change notification settings - Fork 406
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: match_ingredient_origin unit test (#8174)
- Loading branch information
Showing
10 changed files
with
334 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
tests/unit/expected_test_results/parse_origins_from_text/empty.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"lc" : "en", | ||
"origin_en" : "" | ||
} |
4 changes: 4 additions & 0 deletions
4
tests/unit/expected_test_results/parse_origins_from_text/just-a-country.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"lc" : "en", | ||
"origin_en" : "Italy" | ||
} |
48 changes: 48 additions & 0 deletions
48
tests/unit/expected_test_results/parse_origins_from_text/real-list-fr.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
{ | ||
"lc" : "fr", | ||
"origin_fr" : "amandes d’Espagne. Chocolat noir de Côte d’Ivoire. Huile de noisette d’Italie. sucre de France. noisettes d’Italie. fèves de cacao de Côte d’Ivoire. Framboises lyophilisées d’Espagne. arôme naturel de framboise fabriqué en France.", | ||
"specific_ingredients" : [ | ||
{ | ||
"id" : "en:almond", | ||
"ingredient" : "amandes", | ||
"origins" : "en:spain", | ||
"text" : "amandes d'Espagne." | ||
}, | ||
{ | ||
"id" : "en:dark-chocolate", | ||
"ingredient" : "Chocolat noir", | ||
"origins" : "en:cote-d-ivoire", | ||
"text" : "Chocolat noir de Côte d'Ivoire." | ||
}, | ||
{ | ||
"id" : "en:hazelnut-oil", | ||
"ingredient" : "Huile de noisette", | ||
"origins" : "en:italy", | ||
"text" : "Huile de noisette d'Italie." | ||
}, | ||
{ | ||
"id" : "en:sugar", | ||
"ingredient" : "sucre", | ||
"origins" : "en:france", | ||
"text" : "sucre de France." | ||
}, | ||
{ | ||
"id" : "en:hazelnut", | ||
"ingredient" : "noisettes", | ||
"origins" : "en:italy", | ||
"text" : "noisettes d'Italie." | ||
}, | ||
{ | ||
"id" : "en:cocoa-bean", | ||
"ingredient" : "fèves de cacao", | ||
"origins" : "en:cote-d-ivoire", | ||
"text" : "fèves de cacao de Côte d'Ivoire." | ||
}, | ||
{ | ||
"id" : "en:freeze-dried-raspberries", | ||
"ingredient" : "Framboises lyophilisées", | ||
"origins" : "en:spain", | ||
"text" : "Framboises lyophilisées d'Espagne." | ||
} | ||
] | ||
} |
4 changes: 4 additions & 0 deletions
4
tests/unit/expected_test_results/parse_origins_from_text/rubish-entry.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"lc" : "en", | ||
"origin_en" : "NNSTeia nauns" | ||
} |
12 changes: 12 additions & 0 deletions
12
tests/unit/expected_test_results/parse_origins_from_text/simple-extraction-en.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"lc" : "en", | ||
"origin_en" : "Sugar from Italy.", | ||
"specific_ingredients" : [ | ||
{ | ||
"id" : "en:sugar", | ||
"ingredient" : "Sugar", | ||
"origins" : "en:italy", | ||
"text" : "Sugar from Italy." | ||
} | ||
] | ||
} |
12 changes: 12 additions & 0 deletions
12
tests/unit/expected_test_results/parse_origins_from_text/simple-extraction-fr.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"lc" : "fr", | ||
"origin_fr" : "Sucre France.", | ||
"specific_ingredients" : [ | ||
{ | ||
"id" : "en:sugar", | ||
"ingredient" : "Sucre", | ||
"origins" : "en:france", | ||
"text" : "Sucre France." | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
use ProductOpener::PerlStandards; | ||
|
||
use Test::More; | ||
use Log::Any::Adapter 'TAP'; | ||
|
||
use ProductOpener::Ingredients qw/match_ingredient_origin init_origins_regexps/; | ||
|
||
my @tests = ( | ||
|
||
{ | ||
desc => "Empty", | ||
lc => "en", | ||
text => "", | ||
expected => [], | ||
}, | ||
{ | ||
desc => "Just a country", | ||
lc => "en", | ||
text => "Italy", | ||
expected => [], | ||
}, | ||
{ | ||
desc => "Rubish entry", | ||
lc => "en", | ||
text => "NNSTeia nauns", | ||
expected => [], | ||
}, | ||
{ | ||
desc => "simple en extraction", | ||
lc => "en", | ||
text => "Sugar from Italy.", | ||
expected => [ | ||
{ | ||
ingredient => 'Sugar', | ||
matched_text => 'Sugar from Italy.', | ||
origins => 'Italy' | ||
} | ||
], | ||
}, | ||
{ | ||
desc => "simple fr extraction", | ||
lc => "fr", | ||
text => "Sucre France.", | ||
expected => [ | ||
{ | ||
ingredient => 'Sucre', | ||
matched_text => 'Sucre France.', | ||
origins => 'France' | ||
} | ||
], | ||
}, | ||
{ | ||
desc => "Real well written case in fr", | ||
lc => "fr", | ||
text => | ||
"amandes d'Espagne. Chocolat noir de Côte d'Ivoire. Huile de noisette d'Italie. sucre de France. noisettes d'Italie. fèves de cacao de Côte d'Ivoire. Framboises lyophilisées d'Espagne. arôme naturel de framboise fabriqué en France.", | ||
expected => [ | ||
{ | ||
'ingredient' => 'amandes', | ||
'matched_text' => 'amandes d\'Espagne.', | ||
'origins' => 'Espagne' | ||
}, | ||
{ | ||
'ingredient' => 'Chocolat noir', | ||
'matched_text' => " Chocolat noir de Côte d'Ivoire.", | ||
'origins' => "Côte d'Ivoire" | ||
}, | ||
{ | ||
'ingredient' => 'Huile de noisette', | ||
'matched_text' => ' Huile de noisette d\'Italie.', | ||
'origins' => 'Italie' | ||
}, | ||
{ | ||
'ingredient' => 'sucre', | ||
'matched_text' => ' sucre de France.', | ||
'origins' => 'France' | ||
}, | ||
{ | ||
'ingredient' => 'noisettes', | ||
'matched_text' => ' noisettes d\'Italie.', | ||
'origins' => 'Italie' | ||
}, | ||
{ | ||
'ingredient' => "fèves de cacao", | ||
'matched_text' => " fèves de cacao de Côte d'Ivoire.", | ||
'origins' => "Côte d'Ivoire" | ||
}, | ||
{ | ||
'ingredient' => "Framboises lyophilisées", | ||
'matched_text' => " Framboises lyophilisées d'Espagne.", | ||
'origins' => 'Espagne' | ||
} | ||
], | ||
}, | ||
); | ||
|
||
init_origins_regexps(); | ||
|
||
foreach my $test_ref (@tests) { | ||
my $matched_ingredients_ref = []; | ||
my $result = 1; | ||
my $input_text = $test_ref->{text}; | ||
while ($result) { | ||
my $matched_ingredient_ref = {}; | ||
$result = match_ingredient_origin($test_ref->{lc}, \$test_ref->{text}, $matched_ingredient_ref); | ||
if ($result) { | ||
push @$matched_ingredients_ref, $matched_ingredient_ref; | ||
} | ||
} | ||
my $expected = $test_ref->{expected}; | ||
is_deeply($matched_ingredients_ref, $expected, $test_ref->{desc}) | ||
|| diag( | ||
explain( | ||
{ | ||
lc => $test_ref->{lc}, | ||
input_text => $input_text, | ||
remaining_text => $test_ref->{text}, | ||
matched => $matched_ingredients_ref, | ||
expected => $expected | ||
} | ||
) | ||
); | ||
} | ||
|
||
done_testing(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
use ProductOpener::PerlStandards; | ||
|
||
use Test::More; | ||
use Log::Any::Adapter 'TAP'; | ||
|
||
use ProductOpener::Test qw/:all/; | ||
|
||
use ProductOpener::Ingredients qw/parse_origins_from_text init_origins_regexps/; | ||
|
||
my ($test_id, $test_dir, $expected_result_dir, $update_expected_results) = (init_expected_results(__FILE__)); | ||
|
||
my @tests = ( | ||
{ | ||
id => "empty", | ||
desc => "Empty", | ||
product => { | ||
lc => "en", | ||
origin_en => "", | ||
} | ||
}, | ||
{ | ||
id => "just-a-country", | ||
desc => "Just a country", | ||
product => { | ||
lc => "en", | ||
origin_en => "Italy", | ||
} | ||
}, | ||
{ | ||
id => "rubish-entry", | ||
desc => "Rubish entry", | ||
product => { | ||
lc => "en", | ||
origin_en => "NNSTeia nauns", | ||
} | ||
}, | ||
{ | ||
id => "simple-extraction-en", | ||
desc => "simple en extraction", | ||
product => { | ||
lc => "en", | ||
origin_en => "Sugar from Italy.", | ||
} | ||
}, | ||
{ | ||
id => "simple-extraction-fr", | ||
desc => "simple fr extraction", | ||
product => { | ||
lc => "fr", | ||
origin_fr => "Sucre France." | ||
}, | ||
}, | ||
{ | ||
id => "real-list-fr", | ||
desc => "Real well written case in fr", | ||
product => { | ||
lc => "fr", | ||
origin_fr => | ||
"amandes d’Espagne. Chocolat noir de Côte d’Ivoire. Huile de noisette d’Italie. sucre de France. noisettes d’Italie. fèves de cacao de Côte d’Ivoire. Framboises lyophilisées d’Espagne. arôme naturel de framboise fabriqué en France.", | ||
}, | ||
} | ||
); | ||
|
||
init_origins_regexps(); | ||
|
||
my $json = JSON->new->allow_nonref->canonical; | ||
|
||
foreach my $test_ref (@tests) { | ||
|
||
my $testid = $test_ref->{id}; | ||
my $product_ref = $test_ref->{product}; | ||
my $text = $product_ref->{"origin_" . $product_ref->{lc}}; | ||
|
||
parse_origins_from_text($product_ref, $text); | ||
|
||
compare_to_expected_results($product_ref, "$expected_result_dir/$testid.json", $update_expected_results, $test_ref); | ||
|
||
} | ||
|
||
done_testing(); |