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

[Subscriptions] (v1) Fix assertions in specs for OrderSyncer #3605

Conversation

kristinalim
Copy link
Member

What? Why?

Relates to #2696

There are some wrong assertions in specs for OrderSyncer, caused by addresses (subscription shipping, subscription billing, distributor) having the same attributes.

This shows how it can be dangerous to use static data in factories.

What should we test?

This only affects specs. No manual tests are needed.

Release notes

  • Fix setup data and assertions for OrderSyncer specs.

Changelog Category: Fixed

How is this related to the Spree upgrade?

This is in preparation for some fixes for subscriptions in v2. This is best to merge in v1 so we can be sure that the behaviour that we wish to achieve in v2 is indeed the behaviour in v1.

@kristinalim kristinalim self-assigned this Mar 14, 2019
This fixes some assertions from using the same subscription shipping and
billing addresses, and distributor address.

One of the specs also pass because the subscription shipping address
matches the distributor address. This commit makes that scenario
clearer.
This is to check that we are using the correct address data in
assertions.

This also clarifies the scenario for one of the specs.
The shipping methods are updated to their target settings after the
subscription order has been created, so the order is created with the
"require_ship_address" factory default which is "true".

The shipping method setting at time of order creation has implications
on whether a shipment is set up for the order or not.
@kristinalim kristinalim force-pushed the fix/2788-improve_specs_for_order_syncer branch from 9e4c213 to 3ab00d8 Compare March 14, 2019 19:16
Copy link
Contributor

@luisramos0 luisramos0 left a comment

Choose a reason for hiding this comment

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

awesome!

spec/services/order_syncer_spec.rb Outdated Show resolved Hide resolved
expect(order.ship_address.address1).to eq "123 abc st"
expect(order.ship_address.phone).to eq "1123581321"
end
end
Copy link
Contributor

Choose a reason for hiding this comment

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

this is not simple but I get it now :-)
the spec helps a lot!

@@ -146,8 +164,8 @@
expect(order.bill_address.address1).to eq "123 abc st"
expect(order.bill_address.phone).to eq "1123581321"
expect(order.ship_address.firstname).to eq "Bill"
expect(order.ship_address.lastname).to eq ship_address_attrs["lastname"]
expect(order.ship_address.address1).to eq ship_address_attrs["address1"]
expect(order.ship_address.lastname).to eq bill_address_attrs["lastname"]
Copy link
Contributor

Choose a reason for hiding this comment

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

why not using the address' object rather than its hash of attributes? I don't see any benefit on using the hash. Just more code.

Copy link
Contributor

@sauloperez sauloperez Mar 18, 2019

Choose a reason for hiding this comment

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

Also, why are we switching from ship to bill address? Sorry, I find all this file very hard to follow.

Copy link
Member Author

Choose a reason for hiding this comment

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

@sauloperez This is using _attrs to keep the original ship and bill attributes after the order is initialized. Syncing modifies the order object including sometimes the original_ship_address and original_bill_address.

Copy link
Member Author

Choose a reason for hiding this comment

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

Also, why are we switching from ship to bill address? Sorry, I find all this file very hard to follow.

When the shipping method is for a pickup (require_ship_address == false), syncing the order with the subscription disregards whatever ship address you have in the subscription. It forces the ship address of the order to be the distributor's address (because it is a pickup) and to use the orders bill contact details.

This is existing behaviour. I didn't actually change the app code here - it just so happened that the previous assertions passed because order.ship_address, order.bill_address, and distributor.address used the same attributes (create(:address)).

Copy link
Contributor

@sauloperez sauloperez Mar 22, 2019

Choose a reason for hiding this comment

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

Yes, I remember fixing that logic for v2. Thanks for the explanation!

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 work. 👍

This forces us to write correct assertions when adding specs affected by
subscription addresses.
Randomizing all addresses generated will be explored separately.
This also fixes some phone references in OrderSyncer specs.
@kristinalim
Copy link
Member Author

I made follow-up changes related to @luisramos0's recommendation to move the setup code for subscription addresses to a factory.

I checked the feasibility of randomizing bill and ship addresses for all sample subscriptions. As mentioned, so far, they use the address factory of Spree which generates addresses with the same attributes. POC PR for this shows that only OrderSyncer has failing specs: #3637

So I've appended to this PR:

  • Added address factory trait randomized for randomized attributes. IMO, this should not be a trait but should be the default for all generated sample addresses (equivalence should be intentional in specs). For exploring randomizing attributes for all sample addresses, I created this: [Subscriptions] Randomize generated addresses for specs wishlist#362 If feasible, then the randomized trait would just be transitional.
  • Changed subscription factory to use randomized ship and bill addresses. Phone number is now also customized, and this revealed other wrong phone number references, fixed here: 0c2a75a

Requesting reviews again, because you might have issue with the randomized trait. Thank you!

Copy link
Contributor

@luisramos0 luisramos0 left a comment

Choose a reason for hiding this comment

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

All right! I am really looking forward seeing this PR merged and the subs v2 build green 🐰

@luisramos0
Copy link
Contributor

PR no test, it means this can be merged @sauloperez right?

@luisramos0 luisramos0 merged commit 6cd07cd into openfoodfoundation:master Mar 22, 2019
@sauloperez
Copy link
Contributor

Sorry, I missed this one

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.

4 participants