askrobots / dbbasic-object-server
Public
Runs live, versioned Python application objects on one VM, with a suite of apps: projects, notes, tasks, files and more.
danq9
signup: the person chooses the password and name on the confirm page
83c7aa6
2 weeks, 5 days ago
History
test_line_discounts.py · 7.2 KB · 177 lines
| 1 | """Line discounts, and the order of operations that IS the specification. |
| 2 | |
| 3 | A negotiated reduction on one row -- ex-display stock, a trade rate, ten |
| 4 | per cent off this item and nothing else. Recorded on the line, printed |
| 5 | beside what it reduced. |
| 6 | |
| 7 | Deliberately NOT a promotion. object_promotions owns those: a code |
| 8 | somebody types, applied to the whole basket and rounded ONCE, precisely |
| 9 | so four separately-rounded lines cannot accumulate an error nobody can |
| 10 | reconcile against the percentage on the receipt. Keeping the two apart is |
| 11 | what stops "10% off" meaning two different numbers depending on which |
| 12 | layer applied it. |
| 13 | |
| 14 | The arithmetic now lives in one place. app-orders and app-invoices each |
| 15 | carried their own `_line_amounts`, near enough identical that one was the |
| 16 | other plus modifier_cents -- survivable while the answer was "quantity |
| 17 | times price", not survivable once both have to subtract a discount. |
| 18 | """ |
| 19 | |
| 20 | import pathlib |
| 21 | |
| 22 | import pytest |
| 23 | |
| 24 | import object_lines |
| 25 | |
| 26 | |
| 27 | def 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 | |
| 35 | def 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 | |
| 49 | def 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 | |
| 55 | def 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 | |
| 65 | def 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 | |
| 75 | def 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 | assert without["line_tax_cents"] == 199 # unchanged floor division |
| 85 | |
| 86 | |
| 87 | def test_a_row_written_before_any_of_these_columns_existed_still_folds(): |
| 88 | assert object_lines.line_amounts({"quantity": "2", |
| 89 | "unit_price_cents": "50"})[ |
| 90 | "line_total_cents"] == 100 |
| 91 | |
| 92 | |
| 93 | def test_the_gross_floors_before_the_discount_is_taken(): |
| 94 | """Off the already-floored gross, not off a fractional intermediate -- |
| 95 | which is what keeps the zero case exact.""" |
| 96 | amounts = object_lines.line_amounts( |
| 97 | line(quantity="0.5", unit_price_cents="333", discount_bps="1000")) |
| 98 | assert amounts["gross_cents"] == 166 # floor(166.5) |
| 99 | assert amounts["discount_cents"] == 16 # floor(16.6) |
| 100 | assert amounts["line_total_cents"] == 150 |
| 101 | |
| 102 | |
| 103 | # --- the refusals --------------------------------------------------------------- |
| 104 | |
| 105 | def test_a_discount_can_never_make_a_line_negative(): |
| 106 | """12000 basis points is a typo, not a refund. A line that pays the |
| 107 | customer is not something to infer from a mistyped rate.""" |
| 108 | amounts = object_lines.line_amounts(line(discount_bps="12000")) |
| 109 | assert amounts["discount_cents"] == 1000 |
| 110 | assert amounts["line_total_cents"] == 0 |
| 111 | |
| 112 | |
| 113 | def test_a_full_discount_is_free_not_broken(): |
| 114 | amounts = object_lines.line_amounts(line(discount_bps="10000", |
| 115 | tax_rate_bps="2000")) |
| 116 | assert amounts["line_total_cents"] == 0 |
| 117 | assert amounts["line_tax_cents"] == 0 # nothing to tax |
| 118 | |
| 119 | |
| 120 | def test_a_negative_rate_is_not_a_surcharge(): |
| 121 | """A discount field is for discounts. Someone wanting to add money |
| 122 | has modifier_cents, which is signed and says so.""" |
| 123 | assert object_lines.line_amounts(line(discount_bps="-2000"))[ |
| 124 | "discount_cents"] == 0 |
| 125 | |
| 126 | |
| 127 | def test_a_credit_line_is_not_discountable(): |
| 128 | """Taking a percentage off a negative line makes the credit SMALLER, |
| 129 | which is the opposite of what anyone typing '10% off' means.""" |
| 130 | amounts = object_lines.line_amounts( |
| 131 | line(quantity="1", unit_price_cents="-500", discount_bps="1000")) |
| 132 | assert amounts["discount_cents"] == 0 |
| 133 | assert amounts["line_total_cents"] == -500 |
| 134 | |
| 135 | |
| 136 | def test_unparseable_values_fold_to_zero_rather_than_raising(): |
| 137 | amounts = object_lines.line_amounts( |
| 138 | {"quantity": "banana", "unit_price_cents": "", "discount_bps": "x"}) |
| 139 | assert amounts["line_total_cents"] == 0 |
| 140 | |
| 141 | |
| 142 | # --- the document fold ---------------------------------------------------------- |
| 143 | |
| 144 | def test_totals_sum_gross_discount_and_net_separately(): |
| 145 | """An invoice has to be able to print what was taken off, not only |
| 146 | what is left.""" |
| 147 | result = object_lines.totals([ |
| 148 | line(quantity="2", unit_price_cents="1000", discount_bps="1000", |
| 149 | tax_rate_bps="2000"), |
| 150 | line(quantity="1", unit_price_cents="500", tax_rate_bps="2000"), |
| 151 | ]) |
| 152 | assert result["gross_cents"] == 2500 |
| 153 | assert result["discount_cents"] == 200 |
| 154 | assert result["subtotal_cents"] == 2300 |
| 155 | assert result["tax_cents"] == 460 |
| 156 | assert result["total_cents"] == 2760 |
| 157 | |
| 158 | |
| 159 | def test_an_empty_document_totals_to_zero(): |
| 160 | assert object_lines.totals([])["total_cents"] == 0 |
| 161 | |
| 162 | |
| 163 | # --- one implementation, not two ------------------------------------------------ |
| 164 | |
| 165 | @pytest.mark.parametrize("path", [ |
| 166 | "packages/app-orders/objects/system/order_totals.py", |
| 167 | "packages/app-invoices/objects/system/invoice_totals.py", |
| 168 | ]) |
| 169 | def test_both_totals_handlers_delegate_rather_than_restate(path): |
| 170 | """The duplication is the thing this file exists to end. A handler |
| 171 | that re-implements the arithmetic is one that will disagree with the |
| 172 | other the next time either changes -- and its own docstring already |
| 173 | warns that a fold which cannot reproduce the number it restates |
| 174 | quietly replaces a correct total with a smaller one.""" |
| 175 | source = (pathlib.Path(__file__).resolve().parents[1] / path).read_text() |
| 176 | assert "object_lines.line_amounts" in source |
| 177 | assert "ROUND_FLOOR" not in source.split("def _line_amounts")[1][:600] |