Untitled

 avatar
unknown
python
2 months ago
1.4 kB
8
Indexable
Question: POM + Logic Validation
You're testing a shopping cart. Below is the app logic and a partial Page Object Model. Complete the POM and write tests.

# --- cart.py (the app logic, don't modify) ---

class ShoppingCart:
    def __init__(self):
        self.items = {}

    def add_item(self, name, price, qty):
        if qty <= 0:
            raise ValueError("Quantity must be positive")
        if name in self.items:
            self.items[name]['qty'] += qty
        else:
            self.items[name] = {'price': price, 'qty': qty}

    def remove_item(self, name):
        if name not in self.items:
            raise KeyError(f"{name} not in cart")
        del self.items[name]

    def get_total(self):
        return sum(v['price'] * v['qty'] for v in self.items.values())


# --- cart_page.py (Page Object Model — complete this) ---

class CartPage:
    def __init__(self):
        self.cart = ShoppingCart()

    def add_product(self, name, price, qty):
        pass  # TODO

    def remove_product(self, name):
        pass  # TODO

    def fetch_total(self):
        pass  # TODO


# --- test_cart.py — Write these 4 tests using CartPage ---

# Test 1: Adding 2 different items gives correct total
# Test 2: Adding same item twice accumulates quantity
# Test 3: Removing an item updates the total
# Test 4: Adding item with qty=0 raises ValueError
Editor is loading...
Leave a Comment