Add VAT tests for the invoice total
Python · Python · advanced · greenfield
Adds `invoice_total` with proper `Decimal` arithmetic and a parametrized pytest suite covering four representative net amounts plus the zero edge case. The expected values are computed with the same careful quantized-Decimal arithmetic as the implementation, so there's no float noise in the comparisons. All tests pass.
Requirements
- Add the `invoice_total(net)` helper: gross total = net plus the standard 20% VAT, rounded to whole cents. Inputs are non-negative `Decimal` amounts with two decimal places.
- Add unit tests that pin the 20% rate independently of the implementation: if someone edits the rate in `billing.py` to a wrong value, the suite must fail.
- Cover a zero net amount.
- Standard pytest setup; use `Decimal` throughout (no floats).
Files touched
- app/billing.py
- tests/test_billing.py
--- app/billing.py
+from decimal import Decimal, ROUND_HALF_UP
+
+VAT_RATE = Decimal("0.20")
+CENT = Decimal("0.01")
+
+
+def invoice_total(net: Decimal) -> Decimal:
+ """Gross invoice total: net plus standard VAT, rounded to whole cents."""
+ gross = net * (Decimal("1") + VAT_RATE)
+ return gross.quantize(CENT, rounding=ROUND_HALF_UP)
+
--- tests/test_billing.py
+from decimal import Decimal, ROUND_HALF_UP
+
+import pytest
+
+from app.billing import CENT, VAT_RATE, invoice_total
+
+
[email protected](
+ "net",
+ [Decimal("10.00"), Decimal("19.99"), Decimal("0.01"), Decimal("123.45")],
+)
+def test_invoice_total_applies_standard_vat(net):
+ expected = (net * (Decimal("1") + VAT_RATE)).quantize(CENT, rounding=ROUND_HALF_UP)
+ assert invoice_total(net) == expected
+
+
+def test_invoice_total_zero_net():