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 redundant presence validation on belongs part V #12514

Conversation

cyrillefr
Copy link
Contributor

What? Why?

What should we test?

After migration, all tests should pass and no rubocop offense should be raised.

No migration file here because the null were already enforced at DB level.
Also no need to check for corrupt data.

Need to know: May be some of these changes are unnecessary

I had the following message when, after code modifications I ran the
corresponding specs (see end of the thread).

So, I changed the spec accordingly. In the end belonged ids must not be null.
However, since belongs_to enforce validation, testing that a belongs_to is
enforced in specs appears to me redundant.

So, I would delete the 2 first examples in the spec/models/subscription_line_item_spec.rb file,
and the spec/models/tag_rule_spec.rb file entirely.

I share the opinion that one not should test what an external lib should do:
https://www.codewithjason.com/dont-use-shoulda-matchers/

But I did the changes in cases reviewers still want to keep the specs.
It is also easy to add a commit to delete those parts.

SubscriptionLineItem validations requires a variant
     Failure/Error: expect(subject).to validate_presence_of :variant
     
       Expected SubscriptionLineItem to validate that :variant cannot be
       empty/falsy, but this could not be proved.
         After setting :variant to ‹nil›, the matcher expected the
         SubscriptionLineItem to be invalid and to produce the validation error
         "can't be blank" on :variant. The record was indeed invalid, but it
         produced these validation errors instead:
     
         * subscription: ["must exist"]
         * variant: ["must exist"]
         * quantity: ["can't be blank", "is not a number"]
     
         You're getting this error because ActiveRecord is configured to add a
         presence validation to all `belongs_to` associations, and this
         includes yours. *This* presence validation doesn't use "can't be
         blank", the usual validation message, but "must exist" instead.
     
         With that said, did you know that the `belong_to` matcher can test
         this validation for you? Instead of using `validate_presence_of`, try
         the following instead:
     
             it { should belong_to(:variant) }

Release notes

Changelog Category (reviewers may add a label for the release notes):

  • User facing changes
  • API changes (V0, V1, DFC or Webhook)
  • Technical changes only
  • Feature toggled

The title of the pull request will be included in the release notes.

 - presence: true is redundant since Rails 5.0 BUT applies
   with new default config of
   belongs_to_required_by_default to true.
   Lots of files with belongs_to_required_by_default = false
   (backward compatibility).
   So: deleting this setting implies to adding optional: true
Copy link
Member

@mkllnk mkllnk left a comment

Choose a reason for hiding this comment

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

Perfect! The spec changes make sense.

@rioug rioug added the technical changes only These pull requests do not contain user facing changes and are grouped in release notes label May 28, 2024
Copy link
Collaborator

@rioug rioug left a comment

Choose a reason for hiding this comment

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

Looks good 👍 , thanks @cyrillefr

@rioug rioug merged commit 0e9b753 into openfoodfoundation:master May 28, 2024
52 checks passed
@cyrillefr cyrillefr deleted the RedundantPresenceValidationOnBelongs_part_V branch June 11, 2024 12:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
technical changes only These pull requests do not contain user facing changes and are grouped in release notes
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

3 participants