Skip to content

Saleor Core: the high performance, composable, headless commerce API.

add test for voucher without codes edge case to check if used function calculation works properly (#14836)

Michał Macioszczykc14ce5cfd8aa
diff --git a/saleor/graphql/discount/tests/mutations/test_voucher_create.py b/saleor/graphql/discount/tests/mutations/test_voucher_create.py
index 6b780da746..f99f52e18c 100644
--- a/saleor/graphql/discount/tests/mutations/test_voucher_create.py
+++ b/saleor/graphql/discount/tests/mutations/test_voucher_create.py
@@ -118,6 +118,55 @@ def test_create_voucher(
assert len(codes) == 2
+def test_create_voucher_no_codes(
+ staff_api_client,
+ permission_manage_discounts,
+ product,
+ variant,
+ collection,
+ category,
+):
+ # given
+ start_date = timezone.now() - timedelta(days=365)
+ end_date = timezone.now() + timedelta(days=365)
+
+ name = "test voucher"
+ variables = {
+ "input": {
+ "name": name,
+ "type": VoucherTypeEnum.ENTIRE_ORDER.name,
+ "addCodes": [""],
+ "discountValueType": DiscountValueTypeEnum.FIXED.name,
+ "minCheckoutItemsQuantity": 10,
+ "startDate": start_date.isoformat(),
+ "endDate": end_date.isoformat(),
+ "applyOncePerOrder": True,
+ "applyOncePerCustomer": True,
+ "singleUse": True,
+ "usageLimit": 3,
+ "products": [graphene.Node.to_global_id("Product", product.pk)],
+ "variants": [graphene.Node.to_global_id("ProductVariant", variant.pk)],
+ "collections": [graphene.Node.to_global_id("Collection", collection.pk)],
+ "categories": [graphene.Node.to_global_id("Category", category.pk)],
+ }
+ }
+
+ # when
+ response = staff_api_client.post_graphql(
+ CREATE_VOUCHER_MUTATION, variables, permissions=[permission_manage_discounts]
+ )
+ content = get_graphql_content(response)
+ data = content["data"]["voucherCreate"]
+ assert not data["errors"]
+ voucher = Voucher.objects.get()
+
+ assert voucher.name == name
+ assert voucher.type == VoucherType.ENTIRE_ORDER
+ # check if used function is calculated properly
+ assert data["voucher"]["used"] == 0
+ assert data["voucher"]["usageLimit"] == 3
+
+
def test_create_voucher_return_error_when_code_and_codes_args_combined(
staff_api_client, permission_manage_discounts
):
diff --git a/saleor/graphql/discount/tests/queries/test_vouchers.py b/saleor/graphql/discount/tests/queries/test_vouchers.py
index f97b8755e9..74c9fd2878 100644
--- a/saleor/graphql/discount/tests/queries/test_vouchers.py
+++ b/saleor/graphql/discount/tests/queries/test_vouchers.py
@@ -90,6 +90,36 @@ def test_voucher_query(
} in data["channelListings"]
+def test_voucher_query_no_codes(
+ staff_api_client,
+ voucher,
+ product,
+ permission_manage_discounts,
+ permission_manage_products,
+):
+ query = VOUCHERS_QUERY
+ voucher.products.add(product)
+ voucher.codes.all().delete()
+ # This simulates edge case when we don't have any codes assigned to voucher
+ assert len(voucher.codes.all()) == 0
+
+ response = staff_api_client.post_graphql(
+ query, permissions=[permission_manage_discounts, permission_manage_products]
+ )
+
+ content = get_graphql_content(response)
+ data = content["data"]["vouchers"]["edges"][0]["node"]
+
+ assert data["type"] == voucher.type.upper()
+ assert data["name"] == voucher.name
+ assert data["code"] is None
+ # check if used function is calculated properly even if we don't have code assigned
+ # to voucher
+ assert data["used"] == 0
+ assert data["usageLimit"] == voucher.usage_limit
+ assert data["products"]["edges"][0]["node"]["name"] == product.name
+
+
def test_voucher_query_with_channel_slug(
staff_api_client,
voucher_with_many_channels_and_countries,