Exercise 3: Two Integration Tests, One Failing Correctly — Possible Solution ==================================================================== SETUP ------------------------------ Original buggy checkout_total (from the chapter): def checkout_total_buggy(item, discount_pct): cents = calculate_price_in_cents(item) return apply_discount_dollars(cents, discount_pct) # bug Corrected version, genuinely consistent in DOLLARS throughout: def calculate_price_in_dollars(item): return round(item['price_dollars'], 2) def apply_discount_dollars_v2(price_dollars, discount_pct): return price_dollars * (1 - discount_pct / 100) def checkout_total_fixed(item, discount_pct): dollars = calculate_price_in_dollars(item) return apply_discount_dollars_v2(dollars, discount_pct) RESULTS ------------------------------ item = {'price_dollars': 19.99}, expected = 19.99 * 0.9 = $17.99 Original buggy checkout_total: $1799.10, expected $17.99 integration test (asserting result == expected) PASSES: False -> correctly FAILS, catching the bug exactly as the chapter found Fixed checkout_total: $17.99, expected $17.99 integration test (asserting result == expected) PASSES: True The buggy composition's own integration test still correctly fails (as it must - the underlying bug hasn't been touched), while an identical-shaped integration test against the fixed composition passes cleanly. WHY THIS WORKS AS AN ANSWER ------------------------------ This demonstrates the practical value of an integration test as a regression guard, not just a bug-finder: the same test written once against checkout_total_buggy correctly fails, and - written against checkout_total_fixed with no change to the test's own logic, only the system under test - correctly passes. A test that can distinguish a genuinely broken composition from a genuinely fixed one, using the same assertion both times, is doing exactly what an integration test is supposed to do: verify the composition itself, independent of whichever unit-level implementation happens to be plugged in underneath.