Skip to content

Commit

Permalink
fix: re-canonicalize packaging component properties (#8246)
Browse files Browse the repository at this point in the history
  • Loading branch information
stephanegigandet committed Mar 24, 2023
1 parent 57905e7 commit 4668488
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
24 changes: 24 additions & 0 deletions lib/ProductOpener/Packaging.pm
Original file line number Diff line number Diff line change
Expand Up @@ -706,6 +706,27 @@ sub migrate_old_number_and_quantity_fields_202211 ($product_ref) {
return;
}

=head2 canonicalize_packaging_components_properties ($product_ref) {
Re-canonicalize the shape, material and recycling properties of packaging components.
This is useful in particular if the corresponding taxonomies have changed.
=cut

sub canonicalize_packaging_components_properties ($product_ref) {

foreach my $packaging_ref (@{$product_ref->{packagings}}) {
foreach my $property ("shape", "material", "recycling") {
if (defined $packaging_ref->{$property}) {
my $tagtype = $packaging_taxonomies{$property};
$packaging_ref->{$property}
= canonicalize_taxonomy_tag($product_ref->{lc}, $tagtype, $packaging_ref->{$property});
}
}
}
return;
}

=head2 set_packaging_misc_tags($product_ref)
Set some tags in the /misc/ facet so that we can track the products that have
Expand Down Expand Up @@ -867,6 +888,9 @@ sub analyze_and_combine_packaging_data ($product_ref, $response_ref) {
# TODO: remove once all products have been migrated
migrate_old_number_and_quantity_fields_202211($product_ref);

# Re-canonicalize the packaging components properties, in case the corresponding taxonomies have changed
canonicalize_packaging_components_properties($product_ref);

# The packaging text field (populated by OCR of the packaging image and/or contributors or producers)
# is used as input only if the packagings structure is empty
if ((scalar @{$product_ref->{packagings}} == 0) and (defined $product_ref->{packaging_text})) {
Expand Down
24 changes: 24 additions & 0 deletions tests/unit/packaging.t
Original file line number Diff line number Diff line change
Expand Up @@ -592,6 +592,30 @@ foreach my $test_ref (@tests) {
}
}

my $product_ref = {
lc => "fr",
packagings => [
{
shape => 'fr:bouteille',
material => 'en:pet',
recycling => '脿 jeter'
},
],
};

ProductOpener::Packaging::canonicalize_packaging_components_properties($product_ref);

is_deeply(
$product_ref->{packagings},
[
{
'material' => 'en:pet-1-polyethylene-terephthalate',
'recycling' => 'en:discard',
'shape' => 'en:bottle'
},
]
) or diag explain $product_ref->{packagings};

#

done_testing();

0 comments on commit 4668488

Please sign in to comment.