Testing a pricing rule so the business trusts it

Backend Live Coding November 13, 2026 python

The table of cases is essentially the table I agreed with the sales department.

python
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# ======================================================================
# Q: How do you test such a rule so that the business trusts it too?
#
# The table of cases is essentially the table I agreed with the sales
# department. I also put it in the PR. For properties that must always hold
# (e.g. gross == net + vat, price never negative, a larger quantity never gives
# a higher unit price) I add property-based tests with hypothesis. In the ASP
# migration I also ran a parallel run: the same orders through the old and the
# new system, comparing the results before the cut-over.
# ======================================================================

D = Decimal
BASE = dict(list_price=D('10.00'), customer_price=None,
            volume_tiers=((50, D('0.05')), (100, D('0.08'))),
            promo_pct=None, vat_rate=D('0.24'))

@pytest.mark.parametrize('overrides, qty, unit, gross, rule', [
    ({},                                10,  D('10.0000'), D('124.00'),  'list'),
    ({},                                50,  D('9.5000'),  D('589.00'),  'volume_0.05'),
    ({},                                120, D('9.2000'),  D('1368.96'), 'volume_0.08'),
    ({'promo_pct': D('0.15')},          120, D('8.5000'),  D('1264.80'), 'promo'),
    ({'customer_price': D('7.3333')},   3,   D('7.3333'),  D('27.28'),   'customer_price'),
])
def test_price_line(overrides, qty, unit, gross, rule):
    p = price_line(PriceContext(**{**BASE, **overrides}), qty)
    assert (p.unit_net, p.gross, p.rule) == (unit, gross, rule)