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: Added remove tags from field function #9960

Draft
wants to merge 10 commits into
base: main
Choose a base branch
from

Conversation

anaritadauane
Copy link
Contributor

What

Added a function to remove a tag/tags from taxonomized fields

I achieved this by:

  • Added a function remove_tags_from_fields, a function that removes tags from fields
  • Updated the update_tags_fieds function, now a field can be updated by removing or adding tags
  • Creating a test case to remove tags from a field

Screenshot

Related issue(s) and discussion

@github-actions github-actions bot added the API WRITE WRITE API to allow sending product info and image label Mar 19, 2024
@@ -4613,6 +4613,49 @@ sub add_tags_to_field ($product_ref, $tag_lc, $field, $additional_fields) {
return;
}

sub remove_tag_from_field($product_ref, $tag_lc, $field, $tags_to_remove) {
Copy link
Member

@alexgarel alexgarel Mar 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You except a list of tag to remove so:

  • use plural in function name (remove_tags_from_field)
  • your last parameter should be a reference to that list

Copy link
Member

@alexgarel alexgarel Mar 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here again I misread… you have a string which is a comma separated list of values (maybe it's a good indication that you should document the function ;-) )

So apart from the function name, it's all ok :-)

Suggested change
sub remove_tag_from_field($product_ref, $tag_lc, $field, $tags_to_remove) {
sub remove_tags_from_field($product_ref, $tag_lc, $field, $tags_to_remove) {

Copy link
Member

@alexgarel alexgarel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You did quite well for an issue that is not the easiest !

Here are some inputs to let you continue.

Comment on lines 220 to 222
foreach my $tag (@$remove_tags) {
remove_tags_from_field($product_ref, $tags_lc, $field, $tag);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a mistmacht between what you do here and you implementation which expect a list reference (which is the right way I think).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand, are you saying this because of the for loop or because the $tag variable is an element of the reference list?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your code is working but you are doing one call per field, while it could be grouped (or am I missing something ?)

Why not:

Suggested change
foreach my $tag (@$remove_tags) {
remove_tags_from_field($product_ref, $tags_lc, $field, $tag);
}
remove_tags_from_field($product_ref, $tags_lc, $field, join(',', @$remove_tags));

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, you are right. I did that because it is already handled by the remove_tags_from_field method

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is one call per tag to be removed

lib/ProductOpener/Tags.pm Outdated Show resolved Hide resolved
tests/integration/api_v3_product_write.t Show resolved Hide resolved
@codecov-commenter
Copy link

codecov-commenter commented Mar 22, 2024

Codecov Report

Attention: Patch coverage is 76.19048% with 5 lines in your changes are missing coverage. Please review.

Project coverage is 49.61%. Comparing base (dc04d18) to head (0fb1de2).
Report is 198 commits behind head on main.

Files Patch % Lines
lib/ProductOpener/APIProductWrite.pm 0.00% 5 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #9960      +/-   ##
==========================================
+ Coverage   49.54%   49.61%   +0.06%     
==========================================
  Files          67       70       +3     
  Lines       20650    20952     +302     
  Branches     4980     5022      +42     
==========================================
+ Hits        10231    10395     +164     
- Misses       9131     9267     +136     
- Partials     1288     1290       +2     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Member

@alexgarel alexgarel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some more comments !

tests/integration/api_v3_product_write.t Show resolved Hide resolved
categories => "pommes, bananes, en:pears, fr:fraises, es:limones",
};

remove_tag_from_field($product_ref, "fr", "categories", "bananes, fraises");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you forgot to change the name here.

Better run your tests locally before committing:

  • make test-unit test=tags.t

(and make test-int test=youfile.t for the integration)
See https://openfoodfacts.github.io/openfoodfacts-server/dev/how-to-write-and-run-tests/

tests/unit/tags.t Outdated Show resolved Hide resolved
Comment on lines 220 to 222
foreach my $tag (@$remove_tags) {
remove_tags_from_field($product_ref, $tags_lc, $field, $tag);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your code is working but you are doing one call per field, while it could be grouped (or am I missing something ?)

Why not:

Suggested change
foreach my $tag (@$remove_tags) {
remove_tags_from_field($product_ref, $tags_lc, $field, $tag);
}
remove_tags_from_field($product_ref, $tags_lc, $field, join(',', @$remove_tags));

Comment on lines +666 to +679
{
test_case => 'patch-tags-fields-remove',
method => 'PATCH',
path => '/api/v3/product/1234567890100',
body => '{
"fields" : "updated",
"product": {
"categories_tags_remove": ["en:tea"],
"stores_tags_remove": ["Carrefour", "Mon Ptit magasin"],
"countries_tags_fr_remove": ["Italie", "en:spain"],
"labels_tags_remove": ["végétarien", "Something unrecognized in French"]
}
}',
},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also you should have a json file resulting from this test. I let you see the documentation https://openfoodfacts.github.io/openfoodfacts-server/dev/how-to-use-gitpod/

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have made a few changes to the code and want to test it, but I'm having trouble getting a json file with the expected results. I'm running this command: make test-unit="api_v3_product_write.t --update-expected-results"

Copy link
Contributor Author

@anaritadauane anaritadauane Mar 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Screenshot 2024-03-25 at 12 53 35

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be because your backend is crashing.

Are you able to run the website locally ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes I can run the website locally

@@ -196,7 +196,7 @@ Update packagings.

=cut

sub update_tags_fields ($request_ref, $product_ref, $field, $add_to_existing_tags, $tags_lc, $value) {
sub update_tags_fields ($request_ref, $product_ref, $field, $add_to_existing_tags, $remove_tags, $tags_lc, $value) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you change the parameters of a n existing function, you need to look for all the uses of that function to update the function calls to send the right parameters.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I made the changes but I'm having trouble generating a json file with the expected results. I'm running this command make test-unit="api_v3_product_write.t --update-expected-results"

Copy link
Member

@alexgarel alexgarel Mar 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oooh, this is make test-int test="api_v3_product_write.t :: --update-expected-results"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I corrected the code but still
I am going to try one more time

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@anaritadauane changed a bit the command (we have to add :: now because of yath the new test runner)

foreach my $tag (@$remove_tags) {
remove_tags_from_field($product_ref, $tags_lc, $field, $tag);
}
remove_tags_from_field($product_ref, $tags_lc, $field, $remove_tags); #join(',', @$remove_tags)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you have a ref to an array but then in remove_tags_from_field you treat it like a string… it wont work ! Why don't you use a join ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because I'm calling the join function inside the remove_tags_from_field function

my @removed_tags = ();
my @tags_to_remove = split(/,/, $tags_to_remove);
# Split the tags to remove string into separate tags
my @excluded_tags = split(/,\s*/, $tags_to_remove);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if you specify possible spaces after the comma, also add them before maybe ?

Suggested change
my @excluded_tags = split(/,\s*/, $tags_to_remove);
my @excluded_tags = split(/\s*,\s*/, $tags_to_remove);

@@ -673,7 +673,7 @@ my $tests_ref = [
"categories_tags_remove": ["en:tea"],
"stores_tags_remove": ["Carrefour", "Mon Ptit magasin"],
"countries_tags_fr_remove": ["Italie", "en:spain"],
"labels_tags_remove": ["végétarien", "Something unrecognized in French"]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

aren't you testing tags removal ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I am

tests/unit/tags.t Outdated Show resolved Hide resolved
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I pushed it, but as you see it's not what we expect.

It's important that you are able to run tests locally otherwise it will be very complicated to fix your PR.

Copy link

sonarcloud bot commented Mar 29, 2024

Quality Gate Passed Quality Gate passed

Issues
0 New issues
0 Accepted issues

Measures
0 Security Hotspots
No data about Coverage
0.0% Duplication on New Code

See analysis details on SonarCloud

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is some Apple-specific file? Should this be in .gitignore or is it actually useful?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes that is an apple file related to the directory.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API v3 API WRITE WRITE API to allow sending product info and image Display Tags 🧪 tests 🧪 unit tests
Projects
Development

Successfully merging this pull request may close these issues.

Add the possibility to remove a tag from taxonomized fields
6 participants