Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Ignore conflicts on stock save in ProductVariantBulkUpdate (#16700)
Krzysztof Waliczek331b54425293
diff --git a/saleor/graphql/product/bulk_mutations/product_variant_bulk_update.py b/saleor/graphql/product/bulk_mutations/product_variant_bulk_update.pyindex f76ceb766a..105d1f7055 100644--- a/saleor/graphql/product/bulk_mutations/product_variant_bulk_update.py+++ b/saleor/graphql/product/bulk_mutations/product_variant_bulk_update.py@@ -636,7 +636,7 @@ class ProductVariantBulkUpdate(BaseMutation):@classmethod@traced_atomic_transaction()- def save_variants(cls, variants_data_with_errors_list):+ def save_variants(cls, variants_data_with_errors_list, error_policy):variants_to_update: list = []stocks_to_create: list = []stocks_to_update: list = []@@ -686,7 +686,12 @@ class ProductVariantBulkUpdate(BaseMutation):"external_reference",],)- warehouse_models.Stock.objects.bulk_create(stocks_to_create)+ if error_policy == ErrorPolicyEnum.REJECT_EVERYTHING.value:+ warehouse_models.Stock.objects.bulk_create(stocks_to_create)+ else:+ warehouse_models.Stock.objects.bulk_create(+ stocks_to_create, ignore_conflicts=True+ )warehouse_models.Stock.objects.bulk_update(stocks_to_update, ["quantity"])models.ProductVariantChannelListing.objects.bulk_create(listings_to_create)models.ProductVariantChannelListing.objects.bulk_update(@@ -821,7 +826,7 @@ class ProductVariantBulkUpdate(BaseMutation):data["instance"] = None# save all objects- cls.save_variants(instances_data_with_errors_list)+ cls.save_variants(instances_data_with_errors_list, error_policy)# prepare and return dataresults = get_results(instances_data_with_errors_list)diff --git a/saleor/graphql/product/tests/mutations/test_product_variant_bulk_update.py b/saleor/graphql/product/tests/mutations/test_product_variant_bulk_update.pyindex cbbd844257..bde28de863 100644--- a/saleor/graphql/product/tests/mutations/test_product_variant_bulk_update.py+++ b/saleor/graphql/product/tests/mutations/test_product_variant_bulk_update.py@@ -2,14 +2,20 @@ from unittest import mockfrom unittest.mock import patchimport graphene+import pytestfrom django.test import override_settingsfrom .....discount.models import PromotionRulefrom .....discount.utils.promotion import get_active_catalogue_promotion_rules+from .....graphql.core.enums import ErrorPolicyEnum+from .....graphql.product.bulk_mutations.product_variant_bulk_update import (+ ProductVariantBulkUpdate,+)from .....graphql.webhook.subscription_payload import get_pre_save_payload_keyfrom .....product.error_codes import ProductVariantBulkErrorCodefrom .....product.models import ProductChannelListingfrom .....tests.utils import flush_post_commit_hooks+from .....warehouse.models import Stockfrom .....webhook.event_types import WebhookEventAsyncTypefrom .....webhook.models import Webhookfrom ....tests.utils import get_graphql_content@@ -69,6 +75,11 @@ PRODUCT_VARIANT_BULK_UPDATE_MUTATION = """}}count+ errors{+ field+ message+ code+ }}}"""@@ -136,6 +147,90 @@ def test_product_variant_bulk_update(assert rule.variants_dirty+ "error_policy",+ [ErrorPolicyEnum.REJECT_FAILED_ROWS.name, ErrorPolicyEnum.IGNORE_FAILED.name],+)+@patch(+ "saleor.graphql.product.bulk_mutations."+ "product_variant_bulk_update.get_webhooks_for_event"+)[email protected](ProductVariantBulkUpdate, "save_variants")+def test_product_variant_bulk_create_stock_thread_race(+ mocked_save,+ mocked_get_webhooks_for_event,+ error_policy,+ staff_api_client,+ variant_with_many_stocks,+ warehouse,+ permission_manage_products,+ any_webhook,+ settings,+):+ # given+ mocked_get_webhooks_for_event.return_value = [any_webhook]+ settings.PLUGINS = ["saleor.plugins.webhook.plugin.WebhookPlugin"]++ variant = variant_with_many_stocks+ new_stock_quantity_created_before_save = 999++ def add_stock_before_save(*args, **kwargs):+ stock = Stock(+ warehouse=warehouse,+ quantity=new_stock_quantity_created_before_save,+ product_variant=variant,+ )+ stock.save()++ mocked_save.side_effect = add_stock_before_save++ product_id = graphene.Node.to_global_id("Product", variant.product_id)+ variant_id = graphene.Node.to_global_id("ProductVariant", variant.pk)+ stocks = variant.stocks.all()+ assert len(stocks) == 2+ new_stock_quantity = 100++ variants = [+ {+ "id": variant_id,+ "stocks": {+ "create": [+ {+ "quantity": new_stock_quantity,+ "warehouse": graphene.Node.to_global_id(+ "Warehouse", warehouse.pk+ ),+ },+ ],+ },+ }+ ]++ variables = {+ "productId": product_id,+ "variants": variants,+ "errorPolicy": error_policy,+ }++ # when+ staff_api_client.user.user_permissions.add(permission_manage_products)+ response = staff_api_client.post_graphql(+ PRODUCT_VARIANT_BULK_UPDATE_MUTATION, variables+ )++ content = get_graphql_content(response)+ flush_post_commit_hooks()+ data = content["data"]["productVariantBulkUpdate"]++ # then+ assert not data["results"][0]["errors"]+ assert data["count"] == 1+ assert variant.stocks.count() == 3+ assert variant.stocks.last().quantity == new_stock_quantity_created_before_save+ for rule in get_active_catalogue_promotion_rules():+ assert rule.variants_dirty++@patch("saleor.graphql.product.bulk_mutations.""product_variant_bulk_update.get_webhooks_for_event"