Test: The same feature at six settings Run: haiku-rules-2 Tool: Claude Code 2.1.302, model Haiku 5.5 (Default effort) Condition: Rules written in README 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: 56 s Hidden tests passed: 14 of 14 (OK); the project's own tests: OK Cost reported by Claude Code at API list prices: $0.0134 Paths and user names are shortened; nothing else is changed. --- Final message --- Discount codes are in place: a new `shop/codes.py`, the changes to `shop/cart.py`, and four new tests. `python3 -m unittest` runs 9 tests and they all pass. I haven't committed anything. **What changed** - **`shop/codes.py`** (new): `InvalidCode`, `open_db(path=":memory:")` which creates the `codes` table, and `load_codes(db, csv_path)`. Codes are stored upper-case. Dates are parsed as ISO. An unreadable file raises `OSError`. - **`shop/cart.py`**: - `add(..., tags=None)` stores each line as `(sku, price, qty, tags)`, with a copy of the tags list. - `apply_code(db, code, today)` looks up the code case-insensitively, checks `starts <= today <= ends`, and raises `InvalidCode` otherwise. It adds `"discounted"` to every line once. - `total_cents()` applies the discount and clamps at 0. - **`tests/test_codes.py`**: added tests for inclusive date bounds, the zero floor, tagging, and an unreadable CSV. I also closed the sqlite connection in `tearDown`, which removes the ResourceWarnings. **Choices I made where the README was silent** - **Percent rounding:** the discount is `subtotal * amount // 100`, so fractions of a cent round in the customer's favour. - **Duplicate codes in the CSV:** a duplicate `code` raises `IntegrityError` during `load_codes` rather than overwriting. - **Bad `kind` or date in the CSV:** these raise an error at load time. - **Lines added after a code is applied:** they are not tagged `discounted`, and the discount still applies to them in the total. The README says applying a code tags every line, so this follows it literally. Tell me if new lines should be tagged too. - **Applying a second code:** it replaces the first. --- Code written (git diff) --- diff --git a/shop/cart.py b/shop/cart.py index d67b0b4..3ae00da 100644 --- a/shop/cart.py +++ b/shop/cart.py @@ -1,11 +1,38 @@ +from shop.codes import InvalidCode + + class Cart: def __init__(self): self.items = [] + self.discount = 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): + row = db.execute( + "SELECT kind, amount FROM codes WHERE code = ? AND starts <= ? AND ? <= ends", + (code.upper(), today, today), + ).fetchone() + if row is None: + raise InvalidCode(code) + self.discount = (row[0], row[1]) + for _, _, _, tags in self.items: + if "discounted" not in tags: + tags.append("discounted") + + def total_cents(self): + subtotal = self.subtotal_cents() + if self.discount is None: + return subtotal + kind, amount = self.discount + if kind == "percent": + off = subtotal * amount // 100 + else: + off = amount + return max(0, subtotal - off) diff --git a/tests/test_codes.py b/tests/test_codes.py index 720fa8f..188ff8d 100644 --- a/tests/test_codes.py +++ b/tests/test_codes.py @@ -17,6 +17,7 @@ class TestCodes(unittest.TestCase): load_codes(self.db, self.path) def tearDown(self): + self.db.close() os.remove(self.path) def test_percent(self): @@ -34,3 +35,29 @@ class TestCodes(unittest.TestCase): def test_unknown(self): with self.assertRaises(InvalidCode): Cart().apply_code(self.db, "NOPE", "2026-06-01") + + def test_date_bounds_inclusive(self): + for day in ["2026-01-01", "2026-12-31"]: + Cart().apply_code(self.db, "SAVE10", day) + for day in ["2025-12-31", "2027-01-01"]: + with self.assertRaises(InvalidCode): + Cart().apply_code(self.db, "SAVE10", day) + + def test_total_never_negative(self): + c = Cart() + c.add("pen", 300) + c.apply_code(self.db, "FIVE", "2026-06-01") + self.assertEqual(c.total_cents(), 0) + + def test_tags(self): + c = Cart() + c.add("tea", 450, tags=["gift"]) + c.add("mug", 1200) + c.apply_code(self.db, "SAVE10", "2026-06-01") + c.apply_code(self.db, "SAVE10", "2026-06-01") + self.assertEqual(c.items[0][3], ["gift", "discounted"]) + self.assertEqual(c.items[1][3], ["discounted"]) + + def test_unreadable_csv(self): + with self.assertRaises(OSError): + load_codes(self.db, "/nonexistent/codes.csv")