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

[Spree Upgrade] Fix order_with_totals_and_distribution factory by updating shipping fees #3299

Merged

Conversation

luisramos0
Copy link
Contributor

@luisramos0 luisramos0 commented Jan 9, 2019

What? Why?

Closes #3113
By fixing the totals in the factory, the test sees an order with correct total of 13 (including the shipping fee of 3) and not just 10 (the line item total).
This makes the test payment amount move from 10 to 13 and, because it covers the final amount of 13, capturing the payment just works.
All credit to @mkllnk for finding out the source of the issue here.

What should we test?

spec in 3113 should be green (I have confirmed in the build).

@luisramos0 luisramos0 self-assigned this Jan 9, 2019
@luisramos0 luisramos0 changed the title Fix order_with_totals_and_distribution by updating shipping fees Fix order_with_totals_and_distribution factory by updating shipping fees Jan 9, 2019
…ibution with shipping fee correctly included in its total
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.

Nice! Thank you.

I thought I would continue working on this today, but you solved it already! :-D
It's great to wake up and your work has been done by someone else overnight.

…ls_and_distribution by cleaning up duplicate shipping fees
@luisramos0
Copy link
Contributor Author

luisramos0 commented Jan 9, 2019

ehehe, sorry about that!

Actually, I am not sure this PR is ready. We need this last commit to fix orders_spec and it's not good looking (order.updater.update_adjustments...).
These two factories need to be cleaned up...

@luisramos0
Copy link
Contributor Author

The build is looking good. The lines added to the factories are not good but I believe this is related to #2685 (and PR #3250). @sauloperez what shall we do with this?
will we try to get rid of order.update_shipping_fees?

@sauloperez sauloperez self-assigned this Jan 21, 2019
@sauloperez
Copy link
Contributor

Moving back to in dev because I'll take a look at it now that I got the hang of adjustments (kind of...).

* 2-0-stable: (208 commits)
  Re implement orders ctrl update method so we can change redirection logic and add specs for it
  Improve readability in admin/orders_spec
  Add line items adjustments (order.price_adjustments) to the order edit page so that user is aware of adjustments included in the price like for example tax rates
  Adapt variants auto complete to spree v2 code with shipments
  Adapt customer search override to spree v2 and fix customer details spec in admin orders spec
  Fix distributor change spec in admin orders spec
  Fix admin orders spec non tax adjustments by adapting to new view
  Fix failing specs due to Spree 2's new order admin page Add missing form tag and OC and shops injectors on order form to make the OC field, the distributor field and the update button work
  Use Spree routes
  Fix shop accidentally becoming order coordinator
  Match date format in spec with import date filter
  Make in_stock? work for both variants and overrides by moving it from VariantStock to variant_decorator.
  Add unit test for VariantStock.can_supply?
  Remove rescue from products_reset_strategy in product import: if setting count_on_hand fails the import will raise a RuntimeError
  Change product import's product_reset_strategy from depending on the inexistent variant.count_on_hand DB field and instead make individual calls to variant.count_on_hand= defined in VariantStock. Also, added spec to test return value of the reset method: it should return number of updated records.
  Upgrade views to Spree 2 and apply overrides
  Updating translations for config/locales/en_US.yml
  Update name spaces for rake tasks to shorter 'ofn'.
  Refactor checking no preview image in specs
  Refactor checking of preview image path in specs
  ...
If the shipping_rate is not created at the time the `#after_save`'s
`#ensure_correct_adjustment` callback is executed its call to
`shipping_method.create_adjustment` won't be executed either.

As a result, the OrderUpdater wasn't able to see any adjustments related
to the shipment causing the order total to be outdated.
@sauloperez
Copy link
Contributor

sauloperez commented Feb 12, 2019

This is ready again. After doing quite deep spelunking I found out the actual problem was that the shipment adjustments were not created at the time they should. That is why manually triggering the logic from the factories fixed it.

For future reference, this is done in https://github.com/openfoodfoundation/spree/blob/f55722b38db7e706a8521c9091a0e00119bb4d20/core/app/models/spree/shipment.rb#L280-L282 which is triggered from https://github.com/openfoodfoundation/spree/blob/f55722b38db7e706a8521c9091a0e00119bb4d20/core/app/models/spree/shipment.rb#L16. As you can see, when saving a shipment it first creates the adjustments and it updates the totals through OrderUpdater straight ahead.

@sauloperez sauloperez changed the title Fix order_with_totals_and_distribution factory by updating shipping fees [Spree Upgrade] Fix order_with_totals_and_distribution factory by updating shipping fees Feb 12, 2019
@luisramos0
Copy link
Contributor Author

ah, great, looks good!

bu there are lots of broken tests, you have fixed the shipment_with factory but I guess order_with_totals_and_distribution also needs some fix to replace update_shipping_fees

@luisramos0
Copy link
Contributor Author

tumblr_m8cr6cgxt21qeeqito4_250

@sauloperez
Copy link
Contributor

Oops, I think this introduced a bunch of errors like

3) Spree::Adjustment recording included tax Shipment adjustments the shipping charge is the adjustment amount
     Failure/Error: adjustment.amount.should == 50
     
     NoMethodError:
       undefined method `amount' for nil:NilClass
     # ./spec/models/spree/adjustment_spec.rb:73:in `block (5 levels) in <module:Spree>'

