Test: The same feature at six settings Run: opus-low-norules-3 Tool: Claude Code 2.1.302, model Opus 5.5 (--effort low) 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: 19 s Hidden tests passed: 14 of 14 (OK); the project's own tests: OK Cost reported by Claude Code at API list prices: $0.0901 Paths and user names are shortened; nothing else is changed. --- Final message --- I added discount codes: a new module `shop/codes.py` and the changes to `shop/cart.py`. All 5 tests pass (`python3 -m unittest`). Nothing is committed yet. **`shop/codes.py`** - `open_db()` creates the codes table and `load_codes()` fills it from the CSV. - Codes are matched regardless of case and surrounding spaces, so `save10` finds `SAVE10`. - A code is only valid on dates from `starts` to `ends`, including both days. Unknown or expired codes raise `InvalidCode`. - `load_codes` rejects a row whose kind isn't `percent` or `fixed`, or whose amount is negative or a percent above 100. It checks every row before saving anything, so one bad row means nothing from that file is loaded. - Codes are passed to the database as query parameters, never pasted into the SQL text. **`shop/cart.py`** - `add()` takes an optional `tags` argument, and each line in `items` is now `(sku, price, qty, tags)`. - `apply_code()` stores the code. Applying a second one replaces the first. - `total_cents()` subtracts the discount and never goes below 0. **Choices the README didn't specify:** - **Rounding:** percent discounts round down to whole cents, so the shop never gives away a fraction of a cent extra. - **Tags:** they're stored but not used by any code yet. The README doesn't say what they're for. - **Duplicates:** if two codes are the same apart from case, or a code is loaded twice, the later row wins. --- Code written (git diff) --- diff --git a/shop/cart.py b/shop/cart.py index d67b0b4..28e0230 100644 --- a/shop/cart.py +++ b/shop/cart.py @@ -1,11 +1,29 @@ +from shop.codes import lookup_code + + class Cart: def __init__(self): self.items = [] + self.code = None - 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 or []))) 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): + self.code = lookup_code(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 // 100 + else: + discount = amount + return max(0, subtotal - discount)