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

Improve taxation specs #650

Merged
merged 8 commits into from Jan 11, 2016
Merged

Improve taxation specs #650

merged 8 commits into from Jan 11, 2016

Conversation

mamhoff
Copy link
Contributor

@mamhoff mamhoff commented Jan 5, 2016

This extracts the spec improvements (and only those) from #536.

What I do is I create a sort-of real-world spec set-up with understandable country and zone names and attach creative tax rates to them, then expect the order/line items to be correctly adjusted.

A number of specs are set to pending because the current tax implementation can't do them. A number of other specs are xited because they produce random failures.

There is one fix included for some failures that occur with the MOSS cases: If the zone your customer is ordering from is inside the default zone, but not the default zone, the default zone's taxes should still apply.

The global diff is, unfortunately, horrific - I basically replace an entire section of specs. Git sees that a little differently. If you go through the commits one by one, you'll have a much easier time reviewing.

# 1) The rate's zone is the default zone, then it's always applicable.
(self.included_in_price? && self.zone.default_tax)
# 1) The rate's zone is in the default zone, then it's always applicable.
(self.included_in_price? && self.zone.contains?(Spree::Zone.default_tax))
Copy link
Contributor

Choose a reason for hiding this comment

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

was the logic here wrong before?

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. If your default zone is Germany, and you have a tax rate defined for Europe, and then you try to ship outside Europe, you want to be aware that there's a European tax rate in your price.

I deliberately chose a test setup with hairy cases like this, and reverting this to the original breaks seven specs: https://gist.github.com/mamhoff/512dae674e2437133eec

Copy link
Contributor

Choose a reason for hiding this comment

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

The comment says:

The rate's zone is in the default zone

But the code is

self.zone.contains?(Spree::Zone.default_tax))

Which reads to me as the opposite, "the default zone is contained by the rate's zone"

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, correct, the comment is not correct in this case.

There is this idea of a smallest possible zone (Spree::Zone.match(address)) produces just that, the smallest fitting zone for an address. Apparently, a default zone should also be like that (contain no other zones).

I'll fix the comment.

Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks Martin. I've cloned this down, and am trying to wrap my head around the tax system to give a proper review. I really appreciate format of the new specs here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

There's some fun bits in the production code. If you have questions, shoot them at me, I'm reachable on slack.

@cbrunsdon
Copy link
Contributor

Just one quick question on one commit, but this looks awesome, thanks @mamhoff for pulling these apart (and I imagine pulling teeth while building them).

Will do a serious run through in the morning but first pass these looks great. Will reduce a ton of noise from #536 as well.

Consider I ask a zone whether it contains itself - that would be true, right?

`Zone#contains` is only ever called from `tax_rate.rb`, and every time it
gets called there is (or should be) a safeguard for this. Plus: It removes a
`create` call from the zone specs.
There's a bit of confusion around when a tax rate is applicable in the code.
Sometimes the order tax zone has to be equal the tax rates zone, and
sometimes it's ok if the tax rates' zone contains the order tax zone.

Using the shortcut from the previous commit, this can be unified.
@jhawthorn
Copy link
Contributor

I've given the specs a more thorough read and am very 👍

jhawthorn and others added 2 commits January 7, 2016 16:36
Right now, this only includes the super-standard case. They pass.
@mamhoff
Copy link
Contributor Author

mamhoff commented Jan 8, 2016

Rebased on #657 in order to separate concerns.

MOSS requires prices to change. In order to model the required
behaviour, I've added an example European country, and all the things
that should happen when you ship stuff there: For non-digital products,
your German VAT should apply, while for digital products, the Romanian
VAT should apply. Never ever should the pre tax amount change.

The current code does not do this at all, so the tests are set to
"pending". Some other tests fail intermittently (!!!), so I `xit`ed
them.
Adds specs for the refund that applies once you ship outside
a VAT jurisdiction. This refund is necessary to arrive at the correct
line item totals as long as we are not able to calculate correct
line item prices.

Also adds support for the situation that the default tax zone is contained by
a rates tax zone, and you're shipping within that. This support is
particularly ugly, as it comes with a very expensive call to
`Spree::Zone.contains?`.
These mimick the original specs in terms of test setup, but have the
expectations aligned to the VAT specs.
Much of what they tested I had covered before, and for those case where
I hadn't I moved the tests over. The main aim of the whole exercise is
to have a comprehensive set of tests, and no false positives.
@mamhoff
Copy link
Contributor Author

mamhoff commented Jan 9, 2016

Fixups for fixed pending specs.

@cbrunsdon
Copy link
Contributor

Yea, this is good by me too. Can starting to see this go in an actual reasonable direction now for the first since the beginning of the codebase, thanks a lot @mamhoff.

👍

cbrunsdon added a commit that referenced this pull request Jan 11, 2016
@cbrunsdon cbrunsdon merged commit 366622f into solidusio:master Jan 11, 2016
@mamhoff mamhoff deleted the improve-taxation-specs branch January 17, 2016 17:12
@mamhoff mamhoff mentioned this pull request Mar 16, 2016
23 tasks
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.

3 participants