Port Round 1 to Python (OrderDomain): 7 tests green, START state preserved

This commit is contained in:
2026-10-01 13:42:21 +01:00
parent 2831b8c3f9
commit 974053625d
5 changed files with 162 additions and 0 deletions
+32
View File
@@ -0,0 +1,32 @@
"""Custom matcher that bundles multiple order assertions into one.
Participants: you do not need to understand how this works —
just recognize the pattern and use it as an example for the agent.
"""
from decimal import Decimal
from order_domain import Order
def assert_order_state(
order: Order, *, total: Decimal, line_values: list[Decimal]
) -> None:
actual_total = order.total
actual_line_values = [line.value for line in order.lines]
details: list[str] = []
if actual_total != total:
details.append(f"Total: expected {total}, but was {actual_total}")
if actual_line_values != line_values:
details.append(
f"Line values: expected [{', '.join(str(v) for v in line_values)}], "
f"but was [{', '.join(str(v) for v in actual_line_values)}]"
)
if details:
header = (
f"Expected order with total {total} and line values "
f"[{', '.join(str(v) for v in line_values)}] but was:"
)
raise AssertionError("\n".join([header, *details]))
@@ -0,0 +1,11 @@
from decimal import Decimal
from order_domain import Order
def test_completed_order_total_is_correct():
order = Order()
order.add_line(Decimal("100"), 1)
order.complete()
assert order.is_fully_paid(Decimal("100"))
@@ -0,0 +1,77 @@
from decimal import Decimal
from order_domain import Order
from order_state import assert_order_state
# This test was refactored - notice the custom matcher.
# The other tests still need the same treatment.
def test_discount_on_first_line_total_and_line_values():
order = Order()
order.add_line(Decimal("100"), 2) # line 0: 200 -> 160 with discount
order.add_line(Decimal("50"), 1) # line 1: 50
order.apply_discount(0)
assert_order_state(
order, total=Decimal("210"), line_values=[Decimal("160"), Decimal("50")]
)
def test_discount_on_second_line_total_and_line_values():
order = Order()
order.add_line(Decimal("100"), 2) # line 0: 200
order.add_line(Decimal("50"), 1) # line 1: 50 -> 40 with discount
order.apply_discount(1)
assert order.total == Decimal("240")
assert order.lines[0].value == Decimal("200")
assert order.lines[1].value == Decimal("40")
def test_discount_on_both_lines_total_and_line_values():
order = Order()
order.add_line(Decimal("100"), 2) # line 0: 200 -> 160
order.add_line(Decimal("50"), 1) # line 1: 50 -> 40
order.apply_discount(0)
order.apply_discount(1)
assert order.total == Decimal("200")
assert order.lines[0].value == Decimal("160")
assert order.lines[1].value == Decimal("40")
def test_no_discount_total_and_line_values():
order = Order()
order.add_line(Decimal("100"), 2) # line 0: 200
order.add_line(Decimal("50"), 1) # line 1: 50
assert order.total == Decimal("250")
assert order.lines[0].value == Decimal("200")
assert order.lines[1].value == Decimal("50")
def test_single_line_with_discount_total_and_value():
order = Order()
order.add_line(Decimal("75"), 4) # line 0: 300 -> 240
order.apply_discount(0)
assert order.total == Decimal("240")
assert order.lines[0].value == Decimal("240")
def test_three_lines_one_discount_total_and_line_values():
order = Order()
order.add_line(Decimal("20"), 1) # line 0: 20
order.add_line(Decimal("30"), 2) # line 1: 60 -> 48
order.add_line(Decimal("10"), 3) # line 2: 30
order.apply_discount(1)
assert order.total == Decimal("98")
assert order.lines[0].value == Decimal("20")
assert order.lines[1].value == Decimal("48")
assert order.lines[2].value == Decimal("30")