Exemple #1
0
def test_ten_percent_discount():
    catalog = FakeCatalog()
    toothbrush = Product("toothbrush", ProductUnit.EACH)
    catalog.add_product(toothbrush, 0.99)

    apples = Product("apples", ProductUnit.KILO)
    catalog.add_product(apples, 1.99)

    teller = Teller(catalog)
    teller.add_special_offer(SpecialOfferType.TEN_PERCENT_DISCOUNT, toothbrush,
                             10.0)

    cart = ShoppingCart()
    cart.add_item_quantity(apples, 2.5)

    receipt = teller.checks_out_articles_from(cart)

    assert 4.975 == pytest.approx(receipt.total_price(), 0.01)
    assert [] == receipt.discounts
    assert 1 == len(receipt.items)
    receipt_item = receipt.items[0]
    assert apples == receipt_item.product
    assert 1.99 == receipt_item.price
    assert 2.5 * 1.99 == pytest.approx(receipt_item.total_price, 0.01)
    assert 2.5 == receipt_item.quantity
    def test_shopping_cart_handle_offers_ten_percent_discount(self):
        catalog = FakeCatalog()
        toothbrush = Product("toothbrush", ProductUnit.EACH)
        apples = Product("apples", ProductUnit.KILO)
        catalog.add_product(toothbrush, 0.99)
        catalog.add_product(apples, 1.99)

        # teller special offers 10% discount on toothbrush
        teller = Teller(catalog)
        teller.add_special_offer(SpecialOfferType.TEN_PERCENT_DISCOUNT,
                                 toothbrush, 10.0)
        #offers = teller.offers
        #discount = Discount(product, "2 for " + str(offer.argument), discount_n)

        # add apples to shopping cart
        cart = ShoppingCart()
        cart.add_item_quantity(apples, 2.5)
        #cart.add_item_quantity(toothbrush, 2)

        receipt = teller.checks_out_articles_from(cart)

        # add apples to shopping cart : check no discounts and 1 item
        self.assertEqual([], receipt.discounts)
        self.assertEqual(len(cart.items), len(receipt.items))
        receipt_item = receipt.items[0]
        self.assertEqual(apples, receipt_item.product)
        self.assertEqual(catalog.unit_price(apples), receipt_item.price)
        self.assertEqual(cart.product_quantities[apples],
                         receipt_item.quantity)
        self.assertAlmostEqual(
            cart.product_quantities[apples] * catalog.unit_price(apples),
            receipt.total_price(), 0.01)
    def test_catalog_unit_price(self):
        catalog = FakeCatalog()
        toothbrush = Product("toothbrush", ProductUnit.EACH)
        apples = Product("apples", ProductUnit.KILO)

        catalog.add_product(toothbrush, 0.99)
        catalog.add_product(apples, 1.99)

        self.assertEqual(0.99, catalog.unit_price(toothbrush))
        self.assertEqual(1.99, catalog.unit_price(apples))
    def test_receipt_add_product(self):
        toothbrush = Product("toothbrush", ProductUnit.EACH)
        apples = Product("apples", ProductUnit.KILO)
        receipt_toothbrush = ReceiptItem(toothbrush, 2, 1.5, 3)

        receipt = Receipt()
        receipt.discount = []
        receipt._items = [receipt_toothbrush]
        self.assertEqual(1, len(receipt._items))

        receipt.add_product(apples, 2, 1, 2)
        self.assertEqual(2, len(receipt._items))
    def test_catalog_add_product(self):
        catalog = FakeCatalog()
        toothbrush = Product("toothbrush", ProductUnit.EACH)
        apples = Product("apples", ProductUnit.KILO)

        catalog.add_product(toothbrush, 0.99)
        catalog.add_product(apples, 1.99)

        self.assertEqual({
            "apples": apples,
            "toothbrush": toothbrush
        }, catalog.products)
    def test_receipt_total_price(self):
        toothbrush = Product("toothbrush", ProductUnit.EACH)
        apples = Product("apples", ProductUnit.KILO)
        toothbrush_discount = Discount(toothbrush, "my_description", 0.2)
        receipt_toothbrush = ReceiptItem(toothbrush, 2, 1.5, 3)
        receipt_apples = ReceiptItem(apples, 3, 1, 3)

        receipt = Receipt()
        receipt.discounts = [toothbrush_discount]
        receipt.items = [receipt_toothbrush, receipt_apples]
        totalprice = receipt.total_price()

        self.assertEqual([toothbrush_discount], receipt.discounts)
        self.assertEqual(5.8, totalprice)
    def test_shopping_cart_add_item(self):
        cart = ShoppingCart()
        apples = Product("apples", ProductUnit.KILO)

        self.assertEqual(0, len(cart._items))
        cart.add_item(apples)
        self.assertEqual(1, len(cart._items))
    def test_shopping_cart_add_item_quantity(self):
        cart = ShoppingCart()
        apples = Product("apples", ProductUnit.KILO)

        cart.add_item_quantity(apples, 2.5)
        self.assertEqual(2.5, cart.product_quantities[apples])

        cart.add_item_quantity(apples, 1.2)
        self.assertEqual(3.7, cart.product_quantities[apples])
    def test_receipt_add_discount(self):
        toothbrush = Product("toothbrush", ProductUnit.EACH)
        receipt_toothbrush = ReceiptItem(toothbrush, 2, 1.5, 3)

        discount = Discount(toothbrush, "test", 1.5)

        receipt = Receipt()
        receipt._items = [receipt_toothbrush]
        self.assertEqual(0, len(receipt._discounts))

        receipt.add_discount(discount)
        self.assertEqual(1, len(receipt._discounts))
        self.assertEqual(discount, receipt._discounts[0])
    def test_shopping_handle_offers_no_offers(self):
        catalog = FakeCatalog()
        toothbrush = Product("toothbrush", ProductUnit.EACH)
        receipt_toothbrush = ReceiptItem(toothbrush, 2, 1.5, 3)

        receipt = Receipt()
        receipt.items = [receipt_toothbrush]

        receipt_offers = {}

        cart = ShoppingCart()
        cart._items = [toothbrush]
        cart._product_quantities = {toothbrush: 2}

        cart.handle_offers(receipt, receipt_offers, catalog)
        self.assertEqual([], receipt.discounts)
