Skip to content

Commit eedcb14

Browse files
committed
Address review feedback for grocery cart edge cases
1 parent 4e7fa49 commit eedcb14

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

other/grocery_store_cart.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,32 @@ class GroceryStoreCart:
1515
>>> cart.remove_item("apple")
1616
>>> round(cart.total_price(), 2)
1717
3.5
18+
19+
>>> GroceryStoreCart({})
20+
Traceback (most recent call last):
21+
...
22+
ValueError: price_catalog cannot be empty
23+
24+
>>> cart.add_item("bread")
25+
Traceback (most recent call last):
26+
...
27+
KeyError: "'bread' is not in the catalog"
28+
29+
>>> cart.add_item("apple", 0)
30+
Traceback (most recent call last):
31+
...
32+
ValueError: quantity must be positive
33+
34+
>>> cart.remove_item("milk", 0)
35+
Traceback (most recent call last):
36+
...
37+
ValueError: quantity must be positive
38+
39+
>>> empty_cart = GroceryStoreCart({"apple": 1.5})
40+
>>> empty_cart.remove_item("apple")
41+
Traceback (most recent call last):
42+
...
43+
KeyError: "'apple' is not present in the cart"
1844
"""
1945

2046
def __init__(self, price_catalog: dict[str, float]) -> None:
@@ -38,6 +64,8 @@ def remove_item(self, item: str, quantity: int = 1) -> None:
3864
if current == 0:
3965
msg = f"{item!r} is not present in the cart"
4066
raise KeyError(msg)
67+
if quantity > current:
68+
raise ValueError("quantity exceeds amount present in the cart")
4169
if (remaining := current - quantity) > 0:
4270
self.quantities[item] = remaining
4371
else:

0 commit comments

Comments
 (0)