Example #1
0
def test_products_voucher_checkout_discount_not(
    settings,
    monkeypatch,
    prices,
    discount_value,
    discount_type,
    expected_value,
    apply_once_per_order,
    checkout_with_item,
):
    monkeypatch.setattr(
        "saleor.checkout.utils.get_prices_of_discounted_products",
        lambda lines, discounted_products: (
            TaxedMoney(net=Money(price, "USD"), gross=Money(price, "USD"))
            for price in prices
        ),
    )
    voucher = Voucher(
        code="unique",
        type=VoucherType.PRODUCT,
        discount_value_type=discount_type,
        discount_value=discount_value,
        apply_once_per_order=apply_once_per_order,
    )
    voucher.save()
    checkout = checkout_with_item
    discount = get_voucher_discount_for_checkout(voucher, checkout)
    assert discount == Money(expected_value, "USD")
Example #2
0
def test_get_value_voucher_discount(
        total, min_amount_spent, discount_value, discount_value_type,
        expected_value):
    voucher = Voucher(
        code='unique', type=VoucherType.VALUE,
        discount_value_type=discount_value_type,
        discount_value=discount_value,
        min_amount_spent=get_min_amount_spent(min_amount_spent))
    voucher.save()
    total_price = TaxedMoney(
        net=Money(total, 'USD'), gross=Money(total, 'USD'))
    discount = get_value_voucher_discount(voucher, total_price)
    assert discount == Money(expected_value, 'USD')
Example #3
0
def test_get_voucher_discount_all_products(
        prices, discount_value_type, discount_value, voucher_type,
        expected_value):
    prices = [
        TaxedMoney(net=Money(price, 'USD'), gross=Money(price, 'USD'))
        for price in prices]
    voucher = Voucher(
        code='unique', type=voucher_type,
        discount_value_type=discount_value_type,
        discount_value=discount_value, apply_once_per_order=True)
    voucher.save()
    discount = get_products_voucher_discount(voucher, prices)
    assert discount == Money(expected_value, 'USD')
Example #4
0
def test_get_discount_for_cart_collection_voucher_not_applicable(monkeypatch):
    monkeypatch.setattr(
        'saleor.checkout.utils.get_prices_of_products_in_discounted_collections',  # noqa
        lambda cart, product: [])
    voucher = Voucher(
        code='unique', type=VoucherType.COLLECTION,
        discount_value_type=DiscountValueType.FIXED,
        discount_value=10)
    voucher.save()
    cart = Mock()

    with pytest.raises(NotApplicable) as e:
        get_voucher_discount_for_cart(voucher, cart)
    assert str(e.value) == 'This offer is only valid for selected items.'
Example #5
0
def test_product_voucher_checkout_discount_raises_not_applicable(
        order_with_lines, product_with_images):
    discounted_product = product_with_images
    voucher = Voucher(
        code='unique', type=VoucherType.PRODUCT,
        discount_value_type=DiscountValueType.FIXED,
        discount_value=10)
    voucher.save()
    voucher.products.add(discounted_product)
    order_with_lines.voucher = voucher
    order_with_lines.save()
    # Offer is valid only for products listed in voucher
    with pytest.raises(NotApplicable):
        get_voucher_discount_for_order(order_with_lines)
Example #6
0
def test_category_voucher_checkout_discount_raises_not_applicable(order_with_lines):
    discounted_collection = Collection.objects.create(name="Discounted", slug="discou")
    voucher = Voucher(
        code="unique",
        type=VoucherType.COLLECTION,
        discount_value_type=DiscountValueType.FIXED,
        discount_value=10,
    )
    voucher.save()
    voucher.collections.add(discounted_collection)
    order_with_lines.voucher = voucher
    order_with_lines.save()
    # Discount should be valid only for items in the discounted collections
    with pytest.raises(NotApplicable):
        get_voucher_discount_for_order(order_with_lines)