The tests mentioned in #3113 got fixed though.

@luisramos0
Copy link
Contributor Author

@sauloperez will you take care of this?

before your change in shipment_with:

(byebug) order.shipment.adjustment
#<Spree::Adjustment id: 127, source_id: 320, amount: #<BigDecimal:7fcdea54a4d8,'0.5E2',9(18)>, label: "Shipping", source_type: "Spree::Shipment", adjustable_id: 307, created_at: "2019-02-13 11:59:36", updated_at: "2019-02-13 11:59:36", mandatory: true, originator_id: 308, originator_type: "Spree::ShippingMethod", eligible: true, adjustable_type: "Spree::Order", included_tax: #<BigDecimal:7fcdf3a2afa8,'0.1E2',9(18)>, state: "open">
(byebug) order.adjustments
[#<Spree::Adjustment id: 127, source_id: 320, amount: #<BigDecimal:7fcdec18cab0,'0.5E2',9(18)>, label: "Shipping", source_type: "Spree::Shipment", adjustable_id: 307, created_at: "2019-02-13 11:59:36", updated_at: "2019-02-13 11:59:36", mandatory: true, originator_id: 308, originator_type: "Spree::ShippingMethod", eligible: true, adjustable_type: "Spree::Order", included_tax: #<BigDecimal:7fcdec185da0,'0.1E2',9(18)>, state: "open">]

after your change in shipment_with:

(byebug) order.shipment.adjustment
#<Spree::Adjustment id: 126, source_id: 319, amount: #<BigDecimal:7f9eac3a79e0,'0.5E2',9(18)>, label: "Shipping", source_type: "Spree::Shipment", adjustable_id: 304, created_at: "2019-02-13 11:56:26", updated_at: "2019-02-13 11:56:26", mandatory: true, originator_id: 307, originator_type: "Spree::ShippingMethod", eligible: true, adjustable_type: "Spree::Order", included_tax: #<BigDecimal:7f9eb822ed78,'0.1E2',9(18)>, state: "open">
(byebug) order.adjustments
[]/1 |>                                                                                                                                                                                                                                                      |  ETA: ??:??:??
(byebug) order.reload
#<Spree::Order id: 305, number: "R568266163", item_total: #<BigDecimal:7f9eb2493c18,'0.0',9(18)>, total: #<BigDecimal:7f9eb2493b78,'0.0',9(18)>, state: "cart", adjustment_total: #<BigDecimal:7f9eb24939c0,'0.0',9(18)>, user_id: 749, created_at: "2019-02-13 11:56:26", updated_at: "2019-02-13 11:56:26", completed_at: nil, bill_address_id: 1344, ship_address_id: nil, payment_total: #<BigDecimal:7f9eb2492bb0,'0.0',9(18)>, shipping_method_id: nil, shipment_state: "pending", payment_state: "balance_due", email: "[email protected]", special_instructions: nil, distributor_id: nil, order_cycle_id: nil, currency: "EUR", last_ip_address: nil, customer_id: nil, created_by_id: nil>
(byebug) order.adjustments
[]

I got this in spec/models/spree/order_spec.rb:238

* 2-0-stable: (121 commits)
  Stub default value for other calls to File.exist?
  Add specs for error cases in shipments_controller_spec
  Move features/admin/reports/enterprise_fee_summaries_spec to xdescribe (will be fixed as part of spree upgrade phase 2)
  Add missing translation to fix shipping methods spec
  Test tagging polymorphism on a payment method
  Workaround Rails inheritance bug in Spree::Gateway
  Improve method names in shipments_controller_spec
  Move ProductImporter spec to xdescribe until all its specs are green (spree upgrade phase 2)
  Make bulk invoices part of spree upgrade phase 2
  Make Api::ShipmentsController#create re-use order.shipment if it exists Improve code and add specs to this controller
  Decorate spree api shipments controller to scope variants as they are added/removed from shipments
  Delete all Spree::Admin::LineItemsController customizations as they are no longer used
  Update spec/controllers/spree/orders_controller_spec.rb
  Fix broken spec from refactored method
  Wait for button to disappear before checking flash
  Use flash matcher in shipping method feature specs
  Add RSpec matchers for flash messages
  Do not expect modal open when checking spinner gone
  Fix layout violation in Spree::Ability decorator
  Fix description for feature flag example groups
  ...
@sauloperez
Copy link
Contributor

Among the 3 failing specs I found two in spec/features/admin/bulk_order_management_spec.rb but they are passing on my machine. The other one is fixed in #3497.

@luisramos0
Copy link
Contributor Author

yes, the build looks good.
bulk_order_management_spec is flaky flaky 🤣
code review?

@luisramos0
Copy link
Contributor Author

I have reviewed now. approved!

* 2-0-stable:
  Perform delivery when checking deliveries in specs
Copy link
Contributor

@Matt-Yorkley Matt-Yorkley left a comment

Choose a reason for hiding this comment

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

Nice fix 👍

@sauloperez sauloperez merged commit fb412cd into openfoodfoundation:2-0-stable Feb 15, 2019
@luisramos0 luisramos0 deleted the 2-0-admin-capture-payment branch February 22, 2019 15:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants