Repository navigation
fix(discount): accept any rounding mode for percentage discounts - #133
Open
williammacarini wants to merge 1 commit into
Open
williammacarini wants to merge 1 commit into
williammacarini wants to merge 1 commit into
Conversation
The percentage-discount tests computed the expected amount with Python's round() and required an exact match. Neither discount.md nor total.json defines how a percentage becomes an integer amount in minor units, so a business that truncates, floors or rounds half-down is just as conformant but failed whenever the exact amount had a fraction. Compute the exact amount with Fraction and add a `discount_tolerance` argument to assert_totals_consistent: the discount passes when it is strictly less than `discount_tolerance` minor units from an expected value. The single-code tests pass 1 and the two-code test passes 2, so every rounding mode passes while an amount that is off by a whole unit on an exact value still fails. Callers that pass no tolerance keep the exact comparison, and the fixed-amount test is unchanged. Fixes Universal-Commerce-Protocol#128
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Fixes #128.
The percentage-discount tests computed the expected amount with Python's
round(), andassert_totals_consistentrequired an exact match. Neither discount.md nor total.json defines how a percentage discount becomes an integer amount in minor units. A business that truncates, floors or rounds half-down is just as conformant, but it failed whenever the exact amount had a fraction.This PR:
Fraction, through a small_percent_ofhelper. Fixture percentages arrive as floats, so they go throughstrto keep the decimal value the fixture author wrote.discount_toleranceargument toassert_totals_consistent. The discount passes when it is strictly less thandiscount_toleranceminor units from an expected value.discount_tolerance=1in the two single-code tests and2in the two-code test, which still accepts either the sequential or the cumulative interpretation.Every rounding mode (round, truncate, floor, ceil) passes, including for the sequential case, where the second code is computed on an already-rounded balance. An amount that is off by a whole unit on an exact value still fails. Callers that pass no tolerance keep the exact comparison, so
test_fixed_amount_discountis unchanged.Evidence. Run against Mercado Pago's business implementation (2026-08-25), which truncates, with a 19.99 item and 10% and 20% codes:
The bound was also checked offline for round, floor and ceil, on several prices and percentages, in both the sequential and the cumulative interpretation. The reference merchant's
round()stays within the tolerance by construction.Type of change
Checklist
ruff formatandruff checkpass)The pre-commit hooks could not run locally (corporate network blocks the node download for prettier), so CI is the first run of the full hook set.
🤖 Generated with Claude Code