-
-
Notifications
You must be signed in to change notification settings - Fork 724
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
Do not commit to db unchanged products in bulk save #12515
Do not commit to db unchanged products in bulk save #12515
Conversation
69aa10c
to
9ada323
Compare
I hope you don't mind me updating this, because I think it is ready for review now :D |
cool! Thank you! 👍 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice one, but I think we are missing testing on one of the changes David made (see comment ), could you update the specs ? thanks !
end | ||
|
||
it 'does not increase saved_count' do | ||
subject |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's a bit of a nit pick, but using subject
here without knowing the context doesn't tell us what's happening. It'd rather be explicit and use product_set.save
so I know straight away what we are testing.
Same for the various use of subject
below.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I haven't seen anywhere in the project style guide about it. So its comes down to personal preferences. In such cases i follow the betterspecs.org. If talking particularly about subject
a lot arguments are here betterspecs/betterspecs#7
I understand that exist different styles of writing tests. Another one is to put all context definition into into each it
. Its described in book "Testing Rails"(Josh Steiner, Joël Quenneville) https://books.thoughtbot.com/assets/testing-rails.pdf . But it does not looks like OFN follows this approach either.
but using subject here without knowing the context doesn't tell us what's happening
Sounds like a code smell. Why? Because subject
describes what tested subject actually does. And its good when it does only one thing, because of Single Responsibility principle (SOLID). From my experience: If its not possible to write simple, obvious unit tests using rspec interface - code design is not optimal. Is the class ProductSet
really that bad? Well, it uses inheritance, method override. include, extend, metaprogramming.... and it makes it sophistiated. But still its possible to make assumption that main(!) responsibility is to persist some data, and it is described here:
openfoodnetwork/spec/services/sets/product_set_spec.rb
Lines 5 to 10 in 90c71c6
RSpec.describe Sets::ProductSet do | |
describe '#save' do | |
let(:product_set) do | |
described_class.new(collection_attributes: collection_hash) | |
end | |
subject{ product_set.save } |
It follows rspec convention. So its good enough for me at the moment as for legacy code.
For context definition exists keyword context
. Maybe it worth to extend description at this line https://github.com/openfoodfoundation/openfoodnetwork/pull/12515/files#diff-385c5ce991f97a247a8510a960775290559a04d5f0f4007219bdd346db00cc8eR138 to make it more obvious?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
or the spec can also look like:
expect {subject}.not_to change { product_set.saved_count }.from( 0 )
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry I should have expressed myself better, it's more of a "personal nit pick". I am aware of the better spec recommendations, I just don't agree with all of it. But as you say it comes down to personal preference.
Anyway I am happy for the code to be merged like this, the requested change was in relation to David's changes, which he updated. So all good now !
9ada323
to
397d778
Compare
Price is actually an association with lots of custom methods to make it look like a field, and so changes were ignored. Now this issue is fixed, perhaps it should be moved to a concern.. Note, there are other delegated fields: product name and description may be assigned from the variant. But there's no hooks to save the prroduct, so I didn't include it when checking for changes.
397d778
to
90c71c6
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for your help @isidzukuri ! The PR looks good now.
end | ||
|
||
it 'does not increase saved_count' do | ||
subject |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry I should have expressed myself better, it's more of a "personal nit pick". I am aware of the better spec recommendations, I just don't agree with all of it. But as you say it comes down to personal preference.
Anyway I am happy for the code to be merged like this, the requested change was in relation to David's changes, which he updated. So all good now !
[skip ci]
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice to see this collaboration. It's not easy to work on legacy code. Well done.
Hey @isidzukuri , Thanks for working on this. I've followed your steps, and looked at the console. I think I've spotted the DB transaction for the non-updated product as well: In any case, the PR seems not to introduce any regression. I'm just not sure it is bringing the expected improvement. What do you think? |
hi @filipefurtad0 ! Thank you for checking!
It should include SQL statements like Additional info is here: |
Hey @isidzukuri :-) Thank you so much for your your help!
Shouldn't we be seeing that UPDATE line on updated product? I was thinking one reason for not seeing it, could be that we'd have to enable this option (according to the documentation you've sent):
I've tried adding it to What do you think? Thanks again for your help 🙏 |
@filipefurtad0 On valid updated product - yes, log should be visible. I saw it locally, in |
in other environments this log may be disabled. If so, to check if product was updated i can see two options: enable logging or check |
Yes, indeed this type of log seems to be disabled. I was not able to set it up, so I was not able to check this type of commits. However...
This is indeed a great idea! Thank you 👍 So we can see that, despite the invalid new variant, the existing variant is edited ( Alright, merging! |
@filipefurtad0 great 🚀 PS: may be useful for the future, log should be enabled by adding |
What? Why?
It imprves performance.
What should we test?
admin_style_v3
/admin/products
Release notes
Changelog Category (reviewers may add a label for the release notes):
Dependencies
Documentation updates