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

Files touched

--- 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():

Review this PR

Python practice