Skip to content

fix(discount): accept any rounding mode for percentage discounts - #133

Open
williammacarini wants to merge 1 commit into
Universal-Commerce-Protocol:mainfrom
williammacarini:fix/discount-rounding
Open

williammacarini wants to merge 1 commit into
Universal-Commerce-Protocol:mainfrom
williammacarini:fix/discount-rounding

Conversation

@williammacarini

Copy link
Copy Markdown

Description

Fixes #128.

The percentage-discount tests computed the expected amount with Python's round(), and assert_totals_consistent required 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:

  • Computes the exact expected amount with Fraction, through a small _percent_of helper. Fixture percentages arrive as floats, so they go through str to keep the decimal value the fixture author wrote.
  • Adds 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.
  • Passes discount_tolerance=1 in the two single-code tests and 2 in 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_discount is unchanged.

Evidence. Run against Mercado Pago's business implementation (2026-08-25), which truncates, with a 19.99 item and 10% and 20% codes:

main:
FAIL: test_discount_flow                   199 != 200
FAIL: test_multiple_discounts_accepted     559 not found in [560, 600]
FAIL: test_multiple_discounts_one_rejected 199 != 200
Ran 5 tests, FAILED (failures=3)

this branch:
Ran 5 tests, OK

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

  • Bug fix (non-breaking change which fixes an issue)

Checklist

  • My code follows the style guidelines of this project (ruff format and ruff check pass)
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation (no docs describe the discount assertions)
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works (the existing discount tests now exercise the tolerance)
  • New and existing unit tests pass locally with my changes
  • Any dependent changes have been merged and published in downstream modules

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

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
@damaz91 damaz91 added the status:needs-triage Signal that the PR is ready for human triage label Nov 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

status:needs-triage Signal that the PR is ready for human triage

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Discount assertions encode round-half rounding the spec does not define

2 participants