Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Add tests for Carrefour France import, + solve serving_size bug #6476

Merged
merged 9 commits into from
Mar 11, 2022
7 changes: 6 additions & 1 deletion lib/ProductOpener/Import.pm
Original file line number Diff line number Diff line change
Expand Up @@ -1081,6 +1081,8 @@ sub import_csv_file($) {
# Skip data that we have already imported before (even if it has been changed)
# But do import the field "obsolete"
elsif (($field ne "obsolete") and (defined $product_ref->{$field . "_imported"}) and ($product_ref->{$field . "_imported"} eq $imported_product_ref->{$field})) {
# we had a bug that caused serving_size to be set to "serving", this value should be overridden
next if (($field eq "serving_size") and ($product_ref->{"serving_size"} eq "serving"));
$log->debug("skipping field that was already imported", { field => $field, imported_value => $imported_product_ref->{$field}, current_value => $product_ref->{$field} }) if $log->is_debug();
next;
}
Expand Down Expand Up @@ -1552,7 +1554,10 @@ sub import_csv_file($) {
if ($imported_nutrition_data_per_value =~ /^100\s?(g|ml)$/i) {
$imported_nutrition_data_per_value = "100g";
}
# otherwise -> assign the per serving value, and assign serving size
elsif ($imported_nutrition_data_per_value =~ /^serving$/i) {
$imported_nutrition_data_per_value = "serving";
}
# otherwise, assign the per serving value, and assign serving size
else {
$log->debug("nutrition_data_per_field corresponds to serving size", { code => $code, nutrition_data_per_field => $nutrition_data_per_field, $imported_nutrition_data_per_value => $imported_nutrition_data_per_value }) if $log->is_debug();
if ((not defined $product_ref->{serving_size}) or ($product_ref->{serving_size} ne $imported_nutrition_data_per_value)) {
Expand Down
38 changes: 23 additions & 15 deletions lib/ProductOpener/ImportConvert.pm
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ use Time::Local;
use Data::Dumper;
use Text::CSV;
use HTML::Entities qw(decode_entities);
use XML::Rules;

%fields = ();
@fields = ();
Expand Down Expand Up @@ -291,14 +292,14 @@ sub assign_countries_for_product($$$) {
my $lcs_ref = shift;
my $default_country = shift;

foreach my $possible_lc (keys %{$lcs_ref}) {
foreach my $possible_lc (sort keys %{$lcs_ref}) {
if (defined $product_ref->{"product_name_" . $possible_lc}) {
assign_value($product_ref,"countries", $lcs_ref->{$possible_lc});
$log->info("assign_countries_for_product: found lc - assigning value", { lc => $possible_lc, countries => $lcs_ref->{$possible_lc}}) if $log->is_info();
}
}

if (not defined $product_ref->{countries}) {
if ((not defined $product_ref->{countries}) or ($product_ref->{countries} eq "")) {
assign_value($product_ref,"countries", $default_country);
$log->info("assign_countries_for_product: assigning default value", { countries => $default_country}) if $log->is_info();
}
Expand Down Expand Up @@ -536,14 +537,15 @@ sub remove_quantity_from_field($$) {
my $quantity = $product_ref->{quantity};
my $quantity_value = $product_ref->{quantity_value};
my $quantity_unit = $product_ref->{quantity_unit};

$quantity =~ s/\(/\\\(/g;
$quantity =~ s/\)/\\\)/g;
$quantity =~ s/\[/\\\[/g;
$quantity =~ s/\]/\\\]/g;

if ((defined $quantity) and ($product_ref->{$field} =~ /\s*(\b|\s+)($quantity|(\(|\[)$quantity(\)|\]))\s*$/i)) {
$product_ref->{$field} = $`;
if (defined $quantity) {
$quantity =~ s/\(/\\\(/g;
$quantity =~ s/\)/\\\)/g;
$quantity =~ s/\[/\\\[/g;
$quantity =~ s/\]/\\\]/g;
if ($product_ref->{$field} =~ /\s*(\b|\s+)($quantity|(\(|\[)$quantity(\)|\]))\s*$/i) {
$product_ref->{$field} = $`;
}
}
elsif ((defined $quantity_value) and (defined $quantity_unit) and ($product_ref->{$field} =~ /\s*\b\(?$quantity_value $quantity_unit\)?\s*$/i)) {
$product_ref->{$field} = $`;
Expand Down Expand Up @@ -870,6 +872,11 @@ sub clean_fields($) {

$log->debug("clean_fields", { field=>$field, value=>$product_ref->{$field} }) if $log->is_debug();

if (not defined $product_ref->{$field}) {
print STDERR "undefined value for field $field\n";
next;
}

# HTML entities
# e.g. Pâtes alimentaires cuites aromatisées au curcuma
if ($product_ref->{$field} =~ /\&/) {
Expand Down Expand Up @@ -1384,7 +1391,7 @@ sub load_xml_file($$$$) {
# multiple values in different languages

elsif ($source_tag eq '*') {
foreach my $tag ( keys %{$current_tag}) {
foreach my $tag ( sort keys %{$current_tag}) {
my $tag_target = $target;

# special case where we have something like allergens.nuts = traces
Expand Down Expand Up @@ -1776,12 +1783,14 @@ sub get_list_of_files(@) {



sub print_csv_file() {
sub print_csv_file($) {

my $file_handle = shift;

my $csv_out = Text::CSV->new ( { binary => 1 , sep_char => "\t" } ) # should set binary attribute.
my $csv_out = Text::CSV->new ( { binary => 1 , sep_char => "\t", eol => "\n", quote_space => 0 } ) # should set binary attribute.
or die "Cannot use CSV: ".Text::CSV->error_diag ();

print join("\t", @fields) . "\n";
$csv_out->print ($file_handle, \@fields) ;

foreach my $code (sort keys %products) {

Expand All @@ -1797,8 +1806,7 @@ sub print_csv_file() {
}
}

$csv_out->print (*STDOUT, \@values) ;
print "\n";
$csv_out->print ($file_handle, \@values) ;

print STDERR "code: $code\n";
}
Expand Down
2 changes: 1 addition & 1 deletion lib/ProductOpener/Products.pm
Original file line number Diff line number Diff line change
Expand Up @@ -635,7 +635,7 @@ sub init_product($$$$) {
require ProductOpener::GeoIP;
$country = ProductOpener::GeoIP::get_country_for_ip(remote_addr());
}
else {
elsif (defined $countryid) {
$country = $countryid;
$country =~ s/^en://;
}
Expand Down
Loading