Example #7
0
def test_get_discount_for_checkout_product_voucher_not_applicable(monkeypatch):
    monkeypatch.setattr(
        "saleor.checkout.utils.get_prices_of_discounted_products",
        lambda checkout, product: [],
    )
    voucher = Voucher(
        code="unique",
        type=VoucherType.PRODUCT,
        discount_value_type=DiscountValueType.FIXED,
        discount_value=10,
    )
    voucher.save()
    checkout = Mock()

    with pytest.raises(NotApplicable) as e:
        get_voucher_discount_for_checkout(voucher, checkout)
    assert str(e.value) == "This offer is only valid for selected items."
Example #8
0
def test_get_shipping_voucher_discount(
    total,
    min_amount_spent,
    shipping_price,
    discount_value,
    discount_value_type,
    expected_value,
):
    voucher = Voucher(
        code="unique",
        type=VoucherType.ENTIRE_ORDER,
        discount_value_type=discount_value_type,
        discount_value=discount_value,
        min_amount_spent=get_min_amount_spent(min_amount_spent),
    )
    voucher.save()
    total = TaxedMoney(net=Money(total, "USD"), gross=Money(total, "USD"))
    shipping_price = TaxedMoney(net=Money(shipping_price, "USD"),
                                gross=Money(shipping_price, "USD"))
    discount = get_shipping_voucher_discount(voucher, total, shipping_price)
    assert discount == Money(expected_value, "USD")
Example #9
0
def test_get_value_voucher_discount(
    total,
    min_amount_spent,
    total_quantity,
    min_checkout_items_quantity,
    discount_value,
    discount_value_type,
    expected_value,
):
    voucher = Voucher(
        code="unique",
        type=VoucherType.ENTIRE_ORDER,
        discount_value_type=discount_value_type,
        discount_value=discount_value,
        min_amount_spent=get_min_amount_spent(min_amount_spent),
        min_checkout_items_quantity=min_checkout_items_quantity,
    )
    voucher.save()
    total_price = Money(total, "USD")
    discount = get_value_voucher_discount(voucher, total_price, total_quantity)
    assert discount == Money(expected_value, "USD")
Example #10
0
def test_get_shipping_voucher_discount(
    total,
    min_amount_spent,
    shipping_price,
    discount_value,
    discount_value_type,
    expected_value,
):
    voucher = Voucher(
        code="unique",
        type=VoucherType.VALUE,
        discount_value_type=discount_value_type,
        discount_value=discount_value,
        min_amount_spent=get_min_amount_spent(min_amount_spent),
    )
    voucher.save()
    total = TaxedMoney(net=Money(total, "USD"), gross=Money(total, "USD"))
    shipping_price = TaxedMoney(
        net=Money(shipping_price, "USD"), gross=Money(shipping_price, "USD")
    )
    discount = get_shipping_voucher_discount(voucher, total, shipping_price)
    assert discount == Money(expected_value, "USD")
Example #11
0
def test_get_shipping_voucher_discount_not_applicable(
    total,
    min_amount_spent,
    total_quantity,
    min_checkout_items_quantity,
    shipping_price,
    discount_value,
    discount_value_type,
):
    voucher = Voucher(
        code="unique",
        type=VoucherType.ENTIRE_ORDER,
        discount_value_type=discount_value_type,
        discount_value=discount_value,
        min_amount_spent=get_min_amount_spent(min_amount_spent),
        min_checkout_items_quantity=min_checkout_items_quantity,
    )
    voucher.save()
    total = Money(total, "USD")
    shipping_price = Money(shipping_price, "USD")
    with pytest.raises(NotApplicable):
        get_shipping_voucher_discount(voucher, total, shipping_price,
                                      total_quantity)
Example #12
0
def test_products_voucher_checkout_discount_not(
    monkeypatch,
    prices,
    discount_value,
    discount_type,
    expected_value,
    apply_once_per_order,
    checkout_with_item,
):
    monkeypatch.setattr(
        "saleor.checkout.utils.get_prices_of_discounted_products",
        lambda lines, discounted_products: (Money(price, "USD") for price in prices),
    )
    voucher = Voucher(
        code="unique",
        type=VoucherType.PRODUCT,
        discount_value_type=discount_type,
        discount_value=discount_value,
        apply_once_per_order=apply_once_per_order,
    )
    voucher.save()
    checkout = checkout_with_item
    discount = get_voucher_discount_for_checkout(voucher, checkout)
    assert discount == Money(expected_value, "USD")