Example #1
0
def test_add_item():
    cart = Cart()
    cart.add_item("AP1")
    cart.add_item("AP1")
    cart.add_item("CH1")

    assert len(cart.contents.keys()) == 2
    assert cart.contents["CH1"] == 1
    assert cart.contents["AP1"] == 2
    assert sum(cart.contents.values()) == 3

    cart.batch_add_item(["CF1", "CF1", "CF1"])
    assert cart.contents["CF1"] == 3
    assert sum(cart.contents.values()) == 6
Example #2
0
class Client(object):
    def __init__(self):
        self.cart = Cart()

    def add_items(self, code_list):
        self.cart.batch_add_item(code_list)

    def clear_cart(self):
        self.cart = Cart()

    def get_total(self):
        cart_contents_with_prices = dict()

        for code, quantity in self.cart.contents.iteritems():
            product = get_product(code)

            # We don't want to repeat DB lookups when discounts are applied, so create a context
            # of the cart with current prices and quantities
            cart_contents_with_prices[code] = Product(code,
                                                      product["description"],
                                                      product["price"],
                                                      quantity)

        return get_discounted_total(cart_contents_with_prices) / 100.0