def discount_info(category, collection, sale): return DiscountInfo( sale=sale, product_ids=set(), category_ids={category.id}, # assumes this category does not have children collection_ids={collection.id}, )
def test_sale_applies_to_correct_products(product_type, category): product = Product.objects.create( name="Test Product", price=Money(10, "USD"), description="", pk=111, product_type=product_type, category=category, ) variant = ProductVariant.objects.create(product=product, sku="firstvar") product2 = Product.objects.create( name="Second product", price=Money(15, "USD"), description="", product_type=product_type, category=category, ) sec_variant = ProductVariant.objects.create(product=product2, sku="secvar", pk=111) sale = Sale(name="Test sale", value=3, type=DiscountValueType.FIXED) discount = DiscountInfo(sale=sale, product_ids={product.id}, category_ids=set(), collection_ids=set()) product_discount = get_product_discount_on_sale(variant.product, discount) discounted_price = product_discount(product.price) assert discounted_price == Money(7, "USD") with pytest.raises(NotApplicable): get_product_discount_on_sale(sec_variant.product, discount)
def test_percentage_discounts(product): variant = product.variants.get() sale = Sale(type=DiscountValueType.PERCENTAGE, value=50) discount = DiscountInfo( sale=sale, product_ids={product.id}, category_ids=set(), collection_ids={} ) final_price = variant.get_price(discounts=[discount]) assert final_price.gross == Money(5, "USD")
def test_checkout_totals_use_discounts(api_client, checkout_with_item, sale): checkout = checkout_with_item # make sure that we're testing a variant that is actually on sale product = checkout.lines.first().variant.product sale.products.add(product) query = """ query getCheckout($token: UUID) { checkout(token: $token) { lines { totalPrice { gross { amount } } } totalPrice { gross { amount } } subtotalPrice { gross { amount } } } } """ variables = {"token": str(checkout.token)} response = api_client.post_graphql(query, variables) content = get_graphql_content(response) data = content["data"]["checkout"] discounts = [ DiscountInfo( sale=sale, product_ids={product.id}, category_ids=set(), collection_ids=set(), ) ] total = checkout.get_total(discounts=discounts) taxed_total = TaxedMoney(total, total) assert data["totalPrice"]["gross"]["amount"] == taxed_total.gross.amount assert data["subtotalPrice"]["gross"]["amount"] == taxed_total.gross.amount line = checkout.lines.first() line_total = line.get_total(discounts=discounts) assert ( data["lines"][0]["totalPrice"]["gross"]["amount"] == TaxedMoney(line_total, line_total).gross.amount )
def test_variant_discounts(product): variant = product.variants.get() low_sale = Sale(type=DiscountValueType.FIXED, value=5) low_discount = DiscountInfo( sale=low_sale, product_ids={product.id}, category_ids=set(), collection_ids=set(), ) sale = Sale(type=DiscountValueType.FIXED, value=8) discount = DiscountInfo( sale=sale, product_ids={product.id}, category_ids=set(), collection_ids=set() ) high_sale = Sale(type=DiscountValueType.FIXED, value=50) high_discount = DiscountInfo( sale=high_sale, product_ids={product.id}, category_ids=set(), collection_ids=set(), ) final_price = variant.get_price(discounts=[low_discount, discount, high_discount]) assert final_price.gross == Money(0, "USD")