Example #1
0
def test_get_voucher_discount_all_products_not_applicable(
    prices,
    total,
    min_amount_spent,
    total_quantity,
    min_checkout_items_quantity,
    discount_value_type,
    discount_value,
    voucher_type,
    apply_once_per_order,
):
    prices = [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=apply_once_per_order,
        min_amount_spent=get_min_amount_spent(min_amount_spent),
        min_checkout_items_quantity=min_checkout_items_quantity,
    )
    voucher.save()
    total = Money(total, "USD")
    with pytest.raises(NotApplicable):
        get_products_voucher_discount(voucher, prices, total, total_quantity)
Example #2
0
def test_get_voucher_discount_all_products(
    prices,
    total,
    min_amount_spent,
    total_quantity,
    min_checkout_items_quantity,
    discount_value_type,
    discount_value,
    voucher_type,
    apply_once_per_order,
    expected_value,
):
    prices = [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=apply_once_per_order,
        min_amount_spent=get_min_amount_spent(min_amount_spent),
        min_checkout_items_quantity=min_checkout_items_quantity,
    )
    voucher.save()
    total = Money(total, "USD")
    discount = get_products_voucher_discount(voucher, prices, total,
                                             total_quantity)
    assert discount == Money(expected_value, "USD")
Example #3
0
def test_valid_voucher_limit(settings, limit, value):
    voucher = Voucher(
        code='unique', type=VoucherType.SHIPPING,
        discount_value_type=DiscountValueType.FIXED,
        discount_value=Price(10, currency='USD'),
        limit=limit)
    voucher.validate_limit(value)
Example #4
0
def test_valid_voucher_limit(settings, limit, value):
    voucher = Voucher(code='unique',
                      type=Voucher.SHIPPING_TYPE,
                      discount_value_type=Voucher.DISCOUNT_VALUE_FIXED,
                      discount_value=Price(10, currency='USD'),
                      limit=limit)
    voucher.validate_limit(value)
