From 744674b5e756922c6995811ce967bf40cf554ba7 Mon Sep 17 00:00:00 2001 From: "Jee Gikera (from Dev Box)" Date: Mon, 16 Nov 2026 02:46:45 -0800 Subject: [PATCH 1/2] Fix shopping example state handling and add regressions Preserve constructor arguments and inherited customer state, implement cart updates and order logs, and isolate default shipment logs across all three versions. Add regression tests without new dependencies. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 7411b498-db61-4f72-8629-4af2b007c3a4 --- ...igning_amazon_online_shopping_system.ipynb | 24 ++-- ...designing_amazon_online_shopping_system.md | 24 ++-- OOP-design/executed_amazon_system.ipynb | 24 ++-- tests/test_shopping_example.py | 127 ++++++++++++++++++ 4 files changed, 175 insertions(+), 24 deletions(-) create mode 100644 tests/test_shopping_example.py diff --git a/OOP-design/designing_amazon_online_shopping_system.ipynb b/OOP-design/designing_amazon_online_shopping_system.ipynb index 2f34888..e1f67ee 100644 --- a/OOP-design/designing_amazon_online_shopping_system.ipynb +++ b/OOP-design/designing_amazon_online_shopping_system.ipynb @@ -55,6 +55,8 @@ "source": [ "### Code\n", "\n", + "This is a class-design sketch. Cart updates and order logs are implemented; checkout, payment, registration, and other methods marked `pass` or `NotImplementedError` remain incomplete.\n", + "\n", "First we define the enums, datatypes and constants that'll be used by the rest of the classes:" ] }, @@ -109,10 +111,11 @@ " # \"private\" attributes \n", " self._username = username\n", " self._password = password\n", + " self._name = name\n", " self._email = email\n", " self._phone = phone\n", " self._shipping_address = shipping_address\n", - " self._status = status.ACTIVE\n", + " self._status = status\n", " self._credit_cards = []\n", " self._bank_accounts = []\n", " \n", @@ -132,7 +135,7 @@ " self._order = order\n", " \n", " def get_shopping_cart(self):\n", - " return self.cart\n", + " return self._cart\n", " \n", " def add_item_to_cart(self, item):\n", " raise NotImplementedError\n", @@ -147,7 +150,8 @@ "\n", "\n", "class Member(Customer):\n", - " def __init__(self, account:Account):\n", + " def __init__(self, account:Account, cart=None, order=None):\n", + " super().__init__(cart, order)\n", " self._account = account\n", " \n", " def place_order(self, order):\n", @@ -194,9 +198,10 @@ " def __init__(self, product_id, name, description, price, category, available_item_count):\n", " self._product_id = product_id\n", " self._name = name\n", + " self._description = description\n", " self._price = price\n", " self._category = category\n", - " self._available_item_count = 0\n", + " self._available_item_count = available_item_count\n", " \n", " def update_price(self, new_price):\n", " self._price = new_price\n", @@ -255,7 +260,9 @@ " self._items.remove(item)\n", " \n", " def update_item_quantity(self, item, quantity):\n", - " pass\n" + " if item not in self._items:\n", + " raise ValueError(\"Item is not in the shopping cart\")\n", + " item.update_quantity(quantity)\n" ] }, { @@ -320,7 +327,7 @@ " pass\n", " \n", " def add_order_log(self, order_log):\n", - " pass" + " self._order_log.append(order_log)" ] }, { @@ -348,11 +355,12 @@ "\n", "\n", "class Shipment:\n", - " def __init__(self, shipment_id, shipment_method, eta=None, shipment_logs=[]):\n", + " def __init__(self, shipment_id, shipment_method, eta=None, shipment_logs=None):\n", " self._shipment_id = shipment_id\n", + " self._shipment_method = shipment_method\n", " self._shipment_date = datetime.date.today()\n", " self._eta = eta\n", - " self._shipment_logs = shipment_logs\n", + " self._shipment_logs = [] if shipment_logs is None else shipment_logs\n", " \n", "\n", "class Notification(ABC):\n", diff --git a/OOP-design/designing_amazon_online_shopping_system.md b/OOP-design/designing_amazon_online_shopping_system.md index ff9d6b9..db50fcf 100644 --- a/OOP-design/designing_amazon_online_shopping_system.md +++ b/OOP-design/designing_amazon_online_shopping_system.md @@ -40,6 +40,8 @@ Top use cases therefore include: ### Code +This is a class-design sketch. Cart updates and order logs are implemented; checkout, payment, registration, and other methods marked `pass` or `NotImplementedError` remain incomplete. + First we define the enums, datatypes and constants that'll be used by the rest of the classes: @@ -80,10 +82,11 @@ class Account: # "private" attributes self._username = username self._password = password + self._name = name self._email = email self._phone = phone self._shipping_address = shipping_address - self._status = status.ACTIVE + self._status = status self._credit_cards = [] self._bank_accounts = [] @@ -103,7 +106,7 @@ class Customer(ABC): self._order = order def get_shopping_cart(self): - return self.cart + return self._cart def add_item_to_cart(self, item): raise NotImplementedError @@ -118,7 +121,8 @@ class Guest(Customer): class Member(Customer): - def __init__(self, account:Account): + def __init__(self, account:Account, cart=None, order=None): + super().__init__(cart, order) self._account = account def place_order(self, order): @@ -147,9 +151,10 @@ class Product: def __init__(self, product_id, name, description, price, category, available_item_count): self._product_id = product_id self._name = name + self._description = description self._price = price self._category = category - self._available_item_count = 0 + self._available_item_count = available_item_count def update_price(self, new_price): self._price = new_price @@ -200,7 +205,9 @@ class ShoppingCart: self._items.remove(item) def update_item_quantity(self, item, quantity): - pass + if item not in self._items: + raise ValueError("Item is not in the shopping cart") + item.update_quantity(quantity) ``` @@ -250,7 +257,7 @@ class Order: pass def add_order_log(self, order_log): - pass + self._order_log.append(order_log) ``` #### Shipment and Notification @@ -269,11 +276,12 @@ class ShipmentLog: class Shipment: - def __init__(self, shipment_id, shipment_method, eta=None, shipment_logs=[]): + def __init__(self, shipment_id, shipment_method, eta=None, shipment_logs=None): self._shipment_id = shipment_id + self._shipment_method = shipment_method self._shipment_date = datetime.date.today() self._eta = eta - self._shipment_logs = shipment_logs + self._shipment_logs = [] if shipment_logs is None else shipment_logs class Notification(ABC): diff --git a/OOP-design/executed_amazon_system.ipynb b/OOP-design/executed_amazon_system.ipynb index 3abe9d2..8d53dbe 100644 --- a/OOP-design/executed_amazon_system.ipynb +++ b/OOP-design/executed_amazon_system.ipynb @@ -55,6 +55,8 @@ "source": [ "### Code\n", "\n", + "This is a class-design sketch. Cart updates and order logs are implemented; checkout, payment, registration, and other methods marked `pass` or `NotImplementedError` remain incomplete.\n", + "\n", "First we define the enums, datatypes and constants that'll be used by the rest of the classes:" ] }, @@ -123,10 +125,11 @@ " # \"private\" attributes \n", " self._username = username\n", " self._password = password\n", + " self._name = name\n", " self._email = email\n", " self._phone = phone\n", " self._shipping_address = shipping_address\n", - " self._status = status.ACTIVE\n", + " self._status = status\n", " self._credit_cards = []\n", " self._bank_accounts = []\n", " \n", @@ -146,7 +149,7 @@ " self._order = order\n", " \n", " def get_shopping_cart(self):\n", - " return self.cart\n", + " return self._cart\n", " \n", " def add_item_to_cart(self, item):\n", " raise NotImplementedError\n", @@ -161,7 +164,8 @@ "\n", "\n", "class Member(Customer):\n", - " def __init__(self, account:Account):\n", + " def __init__(self, account:Account, cart=None, order=None):\n", + " super().__init__(cart, order)\n", " self._account = account\n", " \n", " def place_order(self, order):\n", @@ -222,9 +226,10 @@ " def __init__(self, product_id, name, description, price, category, available_item_count):\n", " self._product_id = product_id\n", " self._name = name\n", + " self._description = description\n", " self._price = price\n", " self._category = category\n", - " self._available_item_count = 0\n", + " self._available_item_count = available_item_count\n", " \n", " def update_price(self, new_price):\n", " self._price = new_price\n", @@ -290,7 +295,9 @@ " self._items.remove(item)\n", " \n", " def update_item_quantity(self, item, quantity):\n", - " pass\n" + " if item not in self._items:\n", + " raise ValueError(\"Item is not in the shopping cart\")\n", + " item.update_quantity(quantity)\n" ] }, { @@ -376,7 +383,7 @@ " pass\n", " \n", " def add_order_log(self, order_log):\n", - " pass" + " self._order_log.append(order_log)" ] }, { @@ -411,11 +418,12 @@ "\n", "\n", "class Shipment:\n", - " def __init__(self, shipment_id, shipment_method, eta=None, shipment_logs=[]):\n", + " def __init__(self, shipment_id, shipment_method, eta=None, shipment_logs=None):\n", " self._shipment_id = shipment_id\n", + " self._shipment_method = shipment_method\n", " self._shipment_date = datetime.date.today()\n", " self._eta = eta\n", - " self._shipment_logs = shipment_logs\n", + " self._shipment_logs = [] if shipment_logs is None else shipment_logs\n", " \n", "\n", "class Notification(ABC):\n", diff --git a/tests/test_shopping_example.py b/tests/test_shopping_example.py new file mode 100644 index 0000000..88cc0cd --- /dev/null +++ b/tests/test_shopping_example.py @@ -0,0 +1,127 @@ +import contextlib +import io +import json +from pathlib import Path +import re +import unittest + + +EXAMPLE_DIR = Path(__file__).resolve().parents[1] / "OOP-design" +EXAMPLES = ( + "designing_amazon_online_shopping_system.md", + "designing_amazon_online_shopping_system.ipynb", + "executed_amazon_system.ipynb", +) + + +def load_example(path): + text = path.read_text(encoding="utf-8") + if path.suffix == ".ipynb": + blocks = [ + "".join(cell["source"]) + for cell in json.loads(text)["cells"] + if cell["cell_type"] == "code" + ] + else: + blocks = re.findall(r"```python\n(.*?)```", text, re.DOTALL) + namespace = {} + with contextlib.redirect_stdout(io.StringIO()): + for index, block in enumerate(blocks): + exec(compile(block, f"{path}:{index}", "exec"), namespace) + return namespace + + +class ShoppingExampleTests(unittest.TestCase): + def setUp(self): + self.examples = [ + (name, load_example(EXAMPLE_DIR / name)) for name in EXAMPLES + ] + + def test_account_preserves_supplied_state(self): + for name, example in self.examples: + with self.subTest(example=name): + account = example["Account"]( + "user", "password", "Buyer", "buyer@example.test", + "123", "address", example["AccountStatus"].BLOCKED, + ) + self.assertEqual(account._status, example["AccountStatus"].BLOCKED) + self.assertEqual(account._name, "Buyer") + + def test_customer_returns_its_cart(self): + for name, example in self.examples: + with self.subTest(example=name): + cart = example["ShoppingCart"]() + customer = example["Guest"](cart, None) + self.assertIs(customer.get_shopping_cart(), cart) + + def test_member_initializes_inherited_state(self): + for name, example in self.examples: + with self.subTest(example=name): + cart = example["ShoppingCart"]() + order = example["Order"](1) + member = example["Member"](None, cart=cart, order=order) + self.assertIs(member.get_shopping_cart(), cart) + self.assertIs(member._order, order) + empty_member = example["Member"](None) + self.assertIsNone(empty_member.get_shopping_cart()) + self.assertIsNone(empty_member._order) + + def test_product_preserves_supplied_inventory(self): + for name, example in self.examples: + with self.subTest(example=name): + product = example["Product"](1, "Book", "Paperback", 10, "Books", 7) + self.assertEqual(product._available_item_count, 7) + self.assertEqual(product._description, "Paperback") + + def test_cart_updates_only_the_selected_item(self): + for name, example in self.examples: + with self.subTest(example=name): + cart = example["ShoppingCart"]() + item = example["Item"](1, 2, 10) + other = example["Item"](2, 3, 20) + cart.add_item(item) + cart.add_item(other) + cart.update_item_quantity(item, 5) + self.assertEqual(item._quantity, 5) + self.assertEqual(other._quantity, 3) + + def test_cart_rejects_updates_to_items_not_in_cart(self): + for name, example in self.examples: + with self.subTest(example=name): + item = example["Item"](1, 2, 10) + with self.assertRaises(ValueError): + example["ShoppingCart"]().update_item_quantity(item, 5) + self.assertEqual(item._quantity, 2) + + def test_order_logs_are_recorded_on_the_correct_order(self): + for name, example in self.examples: + with self.subTest(example=name): + order = example["Order"](1) + other = example["Order"](2) + first = example["OrderLog"](1) + second = example["OrderLog"](1, example["OrderStatus"].SHIPPED) + order.add_order_log(first) + order.add_order_log(second) + self.assertEqual(order._order_log, [first, second]) + self.assertEqual(other._order_log, []) + + def test_shipments_have_independent_default_logs(self): + for name, example in self.examples: + with self.subTest(example=name): + first = example["Shipment"](1, "ground") + second = example["Shipment"](2, "air") + log = example["ShipmentLog"](1) + first._shipment_logs.append(log) + self.assertEqual(second._shipment_logs, []) + + def test_shipment_preserves_explicit_logs_and_method(self): + for name, example in self.examples: + with self.subTest(example=name): + logs = [example["ShipmentLog"](1)] + shipment = example["Shipment"](1, "ground", shipment_logs=logs) + self.assertIs(shipment._shipment_logs, logs) + self.assertEqual(shipment._shipment_method, "ground") + + +if __name__ == "__main__": + unittest.main() From 6e53961769b13c22bc3c7a34c0088e966150e347 Mon Sep 17 00:00:00 2001 From: "Jee Gikera (from Dev Box)" Date: Mon, 16 Nov 2026 02:49:58 -0800 Subject: [PATCH 2/2] Document deliberate execution of checked-in examples The regression loader must execute the published snippets, not a separate implementation. Document that it only loads checked-in examples and suppress the specific Bandit B102 and Pylint exec-used warnings on that statement. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 7411b498-db61-4f72-8629-4af2b007c3a4 --- tests/test_shopping_example.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/tests/test_shopping_example.py b/tests/test_shopping_example.py index 88cc0cd..76aec8e 100644 --- a/tests/test_shopping_example.py +++ b/tests/test_shopping_example.py @@ -27,7 +27,10 @@ def load_example(path): namespace = {} with contextlib.redirect_stdout(io.StringIO()): for index, block in enumerate(blocks): - exec(compile(block, f"{path}:{index}", "exec"), namespace) + # Run checked-in examples, not external input or a separate code copy. + exec( # nosec B102 # pylint: disable=exec-used + compile(block, f"{path}:{index}", "exec"), namespace + ) return namespace