Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Fix failing product tasks when instances are removed
IKarbowiak8b18f9c79d30
diff --git a/saleor/product/tasks.py b/saleor/product/tasks.pyindex 42adc450cd..b25b514d24 100644--- a/saleor/product/tasks.py+++ b/saleor/product/tasks.py@@ -1,5 +1,8 @@+import loggingfrom typing import Iterable, List, Optional+from django.core.exceptions import ObjectDoesNotExist+from ..attribute.models import Attributefrom ..celeryconf import appfrom ..discount.models import Sale@@ -12,6 +15,8 @@ from .utils.variant_prices import ()from .utils.variants import generate_and_set_variant_name+logger = logging.getLogger(__name__)+def _update_variants_names(instance: ProductType, saved_attributes: Iterable):"""Product variant names are created from names of assigned attributes.@@ -36,14 +41,22 @@ def _update_variants_names(instance: ProductType, saved_attributes: Iterable):@app.taskdef update_variants_names(product_type_pk: int, saved_attributes_ids: List[int]):- instance = ProductType.objects.get(pk=product_type_pk)+ try:+ instance = ProductType.objects.get(pk=product_type_pk)+ except ObjectDoesNotExist:+ logging.warning(f"Cannot find product type with id: {product_type_pk}.")+ returnsaved_attributes = Attribute.objects.filter(pk__in=saved_attributes_ids)_update_variants_names(instance, saved_attributes)@app.taskdef update_product_discounted_price_task(product_pk: int):- product = Product.objects.get(pk=product_pk)+ try:+ product = Product.objects.get(pk=product_pk)+ except ObjectDoesNotExist:+ logging.warning(f"Cannot find product with id: {product_pk}.")+ returnupdate_product_discounted_price(product)@@ -60,7 +73,11 @@ def update_products_discounted_prices_of_catalogues_task(@app.taskdef update_products_discounted_prices_of_discount_task(discount_pk: int):- discount = Sale.objects.get(pk=discount_pk)+ try:+ discount = Sale.objects.get(pk=discount_pk)+ except ObjectDoesNotExist:+ logging.warning(f"Cannot find discount with id: {discount_pk}.")+ returnupdate_products_discounted_prices_of_discount(discount)diff --git a/saleor/product/tests/test_tasks.py b/saleor/product/tests/test_tasks.pynew file mode 100644index 0000000000..df67b4260f--- /dev/null+++ b/saleor/product/tests/test_tasks.py@@ -0,0 +1,89 @@+import logging+from unittest.mock import patch++from ..tasks import (+ update_product_discounted_price_task,+ update_products_discounted_prices_of_discount_task,+ update_variants_names,+)+++@patch("saleor.product.tasks.update_products_discounted_prices_of_discount")+def test_update_products_discounted_prices_of_discount_task(+ update_product_prices_mock, sale+):+ # when+ update_products_discounted_prices_of_discount_task(sale.id)++ # then+ update_product_prices_mock.assert_called_once_with(sale)+++@patch("saleor.product.tasks.update_products_discounted_prices_of_discount")+def test_update_products_discounted_prices_of_discount_task_discount_does_not_exist(+ update_product_prices_mock, caplog+):+ # given+ caplog.set_level(logging.WARNING)+ discount_id = -1++ # when+ update_products_discounted_prices_of_discount_task(discount_id)++ # then+ update_product_prices_mock.assert_not_called()+ assert f"Cannot find discount with id: {discount_id}" in caplog.text+++@patch("saleor.product.tasks.update_product_discounted_price")+def test_update_product_discounted_price_task(update_product_price_mock, product):+ # when+ update_product_discounted_price_task(product.id)++ # then+ update_product_price_mock.assert_called_once_with(product)+++@patch("saleor.product.tasks.update_product_discounted_price")+def test_update_product_discounted_price_task_product_does_not_exist(+ update_product_price_mock, caplog+):+ # given+ caplog.set_level(logging.WARNING)+ product_id = -1++ # when+ update_product_discounted_price_task(product_id)++ # then+ update_product_price_mock.assert_not_called()+ assert f"Cannot find product with id: {product_id}" in caplog.text+++@patch("saleor.product.tasks._update_variants_names")+def test_update_variants_names(+ update_variants_names_mock, product_type, size_attribute+):+ # when+ update_variants_names(product_type.id, [size_attribute.id])++ # then+ args, _ = update_variants_names_mock.call_args+ assert args[0] == product_type+ assert {arg.pk for arg in args[1]} == {size_attribute.pk}+++@patch("saleor.product.tasks.update_products_discounted_prices_of_discount")+def test_update_variants_names_product_type_does_not_exist(+ update_variants_names_mock, caplog+):+ # given+ caplog.set_level(logging.WARNING)+ product_type_id = -1++ # when+ update_variants_names(product_type_id, [])++ # then+ update_variants_names_mock.assert_not_called()+ assert f"Cannot find product type with id: {product_type_id}" in caplog.text