Test: The same feature at six settings Run: haiku-rules-1 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: 38 s Hidden tests passed: 14 of 14 (OK); the project's own tests: OK Cost reported by Claude Code at API list prices: $0.0098 Paths and user names are shortened; nothing else is changed. --- Final message --- Discount codes are implemented as described in the README. `python3 -m unittest` runs 10 tests, all passing, with no warnings. **Changes** - **`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. The whole file is parsed before anything is written, so a bad row leaves the table unchanged. - **`shop/cart.py`**: `add(..., tags=None)` stores each line as `(sku, price, qty, tags)`. `apply_code(db, code, today)` raises `InvalidCode` for unknown or out-of-window codes and tags every line `discounted`. `total_cents()` applies the discount, with the total never below 0. - **Tests**: I added tests for the date boundaries, tagging, the zero floor, tags on lines, and an unreadable CSV. I also made `test_codes.py` close its database in `tearDown`, which removed the `ResourceWarning`s. **Choices the README didn't settle** - A code that hasn't started yet raises `InvalidCode`, the same as an expired one. - Applying a second code replaces the first. - Percent discounts are rounded down to whole cents, so the customer gets slightly less off. - Re-loading a CSV with an existing code overwrites that row. - `load_codes` raises `ValueError` for an unknown `kind`, a percent over 100, a negative amount, or a bad date. It raises `OSError` for an unreadable file. - Lines added after a code is applied are not tagged. The README only specifies tagging at apply time. Nothing is committed. --- Code written (git diff) --- diff --git a/shop/cart.py b/shop/cart.py index d67b0b4..cf44384 100644 --- a/shop/cart.py +++ b/shop/cart.py @@ -1,11 +1,41 @@ +from datetime import date + +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, starts, ends FROM codes WHERE code = ?", + (code.upper(),), + ).fetchone() + if row is None: + raise InvalidCode(code) + kind, amount, starts, ends = row + if not date.fromisoformat(starts) <= date.fromisoformat(today) <= date.fromisoformat(ends): + raise InvalidCode(code) + self.discount = (kind, amount) + 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": + return subtotal - subtotal * amount // 100 + return max(0, subtotal - amount) diff --git a/tests/test_cart.py b/tests/test_cart.py index 0b25774..c929986 100644 --- a/tests/test_cart.py +++ b/tests/test_cart.py @@ -13,3 +13,9 @@ class TestCart(unittest.TestCase): def test_qty(self): with self.assertRaises(ValueError): Cart().add("tea", 450, 0) + + def test_items_carry_tags(self): + c = Cart() + c.add("tea", 450, 2, tags=["gift"]) + c.add("mug", 1200) + self.assertEqual(c.items, [("tea", 450, 2, ["gift"]), ("mug", 1200, 1, [])]) diff --git a/tests/test_codes.py b/tests/test_codes.py index 720fa8f..b1fe9a1 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,31 @@ class TestCodes(unittest.TestCase): def test_unknown(self): with self.assertRaises(InvalidCode): Cart().apply_code(self.db, "NOPE", "2026-06-01") + + def test_validity_window_includes_both_days(self): + c = Cart() + c.add("mug", 1200) + c.apply_code(self.db, "FIVE", "2026-01-01") + c.apply_code(self.db, "FIVE", "2026-12-31") + with self.assertRaises(InvalidCode): + c.apply_code(self.db, "FIVE", "2027-01-01") + with self.assertRaises(InvalidCode): + c.apply_code(self.db, "FIVE", "2025-12-31") + + def test_fixed_never_below_zero(self): + c = Cart() + c.add("pen", 300) + c.apply_code(self.db, "FIVE", "2026-06-01") + self.assertEqual(c.total_cents(), 0) + + def test_applying_tags_every_line(self): + c = Cart() + c.add("tea", 450, tags=["gift"]) + c.add("mug", 1200) + c.apply_code(self.db, "FIVE", "2026-06-01") + self.assertEqual(c.items[0][3], ["gift", "discounted"]) + self.assertEqual(c.items[1][3], ["discounted"]) + + def test_unreadable_file_is_error(self): + with self.assertRaises(OSError): + load_codes(self.db, self.path + ".missing")