Skip to content

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

Add another test case, make code more consistent

Adam Janike21b9ba0b929
diff --git a/saleor/dashboard/product/forms.py b/saleor/dashboard/product/forms.py
index ac6fef3608..5cb9bc1745 100644
--- a/saleor/dashboard/product/forms.py
+++ b/saleor/dashboard/product/forms.py
@@ -131,8 +131,7 @@ class ProductTypeForm(forms.ModelForm):
'product__product_type__variant_attributes__values').all()
attributes = self.instance.variant_attributes.all()
for variant in variants_to_be_updated:
- variant.name = get_name_from_attributes(
- variant, attributes=attributes)
+ variant.name = get_name_from_attributes(variant, attributes)
variant.save()
def check_if_variants_changed(self, has_variants):
@@ -311,9 +310,8 @@ class ProductVariantForm(forms.ModelForm, AttributesMixin):
def save(self, commit=True):
attributes = self.get_saved_attributes()
self.instance.attributes = attributes
- self.instance.name = get_name_from_attributes(
- self.instance,
- self.instance.product.product_type.variant_attributes.all())
+ attrs = self.instance.product.product_type.variant_attributes.all()
+ self.instance.name = get_name_from_attributes(self.instance, attrs)
return super().save(commit=commit)
diff --git a/saleor/graphql/product/mutations/products.py b/saleor/graphql/product/mutations/products.py
index 75436a16c6..fc7fe72f12 100644
--- a/saleor/graphql/product/mutations/products.py
+++ b/saleor/graphql/product/mutations/products.py
@@ -26,8 +26,7 @@ def update_variants_names(instance, saved_attributes):
'product__product_type__variant_attributes__values').all()
attributes = instance.variant_attributes.all()
for variant in variants_to_be_updated:
- variant.name = get_name_from_attributes(
- variant, attributes=attributes)
+ variant.name = get_name_from_attributes(variant, attributes)
variant.save()
diff --git a/tests/api/test_product.py b/tests/api/test_product.py
index 13d707824f..127a73b7d4 100644
--- a/tests/api/test_product.py
+++ b/tests/api/test_product.py
@@ -1,4 +1,5 @@
import json
+from unittest.mock import MagicMock, Mock
import graphene
import pytest
@@ -9,6 +10,7 @@ from prices import Money
from tests.utils import (
create_image, create_pdf_file_with_image_ext, get_graphql_content)
+from saleor.graphql.product.mutations.products import update_variants_names
from saleor.product.models import (
Category, Collection, Product, ProductAttribute, ProductType, ProductImage)
@@ -1253,3 +1255,10 @@ def test_product_type_update_changes_variant_name(
attribute = product.product_type.variant_attributes.first()
value = attribute.values.first().name
assert variant.name == value
+
+
+def test_update_variants_changed_does_nothing_with_no_attributes():
+ product_type = MagicMock(spec=ProductType)
+ product_type.variant_attributes.all = Mock(return_value=[])
+ saved_attributes = []
+ assert update_variants_names(product_type, saved_attributes) is None