Example #5
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 #6
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:
        (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 #7
0
def test_query_vouchers_with_filter_discount_type(
    voucher_filter,
    count,
    discount_value_type,
    staff_api_client,
    query_vouchers_with_filter,
    permission_manage_discounts,
):
    Voucher.objects.bulk_create([
        Voucher(
            name="Voucher1",
            discount_value=123,
            code="abc",
            discount_value_type=DiscountValueType.FIXED,
        ),
        Voucher(
            name="Voucher2",
            discount_value=123,
            code="123",
            discount_value_type=discount_value_type,
        ),
    ])
    variables = {"filter": voucher_filter}
    response = staff_api_client.post_graphql(
        query_vouchers_with_filter,
        variables,
        permissions=[permission_manage_discounts])
    content = get_graphql_content(response)
    data = content["data"]["vouchers"]["edges"]
    assert len(data) == count
Example #8
0
def test_query_vouchers_with_filter_status(
    voucher_filter,
    start_date,
    end_date,
    count,
    staff_api_client,
    query_vouchers_with_filter,
    permission_manage_discounts,
):
    Voucher.objects.bulk_create([
        Voucher(
            name="Voucher1",
            discount_value=123,
            code="abc",
            start_date=timezone.now(),
        ),
        Voucher(
            name="Voucher2",
            discount_value=123,
            code="123",
            start_date=start_date,
            end_date=end_date,
        ),
    ])
    variables = {"filter": voucher_filter}
    response = staff_api_client.post_graphql(
        query_vouchers_with_filter,
        variables,
        permissions=[permission_manage_discounts])
    content = get_graphql_content(response)
    data = content["data"]["vouchers"]["edges"]
    assert len(data) == count
Example #9
0
def test_valid_voucher_min_amount_spent(settings, min_amount_spent, value):
    voucher = Voucher(code='unique',
                      type=VoucherType.SHIPPING,
                      discount_value_type=DiscountValueType.FIXED,
                      discount_value=Money(10, 'RUB'),
                      min_amount_spent=min_amount_spent)
    voucher.validate_min_amount_spent(TaxedMoney(net=value, gross=value))
Example #10
0
def test_specific_products_voucher_checkout_discount(
    monkeypatch,
    prices,
    discount_value,
    discount_type,
    expected_value,
    apply_once_per_order,
    checkout_with_item,
):
    discounts = []
    monkeypatch.setattr(
        "saleor.checkout.utils.get_prices_of_discounted_specific_product",
        lambda lines, discounts, discounted_products: (
            Money(price, "USD") for price in prices
        ),
    )
    voucher = Voucher(
        code="unique",
        type=VoucherType.SPECIFIC_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, discounts)
    assert discount == Money(expected_value, "USD")
Example #11
0
def test_valid_voucher_min_amount_spent(min_amount_spent, value):
    voucher = Voucher(
        code="unique",
        type=VoucherType.SHIPPING,
        discount_value_type=DiscountValueType.FIXED,
        discount_value=Money(10, "USD"),
        min_amount_spent=min_amount_spent,
    )
    voucher.validate_min_amount_spent(value)
Example #12
0
def test_valid_voucher_min_amount_spent(settings, min_amount_spent, value):
    voucher = Voucher(
        code="unique",
        type=VoucherType.SHIPPING,
        discount_value_type=DiscountValueType.FIXED,
        discount_value=Money(10, "USD"),
        min_amount_spent=min_amount_spent,
    )
    voucher.validate_min_amount_spent(TaxedMoney(net=value, gross=value))
Example #13
0
def test_value_voucher_checkout_discount_not_applicable(settings):
    voucher = Voucher(code='unique',
                      type=Voucher.VALUE_TYPE,
                      discount_value_type=Voucher.DISCOUNT_VALUE_FIXED,
                      discount_value=10,
                      limit=100)
    checkout = Mock(get_subtotal=Mock(return_value=Price(10, currency='USD')))
    with pytest.raises(NotApplicable) as e:
        voucher.get_discount_for_checkout(checkout)
    assert str(e.value) == 'This offer is only valid for orders over $100.00.'
Example #14
0
def test_value_voucher_checkout_discount_not_applicable(settings):
    voucher = Voucher(code='unique',
                      type=VoucherType.VALUE,
                      discount_value_type=DiscountValueType.FIXED,
                      discount_value=10,
                      limit=100)
    checkout = Mock(get_subtotal=Mock(return_value=Price(10, currency='USD')))
    with pytest.raises(NotApplicable) as e:
        voucher.get_discount_for_checkout(checkout)
    assert e.value.limit == Price(100, currency='USD')
Example #15
0
def vouchers_for_pagination(db):
    now = timezone.now()
    return Voucher.objects.bulk_create([
        Voucher(
            code="Code1",
            name="Voucher1",
            start_date=now + timezone.timedelta(hours=4),
            end_date=now + timezone.timedelta(hours=14),
            discount_value_type=DiscountValueType.PERCENTAGE,
            discount_value=Decimal("1"),
            min_spent_amount=Decimal("10"),
            usage_limit=10,
            type=VoucherType.SPECIFIC_PRODUCT,
        ),
        Voucher(
            code="Code2",
            name="Voucher2",
            end_date=now + timezone.timedelta(hours=1),
            discount_value=Decimal("7"),
            min_spent_amount=Decimal("10"),
            usage_limit=1000,
            used=10,
            type=VoucherType.ENTIRE_ORDER,
        ),
        Voucher(
            code="Code3",
            name="Voucher3",
            end_date=now + timezone.timedelta(hours=2),
            discount_value_type=DiscountValueType.PERCENTAGE,
            discount_value=Decimal("5"),
            min_spent_amount=Decimal("120"),
            usage_limit=100,
            used=35,
            type=VoucherType.ENTIRE_ORDER,
        ),
        Voucher(
            code="Code4",
            name="Voucher4",
            end_date=now + timezone.timedelta(hours=1),
            discount_value=Decimal("5"),
            min_spent_amount=Decimal("50"),
            usage_limit=100,
            type=VoucherType.SPECIFIC_PRODUCT,
        ),
        Voucher(
            code="Code15",
            name="Voucher15",
            start_date=now + timezone.timedelta(hours=1),
            end_date=now + timezone.timedelta(hours=5),
            discount_value_type=DiscountValueType.PERCENTAGE,
            discount_value=Decimal("2"),
            min_spent_amount=Decimal("100"),
            usage_limit=10,
        ),
    ])
Example #16
0
def test_value_voucher_checkout_discount(settings, total, discount_value,
                                         discount_type, limit, expected_value):
    voucher = Voucher(
        code='unique', type=Voucher.VALUE_TYPE,
        discount_value_type=discount_type,
        discount_value=discount_value,
        limit=Price(limit, currency='USD') if limit is not None else None)
    checkout = Mock(get_subtotal=Mock(return_value=Price(total,
                                                         currency='USD')))
    discount = voucher.get_discount_for_checkout(checkout)
    assert discount.amount == Price(expected_value, currency='USD')
Example #17
0
def test_value_voucher_checkout_discount_not_applicable(settings):
    voucher = Voucher(
        code='unique', type=Voucher.VALUE_TYPE,
        discount_value_type=Voucher.DISCOUNT_VALUE_FIXED,
        discount_value=10,
        limit=100)
    checkout = Mock(get_subtotal=Mock(
        return_value=Price(10, currency='USD')))
    with pytest.raises(NotApplicable) as e:
        voucher.get_discount_for_checkout(checkout)
    assert str(e.value) == 'This offer is only valid for orders over $100.00.'
Example #18
0
def test_value_voucher_checkout_discount(settings, total, discount_value,
                                         discount_type, limit, expected_value):
    voucher = Voucher(
        code='unique', type=Voucher.VALUE_TYPE,
        discount_value_type=discount_type,
        discount_value=discount_value,
        limit=Price(limit, currency='USD') if limit is not None else None)
    checkout = Mock(get_subtotal=Mock(return_value=Price(total,
                                                         currency='USD')))
    discount = voucher.get_discount_for_checkout(checkout)
    assert discount.amount == Price(expected_value, currency='USD')
Example #19
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 #20
0
def test_category_voucher_checkout_discount_not_applicable(settings,
                                                           monkeypatch):
    monkeypatch.setattr(
        'saleor.discount.models.get_category_variants_and_prices',
        lambda cart, product: [])
    voucher = Voucher(
        code='unique', type=Voucher.CATEGORY_TYPE,
        discount_value_type=Voucher.DISCOUNT_VALUE_FIXED,
        discount_value=10)
    checkout = Mock(cart=Mock())
    with pytest.raises(NotApplicable) as e:
        voucher.get_discount_for_checkout(checkout)
    assert str(e.value) == 'This offer is only valid for selected items.'
Example #21
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 #22
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')
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 #24
0
def test_get_voucher_discount_all_products(
    prices, discount_value_type, discount_value, voucher_type, expected_value
):
    prices = [Money(price, "USD") for price in prices]
    voucher = Voucher(
        code="unique",
        type=voucher_type,
        discount_value_type=discount_value_type,
        discount_value=discount_value,
    )
    voucher.save()
    discount = get_products_voucher_discount(voucher, prices)
    assert discount == Money(expected_value, "USD")
Example #25
0
def test_category_voucher_checkout_discount_not_applicable(
        settings, monkeypatch):
    monkeypatch.setattr(
        'saleor.discount.models.get_category_variants_and_prices',
        lambda cart, product: [])
    voucher = Voucher(code='unique',
                      type=VoucherType.CATEGORY,
                      discount_value_type=DiscountValueType.FIXED,
                      discount_value=10)
    checkout = Mock(cart=Mock())
    with pytest.raises(NotApplicable) as e:
        voucher.get_discount_for_checkout(checkout)
    assert str(e.value) == 'This offer is only valid for selected items.'
Example #26
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 #27
0
def test_product_voucher_checkout_discount_not_applicable(settings,
                                                          monkeypatch):
    monkeypatch.setattr(
        'saleor.discount.models.get_product_variants_and_prices',
        lambda cart, product: [])
    settings.DEFAULT_CURRENCY = 'USD'
    voucher = Voucher(
        code='unique', type=Voucher.PRODUCT_TYPE,
        discount_value_type=Voucher.DISCOUNT_VALUE_FIXED,
        discount_value=10)
    checkout = Mock(cart=Mock())
    with pytest.raises(NotApplicable) as e:
        voucher.get_discount_for_checkout(checkout)
    assert str(e.value) == 'This offer is only valid for selected items.'
Example #28
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 #29
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 #30
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 #31
0
def test_get_value_voucher_discount(
    total, min_amount_spent, 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_price = Money(total, "USD")
    discount = get_value_voucher_discount(voucher, total_price)
    assert discount == Money(expected_value, "USD")
Example #32
0
def test_voucher_limit_validation(settings, limit, value, valid):
    voucher = Voucher(
        code='unique', type=Voucher.SHIPPING_TYPE,
        discount_value_type=Voucher.DISCOUNT_VALUE_FIXED,
        discount_value=Price(10, currency='USD'),
        limit=limit)
    if valid:
        voucher.validate_limit(value)
    else:
        with pytest.raises(NotApplicable) as e:
            voucher.validate_limit(value)
            msg = 'This offer is only valid for orders over %(amount)s.' % {
                'amount': net(limit)}
            assert str(e.value) == msg
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 #34
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 #35
0
def test_products_voucher_checkout_discount_not(settings, monkeypatch, prices,
                                                discount_value, discount_type,
                                                apply_to, expected_value):
    monkeypatch.setattr(
        'saleor.discount.models.get_product_variants_and_prices',
        lambda cart, product: (
            (None, Price(p, currency='USD')) for p in prices))
    voucher = Voucher(
        code='unique', type=Voucher.PRODUCT_TYPE,
        discount_value_type=discount_type,
        discount_value=discount_value,
        apply_to=apply_to)
    checkout = Mock(cart=Mock())
    discount = voucher.get_discount_for_checkout(checkout)
    assert discount.amount == Price(expected_value, currency='USD')
Example #36
0
def test_products_voucher_checkout_discount_not(settings, monkeypatch, prices,
                                                discount_value, discount_type,
                                                apply_to, expected_value):
    monkeypatch.setattr(
        'saleor.discount.models.get_product_variants_and_prices',
        lambda cart, product: (
            (None, Price(p, currency='USD')) for p in prices))
    voucher = Voucher(code='unique',
                      type=VoucherType.PRODUCT,
                      discount_value_type=discount_type,
                      discount_value=discount_value,
                      apply_to=apply_to)
    checkout = Mock(cart=Mock())
    discount = voucher.get_discount_for_checkout(checkout)
    assert discount.amount == Price(expected_value, currency='USD')
Example #37
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 #38
0
def test_shipping_voucher_checkout_discountnot_applicable(
        settings, is_shipping_required, shipping_method, discount_value,
        discount_type, apply_to, limit, subtotal, error_msg):
    checkout = Mock(is_shipping_required=is_shipping_required,
                    shipping_method=shipping_method,
                    get_subtotal=Mock(return_value=subtotal))
    voucher = Voucher(
        code='unique', type=Voucher.SHIPPING_TYPE,
        discount_value_type=discount_type,
        discount_value=discount_value,
        limit=Price(limit, currency='USD') if limit is not None else None,
        apply_to=apply_to)
    with pytest.raises(NotApplicable) as e:
        voucher.get_discount_for_checkout(checkout)
    assert str(e.value) == error_msg
Example #39
0
def test_shipping_voucher_checkout_discountnot_applicable(
        settings, is_shipping_required, shipping_method, discount_value,
        discount_type, apply_to, limit, error_msg):
    settings.DEFAULT_CURRENCY = 'USD'
    checkout = Mock(is_shipping_required=is_shipping_required,
                    shipping_method=shipping_method)
    voucher = Voucher(
        code='unique', type=Voucher.SHIPPING_TYPE,
        discount_value_type=discount_type,
        discount_value=discount_value,
        limit=Price(limit, currency='USD') if limit is not None else None,
        apply_to=apply_to)
    with pytest.raises(NotApplicable) as e:
        voucher.get_discount_for_checkout(checkout)
    assert str(e.value) == error_msg
Example #40
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.SPECIFIC_PRODUCT,
        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 #41
0
def test_shipping_voucher_checkout_discount(
        settings, shipping_cost, shipping_country_code, discount_value,
        discount_type, apply_to, expected_value):
    settings.DEFAULT_CURRENCY = 'USD'
    checkout = Mock(
        is_shipping_required=True, shipping_method=Mock(
            price=Price(shipping_cost, currency='USD'),
            country_code=shipping_country_code))
    voucher = Voucher(
        code='unique', type=Voucher.SHIPPING_TYPE,
        discount_value_type=discount_type,
        discount_value=discount_value,
        apply_to=apply_to)
    discount = voucher.get_discount_for_checkout(checkout)
    assert discount.amount == Price(expected_value, currency='USD')
Example #42
0
def test_get_discount_for_checkout_shipping_voucher_all_countries(monkeypatch):
    subtotal = TaxedMoney(Money(100, "USD"), Money(100, "USD"))
    monkeypatch.setattr(
        "saleor.checkout.utils.calculations.checkout_subtotal",
        lambda checkout, discounts: subtotal,
    )
    monkeypatch.setattr(
        "saleor.discount.utils.calculations.checkout_subtotal",
        lambda checkout, discounts: subtotal,
    )
    shipping_total = TaxedMoney(Money(10, "USD"), Money(10, "USD"))
    monkeypatch.setattr(
        "saleor.checkout.utils.calculations.checkout_shipping_price",
        lambda checkout, discounts: shipping_total,
    )
    checkout = Mock(
        spec=Checkout,
        is_shipping_required=Mock(return_value=True),
        shipping_method=Mock(get_total=Mock(return_value=shipping_total)),
        shipping_address=Mock(country=Country("PL")),
    )
    voucher = Voucher(
        code="unique",
        type=VoucherType.SHIPPING,
        discount_value_type=DiscountValueType.PERCENTAGE,
        discount_value=50,
        countries=[],
    )

    discount = get_voucher_discount_for_checkout(voucher, checkout)

    assert discount == Money(5, "USD")
Example #43
0
def test_get_discount_for_checkout_shipping_voucher(
    shipping_cost,
    shipping_country_code,
    discount_value,
    discount_type,
    countries,
    expected_value,
    monkeypatch,
):
    subtotal = TaxedMoney(Money(100, "USD"), Money(100, "USD"))
    monkeypatch.setattr(
        "saleor.checkout.utils.calculations.checkout_subtotal",
        lambda checkout, discounts: subtotal,
    )
    monkeypatch.setattr(
        "saleor.discount.utils.calculations.checkout_subtotal",
        lambda checkout, discounts: subtotal,
    )
    shipping_total = Money(shipping_cost, "USD")
    checkout = Mock(
        spec=Checkout,
        is_shipping_required=Mock(return_value=True),
        shipping_method=Mock(get_total=Mock(return_value=shipping_total)),
        get_shipping_price=Mock(return_value=shipping_total),
        shipping_address=Mock(country=Country(shipping_country_code)),
    )
    voucher = Voucher(
        code="unique",
        type=VoucherType.SHIPPING,
        discount_value_type=discount_type,
        discount_value=discount_value,
        countries=countries,
    )
    discount = get_voucher_discount_for_checkout(voucher, checkout)
    assert discount == Money(expected_value, "USD")
Example #44
0
def test_get_discount_for_checkout_entire_order_voucher_not_applicable(
    total,
    total_quantity,
    discount_value,
    discount_type,
    min_spent_amount,
    min_checkout_items_quantity,
    monkeypatch,
):
    voucher = Voucher(
        code="unique",
        type=VoucherType.ENTIRE_ORDER,
        discount_value_type=discount_type,
        discount_value=discount_value,
        min_spent=(
            Money(min_spent_amount, "USD") if min_spent_amount is not None else None
        ),
        min_checkout_items_quantity=min_checkout_items_quantity,
    )
    checkout = Mock(spec=Checkout, quantity=total_quantity)
    subtotal = TaxedMoney(Money(total, "USD"), Money(total, "USD"))
    monkeypatch.setattr(
        "saleor.checkout.utils.calculations.checkout_subtotal",
        lambda checkout, discounts: subtotal,
    )
    monkeypatch.setattr(
        "saleor.discount.utils.calculations.checkout_subtotal",
        lambda checkout, discounts: subtotal,
    )
    with pytest.raises(NotApplicable):
        get_voucher_discount_for_checkout(voucher, checkout)
Example #45
0
def test_shipping_voucher_checkout_discount(
        settings, shipping_cost, shipping_country_code, discount_value,
        discount_type, apply_to, expected_value):
    checkout = Mock(
        get_subtotal=Mock(return_value=Price(100, currency='USD')),
        is_shipping_required=True, shipping_method=Mock(
            price=Price(shipping_cost, currency='USD'),
            country_code=shipping_country_code))
    voucher = Voucher(
        code='unique', type=Voucher.SHIPPING_TYPE,
        discount_value_type=discount_type,
        discount_value=discount_value,
        apply_to=apply_to,
        limit=None)
    discount = voucher.get_discount_for_checkout(checkout)
    assert discount.amount == Price(expected_value, currency='USD')
Example #46
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 #47
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")