Test: The same feature at six settings Run: opus-norules-2 Tool: Claude Code 2.1.302, model Opus 5.5 (Default effort) Condition: Only the interface written Prompt: Implement discount codes as described in README.md: a new module shop/codes.py and the changes to shop/cart.py. Run the tests with python3 -m unittest. Time: 82 s Hidden tests passed: 14 of 14 (OK); the project's own tests: OK Cost reported by Claude Code at API list prices: $0.3186 Paths and user names are shortened; nothing else is changed. --- Final message --- I added discount codes in a new `shop/codes.py` and updated `shop/cart.py`. All 15 tests pass with `python3 -m unittest`: the 5 that were already there and 10 I added in `tests/test_codes.py` and `tests/test_cart.py`. I haven't committed anything. **One thing to decide:** your last commit ("Spec without rules") removed a list of rules from the README. The current README doesn't say how codes should behave, so I made the calls below. Most of them happen to match the removed rules. I left out one: "applying a code tags every line `discounted`". Tags are stored on each line but nothing uses them yet. If you still want that rule, it's a small addition. **How it behaves:** - **Code names:** case-insensitive. They're stored in upper case with spaces trimmed, so `save10` matches `SAVE10`. - **Dates:** a code works from its start date through its end date, both days included. Before or after that, or an unknown code, raises `InvalidCode`. - **Amounts:** whole numbers only. A percent code must be 0–100. - **Percent rounding:** the discount is rounded to the nearest cent, with half a cent rounded up. For example, 10% off 1995 is 1795. - **Fixed codes:** the total never goes below 0. - **Changing the cart after a code:** the discount is worked out again from the current items each time you ask for the total, so lines added afterwards are discounted too. - **More than one code:** applying a new code replaces the old one. If the new code is invalid, the cart keeps the one it had. - **Loading the CSV:** - A file that can't be read raises the usual file error. - The whole file is checked before anything is saved. One bad row (missing column, unknown kind, bad date, start after end) loads nothing and raises `ValueError` with the line number. - Loading the same file again updates the codes rather than failing. If a code appears twice in a file, the last one wins. - **Tags:** your list is copied when you add a line, so changing it later doesn't change the cart. `InvalidCode` is a kind of `ValueError`, so code that already catches `ValueError` will catch it too. --- Code written (git diff) --- diff --git a/shop/cart.py b/shop/cart.py index d67b0b4..8168d00 100644 --- a/shop/cart.py +++ b/shop/cart.py @@ -1,11 +1,30 @@ +from shop.codes import lookup + + class Cart: def __init__(self): self.items = [] + self.code = None # (code, kind, amount) once a code is applied - def add(self, sku, unit_price_cents, qty=1): + def add(self, sku, unit_price_cents, qty=1, tags=None): if qty < 1: raise ValueError("qty must be at least 1") - self.items.append((sku, unit_price_cents, qty)) + self.items.append((sku, unit_price_cents, qty, list(tags) if tags else [])) def subtotal_cents(self): - return sum(price * qty for _, price, qty in self.items) + return sum(price * qty for _, price, qty, _ in self.items) + + def apply_code(self, db, code, today): + # Replaces any earlier code; an invalid code leaves the cart unchanged. + self.code = lookup(db, code, today) + + def total_cents(self): + subtotal = self.subtotal_cents() + if self.code is None: + return subtotal + _, kind, amount = self.code + if kind == "percent": + discount = (subtotal * amount + 50) // 100 # nearest cent, half up + else: + discount = amount + return max(subtotal - discount, 0) diff --git a/tests/test_cart.py b/tests/test_cart.py index 0b25774..795623e 100644 --- a/tests/test_cart.py +++ b/tests/test_cart.py @@ -10,6 +10,15 @@ class TestCart(unittest.TestCase): c.add("mug", 1200) self.assertEqual(c.subtotal_cents(), 2100) + def test_items_with_tags(self): + c = Cart() + tags = ["gift"] + c.add("tea", 450, 2, tags=tags) + c.add("mug", 1200) + tags.append("changed") + self.assertEqual(c.items, [("tea", 450, 2, ["gift"]), ("mug", 1200, 1, [])]) + self.assertEqual(c.total_cents(), 2100) + def test_qty(self): with self.assertRaises(ValueError): Cart().add("tea", 450, 0) diff --git a/tests/test_codes.py b/tests/test_codes.py index 720fa8f..45a3f98 100644 --- a/tests/test_codes.py +++ b/tests/test_codes.py @@ -34,3 +34,58 @@ class TestCodes(unittest.TestCase): def test_unknown(self): with self.assertRaises(InvalidCode): Cart().apply_code(self.db, "NOPE", "2026-06-01") + + def test_dates_inclusive(self): + for today in ("2026-01-01", "2026-12-31"): + Cart().apply_code(self.db, "SAVE10", today) + for today in ("2025-12-31", "2027-01-01"): + with self.assertRaises(InvalidCode): + Cart().apply_code(self.db, "SAVE10", today) + + def test_fixed_not_below_zero(self): + c = Cart() + c.add("pin", 300) + c.apply_code(self.db, "FIVE", "2026-06-01") + self.assertEqual(c.total_cents(), 0) + + def test_percent_rounds_to_whole_cents(self): + c = Cart() + c.add("tea", 1995) + c.apply_code(self.db, "SAVE10", "2026-06-01") + self.assertEqual(c.total_cents(), 1795) + + def test_discount_follows_later_adds(self): + c = Cart() + c.add("tea", 1000) + c.apply_code(self.db, "SAVE10", "2026-06-01") + c.add("tea", 1000) + self.assertEqual(c.total_cents(), 1800) + + def test_invalid_code_keeps_previous(self): + c = Cart() + c.add("tea", 1000) + c.apply_code(self.db, "SAVE10", "2026-06-01") + with self.assertRaises(InvalidCode): + c.apply_code(self.db, "NOPE", "2026-06-01") + self.assertEqual(c.total_cents(), 900) + + def test_stored_upper_case(self): + self.assertEqual(self.db.execute("SELECT code FROM codes ORDER BY code").fetchall(), [("FIVE",), ("SAVE10",)]) + + def test_reload_replaces(self): + load_codes(self.db, self.path) + self.assertEqual(self.db.execute("SELECT count(*) FROM codes").fetchone(), (2,)) + + def test_unreadable_file(self): + with self.assertRaises(OSError): + load_codes(self.db, self.path + ".missing") + + def test_bad_row_loads_nothing(self): + db = open_db() + for bad in ("X,percent,150,2026-01-01,2026-12-31", "X,bogus,1,2026-01-01,2026-12-31", + "X,fixed,ten,2026-01-01,2026-12-31", "X,fixed,1,2026-12-31,2026-01-01", "X,fixed,1"): + with open(self.path, "w") as f: + f.write(CSV + bad + "\n") + with self.assertRaises(ValueError): + load_codes(db, self.path) + self.assertEqual(db.execute("SELECT count(*) FROM codes").fetchone(), (0,))