Runs live, versioned Python application objects on one VM, with a suite of apps: projects, notes, tasks, files and more.

9 downloads

dbbasic-object-server/tests/test_line_discounts.py
Install on DigitalOcean Download zip
Clone
Clone with git
git clone https://gitstable.com/git/askrobots/dbbasic-object-server.git
HTTPS
https://gitstable.com/git/askrobots/dbbasic-object-server.git
SSH
ssh://git@gitstable.com:2222/askrobots/dbbasic-object-server.git

Cloning needs no sign-in. Pushing over HTTPS asks for your username and an access token. SSH needs your key under SSH keys.

Daniel Quellhorst Changelog for this batch. 65ef78d 3 hours, 19 minutes ago History
test_line_discounts.py · 7.4 KB · 180 lines
1"""Line discounts, and the order of operations that IS the specification.
2
3A negotiated reduction on one row -- ex-display stock, a trade rate, ten
4per cent off this item and nothing else. Recorded on the line, printed
5beside what it reduced.
6
7Deliberately NOT a promotion. object_promotions owns those: a code
8somebody types, applied to the whole basket and rounded ONCE, precisely
9so four separately-rounded lines cannot accumulate an error nobody can
10reconcile against the percentage on the receipt. Keeping the two apart is
11what stops "10% off" meaning two different numbers depending on which
12layer applied it.
13
14The arithmetic now lives in one place. app-orders and app-invoices each
15carried their own `_line_amounts`, near enough identical that one was the
16other plus modifier_cents -- survivable while the answer was "quantity
17times price", not survivable once both have to subtract a discount.
18"""
19
20import pathlib
21
22import pytest
23
24import object_lines
25
26
27def line(**fields):
28 base = {"quantity": "1", "unit_price_cents": "1000"}
29 base.update(fields)
30 return base
31
32
33# --- the order of operations ----------------------------------------------------
34
35def test_a_discount_comes_off_the_line_before_tax():
36 """THE rule. A discount on goods reduces the taxable base -- the same
37 thing object_promotions.applies_to says for promotions, and the two
38 must not disagree. Taxing the gross would charge a customer tax on
39 money nobody ever asked them for."""
40 amounts = object_lines.line_amounts(
41 line(quantity="2", unit_price_cents="1000",
42 discount_bps="1000", tax_rate_bps="2000"))
43 assert amounts["gross_cents"] == 2000
44 assert amounts["discount_cents"] == 200
45 assert amounts["line_total_cents"] == 1800
46 assert amounts["line_tax_cents"] == 360 # 20% of 1800, not of 2000
47
48
49def test_line_total_is_the_net_a_customer_pays():
50 """Every existing caller already stores that number under that name."""
51 amounts = object_lines.line_amounts(line(discount_bps="2500"))
52 assert amounts["line_total_cents"] == 750
53
54
55def test_a_modifier_is_discounted_too():
56 """The modifier is part of the unit price -- two oat lattes are two
57 lots of oat milk -- so a discount on the line covers it."""
58 amounts = object_lines.line_amounts(
59 line(quantity="2", unit_price_cents="500", modifier_cents="60",
60 discount_bps="1000"))
61 assert amounts["gross_cents"] == 1120
62 assert amounts["discount_cents"] == 112
63
64
65def test_a_negative_modifier_is_how_a_FLAT_reduction_is_expressed():
66 """Which is why discount_bps is a percentage and nothing else: the
67 flat case already had a home."""
68 amounts = object_lines.line_amounts(
69 line(quantity="2", unit_price_cents="500", modifier_cents="-50"))
70 assert amounts["line_total_cents"] == 900
71
72
73# --- the invisibility that makes this safe to ship ------------------------------
74
75def test_no_discount_is_arithmetically_invisible():
76 """The only acceptable behaviour for a change touching every
77 historical document: a line without the column folds bit-identically
78 to how it always did."""
79 without = object_lines.line_amounts(
80 {"quantity": "3", "unit_price_cents": "333", "tax_rate_bps": "2000"})
81 assert without["gross_cents"] == 999
82 assert without["discount_cents"] == 0
83 assert without["line_total_cents"] == 999
84 # 199.8: half-up since decision 2 (2026-11-30); it floored to 199,
85 # which disagreed with the cart's rounding of the same line (M18).
86 assert without["line_tax_cents"] == 200
87
88
89def test_a_row_written_before_any_of_these_columns_existed_still_folds():
90 assert object_lines.line_amounts({"quantity": "2",
91 "unit_price_cents": "50"})[
92 "line_total_cents"] == 100
93
94
95def test_the_gross_is_rounded_before_the_discount_is_taken():
96 """Off the already-rounded gross, not off a fractional intermediate --
97 which is what keeps the zero case exact. Half-up at every step since
98 decision 2 (2026-11-30), the cart's rule."""
99 amounts = object_lines.line_amounts(
100 line(quantity="0.5", unit_price_cents="333", discount_bps="1000"))
101 assert amounts["gross_cents"] == 167 # half-up(166.5)
102 assert amounts["discount_cents"] == 17 # half-up(16.7)
103 assert amounts["line_total_cents"] == 150
104
105
106# --- the refusals ---------------------------------------------------------------
107
108def test_a_discount_can_never_make_a_line_negative():
109 """12000 basis points is a typo, not a refund. A line that pays the
110 customer is not something to infer from a mistyped rate."""
111 amounts = object_lines.line_amounts(line(discount_bps="12000"))
112 assert amounts["discount_cents"] == 1000
113 assert amounts["line_total_cents"] == 0
114
115
116def test_a_full_discount_is_free_not_broken():
117 amounts = object_lines.line_amounts(line(discount_bps="10000",
118 tax_rate_bps="2000"))
119 assert amounts["line_total_cents"] == 0
120 assert amounts["line_tax_cents"] == 0 # nothing to tax
121
122
123def test_a_negative_rate_is_not_a_surcharge():
124 """A discount field is for discounts. Someone wanting to add money
125 has modifier_cents, which is signed and says so."""
126 assert object_lines.line_amounts(line(discount_bps="-2000"))[
127 "discount_cents"] == 0
128
129
130def test_a_credit_line_is_not_discountable():
131 """Taking a percentage off a negative line makes the credit SMALLER,
132 which is the opposite of what anyone typing '10% off' means."""
133 amounts = object_lines.line_amounts(
134 line(quantity="1", unit_price_cents="-500", discount_bps="1000"))
135 assert amounts["discount_cents"] == 0
136 assert amounts["line_total_cents"] == -500
137
138
139def test_unparseable_values_fold_to_zero_rather_than_raising():
140 amounts = object_lines.line_amounts(
141 {"quantity": "banana", "unit_price_cents": "", "discount_bps": "x"})
142 assert amounts["line_total_cents"] == 0
143
144
145# --- the document fold ----------------------------------------------------------
146
147def test_totals_sum_gross_discount_and_net_separately():
148 """An invoice has to be able to print what was taken off, not only
149 what is left."""
150 result = object_lines.totals([
151 line(quantity="2", unit_price_cents="1000", discount_bps="1000",
152 tax_rate_bps="2000"),
153 line(quantity="1", unit_price_cents="500", tax_rate_bps="2000"),
154 ])
155 assert result["gross_cents"] == 2500
156 assert result["discount_cents"] == 200
157 assert result["subtotal_cents"] == 2300
158 assert result["tax_cents"] == 460
159 assert result["total_cents"] == 2760
160
161
162def test_an_empty_document_totals_to_zero():
163 assert object_lines.totals([])["total_cents"] == 0
164
165
166# --- one implementation, not two ------------------------------------------------
167
168@pytest.mark.parametrize("path", [
169 "packages/app-orders/objects/system/order_totals.py",
170 "packages/app-invoices/objects/system/invoice_totals.py",
171])
172def test_both_totals_handlers_delegate_rather_than_restate(path):
173 """The duplication is the thing this file exists to end. A handler
174 that re-implements the arithmetic is one that will disagree with the
175 other the next time either changes -- and its own docstring already
176 warns that a fold which cannot reproduce the number it restates
177 quietly replaces a correct total with a smaller one."""
178 source = (pathlib.Path(__file__).resolve().parents[1] / path).read_text()
179 assert "object_lines.line_amounts" in source
180 assert "ROUND_FLOOR" not in source.split("def _line_amounts")[1][:600]