def validate_and_add(product_data: ProductFormData, db):
    if product_data.name == "":
        return Response(0, -2, "Missing Name")
    if product_data.product_type == "":
        return Response(0, -2, "Missing Type")

    product = Product(product_data.name)
    product.product_type = "Unknown"

    if "Eyeshadow" == product_data.product_type or "Mascara" == product_data.product_type:
        product.product_type = product_data.product_type
        product.product_family = ProductFamily.EYES
        if "Eyeshadow" == product_data.product_type and product.name.contains(
                "Queen"):
            product.product_range = ProductRange.QUEEN

    product.range = ProductRange.BUDGET
    if product_data.packaging_recyclable:
        product.range = ProductRange.PROFESSIONAL

    if "Foundation" == product_data.product_type:
        if product_data.suggested_price > 10:
            product.range = ProductRange.PROFESSIONAL

    if "Lipstick" == product_data.product_type:
        product.product_type = product_data.product_type
        product.product_family = ProductFamily.LIPS

    if product_data.suggested_price > 10:
        product.range = ProductRange.PROFESSIONAL

    if product_data.suggested_price > 20:
        if 0 < product_data.weight < 10:
            return Response(0, -1,
                            "Error - failed quality check for Queen Range")
        product.range = ProductRange.QUEEN

    if "Mascara" == product_data.product_type:
        product.product_family = ProductFamily.LASHES
        if product_data.suggested_price > 15:
            product.range = ProductRange.PROFESSIONAL

        if product_data.suggested_price > 25 and product_data.packaging_recyclable:
            product.range = ProductRange.QUEEN

    if product_data.weight < 0:
        return Response(0, -3, "Weight error")

    product.weight = product_data.weight

    if "Blusher" == product_data.product_type or "Foundation" == product_data.product_type:
        product.product_type = product_data.product_type
        product.product_family = ProductFamily.SKIN
        if "Blusher" == product_data.product_type and product_data.weight > 10:
            return Response(0, -3, "Error - weight too high")

    if not product_data.packaging_recyclable and product.range == ProductRange.QUEEN:
        return Response(0, -1, "Error - failed quality check for Queen Range")

    if "Unknown" == product.product_type:
        return Response(0, -1,
                        f"Unknown product type {product_data.product_type}")

    return Response(db.store_product(product), 0, "Product Successfully Added")
from dateutil.parser import parse

from model_objects import Product, Price, Store, StoreEvent, Order

CherryBloom = Product("Cherry Bloom", "LIPSTICK01", 30, Price(14.99, "USD"))
RosePetal = Product("Rose Petal", "LIPSTICK02", 30, Price(14.99, "USD"))
BlusherBrush = Product("Blusher Brush", "TOOL01", 50, Price(24.99, "USD"))
EyelashCurler = Product("Eyelash curler", "TOOL01", 100, Price(19.99, "USD"))
WildRose = Product("Wild Rose", "PURFUME01", 200, Price(34.99, "USD"))
CocoaButter = Product("Cocoa Butter", "SKIN_CREAM01", 250, Price(10.99, "USD"))

FlagshipStore = Store("Nordstan", "4189", [CherryBloom])

# Store events add themselves to the stocked items at their store
Masterclass = StoreEvent("Eyeshadow Masterclass", "EVENT01", FlagshipStore,
                         Price(119.99, "USD"))
Makeover = StoreEvent("Makeover", FlagshipStore, "EVENT02",
                      Price(149.99, "USD"))

RecentOrder = Order("1234", parse("2018-09-01T00:00Z"), FlagshipStore,
                    [Makeover])